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 d, err := daemon.NewDaemon(cfg)
79 if err != nil {
80 log.Fatalln(err)
81 }
82 s.GitServer = d
83 return s
84}
85
86// Reload reloads the server configuration.
87func (s *Server) Reload() error {
88 return nil
89 // return s.config.Reload()
90}
91
92// Start starts the SSH server.
93func (s *Server) Start() error {
94 var errg errgroup.Group
95 errg.Go(func() error {
96 log.Printf("Starting Git server on %s:%d", s.Config.Host, s.Config.Git.Port)
97 if err := s.GitServer.Start(); err != daemon.ErrServerClosed {
98 return err
99 }
100 return nil
101 })
102 errg.Go(func() error {
103 log.Printf("Starting SSH server on %s:%d", s.Config.Host, s.Config.SSH.Port)
104 if err := s.SSHServer.ListenAndServe(); err != ssh.ErrServerClosed {
105 return err
106 }
107 return nil
108 })
109 return errg.Wait()
110}
111
112// Shutdown lets the server gracefully shutdown.
113func (s *Server) Shutdown(ctx context.Context) error {
114 var errg errgroup.Group
115 errg.Go(func() error {
116 return s.SSHServer.Shutdown(ctx)
117 })
118 errg.Go(func() error {
119 return s.GitServer.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 errg.Go(func() error {
131 return s.GitServer.Close()
132 })
133 return errg.Wait()
134}