diff --git a/lib/cli/dispatcher/tunnel.go b/lib/cli/dispatcher/tunnel.go index 2a7e86fb..6817fb3c 100644 --- a/lib/cli/dispatcher/tunnel.go +++ b/lib/cli/dispatcher/tunnel.go @@ -48,18 +48,26 @@ func (dispatcher Dispatcher) Tunnel(args []string) { case "pull": local_address := fmt.Sprintf("%s:%d", dst_host, dst_port) remote_address := fmt.Sprintf("%s:%d", src_host, src_port) - log.Info("Mapping remote (%s) to local (%s)", remote_address, local_address) context.AddPullTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address) case "push": local_address := fmt.Sprintf("%s:%d", src_host, src_port) remote_address := fmt.Sprintf("%s:%d", dst_host, dst_port) - log.Info("Mapping local (%s) to remote (%s)", local_address, remote_address) context.AddPushTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address) case "dynamic": context.Ctx.CurrentTermite.StartSocks5Server() case "internet": - log.Error("TBD") - // context.AddInternetTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address) + local_address := fmt.Sprintf("%s:%d", src_host, src_port) + remote_address := fmt.Sprintf("%s:%d", dst_host, dst_port) + if _, exists := context.Ctx.Socks5Servers[local_address]; exists { + log.Warn("Socks5 server (%s) already exists", local_address) + } else { + err := context.StartSocks5Server(local_address) + if err != nil { + log.Error("Starting local socks5 server failed: %s", err.Error()) + } else { + context.AddPushTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address) + } + } default: log.Error("Invalid mode: %s, should be in {'Pull', 'Push', 'Dynamic', 'Internet'}", mode) } diff --git a/lib/context/context.go b/lib/context/context.go index 4dc693fe..03c0e7b5 100644 --- a/lib/context/context.go +++ b/lib/context/context.go @@ -12,6 +12,7 @@ import ( "github.com/WangYihang/Platypus/lib/util/str" "github.com/WangYihang/Platypus/lib/util/ui" "github.com/WangYihang/readline" + "github.com/armon/go-socks5" "github.com/fatih/color" "github.com/gin-gonic/gin" "gopkg.in/olahol/melody.v1" @@ -50,6 +51,7 @@ type Context struct { PullTunnelInstance map[string]PullTunnelInstance PushTunnelConfig map[string]PushTunnelConfig PushTunnelInstance map[string]PushTunnelInstance + Socks5Servers map[string](*socks5.Server) // Set later in platypus.go Distributor *Distributor RESTful *gin.Engine @@ -72,6 +74,7 @@ func CreateContext() { PullTunnelInstance: make(map[string]PullTunnelInstance), PushTunnelConfig: make(map[string]PushTunnelConfig), PushTunnelInstance: make(map[string]PushTunnelInstance), + Socks5Servers: make(map[string]*socks5.Server), } } // Signal Handler @@ -197,6 +200,7 @@ func Shutdown() { } func AddPushTunnelConfig(termite *TermiteClient, local_address string, remote_address string) { + log.Info("Mapping local (%s) to remote (%s)", local_address, remote_address) termite.AtomLock.Lock() defer func() { termite.AtomLock.Unlock() }() @@ -220,6 +224,7 @@ func AddPushTunnelConfig(termite *TermiteClient, local_address string, remote_ad } func AddPullTunnelConfig(termite *TermiteClient, local_address string, remote_address string) { + log.Info("Mapping remote (%s) to local (%s)", remote_address, local_address) tunnel, err := net.Listen("tcp", local_address) if err != nil { log.Error(err.Error()) @@ -277,6 +282,24 @@ func WriteTunnel(termite *TermiteClient, token string, data []byte) { } } +func StartSocks5Server(local_address string) error { + // Create tcp listener + socks5ServerListener, err := net.Listen("tcp", local_address) + if err != nil { + return err + } + // Create socks5 server + server, err := socks5.New(&socks5.Config{}) + if err != nil { + return err + } + Ctx.Socks5Servers[local_address] = server + // Start socks5 server + go server.Serve(socks5ServerListener) + log.Success("Socks server started at: %s", local_address) + return nil +} + // func DeletePullTunnelConfig(local_host string, local_port uint16, remote_host string, remote_port uint16) { // local_address := fmt.Sprintf("%s:%d", local_host, local_port) // remote_address := fmt.Sprintf("%s:%d", remote_host, remote_port) diff --git a/termite.go b/termite.go index facdf382..1e45727b 100644 --- a/termite.go +++ b/termite.go @@ -400,7 +400,7 @@ func handleConnection(c *Client) { }) c.EncoderLock.Unlock() } else { - log.Error("Server created (%s)", address) + log.Success("Server created (%s)", address) c.EncoderLock.Lock() c.Encoder.Encode(message.Message{ Type: message.PUSH_TUNNEL_CREATED, @@ -408,6 +408,7 @@ func handleConnection(c *Client) { Address: address, }, }) + c.EncoderLock.Unlock() go func() { for {