132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
// main.go
|
|
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"siteproxy/auth"
|
|
"siteproxy/cache"
|
|
"siteproxy/config"
|
|
"siteproxy/proxy"
|
|
"siteproxy/security"
|
|
)
|
|
|
|
func main() {
|
|
// 加载配置
|
|
cfg := config.LoadFromEnv()
|
|
|
|
log.Printf("Starting Secure Site Proxy...")
|
|
log.Printf("Session timeout: %v", cfg.SessionTimeout)
|
|
log.Printf("Rate limit: %d requests per %v", cfg.RateLimit, cfg.RateLimitWindow)
|
|
log.Printf("Cache enabled: %v (max: %d MB)", cfg.CacheEnabled, cfg.CacheMaxSize/1024/1024)
|
|
|
|
// 加载模板
|
|
templates, err := loadTemplates()
|
|
if err != nil {
|
|
log.Fatalf("Failed to load templates: %v", err)
|
|
}
|
|
|
|
// 初始化组件
|
|
sessionMgr := auth.NewSessionManager(cfg.SessionTimeout)
|
|
authMw := auth.NewAuthMiddleware(cfg.Username, cfg.Password, sessionMgr, templates)
|
|
|
|
// 转换 BlockedDomains 为 map
|
|
blockedDomainsMap := make(map[string]bool)
|
|
for _, domain := range cfg.BlockedDomains {
|
|
blockedDomainsMap[domain] = true
|
|
}
|
|
|
|
validator := security.NewRequestValidator(
|
|
blockedDomainsMap,
|
|
cfg.BlockedCIDRs,
|
|
cfg.AllowedSchemes,
|
|
)
|
|
|
|
rateLimiter := security.NewRateLimiter(cfg.RateLimit, cfg.RateLimitWindow)
|
|
|
|
var memCache *cache.MemoryCache
|
|
if cfg.CacheEnabled {
|
|
memCache = cache.NewMemoryCache(cfg.CacheMaxSize, cfg.CacheTTL)
|
|
} else {
|
|
memCache = cache.NewMemoryCache(0, 0)
|
|
}
|
|
|
|
proxyHandler := proxy.NewHandler(
|
|
validator,
|
|
rateLimiter,
|
|
memCache,
|
|
cfg.UserAgent,
|
|
cfg.MaxResponseSize,
|
|
)
|
|
|
|
indexHandler := proxy.NewIndexHandler(templates)
|
|
statsHandler := proxy.NewStatsHandler(memCache)
|
|
|
|
// 设置路由
|
|
mux := http.NewServeMux()
|
|
|
|
// 公开路由
|
|
mux.HandleFunc("/login", authMw.Login)
|
|
mux.HandleFunc("/health", healthCheck)
|
|
|
|
// 受保护路由
|
|
mux.Handle("/", authMw.Require(indexHandler))
|
|
mux.Handle("/proxy", authMw.Require(proxyHandler))
|
|
mux.Handle("/stats", authMw.Require(statsHandler))
|
|
mux.HandleFunc("/logout", authMw.Logout)
|
|
|
|
// 启动服务器
|
|
addr := ":" + cfg.Port
|
|
|
|
log.Printf("Server listening on %s", addr)
|
|
log.Printf("Login with username: %s", cfg.Username)
|
|
log.Printf("Access at: http://localhost:%s", cfg.Port)
|
|
|
|
if err := http.ListenAndServe(addr, mux); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func loadTemplates() (*template.Template, error) {
|
|
// 尝试从多个位置加载模板
|
|
templateDirs := []string{
|
|
"templates",
|
|
"./templates",
|
|
"/app/templates",
|
|
}
|
|
|
|
var templateDir string
|
|
for _, dir := range templateDirs {
|
|
if _, err := os.Stat(dir); err == nil {
|
|
templateDir = dir
|
|
break
|
|
}
|
|
}
|
|
|
|
if templateDir == "" {
|
|
return nil, os.ErrNotExist
|
|
}
|
|
|
|
log.Printf("Loading templates from: %s", templateDir)
|
|
|
|
// 加载所有 .html 文件
|
|
pattern := filepath.Join(templateDir, "*.html")
|
|
tmpl, err := template.ParseGlob(pattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
log.Printf("Loaded templates: %v", tmpl.DefinedTemplates())
|
|
|
|
return tmpl, nil
|
|
}
|
|
|
|
func healthCheck(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write([]byte(`{"status":"ok","version":"1.0.0"}`))
|
|
}
|