CookieRefresh will no longer crash bridge if Xenforo refuses to give a new cookie because its already logged in.

This commit is contained in:
Salastil
2025-10-22 15:57:15 -04:00
parent 1c409694fd
commit ad58e4720c
2 changed files with 360 additions and 586 deletions

52
main.go
View File

@@ -1,11 +1,9 @@
package main
import (
"io"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
@@ -23,62 +21,20 @@ func main() {
}
}
// Load configuration
cfg, err := config.Load(envFile)
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
// Parse --debug flag from CLI (overrides .env)
debug := false
for _, a := range os.Args {
if a == "--debug" {
debug = true
}
}
cfg.Debug = debug
// ---- File logging (env-driven) ------------------------------------------
// Use LOG_FILE or BRIDGE_LOG_FILE (first non-empty wins)
logPath := os.Getenv("LOG_FILE")
if logPath == "" {
logPath = os.Getenv("BRIDGE_LOG_FILE")
}
if logPath != "" {
// Ensure parent dir exists
if dir := filepath.Dir(logPath); dir != "" && dir != "." {
_ = os.MkdirAll(dir, 0o755)
}
f, ferr := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if ferr != nil {
log.Printf("⚠️ Failed to open log file '%s' (%v). Continuing with stdout only.", logPath, ferr)
} else {
// Tee logs to both stdout and file
log.SetOutput(io.MultiWriter(os.Stdout, f))
// microseconds for tighter timing on auth/debug traces
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.Printf("📝 File logging enabled: %s", logPath)
}
} else {
// keep default stdout with microseconds for consistency
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
}
// -------------------------------------------------------------------------
log.Printf("Using .env file: %s", envFile)
log.Printf("Using Sneedchat room ID: %d", cfg.SneedchatRoomID)
log.Printf("Bridge username: %s", cfg.BridgeUsername)
if cfg.Debug {
log.Println("🪲 Debug mode enabled — full HTTP and cookie trace logging active")
}
// Cookie service (HTTP/1.1/2, KiwiFlare PoW, CSRF, deep debug)
cookieSvc, err := cookie.NewCookieRefreshService(cfg.BridgeUsername, cfg.BridgePassword, "kiwifarms.st", cfg.Debug)
// Cookie service
cookieSvc, err := cookie.NewCookieRefreshService(cfg.BridgeUsername, cfg.BridgePassword, "kiwifarms.st")
if err != nil {
log.Fatalf("Failed to create cookie service: %v", err)
}
cookieSvc.Start()
log.Println("⏳ Waiting for initial cookie...")
cookieSvc.WaitForCookie()
if cookieSvc.GetCurrentCookie() == "" {
log.Fatal("❌ Failed to obtain initial cookie, cannot start bridge")
@@ -87,7 +43,7 @@ func main() {
// Sneedchat client
sneedClient := sneed.NewClient(cfg.SneedchatRoomID, cookieSvc)
// Discord bridge (full parity features)
// Discord bridge
bridge, err := discord.NewBridge(cfg, sneedClient)
if err != nil {
log.Fatalf("Failed to create Discord bridge: %v", err)
@@ -97,7 +53,7 @@ func main() {
}
log.Println("🌉 Discord-Sneedchat Bridge started successfully")
// Auto cookie refresh every 4h (in addition to background loop inside service)
// Background 4h refresh loop
go func() {
t := time.NewTicker(4 * time.Hour)
defer t.Stop()