Skip to content

Commit

Permalink
[*] #28 Support for internet tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYihang committed Jul 15, 2021
1 parent cc2709a commit 36af0c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/cli/dispatcher/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 23 additions & 0 deletions lib/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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() }()

Expand All @@ -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())
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion termite.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,15 @@ 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,
Body: message.BodyPushTunnelCreated{
Address: address,
},
})
c.EncoderLock.Unlock()

go func() {
for {
Expand Down

0 comments on commit 36af0c7

Please sign in to comment.