repos / pico

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

commit
fd6635f
parent
8743356
author
Eric Bower
date
2022-08-03 04:02:20 +0000 UTC
fix: add pipe to ssh command back to pastes

Also fixed an issue where pipe to ssh would display a "%".
1 files changed,  +55, -3
M cmd/pastes/ssh/main.go
+55, -3
 1@@ -3,8 +3,11 @@ package main
 2 import (
 3 	"context"
 4 	"fmt"
 5+	"io/fs"
 6 	"os"
 7 	"os/signal"
 8+	"strconv"
 9+	"strings"
10 	"syscall"
11 	"time"
12 
13@@ -16,6 +19,7 @@ import (
14 	"git.sr.ht/~erock/pico/wish/proxy"
15 	"git.sr.ht/~erock/pico/wish/send/scp"
16 	"git.sr.ht/~erock/pico/wish/send/sftp"
17+	"git.sr.ht/~erock/pico/wish/send/utils"
18 	"github.com/charmbracelet/wish"
19 	bm "github.com/charmbracelet/wish/bubbletea"
20 	lm "github.com/charmbracelet/wish/logging"
21@@ -33,19 +37,67 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
22 		cmd := s.Command()
23 		mdw := []wish.Middleware{}
24 
25-		if len(cmd) == 0 {
26+		if len(cmd) > 0 && cmd[0] == "scp" {
27+			mdw = append(mdw, scp.Middleware(handler))
28+		} else {
29 			mdw = append(mdw,
30+				pasteMiddleware(handler),
31 				bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
32 				lm.Middleware(),
33 			)
34-		} else if cmd[0] == "scp" {
35-			mdw = append(mdw, scp.Middleware(handler))
36 		}
37 
38 		return mdw
39 	}
40 }
41 
42+func pasteMiddleware(writeHandler *filehandlers.ScpUploadHandler) wish.Middleware {
43+	return func(sshHandler ssh.Handler) ssh.Handler {
44+		return func(session ssh.Session) {
45+			_, _, activePty := session.Pty()
46+			if activePty {
47+				_ = session.Exit(0)
48+				_ = session.Close()
49+				return
50+			}
51+
52+			err := writeHandler.Validate(session)
53+			if err != nil {
54+				utils.ErrorHandler(session, err)
55+				return
56+			}
57+
58+			name := strings.TrimSpace(strings.Join(session.Command(), " "))
59+			postTime := time.Now()
60+
61+			if name == "" {
62+				name = strconv.Itoa(int(postTime.UnixNano()))
63+			}
64+
65+			result, err := writeHandler.Write(session, &utils.FileEntry{
66+				Name:     name,
67+				Filepath: name,
68+				Mode:     fs.FileMode(0777),
69+				Size:     0,
70+				Mtime:    postTime.Unix(),
71+				Atime:    postTime.Unix(),
72+				Reader:   session,
73+			})
74+			if err != nil {
75+				utils.ErrorHandler(session, err)
76+				return
77+			}
78+
79+			if result != "" {
80+				session.Write([]byte(fmt.Sprintf("%s\n", result)))
81+				return
82+			}
83+
84+			sshHandler(session)
85+		}
86+	}
87+}
88+
89 func withProxy(handler *filehandlers.ScpUploadHandler) ssh.Option {
90 	return func(server *ssh.Server) error {
91 		err := sftp.SSHOption(handler)(server)