generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
forwardServer.go
219 lines (194 loc) · 4.81 KB
/
forwardServer.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"errors"
"fmt"
"io"
"net"
"os"
"os/exec"
"strconv"
proto "go.containerssh.io/libcontainerssh/agentprotocol"
config "go.containerssh.io/libcontainerssh/config"
log "go.containerssh.io/libcontainerssh/log"
)
const (
xauth_path = "/usr/bin/xauth"
)
func serveConnection(log log.Logger, from io.ReadWriteCloser, to io.ReadWriteCloser) {
_, err := io.Copy(from, to)
if err != nil && errors.Is(err, io.EOF) {
log.Warning("Connection error", err)
}
from.Close()
to.Close()
}
func checkCreateXAuthority() error {
xauthority, ok := os.LookupEnv("XAUTHORITY")
if !ok {
xauthority = ".Xauthority"
}
file, err := os.OpenFile(xauthority, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
return file.Close()
}
func setupX11(log log.Logger, setup proto.SetupPacket) proto.SetupPacket {
err := checkCreateXAuthority()
if err != nil {
log.Error("Failed to create .Xauthority", err)
panic(err)
}
cmd := exec.Command(xauth_path, "add", ":10."+setup.Screen, setup.AuthProtocol, setup.AuthCookie)
err = cmd.Run()
if err != nil {
log.Error("Failed to run xauth", err)
panic(err)
}
setup.BindHost = "127.0.0.1"
// Magic X11 formula: 6000 + display number
setup.BindPort = 6010
return setup
}
func parsePort(proto string, host string, port uint32) string {
switch proto {
case "tcp":
return host + ":" + strconv.Itoa(int(port))
case "unix":
return host
default:
panic(fmt.Errorf("unknown protocol %s", proto))
}
}
//nolint:funlen
func localForward(
log log.Logger,
forwardCtx *proto.ForwardCtx,
connChan chan *proto.Connection,
setup proto.SetupPacket,
) {
listenAddr := parsePort(setup.Protocol, setup.BindHost, setup.BindPort)
sock, err := net.Listen(setup.Protocol, listenAddr)
if err != nil {
log.Error("Failed to start listening for connections", listenAddr, err)
os.Exit(1)
}
go func() {
for {
conn, ok := <-connChan
if !ok {
sock.Close()
break
}
_ = conn.Reject()
}
}()
for {
conn, err := sock.Accept()
if err != nil {
log.Warning("Failed to accept connection from os", err)
break
}
var addr string
var port uint32
var agentCon io.ReadWriteCloser
switch setup.Protocol {
case "tcp":
addr = conn.RemoteAddr().(*net.TCPAddr).IP.String()
port = uint32(conn.RemoteAddr().(*net.TCPAddr).Port)
agentCon, err = forwardCtx.NewConnectionTCP(
setup.BindHost,
setup.BindPort,
addr,
uint32(port),
func() error {
return conn.Close()
},
)
case "unix":
agentCon, err = forwardCtx.NewConnectionUnix(
setup.BindHost,
func() error {
return conn.Close()
},
)
default:
panic(fmt.Errorf("unknown protocol %s", setup.Protocol))
}
if err != nil {
log.Warning("Failed to create new connection with backend", err)
}
go serveConnection(log, conn, agentCon)
go serveConnection(log, agentCon, conn)
if setup.SingleConnection {
break
}
}
forwardCtx.WaitFinish()
}
func externalDial(log log.Logger, forwardCtx *proto.ForwardCtx, connChan chan *proto.Connection, setup proto.SetupPacket) {
for {
agentCon, ok := <-connChan
if !ok {
break
}
details := agentCon.Details()
var protocol string
switch details.Protocol {
case proto.PROTOCOL_TCP:
protocol = "tcp"
case "unix":
protocol = "unix"
default:
panic(fmt.Errorf("unknown protocol %s", details.Protocol))
}
log.Warning(fmt.Sprintf("Dialing %s %s:%d", setup.Protocol, details.ConnectedAddress, details.ConnectedPort))
dialAddr := parsePort(protocol, details.ConnectedAddress, details.ConnectedPort)
conn, err := net.Dial(protocol, dialAddr)
if err != nil {
log.Warning("Failed to dial %s", dialAddr, err)
_ = agentCon.Reject()
continue
}
err = agentCon.Accept()
if err != nil {
log.Warning("Failed to accept connection", err)
continue
}
go serveConnection(log, conn, agentCon)
go serveConnection(log, agentCon, conn)
}
forwardCtx.WaitFinish()
}
func forwardServer(stdin io.Reader, stdout io.Writer, stderr io.Writer, exit exitFunc) {
logConfig := config.LogConfig{
Level: config.LogLevelDebug,
Destination: config.LogDestinationStdout,
File: "/tmp/agent.log",
Stdout: stderr,
Format: config.LogFormatLJSON,
}
log, err := log.NewLogger(logConfig)
if err != nil {
panic(err)
}
log.Debug("Starting agent")
forwardCtx := proto.NewForwardCtx(stdin, stdout, log)
conType, setup, connChan, err := forwardCtx.StartClient()
if err != nil {
panic(err)
}
switch conType {
case proto.CONNECTION_TYPE_X11:
setup = setupX11(log, setup)
fallthrough
case proto.CONNECTION_TYPE_SOCKET_FORWARD:
fallthrough
case proto.CONNECTION_TYPE_PORT_FORWARD:
localForward(log, forwardCtx, connChan, setup)
case proto.CONNECTION_TYPE_SOCKET_DIAL:
fallthrough
case proto.CONNECTION_TYPE_PORT_DIAL:
externalDial(log, forwardCtx, connChan, setup)
}
}