Skip to content

Commit

Permalink
#17 add command implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Apr 23, 2024
1 parent 4d73a2c commit 6a3ca49
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
47 changes: 47 additions & 0 deletions cmd/tcpproxy/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
package main

import (
"context"
"fmt"
"log"
"net"

"waffle/internal/proxy"
)

func main() {
ctx := context.Background()

go func() {
if err := tcpDummy(); err != nil {
log.Panicf("dummy: %s", err.Error())
}
}()

sender := proxy.NewTCPSender("127.0.0.1:8083", "127.0.0.1:8081")
receiver := proxy.NewTCPReceiver("127.0.0.1:8080", sender)

if err := sender.Start(ctx); err != nil {
log.Panicf("start sender: %s", err.Error())
}

if err := receiver.Run(); err != nil {
log.Panicf("run receiver: %s", err.Error())
}
}

func tcpDummy() error {
addr, err := net.ResolveTCPAddr("tcp", ":8081")
if err != nil {
return fmt.Errorf("resolve tcp addr: %w", err)
}

listener, err := net.ListenTCP("tcp", addr)
if err != nil {
return fmt.Errorf("listen tcp: %w", err)
}

for {
conn, err := listener.Accept()
if err != nil {
return fmt.Errorf("liistener accept: %w", err)
}

log.Println(conn)
}
}
6 changes: 4 additions & 2 deletions internal/proxy/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type TCPReceiver struct {
sender Sender // sender is a sender where tcp bytes are supposed to be redirected
}

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

Expand Down Expand Up @@ -106,9 +107,10 @@ func (s *TCPSender) Accept(rw io.ReadWriter) error {
// startRemoteSender is used to start process of sending incoming bytes to the host.
// It should be called in another go routine in order to not block the main routine.
func (s *TCPSender) startRemoteSender(ctx context.Context, listenAddr, remoteAddr *net.TCPAddr) {
conn, err := net.DialTCP("tcp", listenAddr, remoteAddr)
conn, err := net.DialTCP("tcp", remoteAddr, listenAddr)
if err != nil {
fmt.Printf("error dialing remote host: %s", err.Error())
return
}

defer func() {
Expand Down

0 comments on commit 6a3ca49

Please sign in to comment.