repos / pico

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

commit
f71a10d
parent
29ab4f5
author
Antonio Mika
date
2022-08-28 04:03:01 +0000 UTC
Implement new buffer for sftp and simplify auth logic
12 files changed,  +74, -29
M cmd/imgs/ssh/main.go
+2, -0
 1@@ -17,6 +17,7 @@ import (
 2 	"git.sr.ht/~erock/pico/wish/list"
 3 	"git.sr.ht/~erock/pico/wish/pipe"
 4 	"git.sr.ht/~erock/pico/wish/proxy"
 5+	"git.sr.ht/~erock/pico/wish/send/auth"
 6 	wishrsync "git.sr.ht/~erock/pico/wish/send/rsync"
 7 	"git.sr.ht/~erock/pico/wish/send/scp"
 8 	"git.sr.ht/~erock/pico/wish/send/sftp"
 9@@ -43,6 +44,7 @@ func createRouter(cfg *shared.ConfigSite, handler utils.CopyFromClientHandler) p
10 			wishrsync.Middleware(handler),
11 			bm.Middleware(cms.Middleware(&cfg.ConfigCms, cfg)),
12 			lm.Middleware(),
13+			auth.Middleware(handler),
14 		}
15 	}
16 }
M cmd/lists/ssh/main.go
+2, -0
 1@@ -17,6 +17,7 @@ import (
 2 	"git.sr.ht/~erock/pico/wish/list"
 3 	"git.sr.ht/~erock/pico/wish/pipe"
 4 	"git.sr.ht/~erock/pico/wish/proxy"
 5+	"git.sr.ht/~erock/pico/wish/send/auth"
 6 	wishrsync "git.sr.ht/~erock/pico/wish/send/rsync"
 7 	"git.sr.ht/~erock/pico/wish/send/scp"
 8 	"git.sr.ht/~erock/pico/wish/send/sftp"
 9@@ -42,6 +43,7 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
10 			wishrsync.Middleware(handler),
11 			bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
12 			lm.Middleware(),
13+			auth.Middleware(handler),
14 		}
15 	}
16 }
M cmd/pastes/ssh/main.go
+2, -0
 1@@ -17,6 +17,7 @@ import (
 2 	"git.sr.ht/~erock/pico/wish/list"
 3 	"git.sr.ht/~erock/pico/wish/pipe"
 4 	"git.sr.ht/~erock/pico/wish/proxy"
 5+	"git.sr.ht/~erock/pico/wish/send/auth"
 6 	wishrsync "git.sr.ht/~erock/pico/wish/send/rsync"
 7 	"git.sr.ht/~erock/pico/wish/send/scp"
 8 	"git.sr.ht/~erock/pico/wish/send/sftp"
 9@@ -42,6 +43,7 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
10 			wishrsync.Middleware(handler),
11 			bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
12 			lm.Middleware(),
13+			auth.Middleware(handler),
14 		}
15 	}
16 }
M cmd/prose/ssh/main.go
+2, -0
 1@@ -17,6 +17,7 @@ import (
 2 	"git.sr.ht/~erock/pico/wish/list"
 3 	"git.sr.ht/~erock/pico/wish/pipe"
 4 	"git.sr.ht/~erock/pico/wish/proxy"
 5+	"git.sr.ht/~erock/pico/wish/send/auth"
 6 	wishrsync "git.sr.ht/~erock/pico/wish/send/rsync"
 7 	"git.sr.ht/~erock/pico/wish/send/scp"
 8 	"git.sr.ht/~erock/pico/wish/send/sftp"
 9@@ -42,6 +43,7 @@ func createRouter(handler *filehandlers.ScpUploadHandler) proxy.Router {
10 			wishrsync.Middleware(handler),
11 			bm.Middleware(cms.Middleware(&handler.Cfg.ConfigCms, handler.Cfg)),
12 			lm.Middleware(),
13+			auth.Middleware(handler),
14 		}
15 	}
16 }
M wish/list/list.go
+0, -6
 1@@ -18,12 +18,6 @@ func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
 2 				return
 3 			}
 4 
 5-			err := writeHandler.Validate(session)
 6-			if err != nil {
 7-				utils.ErrorHandler(session, err)
 8-				return
 9-			}
10-
11 			fileList, err := writeHandler.List(session, "/")
12 			if err != nil {
13 				utils.ErrorHandler(session, err)
M wish/pipe/pipe.go
+0, -6
 1@@ -22,12 +22,6 @@ func Middleware(writeHandler utils.CopyFromClientHandler, ext string) wish.Middl
 2 				return
 3 			}
 4 
 5-			err := writeHandler.Validate(session)
 6-			if err != nil {
 7-				utils.ErrorHandler(session, err)
 8-				return
 9-			}
10-
11 			name := strings.TrimSpace(strings.Join(session.Command(), " "))
12 			postTime := time.Now()
13 
A wish/send/auth/auth.go
+21, -0
 1@@ -0,0 +1,21 @@
 2+package auth
 3+
 4+import (
 5+	"git.sr.ht/~erock/pico/wish/send/utils"
 6+	"github.com/charmbracelet/wish"
 7+	"github.com/gliderlabs/ssh"
 8+)
 9+
