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