update sqlite
All checks were successful
Build Multi-Platform Binaries / build (push) Successful in 13m17s

This commit is contained in:
Flik
2025-12-25 20:15:31 +08:00
parent 7100362cd7
commit db5bd942d3
8 changed files with 237 additions and 31 deletions

View File

@@ -7,6 +7,7 @@ import (
"log"
"net/http"
"github.com/gotunnel/internal/server/config"
"github.com/gotunnel/internal/server/db"
"github.com/gotunnel/internal/server/router"
)
@@ -45,13 +46,17 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
type WebServer struct {
ClientStore db.ClientStore
Server router.ServerInterface
Config *config.ServerConfig
ConfigPath string
}
// NewWebServer 创建Web服务
func NewWebServer(cs db.ClientStore, srv router.ServerInterface) *WebServer {
func NewWebServer(cs db.ClientStore, srv router.ServerInterface, cfg *config.ServerConfig, cfgPath string) *WebServer {
return &WebServer{
ClientStore: cs,
Server: srv,
Config: cfg,
ConfigPath: cfgPath,
}
}
@@ -110,3 +115,18 @@ func (w *WebServer) GetClientStore() db.ClientStore {
func (w *WebServer) GetServer() router.ServerInterface {
return w.Server
}
// GetConfig 获取配置
func (w *WebServer) GetConfig() *config.ServerConfig {
return w.Config
}
// GetConfigPath 获取配置文件路径
func (w *WebServer) GetConfigPath() string {
return w.ConfigPath
}
// SaveConfig 保存配置
func (w *WebServer) SaveConfig() error {
return config.SaveServerConfig(w.ConfigPath, w.Config)
}

View File

@@ -1,6 +1,8 @@
package config
import (
"crypto/rand"
"encoding/hex"
"os"
"gopkg.in/yaml.v3"
@@ -33,17 +35,24 @@ type WebSettings struct {
// LoadServerConfig 加载服务端配置
func LoadServerConfig(path string) (*ServerConfig, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var cfg ServerConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, err
// 尝试读取配置文件,不存在则使用默认配置
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"
}
@@ -60,13 +69,33 @@ func LoadServerConfig(path string) (*ServerConfig, error) {
cfg.Server.DBPath = "gotunnel.db"
}
// Web 默认
// Web 默认启用
if cfg.Web.BindAddr == "" {
cfg.Web.BindAddr = "0.0.0.0"
}
if cfg.Web.BindPort == 0 {
cfg.Web.BindPort = 7500
cfg.Web.Enabled = true
}
return &cfg, nil
// Token 未配置时自动生成 32 位
if cfg.Server.Token == "" {
cfg.Server.Token = generateToken(32)
}
}
// generateToken 生成随机 token
func generateToken(length int) string {
bytes := make([]byte, length/2)
rand.Read(bytes)
return hex.EncodeToString(bytes)
}
// SaveServerConfig 保存服务端配置
func SaveServerConfig(path string, cfg *ServerConfig) error {
data, err := yaml.Marshal(cfg)
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}

View File

@@ -5,7 +5,7 @@ import (
"encoding/json"
"sync"
_ "github.com/mattn/go-sqlite3"
_ "modernc.org/sqlite"
"github.com/gotunnel/pkg/protocol"
)
@@ -18,7 +18,7 @@ type SQLiteStore struct {
// NewSQLiteStore 创建 SQLite 存储
func NewSQLiteStore(dbPath string) (*SQLiteStore, error) {
db, err := sql.Open("sqlite3", dbPath)
db, err := sql.Open("sqlite", dbPath)
if err != nil {
return nil, err
}

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"github.com/gotunnel/internal/server/config"
"github.com/gotunnel/internal/server/db"
"github.com/gotunnel/pkg/protocol"
)
@@ -32,12 +33,16 @@ type ServerInterface interface {
type AppInterface interface {
GetClientStore() db.ClientStore
GetServer() ServerInterface
GetConfig() *config.ServerConfig
GetConfigPath() string
SaveConfig() error
}
// APIHandler API处理器
type APIHandler struct {
clientStore db.ClientStore
server ServerInterface
app AppInterface
}
// RegisterRoutes 注册所有 API 路由
@@ -45,12 +50,14 @@ func RegisterRoutes(r *Router, app AppInterface) {
h := &APIHandler{
clientStore: app.GetClientStore(),
server: app.GetServer(),
app: app,
}
api := r.Group("/api")
api.HandleFunc("/status", h.handleStatus)
api.HandleFunc("/clients", h.handleClients)
api.HandleFunc("/client/", h.handleClient)
api.HandleFunc("/config", h.handleConfig)
api.HandleFunc("/config/reload", h.handleReload)
}
@@ -198,6 +205,100 @@ func (h *APIHandler) deleteClient(rw http.ResponseWriter, clientID string) {
h.jsonResponse(rw, map[string]string{"status": "ok"})
}
func (h *APIHandler) handleConfig(rw http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
h.getConfig(rw)
case http.MethodPut:
h.updateConfig(rw, r)
default:
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)
}
}
func (h *APIHandler) getConfig(rw http.ResponseWriter) {
cfg := h.app.GetConfig()
h.jsonResponse(rw, map[string]interface{}{
"server": map[string]interface{}{
"bind_addr": cfg.Server.BindAddr,
"bind_port": cfg.Server.BindPort,
"token": cfg.Server.Token,
"heartbeat_sec": cfg.Server.HeartbeatSec,
"heartbeat_timeout": cfg.Server.HeartbeatTimeout,
},
"web": map[string]interface{}{
"enabled": cfg.Web.Enabled,
"bind_addr": cfg.Web.BindAddr,
"bind_port": cfg.Web.BindPort,
"username": cfg.Web.Username,
"password": cfg.Web.Password,
},
})
}
func (h *APIHandler) updateConfig(rw http.ResponseWriter, r *http.Request) {
var req struct {
Server *struct {
BindAddr string `json:"bind_addr"`
BindPort int `json:"bind_port"`
Token string `json:"token"`
HeartbeatSec int `json:"heartbeat_sec"`
HeartbeatTimeout int `json:"heartbeat_timeout"`
} `json:"server"`
Web *struct {
Enabled bool `json:"enabled"`
BindAddr string `json:"bind_addr"`
BindPort int `json:"bind_port"`
Username string `json:"username"`
Password string `json:"password"`
} `json:"web"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(rw, err.Error(), http.StatusBadRequest)
return
}
cfg := h.app.GetConfig()
// 更新 Server 配置
if req.Server != nil {
if req.Server.BindAddr != "" {
cfg.Server.BindAddr = req.Server.BindAddr
}
if req.Server.BindPort > 0 {
cfg.Server.BindPort = req.Server.BindPort
}
if req.Server.Token != "" {
cfg.Server.Token = req.Server.Token
}
if req.Server.HeartbeatSec > 0 {
cfg.Server.HeartbeatSec = req.Server.HeartbeatSec
}
if req.Server.HeartbeatTimeout > 0 {
cfg.Server.HeartbeatTimeout = req.Server.HeartbeatTimeout
}
}
// 更新 Web 配置
if req.Web != nil {
cfg.Web.Enabled = req.Web.Enabled
if req.Web.BindAddr != "" {
cfg.Web.BindAddr = req.Web.BindAddr
}
if req.Web.BindPort > 0 {
cfg.Web.BindPort = req.Web.BindPort
}
cfg.Web.Username = req.Web.Username
cfg.Web.Password = req.Web.Password
}
if err := h.app.SaveConfig(); err != nil {
http.Error(rw, err.Error(), http.StatusInternalServerError)
return
}
h.jsonResponse(rw, map[string]string{"status": "ok"})
}
func (h *APIHandler) handleReload(rw http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(rw, "Method not allowed", http.StatusMethodNotAllowed)