forked from spolu/warp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv.go
229 lines (203 loc) · 4.57 KB
/
srv.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
package daemon
import (
"context"
"crypto/tls"
"fmt"
"net"
"sync"
"github.com/spolu/warp"
"github.com/spolu/warp/lib/errors"
"github.com/spolu/warp/lib/logging"
)
// Srv represents a running warpd server.
type Srv struct {
address string
certFile string
keyFile string
warps map[string]*Warp
mutex *sync.Mutex
}
// NewSrv constructs a Srv ready to start serving requests.
func NewSrv(
ctx context.Context,
address string,
certFile string,
keyFile string,
) *Srv {
return &Srv{
address: address,
certFile: certFile,
keyFile: keyFile,
warps: map[string]*Warp{},
mutex: &sync.Mutex{},
}
}
// Run starts the server.
func (s *Srv) Run(
ctx context.Context,
) error {
var ln net.Listener
if s.certFile != "" && s.keyFile != "" {
cer, err := tls.LoadX509KeyPair(s.certFile, s.keyFile)
if err != nil {
return errors.Trace(err)
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cer},
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{
tls.CurveP521, tls.CurveP384, tls.CurveP256,
},
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
}
ln, err = tls.Listen("tcp", s.address, tlsConfig)
if err != nil {
return errors.Trace(err)
}
logging.Logf(ctx,
"Listening: address=%s tls=true cert_file=%s key_file=%s",
s.address, s.certFile, s.keyFile)
} else {
var err error
ln, err = net.Listen("tcp", s.address)
if err != nil {
return errors.Trace(err)
}
logging.Logf(ctx, "Listening: address=%s tls=false", s.address)
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
logging.Logf(ctx,
"Error accepting connection: remote=%s error=%v",
conn.RemoteAddr().String(), err,
)
continue
}
go func() {
err := s.handle(ctx, conn)
if err != nil {
logging.Logf(ctx,
"Error handling connection: remote=%s error=%v",
conn.RemoteAddr().String(), err,
)
} else {
logging.Logf(ctx,
"Done handling connection: remote=%s",
conn.RemoteAddr().String(),
)
}
}()
}
}
// handle an incoming connection.
func (s *Srv) handle(
ctx context.Context,
conn net.Conn,
) error {
logging.Logf(ctx,
"Handling new connection: remote=%s",
conn.RemoteAddr().String(),
)
// Create a new context for this client with its own cancelation function.
ctx, cancel := context.WithCancel(ctx)
ss, err := NewSession(ctx, cancel, conn)
if err != nil {
return errors.Trace(err)
}
// Close and reclaims all session related state.
defer ss.TearDown()
switch ss.sessionType {
case warp.SsTpHost:
err = s.handleHost(ctx, ss)
case warp.SsTpShellClient:
err = s.handleShellClient(ctx, ss)
}
if err != nil {
return errors.Trace(err)
}
return nil
}
// handleHost handles an host connecting, creating the warp if it does not
// exists or erroring accordingly.
func (s *Srv) handleHost(
ctx context.Context,
ss *Session,
) error {
var initial warp.HostUpdate
if err := ss.updateR.Decode(&initial); err != nil {
ss.SendInternalError(ctx)
return errors.Trace(
errors.Newf("Initial host update error: %v", err),
)
}
logging.Logf(ctx,
"Initial host update received: session=%s\n",
ss.ToString(),
)
s.mutex.Lock()
_, ok := s.warps[ss.warp]
if ok {
s.mutex.Unlock()
ss.SendError(ctx,
"warp_in_use",
fmt.Sprintf(
"The warp you attempted to open is already in use: %s.",
ss.warp,
),
)
return errors.Trace(
errors.Newf("Host error: warp already in use: %s", ss.warp),
)
}
s.warps[ss.warp] = &Warp{
token: ss.warp,
windowSize: initial.WindowSize,
host: nil,
clients: map[string]*UserState{},
data: make(chan []byte),
mutex: &sync.Mutex{},
}
s.mutex.Unlock()
s.warps[ss.warp].handleHost(ctx, ss)
// Clean-up warp.
logging.Logf(ctx,
"Cleaning-up warp: session=%s",
ss.ToString(),
)
s.mutex.Lock()
delete(s.warps, ss.warp)
s.mutex.Unlock()
return nil
}
// handleShellClient handles a client connecting, retrieving the required warp
// or erroring accordingly.
func (s *Srv) handleShellClient(
ctx context.Context,
ss *Session,
) error {
s.mutex.Lock()
_, ok := s.warps[ss.warp]
s.mutex.Unlock()
if !ok {
ss.SendError(ctx,
"warp_unknown",
fmt.Sprintf(
"The warp you attempted to connect does not exist: %s.",
ss.warp,
),
)
return errors.Trace(
errors.Newf("Client error: warp unknown %s", ss.warp),
)
}
s.warps[ss.warp].handleShellClient(ctx, ss)
return nil
}