-
Notifications
You must be signed in to change notification settings - Fork 251
/
main.go
266 lines (229 loc) · 5.78 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"bufio"
"io"
"net"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
set "github.com/deckarep/golang-set"
"github.com/docopt/docopt-go"
log "github.com/sirupsen/logrus"
"github.com/taskcluster/taskcluster/v64/internal"
"github.com/taskcluster/taskcluster/v64/tools/websocktunnel/client"
)
const usage = `Websocketunnel Client is a command line utility which establishes a connection
to the websocktunnel service and allows serving http without exposing ports to
the internet.
[Firewall/NAT [User] <--]---> [websocktunnel] <--- [Web]
Usage:
wst-client <wstServer> <wstClientID> <targetPort> [--token <jwtToken>] [--out-file=<outFile>]
[--verbose] [--json]
wst-client -h | --help
The wstClientID is the ID to register with the websocktunnel server. The JWT
token must authorize the same ID.
If --token is not provided, then each line of input on stdin will be treated as
a token as soon as it arrives. This allows wst-client to operate as a
subprocess of some other server for which it is managing the tunnel. That
server can then "feed" wst-client a new token before the most recent token
expires.
Options:
-h --help Show help
--verbose Verbose logging
--token JWT Token, if not given on stdin (see above)
--out-file=<outFile> Dump url to this file
--json Output logs in JSON format`
const closeWait = 2 * time.Second
func main() {
arguments, _ := docopt.ParseArgs(usage, nil, "wst-client "+internal.Version)
var jwtToken string
wstServer, wstClientID := arguments["<wstServer>"].(string), arguments["<wstClientID>"].(string)
if arguments["<jwtToken>"] != nil {
jwtToken = arguments["<jwtToken>"].(string)
}
targetPort, err := strconv.Atoi(arguments["<targetPort>"].(string))
if err != nil || targetPort < 0 || targetPort > 65535 {
log.Fatal(usage)
}
outFile := ""
if arguments["--out-file"] != nil {
outFile = arguments["--out-file"].(string)
}
if arguments["--json"].(bool) {
log.SetFormatter(&log.JSONFormatter{})
}
if arguments["--verbose"].(bool) {
log.SetLevel(log.DebugLevel)
}
// Configure signals for graceful handling
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// hold count of running connections
var running sync.WaitGroup
// used for set operations
count := uint64(0)
runSet := set.NewSet()
run := true
// accept new streams from this channel
strChan := make(chan net.Conn, 1)
client, err := client.New(makeConfigurer(wstServer, wstClientID, jwtToken))
if err != nil {
log.Fatal(err)
}
go func() {
for {
// This simulates http.Server's Serve method
stream, err := client.Accept()
if err != nil {
_, ok := err.(net.Error)
if !ok {
log.Fatal(err)
}
}
strChan <- stream
}
}()
defer func() {
_ = client.Close()
close(strChan)
close(sigChan)
}()
whurl := client.URL()
log.WithFields(log.Fields{"url": whurl}).Info("connected to websocktunnel")
// dump if outFile provided
if outFile != "" {
file, err := os.Create(outFile)
if err != nil {
panic(err)
}
_, _ = file.Write([]byte(whurl))
_ = file.Close()
log.WithFields(log.Fields{"url": whurl, "file": outFile}).Info("wrote url to file")
}
for run {
select {
case <-sigChan:
run = false
done := make(chan struct{})
go func() {
running.Wait()
close(done)
}()
select {
case <-time.After(closeWait):
case <-done:
}
os.Exit(0)
case stream := <-strChan:
log.Debug("Accepting new connection")
f := &forwarder{
stream: stream,
port: targetPort,
count: count,
}
f.notify = func() {
defer running.Done()
runSet.Remove(f)
}
count++
_ = runSet.Add(f)
running.Add(1)
go f.forward()
}
}
}
// struct for handling connection forwarding
type forwarder struct {
stream net.Conn
conn net.Conn
port int
count uint64
notify func()
}
// forwards multiplexed stream to port
func (f *forwarder) forward() {
var err error
strPort := strconv.Itoa(f.port)
f.conn, err = net.Dial("tcp", ":"+strPort)
defer f.notify()
// just to be sure
defer f.kill()
if err != nil {
log.Error(err)
return
}
var wg sync.WaitGroup
wg.Add(2)
// outgoing stream (local -> tunnel)
go func() {
defer wg.Done()
_, _ = io.Copy(f.stream, f.conn)
_ = f.stream.Close()
}()
// incoming stream (tunnel -> local)
go func() {
defer wg.Done()
_, _ = io.Copy(f.conn, f.stream)
_ = f.conn.Close()
}()
wg.Wait()
}
func (f *forwarder) kill() {
if f.stream != nil {
_ = f.stream.Close()
}
if f.conn != nil {
_ = f.conn.Close()
}
}
func makeConfigurer(wstServer, wstClientID, jwtToken string) func() (client.Config, error) {
if jwtToken != "" {
configurer := func() (client.Config, error) {
return client.Config{
ID: wstClientID,
Token: jwtToken,
TunnelAddr: wstServer,
}, nil
}
return configurer
}
// we now have the more complex case of getting updated JWTs on stdin
// define a condition variable over the `jwtToken` local variable
var mux sync.Mutex
var cond = sync.NewCond(&mux)
// read from stdin, rewriting `token` on each
go func() {
reader := bufio.NewReader(os.Stdin)
for {
line, err := reader.ReadString('\n')
if err != nil {
log.Warningf("error reading stdin; no more JWT tokens will be read: %s", err)
return
}
log.Debug("Got new JWT token on stdin")
mux.Lock()
jwtToken = line
cond.Broadcast()
mux.Unlock()
}
}()
configurer := func() (client.Config, error) {
log.Info("Reconfiguring")
mux.Lock()
for jwtToken == "" {
log.Debug("Waiting for first JWT token on stdin")
cond.Wait()
}
token := jwtToken
mux.Unlock()
return client.Config{
ID: wstClientID,
Token: token,
TunnelAddr: wstServer,
}, nil
}
return configurer
}