- commit
- 893485f
- parent
- 3583e4a
- author
- Eric Bower
- date
- 2022-08-09 19:29:12 +0000 UTC
feat: add ability to pipe to any service Implements https://todo.sr.ht/~erock/pico.sh/47
4 files changed,
+76,
-61
+5,
-3
1@@ -13,6 +13,7 @@ import (
2 "git.sr.ht/~erock/pico/lists"
3 "git.sr.ht/~erock/pico/shared"
4 "git.sr.ht/~erock/pico/wish/cms"
5+ "git.sr.ht/~erock/pico/wish/pipe"
6 "git.sr.ht/~erock/pico/wish/proxy"
7 "git.sr.ht/~erock/pico/wish/send/scp"
8 "git.sr.ht/~erock/pico/wish/send/sftp"
9@@ -34,13 +35,14 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
10 cmd := s.Command()
11 mdw := []wish.Middleware{}
12
13- if len(cmd) == 0 {
14+ if len(cmd) > 0 && cmd[0] == "scp" {
15+ mdw = append(mdw, scp.Middleware(handler))
16+ } else {
17 mdw = append(mdw,
18+ pipe.Middleware(handler, ".txt"),
19 bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
20 lm.Middleware(),
21 )
22- } else if cmd[0] == "scp" {
23- mdw = append(mdw, scp.Middleware(handler))
24 }
25
26 return mdw
+2,
-55
1@@ -3,11 +3,8 @@ 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,10 +13,10 @@ import (
14 "git.sr.ht/~erock/pico/pastes"
15 "git.sr.ht/~erock/pico/shared"
16 "git.sr.ht/~erock/pico/wish/cms"
17+ "git.sr.ht/~erock/pico/wish/pipe"
18 "git.sr.ht/~erock/pico/wish/proxy"
19 "git.sr.ht/~erock/pico/wish/send/scp"
20 "git.sr.ht/~erock/pico/wish/send/sftp"
21- "git.sr.ht/~erock/pico/wish/send/utils"
22 "github.com/charmbracelet/promwish"
23 "github.com/charmbracelet/wish"
24 bm "github.com/charmbracelet/wish/bubbletea"
25@@ -42,7 +39,7 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
26 mdw = append(mdw, scp.Middleware(handler))
27 } else {
28 mdw = append(mdw,
29- pasteMiddleware(handler),
30+ pipe.Middleware(handler, ""),
31 bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
32 lm.Middleware(),
33 )
34@@ -52,56 +49,6 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
35 }
36 }
37
38-func pasteMiddleware(writeHandler *filehandlers.ScpUploadHandler) wish.Middleware {
39- return func(sshHandler ssh.Handler) ssh.Handler {
40- return func(session ssh.Session) {
41- _, _, activePty := session.Pty()
42- if activePty {
43- _ = session.Exit(0)
44- _ = session.Close()
45- return
46- }
47-
48- err := writeHandler.Validate(session)
49- if err != nil {
50- utils.ErrorHandler(session, err)
51- return
52- }
53-
54- name := strings.TrimSpace(strings.Join(session.Command(), " "))
55- postTime := time.Now()
56-
57- if name == "" {
58- name = strconv.Itoa(int(postTime.UnixNano()))
59- }
60-
61- result, err := writeHandler.Write(session, &utils.FileEntry{
62- Name: name,
63- Filepath: name,
64- Mode: fs.FileMode(0777),
65- Size: 0,
66- Mtime: postTime.Unix(),
67- Atime: postTime.Unix(),
68- Reader: session,
69- })
70- if err != nil {
71- utils.ErrorHandler(session, err)
72- return
73- }
74-
75- if result != "" {
76- _, err = session.Write([]byte(fmt.Sprintf("%s\n", result)))
77- if err != nil {
78- utils.ErrorHandler(session, err)
79- }
80- return
81- }
82-
83- sshHandler(session)
84- }
85- }
86-}
87-
88 func withProxy(handler *filehandlers.ScpUploadHandler, otherMiddleware ...wish.Middleware) ssh.Option {
89 return func(server *ssh.Server) error {
90 err := sftp.SSHOption(handler)(server)
+5,
-3
1@@ -13,6 +13,7 @@ import (
2 "git.sr.ht/~erock/pico/prose"
3 "git.sr.ht/~erock/pico/shared"
4 "git.sr.ht/~erock/pico/wish/cms"
5+ "git.sr.ht/~erock/pico/wish/pipe"
6 "git.sr.ht/~erock/pico/wish/proxy"
7 "git.sr.ht/~erock/pico/wish/send/scp"
8 "git.sr.ht/~erock/pico/wish/send/sftp"
9@@ -34,13 +35,14 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
10 cmd := s.Command()
11 mdw := []wish.Middleware{}
12
13- if len(cmd) == 0 {
14+ if len(cmd) > 0 && cmd[0] == "scp" {
15+ mdw = append(mdw, scp.Middleware(handler))
16+ } else {
17 mdw = append(mdw,
18+ pipe.Middleware(handler, ".md"),
19 bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
20 lm.Middleware(),
21 )
22- } else if cmd[0] == "scp" {
23- mdw = append(mdw, scp.Middleware(handler))
24 }
25
26 return mdw
+64,
-0
1@@ -0,0 +1,64 @@
2+package pipe
3+
4+import (
5+ "fmt"
6+ "io/fs"
7+ "strconv"
8+ "strings"
9+ "time"
10+
11+ "git.sr.ht/~erock/pico/filehandlers"
12+ "git.sr.ht/~erock/pico/wish/send/utils"
13+ "github.com/charmbracelet/wish"
14+ "github.com/gliderlabs/ssh"
15+)
16+
17+func Middleware(writeHandler *filehandlers.ScpUploadHandler, ext string) wish.Middleware {
18+ return func(sshHandler ssh.Handler) ssh.Handler {
19+ return func(session ssh.Session) {
20+ _, _, activePty := session.Pty()
21+ if activePty {
22+ _ = session.Exit(0)
23+ _ = session.Close()
24+ return
25+ }
26+
27+ err := writeHandler.Validate(session)
28+ if err != nil {
29+ utils.ErrorHandler(session, err)
30+ return
31+ }
32+
33+ name := strings.TrimSpace(strings.Join(session.Command(), " "))
34+ postTime := time.Now()
35+
36+ if name == "" {
37+ name = fmt.Sprintf("%s%s", strconv.Itoa(int(postTime.UnixNano())), ext)
38+ }
39+
40+ result, err := writeHandler.Write(session, &utils.FileEntry{
41+ Name: name,
42+ Filepath: name,
43+ Mode: fs.FileMode(0777),
44+ Size: 0,
45+ Mtime: postTime.Unix(),
46+ Atime: postTime.Unix(),
47+ Reader: session,
48+ })
49+ if err != nil {
50+ utils.ErrorHandler(session, err)
51+ return
52+ }
53+
54+ if result != "" {
55+ _, err = session.Write([]byte(fmt.Sprintf("%s\n", result)))
56+ if err != nil {
57+ utils.ErrorHandler(session, err)
58+ }
59+ return
60+ }
61+
62+ sshHandler(session)
63+ }
64+ }
65+}