Skip to content

Commit

Permalink
#17 add outline of the TCP proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Apr 19, 2024
1 parent 70d30ec commit 6d952ef
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cmd/tcpproxy/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {

}
72 changes: 72 additions & 0 deletions internal/proxy/tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package proxy

import (
"fmt"
"net"
)

type Sender interface {
Accept() error
}

// TCPReceiver is responsible for the reading
// incoming tcp connections
type TCPReceiver struct {
localAddr string // localAddr is an address of the proxy server
}

func NewTCPReceiver(localAddr string) *TCPReceiver {
return &TCPReceiver{
localAddr: localAddr,
}
}

func (r *TCPReceiver) Run() error {
lAddr, err := net.ResolveTCPAddr("tcp", r.localAddr)
if err != nil {
return fmt.Errorf("resolving local tcp addr: '%s': %w", r.localAddr, err)
}

listener, err := net.ListenTCP("tcp", lAddr)
if err != nil {
return fmt.Errorf("creating TCP listener for local address: %w", err)
}

for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Printf("failed to accept TCP connection: %s", err.Error())
}
}

return nil
}

// TCPSender is responsible for the sending
// of the incoming tcp connections to
// desired TCP location.
type TCPSender struct {
// pipes is a map of ipaddr string
// and channel of bytes used to send
// TCP connection to desired location
pipes map[string]any
remoteAddr string
}

func NewTCPSender(remoteAddr string) *TCPSender {
return &TCPSender{
pipes: make(map[string]any),
remoteAddr: remoteAddr,
}
}

func (s *TCPSender) Run() error {
rAddr, err := net.ResolveTCPAddr("tcp", r.remoteAddr)
if err != nil {
return fmt.Errorf("resolving remote tcp addr: '%s': %w", r.remoteAddr, err)
}
}

type senderConstruct struct {
locationIPAddr *net.IPAddr
}

0 comments on commit 6d952ef

Please sign in to comment.