repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
d1895d2
parent
9cd320e
author
Eric Bower
date
2024-01-16 15:36:48 +0000 UTC
refactor(imgs): use prose ssh app

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