1package server
2
3import (
4 "context"
5 "fmt"
6 "log"
7 "time"
8
9 cm "github.com/charmbracelet/soft-serve/server/cmd"
10 "github.com/charmbracelet/soft-serve/server/config"
11 "github.com/charmbracelet/soft-serve/server/git/daemon"
12 gm "github.com/charmbracelet/soft-serve/server/git/ssh"
13 "github.com/charmbracelet/wish"
14 bm "github.com/charmbracelet/wish/bubbletea"
15 lm "github.com/charmbracelet/wish/logging"
16 rm "github.com/charmbracelet/wish/recover"
17 "github.com/gliderlabs/ssh"
18 "github.com/muesli/termenv"
19 "golang.org/x/sync/errgroup"
20)
21
22// Server is the Soft Serve server.
23type Server struct {
24 SSHServer *ssh.Server
25 GitServer *daemon.Daemon
26 Config *config.Config
27}
28
29// NewServer returns a new *ssh.Server configured to serve Soft Serve. The SSH
30// server key-pair will be created if none exists. An initial admin SSH public
31// key can be provided with authKey. If authKey is provided, access will be
32// restricted to that key. If authKey is not provided, the server will be
33// publicly writable until configured otherwise by cloning the `config` repo.
34func NewServer(cfg *config.Config) *Server {
35 s := &Server{Config: cfg}
36 mw := []wish.Middleware{
37 rm.MiddlewareWithLogger(
38 log.Default(),
39 // BubbleTea middleware.
40 bm.MiddlewareWithProgramHandler(SessionHandler(cfg), termenv.ANSI256),
41 // Command middleware must come after the git middleware.
42 cm.Middleware(cfg),
43 // Git middleware.
44 gm.Middleware(cfg.RepoPath(), cfg),
45 // Logging middleware must be last to be executed first.
46 lm.Middleware(),
47 ),
48 }
49
50 opts := []ssh.Option{
51 wish.WithAddress(fmt.Sprintf("%s:%d", cfg.Host, cfg.SSH.Port)),
52 wish.WithPublicKeyAuth(cfg.PublicKeyHandler),
53 wish.WithMiddleware(mw...),
54 }
55 if cfg.SSH.AllowKeyless {
56 opts = append(opts, ssh.KeyboardInteractiveAuth(cfg.KeyboardInteractiveHandler))
57 }
58 if cfg.SSH.AllowPassword {
59 opts = append(opts, ssh.PasswordAuth(cfg.PasswordHandler))
60 }
61 if cfg.SSH.Key != "" {
62 opts = append(opts, wish.WithHostKeyPEM([]byte(cfg.SSH.Key)))
63 } else {
64 opts = append(opts, wish.WithHostKeyPath(cfg.PrivateKeyPath()))
65 }
66 opts = append(opts)
67 sh, err := wish.NewServer(opts...)
68 if err != nil {
69 log.Fatalln(err)
70 }
71 if cfg.SSH.MaxTimeout > 0 {
72 sh.MaxTimeout = time.Duration(cfg.SSH.MaxTimeout) * time.Second
73 }
74 if cfg.SSH.IdleTimeout > 0 {
75 sh.IdleTimeout = time.Duration(cfg.SSH.IdleTimeout) * time.Second
76 }
77 s.SSHServer = sh
78 if cfg.Git.Enabled {
79 d, err := daemon.NewDaemon(cfg)
80 if err != nil {
81 log.Fatalln(err)
82 }
83 s.GitServer = d
84 }
85 return s
86}
87
88// Start starts the SSH server.
89func (s *Server) Start() error {
90 var errg errgroup.Group
91 if s.Config.Git.Enabled {
92 errg.Go(func() error {
93 log.Printf("Starting Git server on %s:%d", s.Config.Host, s.Config.Git.Port)
94 if err := s.GitServer.Start(); err != daemon.ErrServerClosed {
95 return err
96 }
97 return nil
98 })
99 }
100 errg.Go(func() error {
101 log.Printf("Starting SSH server on %s:%d", s.Config.Host, s.Config.SSH.Port)
102 if err := s.SSHServer.ListenAndServe(); err != ssh.ErrServerClosed {
103 return err
104 }
105 return nil
106 })
107 return errg.Wait()
108}
109
110// Shutdown lets the server gracefully shutdown.
111func (s *Server) Shutdown(ctx context.Context) error {
112 var errg errgroup.Group
113 if s.Config.Git.Enabled {
114 errg.Go(func() error {
115 return s.GitServer.Shutdown(ctx)
116 })
117 }
118 errg.Go(func() error {
119 return s.SSHServer.Shutdown(ctx)
120 })
121 return errg.Wait()
122}
123
124// Close closes the SSH server.
125func (s *Server) Close() error {
126 var errg errgroup.Group
127 errg.Go(func() error {
128 return s.SSHServer.Close()
129 })
130 if s.Config.Git.Enabled {
131 errg.Go(func() error {
132 return s.GitServer.Close()
133 })
134 }
135 return errg.Wait()
136}