From 27fc9476fac7815451fadc25982af5f0a3010c82 Mon Sep 17 00:00:00 2001 From: Dominic Ferrando Date: Thu, 24 Apr 2025 00:14:09 -0400 Subject: [PATCH] Code cleanup --- internal/util/admins.go | 14 ++------- internal/util/cooldowns.go | 28 ----------------- internal/util/posts.go | 24 +------------- internal/util/util.go | 64 ++++++++++++++++++++++++++++++++++++++ web/main.go | 29 ++++++++--------- web/views/thread.templ | 4 +-- 6 files changed, 85 insertions(+), 78 deletions(-) create mode 100644 internal/util/util.go diff --git a/internal/util/admins.go b/internal/util/admins.go index 7d93275..93aff83 100644 --- a/internal/util/admins.go +++ b/internal/util/admins.go @@ -1,8 +1,6 @@ package util import ( - "crypto/rand" - "encoding/hex" "sync" "time" ) @@ -17,17 +15,11 @@ var ( AdminMutex = sync.RWMutex{} ) -func GenToken() (string, error) { - bytes := make([]byte, 32) - _, err := rand.Read(bytes) - if err != nil { - return "", err +func IsAdminSessionValid(token string) bool { + if DevMode { + return true } - return hex.EncodeToString(bytes), nil -} - -func IsAdminSessionValid(token string) bool { AdminMutex.RLock() session, exists := AdminSessions[token] AdminMutex.RUnlock() diff --git a/internal/util/cooldowns.go b/internal/util/cooldowns.go index 951da7b..addf4ee 100644 --- a/internal/util/cooldowns.go +++ b/internal/util/cooldowns.go @@ -1,11 +1,6 @@ package util import ( - "crypto/sha256" - "encoding/hex" - "net" - "net/http" - "strings" "sync" "time" ) @@ -19,12 +14,8 @@ var CooldownMutex sync.RWMutex const POST_COOLDOWN = 15 * time.Second -// const POST_COOLDOWN = 0 * time.Second - const THREAD_COOLDOWN = 2 * time.Minute -// const THREAD_COOLDOWN = 0 * time.Minute - func GetRemainingCooldown(ip string, m map[string]time.Time, duration time.Duration) time.Duration { CooldownMutex.RLock() last, exists := m[ip] @@ -44,22 +35,3 @@ func BeginCooldown(ip string, m map[string]time.Time, duration time.Duration) { m[ip] = time.Now() } } - -func GetIP(r *http.Request) string { - if xff := r.Header.Get("X-Forwarded-For"); xff != "" { - parts := strings.Split(xff, ",") - return strings.TrimSpace(parts[0]) - } - - // Fallback to RemoteAddr - ip, _, err := net.SplitHostPort(r.RemoteAddr) - if err != nil { - return r.RemoteAddr - } - return ip -} - -func HashIp(ip string) string { - checksum := sha256.Sum256([]byte(ip)) - return hex.EncodeToString(checksum[:]) -} diff --git a/internal/util/posts.go b/internal/util/posts.go index 25424a0..846e864 100644 --- a/internal/util/posts.go +++ b/internal/util/posts.go @@ -232,29 +232,7 @@ func GetPostFileInfo(mediaPath string) PostFileInfo { return result } -func FormatBytes(bytes int64) string { - const ( - KB = 1 << 10 // 1024 - MB = 1 << 20 - GB = 1 << 30 - TB = 1 << 40 - ) - - switch { - case bytes >= TB: - return fmt.Sprintf("%.2f TB", float64(bytes)/float64(TB)) - case bytes >= GB: - return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB)) - case bytes >= MB: - return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB)) - case bytes >= KB: - return fmt.Sprintf("%.2f KB", float64(bytes)/float64(KB)) - default: - return fmt.Sprintf("%d B", bytes) - } -} - -func FormatFileInfo(fileInfo PostFileInfo) string { +func FormatPostFileInfo(fileInfo PostFileInfo) string { humanSize := FormatBytes(fileInfo.Size) if fileInfo.IsVideo { return fmt.Sprintf("(%s)", humanSize) diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 0000000..971147c --- /dev/null +++ b/internal/util/util.go @@ -0,0 +1,64 @@ +package util + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/hex" + "fmt" + "net" + "net/http" + "strings" +) + +const DevMode = false + +func FormatBytes(bytes int64) string { + const ( + KB = 1 << 10 // 1024 + MB = 1 << 20 + GB = 1 << 30 + TB = 1 << 40 + ) + + switch { + case bytes >= TB: + return fmt.Sprintf("%.2f TB", float64(bytes)/float64(TB)) + case bytes >= GB: + return fmt.Sprintf("%.2f GB", float64(bytes)/float64(GB)) + case bytes >= MB: + return fmt.Sprintf("%.2f MB", float64(bytes)/float64(MB)) + case bytes >= KB: + return fmt.Sprintf("%.2f KB", float64(bytes)/float64(KB)) + default: + return fmt.Sprintf("%d B", bytes) + } +} + +func GetIP(r *http.Request) string { + if xff := r.Header.Get("X-Forwarded-For"); xff != "" { + parts := strings.Split(xff, ",") + return strings.TrimSpace(parts[0]) + } + + // Fallback to RemoteAddr + ip, _, err := net.SplitHostPort(r.RemoteAddr) + if err != nil { + return r.RemoteAddr + } + return ip +} + +func HashIp(ip string) string { + checksum := sha256.Sum256([]byte(ip)) + return hex.EncodeToString(checksum[:]) +} + +func GenToken() (string, error) { + bytes := make([]byte, 32) + _, err := rand.Read(bytes) + if err != nil { + return "", err + } + + return hex.EncodeToString(bytes), nil +} diff --git a/web/main.go b/web/main.go index 8fdb06e..d2f9709 100644 --- a/web/main.go +++ b/web/main.go @@ -4,15 +4,6 @@ import ( "database/sql" "errors" "fmt" - "io" - "log" - "mime/multipart" - "net/http" - "path/filepath" - "strconv" - "strings" - "time" - "github.com/dominicf2001/comfychan/internal/database" "github.com/dominicf2001/comfychan/internal/util" "github.com/dominicf2001/comfychan/web/views" @@ -21,10 +12,16 @@ import ( "github.com/go-chi/chi/v5/middleware" _ "github.com/mattn/go-sqlite3" "golang.org/x/crypto/bcrypt" + "io" + "log" + "mime/multipart" + "net/http" + "path/filepath" + "strconv" + "strings" + "time" ) -var dev = true - func AdminOnlyMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c, err := r.Cookie("comfy_admin") @@ -43,7 +40,7 @@ func AdminOnlyMiddleware(next http.Handler) http.Handler { } func disableCacheInDevMode(next http.Handler) http.Handler { - if !dev { + if !util.DevMode { return next } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -61,6 +58,10 @@ func SumUniquePostIps(posts []database.Post) int { } func isAdmin(r *http.Request) bool { + if util.DevMode { + return true + } + admin := false if c, err := r.Cookie("comfy_admin"); err == nil { admin = util.IsAdminSessionValid(c.Value) @@ -487,7 +488,7 @@ func main() { Name: "comfy_admin", Value: token, HttpOnly: true, - Secure: !dev, + Secure: !util.DevMode, Expires: tokenValidUntil, SameSite: http.SameSiteStrictMode, Path: "/", @@ -516,7 +517,7 @@ func main() { Name: "comfy_admin", Value: "", HttpOnly: true, - Secure: !dev, + Secure: !util.DevMode, Expires: time.Now(), SameSite: http.SameSiteStrictMode, Path: "/", diff --git a/web/views/thread.templ b/web/views/thread.templ index 5e52ef6..1627aaf 100644 --- a/web/views/thread.templ +++ b/web/views/thread.templ @@ -76,7 +76,7 @@ templ PostOriginal(post database.Post, thread database.Thread) { { post.MediaPath }
- { util.FormatFileInfo(util.GetPostFileInfo(post.MediaPath)) } + { util.FormatPostFileInfo(util.GetPostFileInfo(post.MediaPath)) }
- { util.FormatFileInfo(util.GetPostFileInfo(post.MediaPath)) } + { util.FormatPostFileInfo(util.GetPostFileInfo(post.MediaPath)) }