-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (131 loc) · 3.07 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"flag"
"fmt"
"io"
"net"
"os"
"os/exec"
"github.com/hashicorp/yamux"
)
type yamuxReadWriter struct {
r io.ReadCloser
w io.WriteCloser
}
func (y yamuxReadWriter) Close() error {
return nil
}
func (y yamuxReadWriter) Read(p []byte) (int, error) {
return y.r.Read(p)
}
func (y yamuxReadWriter) Write(p []byte) (int, error) {
return y.w.Write(p)
}
func listen(port int, yamuxConn io.ReadWriteCloser) {
session, err := yamux.Client(yamuxConn, nil)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating yamux client:", err)
os.Exit(1)
}
defer session.Close()
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Fprintln(os.Stderr, "Error listening:", err)
os.Exit(1)
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Fprintln(os.Stderr, "Error accepting connection:", err)
continue
}
go func() {
defer conn.Close()
stream, err := session.Open()
if err != nil {
fmt.Fprintln(os.Stderr, "Error opening stream:", err)
return
}
defer stream.Close()
go io.Copy(stream, conn)
io.Copy(conn, stream)
}()
}
}
func connect(addr string, yamuxConn io.ReadWriteCloser) {
session, err := yamux.Server(yamuxConn, nil)
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating yamux client:", err)
os.Exit(1)
}
defer session.Close()
for {
stream, err := session.Accept()
if err != nil {
fmt.Fprintln(os.Stderr, "Error accepting stream:", err)
continue
}
go func() {
defer stream.Close()
conn, err := net.Dial("tcp", addr)
if err != nil {
fmt.Fprintln(os.Stderr, "Error connecting to host:", err)
return
}
defer conn.Close()
go io.Copy(stream, conn)
io.Copy(conn, stream)
}()
}
}
func main() {
portPtr := flag.Int("l", 0, "listen port")
addrPtr := flag.String("c", "", "destination address")
reversePtr := flag.Bool("R", false, "reverse mode")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [OPTIONS] <command>\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
listenMode := *portPtr != 0
args := flag.Args()
if !listenMode {
if *addrPtr == "" {
fmt.Fprintln(os.Stderr, "Either -l [port] or -c [address] must be provided")
os.Exit(1)
}
}
var yamuxConn io.ReadWriteCloser
if listenMode == *reversePtr {
yamuxConn = yamuxReadWriter{r: os.Stdin, w: os.Stdout}
} else {
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "A command must be provided")
os.Exit(1)
}
cmd := exec.Command(args[0], args[1:]...)
stdinPipe, err := cmd.StdinPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating stdin pipe:", err)
os.Exit(1)
}
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating stdout pipe:", err)
os.Exit(1)
}
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting subcommand:", err)
os.Exit(1)
}
yamuxConn = yamuxReadWriter{r: stdoutPipe, w: stdinPipe}
}
if !listenMode {
connect(*addrPtr, yamuxConn)
} else {
listen(*portPtr, yamuxConn)
}
}