Mirror of https://git.jolheiser.com/ugit
at form 51 lines 1.2 kB view raw
1package ssh 2 3import ( 4 "fmt" 5 6 "github.com/charmbracelet/log" 7 "github.com/charmbracelet/ssh" 8 "github.com/charmbracelet/wish" 9 "github.com/charmbracelet/wish/logging" 10) 11 12// Settings holds the configuration for the SSH server 13type Settings struct { 14 AuthorizedKeys string 15 CloneURL string 16 Port int 17 HostKey string 18 RepoDir string 19} 20 21// New creates a new SSH server. 22func New(settings Settings) (*ssh.Server, error) { 23 s, err := wish.NewServer( 24 wish.WithAuthorizedKeys(settings.AuthorizedKeys), 25 wish.WithAddress(fmt.Sprintf(":%d", settings.Port)), 26 wish.WithHostKeyPath(settings.HostKey), 27 wish.WithMiddleware( 28 Middleware(settings.RepoDir, settings.CloneURL, settings.Port, hooks{}), 29 logging.MiddlewareWithLogger(DefaultLogger), 30 ), 31 ) 32 if err != nil { 33 return nil, fmt.Errorf("could not create new SSH server: %w", err) 34 } 35 36 return s, nil 37} 38 39type hooks struct{} 40 41func (a hooks) Push(_ string, _ ssh.PublicKey) {} 42func (a hooks) Fetch(_ string, _ ssh.PublicKey) {} 43 44var ( 45 DefaultLogger logging.Logger = log.StandardLog() 46 NoopLogger logging.Logger = noopLogger{} 47) 48 49type noopLogger struct{} 50 51func (n noopLogger) Printf(format string, v ...interface{}) {}