Files
GoTunnel/internal/server/config/config.go
Flik d1058f9e89
All checks were successful
Build Multi-Platform Binaries / build-frontend (push) Successful in 38s
Build Multi-Platform Binaries / build-binaries (amd64, linux, client, true) (push) Successful in 1m47s
Build Multi-Platform Binaries / build-binaries (amd64, darwin, server, false) (push) Successful in 1m48s
Build Multi-Platform Binaries / build-binaries (amd64, windows, client, true) (push) Successful in 1m29s
Build Multi-Platform Binaries / build-binaries (amd64, linux, server, true) (push) Successful in 1m53s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, client, true) (push) Successful in 1m14s
Build Multi-Platform Binaries / build-binaries (amd64, windows, server, true) (push) Successful in 1m40s
Build Multi-Platform Binaries / build-binaries (arm64, darwin, server, false) (push) Successful in 1m36s
Build Multi-Platform Binaries / build-binaries (arm, 7, linux, server, true) (push) Successful in 1m51s
Build Multi-Platform Binaries / build-binaries (arm64, linux, client, true) (push) Successful in 1m17s
Build Multi-Platform Binaries / build-binaries (arm64, linux, server, true) (push) Successful in 1m50s
Build Multi-Platform Binaries / build-binaries (arm64, windows, server, false) (push) Successful in 1m18s
feat(app): 添加流量统计功能和服务器配置管理
- 在WebServer中添加TrafficStore存储接口
- 将Web配置从根级别移动到Server.Web子结构下
- 移除Web配置中的BindAddr字段并调整默认值逻辑
- 在前端HomeView中替换模拟流量数据显示真实统计数据
- 添加流量统计API接口(/traffic/stats和/traffic/hourly)
- 实现SQLite数据库流量统计表创建和CRUD操作
- 在Relay包中添加带流量统计的数据转发功能
- 在设置页面添加服务器配置编辑和保存功能
- 创建流量统计处理器和相关数据模型定义
2026-01-22 19:53:40 +08:00

143 lines
3.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"crypto/rand"
"encoding/hex"
"os"
"gopkg.in/yaml.v3"
)
// ServerConfig 服务端配置
type ServerConfig struct {
Server ServerSettings `yaml:"server"`
PluginStore PluginStoreSettings `yaml:"plugin_store"`
JSPlugins []JSPluginConfig `yaml:"js_plugins,omitempty"`
}
// JSPluginConfig JS 插件配置
type JSPluginConfig struct {
Name string `yaml:"name"`
Path string `yaml:"path"` // JS 文件路径
SigPath string `yaml:"sig_path,omitempty"` // 签名文件路径 (默认为 path + ".sig")
AutoPush []string `yaml:"auto_push,omitempty"` // 自动推送到的客户端 ID 列表
Config map[string]string `yaml:"config,omitempty"` // 插件配置
AutoStart bool `yaml:"auto_start,omitempty"` // 是否自动启动
}
// PluginStoreSettings 插件仓库设置
type PluginStoreSettings struct {
URL string `yaml:"url"` // 插件仓库 URL为空则使用默认值
}
// 默认插件仓库 URL
const DefaultPluginStoreURL = "https://git.92coco.cn/flik/GoTunnel-Plugins/raw/branch/main/store.json"
// GetPluginStoreURL 获取插件仓库 URL
func (s *PluginStoreSettings) GetPluginStoreURL() string {
if s.URL != "" {
return s.URL
}
return DefaultPluginStoreURL
}
// ServerSettings 服务端设置
type ServerSettings struct {
BindAddr string `yaml:"bind_addr"`
BindPort int `yaml:"bind_port"`
Token string `yaml:"token"`
HeartbeatSec int `yaml:"heartbeat_sec"`
HeartbeatTimeout int `yaml:"heartbeat_timeout"`
DBPath string `yaml:"db_path"`
TLSDisabled bool `yaml:"tls_disabled"`
Web WebSettings `yaml:"web"`
}
// WebSettings Web控制台设置
type WebSettings struct {
Enabled bool `yaml:"enabled"`
BindPort int `yaml:"bind_port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
}
// LoadServerConfig 加载服务端配置
func LoadServerConfig(path string) (*ServerConfig, error) {
var cfg ServerConfig
// 尝试读取配置文件,不存在则使用默认配置
data, err := os.ReadFile(path)
if err == nil {
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
}
// 设置默认值
setDefaults(&cfg)
return &cfg, nil
}
// setDefaults 设置默认值
func setDefaults(cfg *ServerConfig) {
if cfg.Server.BindAddr == "" {
cfg.Server.BindAddr = "0.0.0.0"
}
if cfg.Server.BindPort == 0 {
cfg.Server.BindPort = 7000
}
if cfg.Server.HeartbeatSec == 0 {
cfg.Server.HeartbeatSec = 30
}
if cfg.Server.HeartbeatTimeout == 0 {
cfg.Server.HeartbeatTimeout = 90
}
if cfg.Server.DBPath == "" {
cfg.Server.DBPath = "gotunnel.db"
}
// Web 默认启用
if cfg.Server.Web.BindPort == 0 {
cfg.Server.Web.BindPort = 7500
cfg.Server.Web.Enabled = true
}
// Token 未配置时自动生成 32 位
if cfg.Server.Token == "" {
cfg.Server.Token = generateToken(32)
}
}
// generateToken 生成随机 token
func generateToken(length int) string {
bytes := make([]byte, length/2)
n, err := rand.Read(bytes)
if err != nil || n != len(bytes) {
// 安全关键:随机数生成失败时 panic
panic("crypto/rand failed: unable to generate secure token")
}
return hex.EncodeToString(bytes)
}
// GenerateWebCredentials 生成 Web 控制台凭据
func GenerateWebCredentials(cfg *ServerConfig) bool {
if cfg.Server.Web.Username == "" {
cfg.Server.Web.Username = "admin"
}
if cfg.Server.Web.Password == "" {
cfg.Server.Web.Password = generateToken(16)
return true // 表示生成了新密码
}
return false
}
// SaveServerConfig 保存服务端配置
func SaveServerConfig(path string, cfg *ServerConfig) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}