-
Notifications
You must be signed in to change notification settings - Fork 11
/
transport.go
110 lines (96 loc) · 2.17 KB
/
transport.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"errors"
"fmt"
"iconsole/tunnel"
"io"
"net"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/urfave/cli"
)
func transportAction(ctx *cli.Context) error {
udid := ctx.String("UDID")
port := ctx.Int64("port")
args := ctx.Args()
t := "tcp"
a := ""
if len(args) == 2 {
t = strings.ToLower(args[0])
a = args[1]
} else {
return fmt.Errorf("Arguments not enough\nuseage example.\n\ticonsole relay -u <xxx> -p 1234 tcp :1234\n\ticonsole relay -u <xxx> -p 1234 tcp 127.0.0.1:443\n\ticonsole relay -u <xxx> -p 1234 unix /opt/debugserver")
}
l, err := net.Listen(t, a)
if err != nil {
return err
}
if t == "unix" {
if err := os.Chmod(a, 0777); err != nil {
return err
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
go func(c chan os.Signal) {
<-c
l.Close()
}(sigc)
}
if udid == "" {
return errors.New("Exec failed unset `UDID` argument")
}
if port < 3 {
return errors.New("Port number range error")
}
device, err := getDevice(udid)
if err != nil {
return err
}
for {
come, err := l.Accept()
if err != nil {
return nil
}
go func(front net.Conn) {
back, err := tunnel.Connect(device, int(port))
if err != nil {
front.Close()
fmt.Printf("Dial device port `%d` error: %s\n", port, err)
return
}
/* clean timeout */
if err := back.RawConn.SetDeadline(time.Time{}); err != nil {
fmt.Printf("SetDeadline %s\n", err)
return
}
fmt.Printf("Accept new connection %s\n", front.RemoteAddr())
go func(front, back net.Conn) {
if _, err := io.Copy(front, back); err != nil {
_ = front.Close()
_ = back.Close()
}
}(front, back.RawConn)
go func(front, back net.Conn) {
if _, err := io.Copy(back, front); err != nil {
_ = front.Close()
_ = back.Close()
}
}(front, back.RawConn)
}(come)
}
}
func initTransportCommand() cli.Command {
return cli.Command{
Name: "relay",
Usage: "Transport communication wrap to local network",
Action: transportAction,
Flags: append(globalFlags, cli.Int64Flag{
Name: "port, p",
Usage: "Connect to device port",
EnvVar: "PORT",
}),
}
}