/
listen.go
160 lines (154 loc) · 4.28 KB
/
listen.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
package cmd
import (
"fmt"
"strconv"
"github.com/glothriel/wormhole/pkg/admin"
"github.com/glothriel/wormhole/pkg/auth"
"github.com/glothriel/wormhole/pkg/peers"
"github.com/glothriel/wormhole/pkg/ports"
"github.com/glothriel/wormhole/pkg/server"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var listenCommand *cli.Command = &cli.Command{
Name: "listen",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Value: "0.0.0.0",
Usage: "Host the tunnel server will be listening on",
},
&cli.IntFlag{
Name: "port",
Value: 8080,
Usage: "Port the tunnel server will be listening on",
},
&cli.IntFlag{
Name: "admin-port",
Value: 8081,
Usage: "Port the admin server will be listening on",
},
&cli.StringFlag{
Name: "path",
Value: "/wh/tunnel",
Usage: "Path under which the tunnel server will expose the tunnel entrypoint. All other paths will be 404",
},
&cli.BoolFlag{
Name: "port-use-range",
Value: false,
Usage: "Uses fixed port range for allocations",
},
&cli.IntFlag{
Name: "port-range-min",
Value: 30000,
Usage: "Port range for allocations of new proxy services",
},
&cli.IntFlag{
Name: "port-range-max",
Value: 30499,
Usage: "Port range for allocations of new proxy services",
},
&cli.BoolFlag{
Name: "kubernetes",
Usage: "Enables kubernetes integration",
},
&cli.StringFlag{
Name: "kubernetes-namespace",
Value: "wormhole",
Usage: "Namespace to create the proxy services in",
},
&cli.StringFlag{
Name: "kubernetes-labels",
Value: "application=wormhole-server",
Usage: "Labels that will be set on proxy service, must match the labels of wormhole server pod",
},
&cli.StringFlag{
Name: "acceptor",
Value: "server",
Usage: "How would you like to accept pairing requests? `server` waits for approval, every " +
"other value triggers DummyAcceptor, that automatically blindly accepts all pairing requests",
},
&cli.StringFlag{
Name: "acceptor-storage-file-path",
Value: "",
Usage: "A file, that holds information about previously accepted fingerprints. If left entry, " +
"the information will be stored in memory",
},
},
Action: func(c *cli.Context) error {
startPrometheusServer(c)
wsTransportFactory, wsTransportFactoryErr := peers.NewWebsocketTransportFactory(
c.String("host"),
strconv.Itoa(c.Int("port")),
c.String("path"),
)
if wsTransportFactoryErr != nil {
return wsTransportFactoryErr
}
consentGatherer := admin.NewConsentGatherer()
peerFactory := peers.NewDefaultPeerFactory(
"my-server",
auth.NewRSAAuthorizedTransportFactory(
wsTransportFactory,
getAcceptor(c, consentGatherer),
),
)
var portOpenerFactory server.PortOpenerFactory
if c.Bool("kubernetes") {
portOpenerFactory = server.NewK8sServicePortOpenerFactory(
c.String("kubernetes-namespace"),
server.CSVToMap(c.String("kubernetes-labels")),
server.NewPerAppPortOpenerFactory(
ports.RandomPortFromARangeAllocator{
Min: c.Int("port-range-min"),
Max: c.Int("port-range-max"),
},
),
)
} else {
var allocator ports.Allocator
if c.Bool("port-use-range") {
allocator = ports.RandomPortFromARangeAllocator{
Min: c.Int("port-range-min"),
Max: c.Int("port-range-max"),
}
} else {
allocator = ports.RandomPortAllocator{}
}
portOpenerFactory = server.NewPerAppPortOpenerFactory(
allocator,
)
}
appExposer := server.NewDefaultAppExposer(
portOpenerFactory,
)
transportServer := server.NewServer(
peerFactory,
appExposer,
)
adminServer := admin.NewWormholeAdminServer(
fmt.Sprintf(":%d", c.Int("admin-port")),
server.NewServerAppsListAdapter(appExposer),
consentGatherer,
)
go func() {
if listenErr := adminServer.Listen(); listenErr != nil {
logrus.Fatal(listenErr)
}
}()
return transportServer.Start()
},
}
func getAcceptor(c *cli.Context, consentGatherer *admin.ConsentGatherer) auth.Acceptor {
if c.String("acceptor") != "server" {
return auth.DummyAcceptor{}
}
serverAcceptor := admin.NewServerAcceptor(consentGatherer)
if c.String("acceptor-storage-file-path") == "" {
return auth.NewInMemoryCachingAcceptor(serverAcceptor)
}
return auth.NewInFileCachingAcceptor(
c.String("acceptor-storage-file-path"),
serverAcceptor,
)
}