repos / pico

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

commit
5a1f3d8
parent
f0191d0
author
Antonio Mika
date
2023-11-15 16:03:44 +0000 UTC
Enable panic recovery in ssh and set logger to be based on debug
12 files changed,  +50, -12
M auth/auth.go
+1, -1
1@@ -289,7 +289,7 @@ func StartApiServer() {
2 		Port:   shared.GetEnv("AUTH_WEB_PORT", "3000"),
3 	}
4 
5-	logger := shared.CreateLogger()
6+	logger := shared.CreateLogger(true)
7 	db := postgres.NewDB(cfg.DbURL, logger)
8 	defer db.Close()
9 
M feeds/config.go
+1, -1
1@@ -50,7 +50,7 @@ func NewConfigSite() *shared.ConfigSite {
2 			Space:         "feeds",
3 			AllowedExt:    []string{".txt"},
4 			HiddenPosts:   []string{"_header.txt", "_readme.txt"},
5-			Logger:        shared.CreateLogger(),
6+			Logger:        shared.CreateLogger(debug == "1"),
7 			AllowRegister: allowRegister == "1",
8 		},
9 	}
M imgs/config.go
+1, -1
1@@ -66,7 +66,7 @@ func NewConfigSite() *shared.ConfigSite {
2 			IntroText:     intro,
3 			Space:         "imgs",
4 			AllowedExt:    []string{".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg"},
5-			Logger:        shared.CreateLogger(),
6+			Logger:        shared.CreateLogger(debug == "1"),
7 			AllowRegister: allowRegister == "1",
8 		},
9 	}
M lists/config.go
+1, -1
1@@ -49,7 +49,7 @@ func NewConfigSite() *shared.ConfigSite {
2 			Space:         "lists",
3 			AllowedExt:    []string{".txt"},
4 			HiddenPosts:   []string{"_header.txt", "_readme.txt"},
5-			Logger:        shared.CreateLogger(),
6+			Logger:        shared.CreateLogger(debug == "1"),
7 			AllowRegister: allowRegister == "1",
8 		},
9 	}
M pastes/config.go
+1, -1
1@@ -45,7 +45,7 @@ func NewConfigSite() *shared.ConfigSite {
2 			Description:   "a pastebin for hackers.",
3 			IntroText:     intro,
4 			Space:         "pastes",
5-			Logger:        shared.CreateLogger(),
6+			Logger:        shared.CreateLogger(debug == "1"),
7 			AllowRegister: allowRegister == "1",
8 		},
9 	}
M pgs/config.go
+1, -1
1@@ -79,7 +79,7 @@ func NewConfigSite() *shared.ConfigSite {
2 			},
3 			MaxSize:       maxSize,
4 			MaxAssetSize:  maxAssetSize,
5-			Logger:        shared.CreateLogger(),
6+			Logger:        shared.CreateLogger(debug == "1"),
7 			AllowRegister: allowRegister == "1",
8 		},
9 	}
M prose/config.go
+1, -1
1@@ -49,7 +49,7 @@ func NewConfigSite() *shared.ConfigSite {
2 			Space:         "prose",
3 			AllowedExt:    []string{".md"},
4 			HiddenPosts:   []string{"_readme.md", "_styles.css", "_footer.md"},
5-			Logger:        shared.CreateLogger(),
6+			Logger:        shared.CreateLogger(debug == "1"),
7 			AllowRegister: allowRegister == "1",
8 		},
9 	}
M shared/config.go
+12, -2
 1@@ -251,8 +251,18 @@ func (c *ConfigSite) AssetURL(username, projectName, fpath string) string {
 2 	)
 3 }
 4 
 5-func CreateLogger() *zap.SugaredLogger {
 6-	logger, err := zap.NewProduction()
 7+func CreateLogger(debug bool) *zap.SugaredLogger {
 8+	var (
 9+		err    error
10+		logger *zap.Logger
11+	)
12+
13+	if debug {
14+		logger, err = zap.NewDevelopment()
15+	} else {
16+		logger, err = zap.NewProduction()
17+	}
18+
19 	if err != nil {
20 		log.Fatal(err)
21 	}
M wish/send/auth/auth.go
+7, -0
 1@@ -9,6 +9,13 @@ import (
 2 func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
 3 	return func(sshHandler ssh.Handler) ssh.Handler {
 4 		return func(session ssh.Session) {
 5+			defer func() {
 6+				if r := recover(); r != nil {
 7+					writeHandler.GetLogger().Error("error running auth middleware: ", r)
 8+					_, _ = session.Stderr().Write([]byte("error running auth middleware\r\n"))
 9+				}
10+			}()
11+
12 			err := writeHandler.Validate(session)
13 			if err != nil {
14 				utils.ErrorHandler(session, err)
M wish/send/rsync/rsync.go
+9, -2
 1@@ -152,6 +152,13 @@ func (h *handler) Put(file *rsyncutils.ReceiverFile) (int64, error) {
 2 func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
 3 	return func(sshHandler ssh.Handler) ssh.Handler {
 4 		return func(session ssh.Session) {
 5+			defer func() {
 6+				if r := recover(); r != nil {
 7+					writeHandler.GetLogger().Error("error running rsync middleware: ", r)
 8+					_, _ = session.Stderr().Write([]byte("error running rsync middleware, check the flags you are using\r\n"))
 9+				}
10+			}()
11+
12 			cmd := session.Command()
13 			if len(cmd) == 0 || cmd[0] != "rsync" {
14 				sshHandler(session)
15@@ -193,7 +200,7 @@ func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
16 					}
17 
18 					if err := rsyncsender.ClientRun(opts, session, fileHandler, fileHandler.root, true); err != nil {
19-						writeHandler.GetLogger().Error("error running rsync sender:", err)
20+						writeHandler.GetLogger().Error("error running rsync sender: ", err)
21 					}
22 					return
23 				}
24@@ -224,7 +231,7 @@ func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
25 			}
26 
27 			if _, err := rsyncreceiver.ClientRun(opts, session, fileHandler, true); err != nil {
28-				writeHandler.GetLogger().Error("error running rsync receiver:", err)
29+				writeHandler.GetLogger().Error("error running rsync receiver: ", err)
30 			}
31 		}
32 	}
M wish/send/scp/scp.go
+7, -0
 1@@ -11,6 +11,13 @@ import (
 2 func Middleware(writeHandler utils.CopyFromClientHandler) wish.Middleware {
 3 	return func(sshHandler ssh.Handler) ssh.Handler {
 4 		return func(session ssh.Session) {
 5+			defer func() {
 6+				if r := recover(); r != nil {
 7+					writeHandler.GetLogger().Error("error running scp middleware: ", r)
 8+					_, _ = session.Stderr().Write([]byte("error running scp middleware, check the flags you are using\r\n"))
 9+				}
10+			}()
11+
12 			cmd := session.Command()
13 			if len(cmd) == 0 || cmd[0] != "scp" {
14 				sshHandler(session)
M wish/send/sftp/sftp.go
+8, -1
 1@@ -22,6 +22,13 @@ func SSHOption(writeHandler utils.CopyFromClientHandler) ssh.Option {
 2 
 3 func SubsystemHandler(writeHandler utils.CopyFromClientHandler) ssh.SubsystemHandler {
 4 	return func(session ssh.Session) {
 5+		defer func() {
 6+			if r := recover(); r != nil {
 7+				writeHandler.GetLogger().Error("error running sftp middleware: ", r)
 8+				_, _ = session.Stderr().Write([]byte("error running sftp middleware, check the flags you are using\r\n"))
 9+			}
10+		}()
11+
12 		err := writeHandler.Validate(session)
13 		if err != nil {
14 			utils.ErrorHandler(session, err)
15@@ -44,7 +51,7 @@ func SubsystemHandler(writeHandler utils.CopyFromClientHandler) ssh.SubsystemHan
16 
17 		err = requestServer.Serve()
18 		if err != nil && !errors.Is(err, io.EOF) {
19-			writeHandler.GetLogger().Error("Error serving sftp subsystem:", err)
20+			writeHandler.GetLogger().Error("Error serving sftp subsystem: ", err)
21 		}
22 	}
23 }