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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package sshserver
import (
"cmp"
"context"
"crypto/tls"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/bluesky-social/indigo/atproto/identity"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
"github.com/johnandersen777/socialweb-computer-kcp/internal/component"
sshcontract "github.com/johnandersen777/socialweb-computer-kcp/internal/contract/ssh"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/httpjson"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/kcp"
"github.com/johnandersen777/socialweb-computer-kcp/internal/ssh/gateway"
"github.com/johnandersen777/socialweb-computer-kcp/internal/ssh/ownersession"
"github.com/johnandersen777/socialweb-computer-kcp/internal/sshtransport"
)
type Service struct {
spec v1alpha1.SshServerSpec
deps Deps
cfg gateway.Config
log *slog.Logger
ownerSessions map[string]ownersession.Session
tls *tls.Config
trust *component.Trust
directory identity.Directory
mu sync.Mutex
started bool
ready bool
readiness component.ReadySignal
stopped bool
ctx context.Context
cancel context.CancelFunc
done chan struct{}
key component.Key
httpURL string
sshURL string
publicURL string
public net.Listener
webURL string
webServer *http.Server
web *webSignIn
perUser bool
gateway *gateway.Gateway
}
var (
_ component.Service = (*Service)(nil)
_ sshcontract.Runner = requester{}
)
type requester struct {
service *Service
ref commonv1.ObjectRef
}
func (r requester) RunCommand(ctx context.Context, account sshcontract.Account, command string, env map[string]string, io sshcontract.CommandIO) error {
s := r.service
if !s.perUser {
runner, err := s.resolveRequester(ctx, r.ref)
if err != nil {
return err
}
return runner.RunCommand(ctx, account, command, env, io)
}
runner, err := s.resolveRequester(ctx, s.accountRequesterRef(account.DID))
switch {
case errors.Is(err, bus.ErrLinkDenied):
s.log.Info("sshserver_account_not_signed_in", "did", account.DID)
_ = io.WriteErr([]byte("sign in at " + s.publicOrigin() + " first: this ssh gateway rents VMs as you, with your own atproto session, and has none for " + account.DID + " yet\n"))
io.Exit(1)
return nil
case err != nil:
s.log.Warn("sshserver_account_requester_unavailable", "did", account.DID, "err", err.Error())
_ = io.WriteErr([]byte("ssh gateway error: your requester is not available: " + err.Error() + "\n"))
io.Exit(1)
return nil
}
return runner.RunCommand(ctx, account, command, env, io)
}
func (s *Service) resolveRequester(ctx context.Context, ref commonv1.ObjectRef) (sshcontract.Runner, error) {
runner, err := bus.Resolve[sshcontract.Runner](s.deps.Caller(ctx), s.deps.Bus, s.deps.Self, ref,
bus.Capability{Name: sshcontract.CapabilityRun, Scope: []string{sshcontract.ScopeRun}})
if err != nil {
return nil, fmt.Errorf("sshserver: the Requester %s/%s: %w", ref.Workspace, ref.Name, err)
}
return runner, nil
}
func (s *Service) Start(ctx context.Context) error {
s.mu.Lock()
if s.started {
s.mu.Unlock()
return errors.New("sshserver: the service is already started")
}
s.started = true
s.ready = false
s.done = make(chan struct{})
runCtx, cancel := context.WithCancel(ctx)
s.ctx = runCtx
s.cancel = cancel
s.mu.Unlock()
fail := func(err error) error {
cancel()
s.shutdown(ctx)
s.mu.Lock()
s.started = false
s.mu.Unlock()
close(s.done)
return err
}
stopTrust, err := s.trust.Follow(runCtx, s.deps.Instance)
if err != nil {
return fail(err)
}
defer stopTrust()
if err := s.setup(runCtx); err != nil {
return fail(err)
}
defer s.followVault(runCtx)()
key := s.deps.Key()
key.Kind = cmp.Or(key.Kind, Kind)
key.Name = cmp.Or(key.Name, "default")
key.Workspace = s.workspace()
handlers := s.deps.Bus
if handlers == nil {
return fail(errors.New("sshserver: no handler registry is available"))
}
host := kcp.KindServiceHost(key.Kind, key.Workspace)
httpURL := "http://" + host
sshURL := "ssh://" + host
if err := handlers.RegisterHTTP(key, s.handler()); err != nil {
return fail(fmt.Errorf("sshserver: registering the handler: %w", err))
}
stream := func(streamCtx context.Context, conn net.Conn) error {
return s.gateway.ServeConn(streamCtx, conn)
}
if err := handlers.RegisterStream(key, stream, "ssh"); err != nil {
handlers.Remove(key)
return fail(fmt.Errorf("sshserver: registering the stream: %w", err))
}
publicURL := ""
if addr := strings.TrimSpace(s.spec.PublicListen); addr != "" {
ln, err := net.Listen("tcp", addr)
if err != nil {
handlers.Remove(key)
return fail(fmt.Errorf("sshserver: the public ssh listener %s: %w", addr, err))
}
publicURL = "ssh://" + ln.Addr().String()
s.mu.Lock()
s.public = ln
s.mu.Unlock()
go s.servePublic(runCtx, ln)
}
webURL := ""
if addr := strings.TrimSpace(s.spec.WebListen); addr != "" {
ln, err := net.Listen("tcp", addr)
if err != nil {
handlers.Remove(key)
return fail(fmt.Errorf("sshserver: the web listener %s: %w", addr, err))
}
webURL = "http://" + ln.Addr().String()
srv := &http.Server{Handler: s.handler(), ReadHeaderTimeout: webReadHeaderTimeout}
s.mu.Lock()
s.webServer = srv
s.mu.Unlock()
go func() {
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
s.log.Warn("sshserver_web_serve_failed", "err", err)
}
}()
}
s.mu.Lock()
s.key = key
s.httpURL = httpURL
s.sshURL = sshURL
s.publicURL = publicURL
s.webURL = webURL
s.ready = true
s.mu.Unlock()
s.log.Info("sshserver_registered", "http", host, "ssh", host, "public", publicURL, "web", webURL)
s.readiness.Signal()
<-runCtx.Done()
s.shutdown(ctx)
close(s.done)
return nil
}
func (s *Service) servePublic(ctx context.Context, ln net.Listener) {
for {
conn, err := ln.Accept()
if err != nil {
if ctx.Err() != nil || errors.Is(err, net.ErrClosed) {
return
}
s.log.Warn("sshserver_public_accept_failed", "err", err)
select {
case <-ctx.Done():
return
case <-time.After(publicAcceptBackoff):
}
continue
}
go func() {
defer conn.Close()
if err := s.gateway.ServeConn(ctx, conn); err != nil && ctx.Err() == nil {
s.log.Info("sshserver_public_conn_closed", "err", err)
}
}()
}
}
func (s *Service) setup(ctx context.Context) error {
var ref commonv1.ObjectRef
if !s.perUser {
var err error
if ref, err = s.requesterRef(); err != nil {
return err
}
}
if err := s.awaitVault(ctx); err != nil {
return err
}
web, err := s.openSignIn(ctx)
if err != nil {
return err
}
if !s.perUser {
if _, err := s.resolveRequester(ctx, ref); err != nil {
return err
}
}
owners := ownersession.New(ownersession.Config{Sessions: s.ownerSessions}, ownersession.Deps{Open: s.openOwner})
var auth sshtransport.Authenticator = owners
var agents sshtransport.AgentProvider = owners
if web != nil {
auth, agents = liveAuth{s}, liveAgents{s}
}
g, err := gateway.New(s.cfg, gateway.Deps{
Runner: requester{service: s, ref: ref},
Directory: s.directory,
Trust: s.inProcess,
Client: s.httpClient(requestTimeout),
Auth: auth,
Agents: agents,
Log: s.log,
})
if err != nil {
web.close()
return err
}
s.mu.Lock()
s.gateway = g
s.web = web
s.mu.Unlock()
return nil
}
func (s *Service) handler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("GET /healthz", s.health)
mux.HandleFunc("GET /connect.json", s.connect)
mux.HandleFunc("GET /.well-known/cocore-app.json", s.cocoreApp)
if s.currentWeb() != nil {
for _, route := range oauthRoutes {
mux.HandleFunc(route, s.oauthRoute)
}
mux.HandleFunc("GET /{$}", s.page)
}
s.gateway.Register(mux)
return mux
}
func (s *Service) health(w http.ResponseWriter, r *http.Request) {
s.mu.Lock()
ready := s.ready
s.mu.Unlock()
if !ready {
httpjson.Write(w, http.StatusServiceUnavailable, map[string]any{"ok": false})
return
}
httpjson.Write(w, http.StatusOK, map[string]any{"ok": true})
}
func (s *Service) connect(w http.ResponseWriter, r *http.Request) {
httpjson.Write(w, http.StatusOK, map[string]any{"sshHost": s.cfg.Hostname})
}
func (s *Service) cocoreApp(w http.ResponseWriter, r *http.Request) {
if s.cfg.DID == "" {
httpjson.Write(w, http.StatusNotFound, map[string]any{"error": "NotFound"})
return
}
httpjson.Write(w, http.StatusOK, map[string]any{"did": s.cfg.DID})
}
func (s *Service) Ready() component.Readiness {
return &s.readiness
}
func (s *Service) Endpoints() []component.Endpoint {
s.mu.Lock()
defer s.mu.Unlock()
if s.httpURL == "" || s.sshURL == "" {
return nil
}
out := []component.Endpoint{
{Name: "http", Protocol: component.ProtocolHTTP, URL: s.httpURL},
{Name: "ssh", Protocol: component.ProtocolSSH, URL: s.sshURL},
}
if s.publicURL != "" {
out = append(out, component.Endpoint{Name: PublicEndpoint, Protocol: component.ProtocolSSH, URL: s.publicURL})
}
if s.webURL != "" {
out = append(out, component.Endpoint{Name: WebEndpoint, Protocol: component.ProtocolHTTP, URL: s.webURL})
}
return out
}
func (s *Service) Stop(ctx context.Context) error {
s.mu.Lock()
cancel := s.cancel
done := s.done
s.mu.Unlock()
if cancel == nil {
return nil
}
cancel()
if done == nil {
return nil
}
select {
case <-done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (s *Service) shutdown(ctx context.Context) {
s.mu.Lock()
if s.stopped {
s.mu.Unlock()
return
}
s.stopped = true
g := s.gateway
public := s.public
s.public = nil
web := s.web
s.web = nil
webServer := s.webServer
s.webServer = nil
s.mu.Unlock()
defer web.close()
if webServer != nil {
_ = webServer.Close()
}
if public != nil {
_ = public.Close()
}
stopCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), DefaultShutdownTimeout)
defer cancel()
if g != nil {
if err := g.Shutdown(stopCtx); err != nil {
s.log.Warn("sshserver_shutdown_failed", "err", err)
}
}
s.log.Info("sshserver_stopped")
s.mu.Lock()
s.started = false
s.ready = false
s.publicURL = ""
s.webURL = ""
s.mu.Unlock()
}