10+func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
11+	return func(sshHandler ssh.Handler) ssh.Handler {
12+		return func(session ssh.Session) {
13+			err := writeHandler.Validate(session)
14+			if err != nil {
15+				utils.ErrorHandler(session, err)
16+				return
17+			}
18+
19+			sshHandler(session)
20+		}
21+	}
22+}
M wish/send/rsync/rsync.go
+0, -6
 1@@ -77,12 +77,6 @@ func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
 2 				return
 3 			}
 4 
 5-			err := writeHandler.Validate(session)
 6-			if err != nil {
 7-				utils.ErrorHandler(session, err)
 8-				return
 9-			}
10-
11 			fileHandler := &handler{
12 				session:      session,
13 				writeHandler: writeHandler,
M wish/send/scp/scp.go
+1, -5
 1@@ -29,11 +29,7 @@ func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
 2 				return
 3 			}
 4 
 5-			err := writeHandler.Validate(session)
 6-			if err != nil {
 7-				utils.ErrorHandler(session, err)
 8-				return
 9-			}
10+			var err error
11 
12 			switch info.Op {
13 			case OpCopyToClient:
M wish/send/send.go
+2, -1
 1@@ -2,6 +2,7 @@ package send
 2 
 3 import (
 4 	"git.sr.ht/~erock/pico/wish/pipe"
 5+	"git.sr.ht/~erock/pico/wish/send/auth"
 6 	"git.sr.ht/~erock/pico/wish/send/rsync"
 7 	"git.sr.ht/~erock/pico/wish/send/scp"
 8 	"git.sr.ht/~erock/pico/wish/send/sftp"
 9@@ -12,7 +13,7 @@ import (
10 
11 func Middleware(writeHandler utils.CopyFromClientHandler) ssh.Option {
12 	return func(server *ssh.Server) error {
13-		err := wish.WithMiddleware(pipe.Middleware(writeHandler, ""), scp.Middleware(writeHandler), rsync.Middleware(writeHandler))(server)
14+		err := wish.WithMiddleware(pipe.Middleware(writeHandler, ""), scp.Middleware(writeHandler), rsync.Middleware(writeHandler), auth.Middleware(writeHandler))(server)
15 		if err != nil {
16 			return err
17 		}
M wish/send/sftp/handler.go
+1, -2
 1@@ -1,7 +1,6 @@
 2 package sftp
 3 
 4 import (
 5-	"bytes"
 6 	"errors"
 7 	"io"
 8 	"os"
 9@@ -65,7 +64,7 @@ func (f *handler) Filewrite(r *sftp.Request) (io.WriterAt, error) {
10 		Atime:    int64(r.Attributes().Atime),
11 	}
12 
13-	buf := bytes.NewBuffer([]byte{})
14+	buf := &buffer{}
15 	fileEntry.Reader = buf
16 
17 	return fakeWrite{fileEntry: fileEntry, buf: buf, handler: f}, nil
M wish/send/sftp/writer.go
+41, -3
 1@@ -1,20 +1,58 @@
 2 package sftp
 3 
 4 import (
 5-	"bytes"
 6 	"fmt"
 7+	"io"
 8+	"sync"
 9 
10 	"git.sr.ht/~erock/pico/wish/send/utils"
11 )
12 
13+type buffer struct {
14+	buf []byte
15+	m   sync.Mutex
16+	off int
17+}
18+
19+func (b *buffer) WriteAt(p []byte, pos int64) (n int, err error) {
20+	pLen := len(p)
21+	expLen := pos + int64(pLen)
22+	b.m.Lock()
23+	defer b.m.Unlock()
24+	if int64(len(b.buf)) < expLen {
25+		if int64(cap(b.buf)) < expLen {
26+			newBuf := make([]byte, expLen)
27+			copy(newBuf, b.buf)
28+			b.buf = newBuf
29+		}
30+		b.buf = b.buf[:expLen]
31+	}
32+	copy(b.buf[pos:], p)
33+	return pLen, nil
34+}
35+
36+func (b *buffer) Read(p []byte) (n int, err error) {
37+	b.m.Lock()
38+	defer b.m.Unlock()
39+	if len(b.buf) <= b.off {
40+		if len(p) == 0 {
41+			return 0, nil
42+		}
43+		return 0, io.EOF
44+	}
45+	n = copy(p, b.buf[b.off:])
46+	b.off += n
47+	return n, nil
48+}
49+
50 type fakeWrite struct {
51 	fileEntry *utils.FileEntry
52 	handler   *handler
53-	buf       *bytes.Buffer
54+	buf       *buffer
55 }
56 
57 func (f fakeWrite) WriteAt(p []byte, off int64) (int, error) {
58-	return f.buf.Write(p)
59+	return f.buf.WriteAt(p, off)
60 }
61 
62 func (f fakeWrite) Close() error {