Skip to content

Commit 36af0c7

Browse files
committed
[*] #28 Support for internet tunnel
1 parent cc2709a commit 36af0c7

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

lib/cli/dispatcher/tunnel.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,26 @@ func (dispatcher Dispatcher) Tunnel(args []string) {
4848
case "pull":
4949
local_address := fmt.Sprintf("%s:%d", dst_host, dst_port)
5050
remote_address := fmt.Sprintf("%s:%d", src_host, src_port)
51-
log.Info("Mapping remote (%s) to local (%s)", remote_address, local_address)
5251
context.AddPullTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address)
5352
case "push":
5453
local_address := fmt.Sprintf("%s:%d", src_host, src_port)
5554
remote_address := fmt.Sprintf("%s:%d", dst_host, dst_port)
56-
log.Info("Mapping local (%s) to remote (%s)", local_address, remote_address)
5755
context.AddPushTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address)
5856
case "dynamic":
5957
context.Ctx.CurrentTermite.StartSocks5Server()
6058
case "internet":
61-
log.Error("TBD")
62-
// context.AddInternetTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address)
59+
local_address := fmt.Sprintf("%s:%d", src_host, src_port)
60+
remote_address := fmt.Sprintf("%s:%d", dst_host, dst_port)
61+
if _, exists := context.Ctx.Socks5Servers[local_address]; exists {
62+
log.Warn("Socks5 server (%s) already exists", local_address)
63+
} else {
64+
err := context.StartSocks5Server(local_address)
65+
if err != nil {
66+
log.Error("Starting local socks5 server failed: %s", err.Error())
67+
} else {
68+
context.AddPushTunnelConfig(context.Ctx.CurrentTermite, local_address, remote_address)
69+
}
70+
}
6371
default:
6472
log.Error("Invalid mode: %s, should be in {'Pull', 'Push', 'Dynamic', 'Internet'}", mode)
6573
}

lib/context/context.go

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/WangYihang/Platypus/lib/util/str"
1313
"github.com/WangYihang/Platypus/lib/util/ui"
1414
"github.com/WangYihang/readline"
15+
"github.com/armon/go-socks5"
1516
"github.com/fatih/color"
1617
"github.com/gin-gonic/gin"
1718
"gopkg.in/olahol/melody.v1"
@@ -50,6 +51,7 @@ type Context struct {
5051
PullTunnelInstance map[string]PullTunnelInstance
5152
PushTunnelConfig map[string]PushTunnelConfig
5253
PushTunnelInstance map[string]PushTunnelInstance
54+
Socks5Servers map[string](*socks5.Server)
5355
// Set later in platypus.go
5456
Distributor *Distributor
5557
RESTful *gin.Engine
@@ -72,6 +74,7 @@ func CreateContext() {
7274
PullTunnelInstance: make(map[string]PullTunnelInstance),
7375
PushTunnelConfig: make(map[string]PushTunnelConfig),
7476
PushTunnelInstance: make(map[string]PushTunnelInstance),
77+
Socks5Servers: make(map[string]*socks5.Server),
7578
}
7679
}
7780
// Signal Handler
@@ -197,6 +200,7 @@ func Shutdown() {
197200
}
198201

199202
func AddPushTunnelConfig(termite *TermiteClient, local_address string, remote_address string) {
203+
log.Info("Mapping local (%s) to remote (%s)", local_address, remote_address)
200204
termite.AtomLock.Lock()
201205
defer func() { termite.AtomLock.Unlock() }()
202206

@@ -220,6 +224,7 @@ func AddPushTunnelConfig(termite *TermiteClient, local_address string, remote_ad
220224
}
221225

222226
func AddPullTunnelConfig(termite *TermiteClient, local_address string, remote_address string) {
227+
log.Info("Mapping remote (%s) to local (%s)", remote_address, local_address)
223228
tunnel, err := net.Listen("tcp", local_address)
224229
if err != nil {
225230
log.Error(err.Error())
@@ -277,6 +282,24 @@ func WriteTunnel(termite *TermiteClient, token string, data []byte) {
277282
}
278283
}
279284

285+
func StartSocks5Server(local_address string) error {
286+
// Create tcp listener
287+
socks5ServerListener, err := net.Listen("tcp", local_address)
288+
if err != nil {
289+
return err
290+
}
291+
// Create socks5 server
292+
server, err := socks5.New(&socks5.Config{})
293+
if err != nil {
294+
return err
295+
}
296+
Ctx.Socks5Servers[local_address] = server
297+
// Start socks5 server
298+
go server.Serve(socks5ServerListener)
299+
log.Success("Socks server started at: %s", local_address)
300+
return nil
301+
}
302+
280303
// func DeletePullTunnelConfig(local_host string, local_port uint16, remote_host string, remote_port uint16) {
281304
// local_address := fmt.Sprintf("%s:%d", local_host, local_port)
282305
// remote_address := fmt.Sprintf("%s:%d", remote_host, remote_port)

termite.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,15 @@ func handleConnection(c *Client) {
400400
})
401401
c.EncoderLock.Unlock()
402402
} else {
403-
log.Error("Server created (%s)", address)
403+
log.Success("Server created (%s)", address)
404404
c.EncoderLock.Lock()
405405
c.Encoder.Encode(message.Message{
406406
Type: message.PUSH_TUNNEL_CREATED,
407407
Body: message.BodyPushTunnelCreated{
408408
Address: address,
409409
},
410410
})
411+
c.EncoderLock.Unlock()
411412

412413
go func() {
413414
for {

0 commit comments

Comments
 (0)