2025-09-20 07:35:50 +00:00
|
|
|
|
// Package web provides the main web server implementation for the 3x-ui panel,
|
|
|
|
|
|
// including HTTP/HTTPS serving, routing, templates, and background job scheduling.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
package web
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
2025-10-07 22:42:52 +00:00
|
|
|
|
"crypto/sha256"
|
2023-02-09 19:18:06 +00:00
|
|
|
|
"crypto/tls"
|
|
|
|
|
|
"embed"
|
|
|
|
|
|
"html/template"
|
|
|
|
|
|
"io"
|
|
|
|
|
|
"io/fs"
|
|
|
|
|
|
"net"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"time"
|
2024-03-10 21:31:24 +00:00
|
|
|
|
|
2025-12-06 14:55:36 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
|
|
2025-09-19 08:05:43 +00:00
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/config"
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/logger"
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/util/common"
|
2025-12-06 14:55:36 +00:00
|
|
|
|
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/util/redis"
|
2025-09-19 08:05:43 +00:00
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/controller"
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/job"
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/locale"
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/middleware"
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/network"
|
|
|
|
|
|
"github.com/mhsanaei/3x-ui/v2/web/service"
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
2023-12-04 20:48:16 +00:00
|
|
|
|
"github.com/gin-contrib/gzip"
|
2025-10-07 22:42:52 +00:00
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
|
|
"github.com/gin-contrib/sessions/cookie"
|
2023-02-09 19:18:06 +00:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-12 11:04:36 +00:00
|
|
|
|
//go:embed assets
|
2023-02-09 19:18:06 +00:00
|
|
|
|
var assetsFS embed.FS
|
|
|
|
|
|
|
|
|
|
|
|
//go:embed html/*
|
|
|
|
|
|
var htmlFS embed.FS
|
|
|
|
|
|
|
|
|
|
|
|
//go:embed translation/*
|
|
|
|
|
|
var i18nFS embed.FS
|
|
|
|
|
|
|
|
|
|
|
|
var startTime = time.Now()
|
|
|
|
|
|
|
|
|
|
|
|
type wrapAssetsFS struct {
|
|
|
|
|
|
embed.FS
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f *wrapAssetsFS) Open(name string) (fs.File, error) {
|
|
|
|
|
|
file, err := f.FS.Open("assets/" + name)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
return &wrapAssetsFile{File: file}, nil
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type wrapAssetsFile struct {
|
|
|
|
|
|
fs.File
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f *wrapAssetsFile) Stat() (fs.FileInfo, error) {
|
|
|
|
|
|
info, err := f.File.Stat()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
return &wrapAssetsFileInfo{FileInfo: info}, nil
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type wrapAssetsFileInfo struct {
|
|
|
|
|
|
fs.FileInfo
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (f *wrapAssetsFileInfo) ModTime() time.Time {
|
|
|
|
|
|
return startTime
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
|
// EmbeddedHTML returns the embedded HTML templates filesystem for reuse by other servers.
|
2025-10-07 21:42:05 +00:00
|
|
|
|
func EmbeddedHTML() embed.FS { return htmlFS }
|
2025-09-14 18:16:40 +00:00
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
|
// EmbeddedAssets returns the embedded assets filesystem for reuse by other servers.
|
2025-10-07 21:42:05 +00:00
|
|
|
|
func EmbeddedAssets() embed.FS { return assetsFS }
|
2025-09-14 18:16:40 +00:00
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
|
// Server represents the main web server for the 3x-ui panel with controllers, services, and scheduled jobs.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
type Server struct {
|
|
|
|
|
|
httpServer *http.Server
|
|
|
|
|
|
listener net.Listener
|
|
|
|
|
|
|
2025-09-24 09:47:14 +00:00
|
|
|
|
index *controller.IndexController
|
|
|
|
|
|
panel *controller.XUIController
|
|
|
|
|
|
api *controller.APIController
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
|
|
|
|
|
xrayService service.XrayService
|
|
|
|
|
|
settingService service.SettingService
|
2023-03-17 16:07:49 +00:00
|
|
|
|
tgbotService service.Tgbot
|
2025-12-06 14:55:36 +00:00
|
|
|
|
wsService *service.WebSocketService
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
|
|
|
|
|
cron *cron.Cron
|
|
|
|
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
|
// NewServer creates a new web server instance with a cancellable context.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
func NewServer() *Server {
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2025-10-07 21:42:05 +00:00
|
|
|
|
return &Server{ctx: ctx, cancel: cancel}
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
|
// getHtmlFiles walks the local `web/html` directory and returns a list of
|
|
|
|
|
|
// template file paths. Used only in debug/development mode.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
func (s *Server) getHtmlFiles() ([]string, error) {
|
|
|
|
|
|
files := make([]string, 0)
|
|
|
|
|
|
dir, _ := os.Getwd()
|
|
|
|
|
|
err := fs.WalkDir(os.DirFS(dir), "web/html", func(path string, d fs.DirEntry, err error) error {
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if d.IsDir() {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
files = append(files, path)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return files, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// getHtmlTemplate parses embedded HTML templates from the bundled `htmlFS`.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
func (s *Server) getHtmlTemplate(funcMap template.FuncMap) (*template.Template, error) {
|
|
|
|
|
|
t := template.New("").Funcs(funcMap)
|
|
|
|
|
|
err := fs.WalkDir(htmlFS, "html", func(path string, d fs.DirEntry, err error) error {
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
if d.IsDir() {
|
|
|
|
|
|
newT, err := t.ParseFS(htmlFS, path+"/*.html")
|
|
|
|
|
|
if err != nil {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// ignore folders without matches
|
2023-02-09 19:18:06 +00:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
t = newT
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil
|
|
|
|
|
|
})
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return t, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// initRouter initializes Gin, registers middleware, templates, static assets,
|
|
|
|
|
|
// controllers and returns the configured engine.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
func (s *Server) initRouter() (*gin.Engine, error) {
|
|
|
|
|
|
if config.IsDebug() {
|
|
|
|
|
|
gin.SetMode(gin.DebugMode)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
gin.DefaultWriter = io.Discard
|
|
|
|
|
|
gin.DefaultErrorWriter = io.Discard
|
|
|
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
engine := gin.Default()
|
2023-04-09 19:43:18 +00:00
|
|
|
|
|
2025-10-07 22:42:52 +00:00
|
|
|
|
// получаем домен и секрет/базовый путь из настроек
|
2023-05-30 20:54:18 +00:00
|
|
|
|
webDomain, err := s.settingService.GetWebDomain()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
if webDomain != "" {
|
|
|
|
|
|
engine.Use(middleware.DomainValidatorMiddleware(webDomain))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 22:42:52 +00:00
|
|
|
|
// вот ЭТО должно быть раньше, чем блок с сессиями:
|
|
|
|
|
|
secret, err := s.settingService.GetSecret()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:45:58 +00:00
|
|
|
|
basePath, err := s.settingService.GetBasePath()
|
|
|
|
|
|
if err != nil {
|
2025-10-07 22:42:52 +00:00
|
|
|
|
return nil, err
|
2025-10-07 21:45:58 +00:00
|
|
|
|
}
|
2025-10-07 22:42:52 +00:00
|
|
|
|
|
|
|
|
|
|
// cookie-сессии на базе секретного ключа
|
|
|
|
|
|
key := sha256.Sum256([]byte(secret))
|
|
|
|
|
|
store := cookie.NewStore(key[:])
|
|
|
|
|
|
store.Options(sessions.Options{
|
|
|
|
|
|
Path: basePath,
|
|
|
|
|
|
HttpOnly: true,
|
|
|
|
|
|
Secure: false, // если HTTPS — поставить true
|
|
|
|
|
|
SameSite: http.SameSiteLaxMode,
|
|
|
|
|
|
})
|
|
|
|
|
|
engine.Use(sessions.Sessions("xui_sess", store))
|
|
|
|
|
|
|
2025-12-06 14:55:36 +00:00
|
|
|
|
// Initialize Redis (in-memory fallback)
|
|
|
|
|
|
redis.Init("", "", 0) // Uses in-memory fallback
|
|
|
|
|
|
|
|
|
|
|
|
// Security middlewares (configurable)
|
|
|
|
|
|
rateLimitEnabled, _ := s.settingService.GetRateLimitEnabled()
|
|
|
|
|
|
if rateLimitEnabled {
|
|
|
|
|
|
rateLimitRequests, _ := s.settingService.GetRateLimitRequests()
|
|
|
|
|
|
if rateLimitRequests <= 0 {
|
|
|
|
|
|
rateLimitRequests = 60
|
|
|
|
|
|
}
|
|
|
|
|
|
rateLimitBurst, _ := s.settingService.GetRateLimitBurst()
|
|
|
|
|
|
if rateLimitBurst <= 0 {
|
|
|
|
|
|
rateLimitBurst = 10
|
|
|
|
|
|
}
|
|
|
|
|
|
config := middleware.RateLimitConfig{
|
|
|
|
|
|
RequestsPerMinute: rateLimitRequests,
|
|
|
|
|
|
BurstSize: rateLimitBurst,
|
|
|
|
|
|
KeyFunc: func(c *gin.Context) string {
|
|
|
|
|
|
return c.ClientIP()
|
|
|
|
|
|
},
|
|
|
|
|
|
SkipPaths: []string{basePath + "assets/", "/favicon.ico"},
|
|
|
|
|
|
}
|
|
|
|
|
|
engine.Use(middleware.RateLimitMiddleware(config))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ipFilterEnabled, _ := s.settingService.GetIPFilterEnabled()
|
|
|
|
|
|
if ipFilterEnabled {
|
|
|
|
|
|
whitelistEnabled, _ := s.settingService.GetIPWhitelistEnabled()
|
|
|
|
|
|
blacklistEnabled, _ := s.settingService.GetIPBlacklistEnabled()
|
|
|
|
|
|
engine.Use(middleware.IPFilterMiddleware(middleware.IPFilterConfig{
|
|
|
|
|
|
WhitelistEnabled: whitelistEnabled,
|
|
|
|
|
|
BlacklistEnabled: blacklistEnabled,
|
|
|
|
|
|
GeoIPEnabled: false, // TODO: Add GeoIP config
|
|
|
|
|
|
SkipPaths: []string{basePath + "assets/", "/favicon.ico"},
|
|
|
|
|
|
}))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
engine.Use(middleware.SessionSecurityMiddleware())
|
|
|
|
|
|
|
|
|
|
|
|
// Audit logging middleware (after session check)
|
|
|
|
|
|
engine.Use(middleware.AuditMiddleware())
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// gzip, excluding API path to avoid double-compressing JSON where needed
|
|
|
|
|
|
engine.Use(gzip.Gzip(
|
|
|
|
|
|
gzip.DefaultCompression,
|
|
|
|
|
|
gzip.WithExcludedPaths([]string{basePath + "panel/api/"}),
|
|
|
|
|
|
))
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// i18n in templates
|
2023-05-20 22:59:27 +00:00
|
|
|
|
i18nWebFunc := func(key string, params ...string) string {
|
2023-05-20 15:16:05 +00:00
|
|
|
|
return locale.I18n(locale.Web, key, params...)
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
funcMap := template.FuncMap{"i18n": i18nWebFunc}
|
2025-09-12 11:04:36 +00:00
|
|
|
|
engine.SetFuncMap(funcMap)
|
2023-05-20 15:16:05 +00:00
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// Static files & templates
|
2023-02-09 19:18:06 +00:00
|
|
|
|
if config.IsDebug() {
|
|
|
|
|
|
files, err := s.getHtmlFiles()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
engine.LoadHTMLFiles(files...)
|
|
|
|
|
|
engine.StaticFS(basePath+"assets", http.FS(os.DirFS("web/assets")))
|
|
|
|
|
|
} else {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
tpl, err := s.getHtmlTemplate(funcMap)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
engine.SetHTMLTemplate(tpl)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
engine.StaticFS(basePath+"assets", http.FS(&wrapAssetsFS{FS: assetsFS}))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// API
|
|
|
|
|
|
api := engine.Group(basePath + "panel/api")
|
|
|
|
|
|
{
|
2025-10-07 21:45:58 +00:00
|
|
|
|
// controller.NewAuthController(api)
|
2025-10-07 21:42:05 +00:00
|
|
|
|
controller.NewUserAdminController(api)
|
2025-12-06 14:55:36 +00:00
|
|
|
|
|
|
|
|
|
|
// New feature controllers
|
|
|
|
|
|
controller.NewAuditController(api)
|
|
|
|
|
|
controller.NewAnalyticsController(api)
|
|
|
|
|
|
controller.NewQuotaController(api)
|
|
|
|
|
|
controller.NewOnboardingController(api)
|
|
|
|
|
|
controller.NewReportsController(api)
|
2025-10-07 21:42:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Redirects (/xui -> /panel etc.)
|
2023-05-30 20:54:18 +00:00
|
|
|
|
engine.Use(middleware.RedirectMiddleware(basePath))
|
2023-05-13 21:12:08 +00:00
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// Web UI groups
|
2023-02-09 19:18:06 +00:00
|
|
|
|
g := engine.Group(basePath)
|
|
|
|
|
|
s.index = controller.NewIndexController(g)
|
2023-05-12 18:06:05 +00:00
|
|
|
|
s.panel = controller.NewXUIController(g)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
s.api = controller.NewAPIController(g)
|
|
|
|
|
|
|
2025-12-06 14:55:36 +00:00
|
|
|
|
// WebSocket for real-time updates
|
|
|
|
|
|
s.wsService = service.NewWebSocketService(s.xrayService)
|
|
|
|
|
|
go s.wsService.Run()
|
|
|
|
|
|
controller.NewWebSocketController(g, s.wsService)
|
|
|
|
|
|
|
2025-10-01 23:47:12 +00:00
|
|
|
|
// Chrome DevTools endpoint for debugging web apps
|
|
|
|
|
|
engine.GET("/.well-known/appspecific/com.chrome.devtools.json", func(c *gin.Context) {
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{})
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// 404 handler
|
2025-09-24 19:30:58 +00:00
|
|
|
|
engine.NoRoute(func(c *gin.Context) {
|
|
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2023-02-09 19:18:06 +00:00
|
|
|
|
return engine, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// startTask schedules background jobs (Xray checks, traffic jobs, cron jobs).
|
2023-02-09 19:18:06 +00:00
|
|
|
|
func (s *Server) startTask() {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
if err := s.xrayService.RestartXray(true); err != nil {
|
2023-02-09 19:18:06 +00:00
|
|
|
|
logger.Warning("start xray failed:", err)
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
|
2024-01-01 15:07:56 +00:00
|
|
|
|
// Check whether xray is running every second
|
|
|
|
|
|
s.cron.AddJob("@every 1s", job.NewCheckXrayRunningJob())
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
2024-01-01 15:07:56 +00:00
|
|
|
|
// Check if xray needs to be restarted every 30 seconds
|
|
|
|
|
|
s.cron.AddFunc("@every 30s", func() {
|
2023-09-06 14:52:37 +00:00
|
|
|
|
if s.xrayService.IsNeedRestartAndSetFalse() {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
if err := s.xrayService.RestartXray(false); err != nil {
|
2023-09-06 14:52:37 +00:00
|
|
|
|
logger.Error("restart xray failed:", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// Traffic stats every 10s (with initial 5s delay)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
go func() {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
time.Sleep(5 * time.Second)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
s.cron.AddJob("@every 10s", job.NewXrayTrafficJob())
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// Client IP checks & maintenance
|
2023-08-01 20:58:16 +00:00
|
|
|
|
s.cron.AddJob("@every 10s", job.NewCheckClientIpJob())
|
2023-08-24 13:44:51 +00:00
|
|
|
|
s.cron.AddJob("@daily", job.NewClearLogsJob())
|
2023-07-01 12:26:43 +00:00
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// Periodic traffic resets
|
2025-09-16 11:41:05 +00:00
|
|
|
|
s.cron.AddJob("@daily", job.NewPeriodicTrafficResetJob("daily"))
|
|
|
|
|
|
s.cron.AddJob("@weekly", job.NewPeriodicTrafficResetJob("weekly"))
|
|
|
|
|
|
s.cron.AddJob("@monthly", job.NewPeriodicTrafficResetJob("monthly"))
|
2025-09-16 07:24:32 +00:00
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// LDAP sync
|
2025-10-01 23:47:12 +00:00
|
|
|
|
if ldapEnabled, _ := s.settingService.GetLdapEnable(); ldapEnabled {
|
|
|
|
|
|
runtime, err := s.settingService.GetLdapSyncCron()
|
|
|
|
|
|
if err != nil || runtime == "" {
|
|
|
|
|
|
runtime = "@every 1m"
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
s.cron.AddJob(runtime, job.NewLdapSyncJob())
|
2025-10-01 23:47:12 +00:00
|
|
|
|
}
|
2025-09-28 19:00:16 +00:00
|
|
|
|
|
2025-12-06 14:55:36 +00:00
|
|
|
|
// Quota check (configurable interval)
|
|
|
|
|
|
quotaInterval, err := s.settingService.GetQuotaCheckInterval()
|
|
|
|
|
|
if err != nil || quotaInterval <= 0 {
|
|
|
|
|
|
quotaInterval = 5 // Default 5 minutes
|
|
|
|
|
|
}
|
|
|
|
|
|
s.cron.AddJob(fmt.Sprintf("@every %dm", quotaInterval), job.NewQuotaCheckJob())
|
|
|
|
|
|
|
|
|
|
|
|
// Weekly reports every Monday at 9 AM
|
|
|
|
|
|
s.cron.AddJob("0 9 * * 1", job.NewReportsJob())
|
|
|
|
|
|
|
|
|
|
|
|
// Monthly reports on 1st of month at 9 AM
|
|
|
|
|
|
s.cron.AddFunc("0 9 1 * *", func() {
|
|
|
|
|
|
job.NewReportsJob().RunMonthly()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Audit log cleanup daily at 2 AM
|
|
|
|
|
|
s.cron.AddJob("0 2 * * *", job.NewAuditCleanupJob())
|
|
|
|
|
|
|
|
|
|
|
|
// Clean expired Redis entries hourly
|
|
|
|
|
|
s.cron.AddFunc("@hourly", func() {
|
|
|
|
|
|
redis.CleanExpired()
|
|
|
|
|
|
})
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// Telegram bot related jobs
|
|
|
|
|
|
if isTgbotenabled, err := s.settingService.GetTgbotEnabled(); (err == nil) && isTgbotenabled {
|
2023-02-09 19:18:06 +00:00
|
|
|
|
runtime, err := s.settingService.GetTgbotRuntime()
|
|
|
|
|
|
if err != nil || runtime == "" {
|
2023-05-20 15:13:59 +00:00
|
|
|
|
logger.Errorf("Add NewStatsNotifyJob error[%s], Runtime[%s] invalid, will run default", err, runtime)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
runtime = "@daily"
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
logger.Infof("Tg notify enabled, run at %s", runtime)
|
|
|
|
|
|
if _, err = s.cron.AddJob(runtime, job.NewStatsNotifyJob()); err != nil {
|
2023-02-09 19:18:06 +00:00
|
|
|
|
logger.Warning("Add NewStatsNotifyJob error", err)
|
|
|
|
|
|
}
|
2023-05-20 15:59:28 +00:00
|
|
|
|
s.cron.AddJob("@every 2m", job.NewCheckHashStorageJob())
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
if cpuThreshold, err := s.settingService.GetTgCpu(); (err == nil) && (cpuThreshold > 0) {
|
2023-03-17 16:07:49 +00:00
|
|
|
|
s.cron.AddJob("@every 10s", job.NewCheckCpuJob())
|
|
|
|
|
|
}
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// Start initializes and starts the web server.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
func (s *Server) Start() (err error) {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err != nil {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
_ = s.Stop()
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
loc, err := s.settingService.GetTimeLocation()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
s.cron = cron.New(cron.WithLocation(loc), cron.WithSeconds())
|
|
|
|
|
|
s.cron.Start()
|
|
|
|
|
|
|
|
|
|
|
|
engine, err := s.initRouter()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
certFile, err := s.settingService.GetCertFile()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
keyFile, err := s.settingService.GetKeyFile()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
listen, err := s.settingService.GetListen()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
port, err := s.settingService.GetPort()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
|
2023-02-09 19:18:06 +00:00
|
|
|
|
listenAddr := net.JoinHostPort(listen, strconv.Itoa(port))
|
|
|
|
|
|
listener, err := net.Listen("tcp", listenAddr)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
|
2023-02-09 19:18:06 +00:00
|
|
|
|
if certFile != "" || keyFile != "" {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
if cert, err := tls.LoadX509KeyPair(certFile, keyFile); err == nil {
|
|
|
|
|
|
cfg := &tls.Config{Certificates: []tls.Certificate{cert}}
|
2024-02-25 16:07:03 +00:00
|
|
|
|
listener = network.NewAutoHttpsListener(listener)
|
2025-10-07 21:42:05 +00:00
|
|
|
|
listener = tls.NewListener(listener, cfg)
|
2024-07-08 21:08:00 +00:00
|
|
|
|
logger.Info("Web server running HTTPS on", listener.Addr())
|
2024-02-25 16:07:03 +00:00
|
|
|
|
} else {
|
2024-07-08 21:08:00 +00:00
|
|
|
|
logger.Error("Error loading certificates:", err)
|
|
|
|
|
|
logger.Info("Web server running HTTP on", listener.Addr())
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2024-07-08 21:08:00 +00:00
|
|
|
|
logger.Info("Web server running HTTP on", listener.Addr())
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
s.listener = listener
|
|
|
|
|
|
s.httpServer = &http.Server{Handler: engine}
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
|
|
|
|
|
go func() {
|
2025-10-07 21:42:05 +00:00
|
|
|
|
_ = s.httpServer.Serve(listener)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
}()
|
|
|
|
|
|
|
2024-05-09 17:45:12 +00:00
|
|
|
|
s.startTask()
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
if isTgbotenabled, err := s.settingService.GetTgbotEnabled(); (err == nil) && isTgbotenabled {
|
2023-03-17 16:07:49 +00:00
|
|
|
|
tgBot := s.tgbotService.NewTgbot()
|
2023-05-20 15:38:01 +00:00
|
|
|
|
tgBot.Start(i18nFS)
|
2023-03-17 16:07:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-02-09 19:18:06 +00:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
|
// Stop gracefully shuts down the web server, stops Xray, cron jobs, and Telegram bot.
|
2023-02-09 19:18:06 +00:00
|
|
|
|
func (s *Server) Stop() error {
|
|
|
|
|
|
s.cancel()
|
|
|
|
|
|
s.xrayService.StopXray()
|
|
|
|
|
|
if s.cron != nil {
|
|
|
|
|
|
s.cron.Stop()
|
|
|
|
|
|
}
|
2023-05-20 15:09:01 +00:00
|
|
|
|
if s.tgbotService.IsRunning() {
|
2023-03-17 16:07:49 +00:00
|
|
|
|
s.tgbotService.Stop()
|
|
|
|
|
|
}
|
2025-10-07 21:42:05 +00:00
|
|
|
|
var err1, err2 error
|
2023-02-09 19:18:06 +00:00
|
|
|
|
if s.httpServer != nil {
|
|
|
|
|
|
err1 = s.httpServer.Shutdown(s.ctx)
|
|
|
|
|
|
}
|
|
|
|
|
|
if s.listener != nil {
|
|
|
|
|
|
err2 = s.listener.Close()
|
|
|
|
|
|
}
|
|
|
|
|
|
return common.Combine(err1, err2)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-07 21:42:05 +00:00
|
|
|
|
// GetCtx returns the server's context.
|
|
|
|
|
|
func (s *Server) GetCtx() context.Context { return s.ctx }
|
2023-02-09 19:18:06 +00:00
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
|
// GetCron returns the server's cron scheduler instance.
|
2025-10-07 21:42:05 +00:00
|
|
|
|
func (s *Server) GetCron() *cron.Cron { return s.cron }
|