- commit
- 6d580be
- parent
- 573e8a5
- author
- Eric Bower
- date
- 2024-01-17 18:10:22 +0000 UTC
revert: imgs ssh
1 files changed,
+116,
-4
+116,
-4
1@@ -1,9 +1,121 @@
2 package main
3
4-import "github.com/picosh/pico/prose"
5+import (
6+ "context"
7+ "fmt"
8+ "os"
9+ "os/signal"
10+ "syscall"
11+ "time"
12+
13+ "github.com/charmbracelet/promwish"
14+ "github.com/charmbracelet/ssh"
15+ "github.com/charmbracelet/wish"
16+ bm "github.com/charmbracelet/wish/bubbletea"
17+ lm "github.com/charmbracelet/wish/logging"
18+ "github.com/picosh/pico/db/postgres"
19+ uploadimgs "github.com/picosh/pico/filehandlers/imgs"
20+ "github.com/picosh/pico/imgs"
21+ "github.com/picosh/pico/shared"
22+ "github.com/picosh/pico/shared/storage"
23+ "github.com/picosh/pico/wish/cms"
24+ "github.com/picosh/send/list"
25+ "github.com/picosh/send/pipe"
26+ "github.com/picosh/send/proxy"
27+ "github.com/picosh/send/send/auth"
28+ wishrsync "github.com/picosh/send/send/rsync"
29+ "github.com/picosh/send/send/scp"
30+ "github.com/picosh/send/send/sftp"
31+ "github.com/picosh/send/send/utils"
32+)
33+
34+type SSHServer struct{}
35+
36+func (me *SSHServer) authHandler(ctx ssh.Context, key ssh.PublicKey) bool {
37+ return true
38+}
39+
40+func createRouter(cfg *shared.ConfigSite, handler utils.CopyFromClientHandler) proxy.Router {
41+ return func(sh ssh.Handler, s ssh.Session) []wish.Middleware {
42+ return []wish.Middleware{
43+ pipe.Middleware(handler, ""),
44+ list.Middleware(handler),
45+ scp.Middleware(handler),
46+ wishrsync.Middleware(handler),
47+ auth.Middleware(handler),
48+ bm.Middleware(cms.Middleware(&cfg.ConfigCms, cfg)),
49+ lm.Middleware(),
50+ }
51+ }
52+}
53+
54+func withProxy(cfg *shared.ConfigSite, handler utils.CopyFromClientHandler, otherMiddleware ...wish.Middleware) ssh.Option {
55+ return func(server *ssh.Server) error {
56+ err := sftp.SSHOption(handler)(server)
57+ if err != nil {
58+ return err
59+ }
60+
61+ return proxy.WithProxy(createRouter(cfg, handler), otherMiddleware...)(server)
62+ }
63+}
64
65 func main() {
66- // we are using prose here because we no longer need a dedicated
67- // SSH app for imgs since prose handles images just fine
68- prose.StartSshServer()
69+ host := shared.GetEnv("IMGS_HOST", "0.0.0.0")
70+ port := shared.GetEnv("IMGS_SSH_PORT", "2222")
71+ promPort := shared.GetEnv("IMGS_PROM_PORT", "9222")
72+ cfg := imgs.NewConfigSite()
73+ logger := cfg.Logger
74+ dbh := postgres.NewDB(cfg.DbURL, cfg.Logger)
75+ defer dbh.Close()
76+
77+ var st storage.ObjectStorage
78+ var err error
79+ if cfg.MinioURL == "" {
80+ st, err = storage.NewStorageFS(cfg.StorageDir)
81+ } else {
82+ st, err = storage.NewStorageMinio(cfg.MinioURL, cfg.MinioUser, cfg.MinioPass)
83+ }
84+
85+ if err != nil {
86+ logger.Fatal(err)
87+ }
88+
89+ handler := uploadimgs.NewUploadImgHandler(
90+ dbh,
91+ cfg,
92+ st,
93+ )
94+
95+ sshServer := &SSHServer{}
96+ s, err := wish.NewServer(
97+ wish.WithAddress(fmt.Sprintf("%s:%s", host, port)),
98+ wish.WithHostKeyPath("ssh_data/term_info_ed25519"),
99+ wish.WithPublicKeyAuth(sshServer.authHandler),
100+ withProxy(
101+ cfg,
102+ handler,
103+ promwish.Middleware(fmt.Sprintf("%s:%s", host, promPort), "imgs-ssh"),
104+ ),
105+ )
106+ if err != nil {
107+ logger.Fatal(err)
108+ }
109+
110+ done := make(chan os.Signal, 1)
111+ signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
112+ logger.Infof("Starting SSH server on %s:%s", host, port)
113+ go func() {
114+ if err = s.ListenAndServe(); err != nil {
115+ logger.Fatal(err)
116+ }
117+ }()
118+
119+ <-done
120+ logger.Info("Stopping SSH server")
121+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
122+ defer func() { cancel() }()
123+ if err := s.Shutdown(ctx); err != nil {
124+ logger.Fatal(err)
125+ }
126 }