import (
	"github.com/charmbracelet/wish"
  "github.com/neurosnap/lists.sh/internal/db/postgres"
  "github.com/picosh/send"
)

type SSHServer struct{}

func (me *SSHServer) authHandler(ctx ssh.Context, key ssh.PublicKey) bool {
	return true
}

func main() {
  host := "0.0.0.0"
	port := "2222"
  handler := &send.DbHandler{}
	dbh := postgres.NewDB()
	defer dbh.Close()
	sshServer := &SSHServer{}

  s, err := wish.NewServer(
		wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
		wish.WithHostKeyPath("ssh_data/term_info_ed25519"),
		wish.WithPublicKeyAuth(sshServer.authHandler),
		wish.WithMiddleware(send.Middleware(handler, dbh)),
	)
	if err != nil {
    panic(err)
	}

	done := make(chan os.Signal, 1)
	signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
	fmt.Printf("Starting SSH server on %s:%s\n", host, port)
	go func() {
		if err = s.ListenAndServe(); err != nil {
      panic(err)
		}
	}()

	<-done
	fmt.Println("Stopping SSH server")
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer func() { cancel() }()
	if err := s.Shutdown(ctx); err != nil {
    panic(err)
	}
}

GitHub

View Github