diff --git a/docker-compose.yaml b/docker-compose.yaml index c1dd4d1e..4f1636d6 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -33,5 +33,19 @@ services: OPENFLARE_SERVER_URL: http://host.docker.internal:3000 OPENFLARE_DISCOVERY_TOKEN: 85464eeb72c49abc430569d6b9c77f78 LOG_LEVEL: "debug" + extra_hosts: + - "host.docker.internal:host-gateway" + + openflared: + build: + context: . + dockerfile: openflared/Dockerfile + container_name: openflared + network_mode: host + restart: unless-stopped + environment: + OPENFLARE_SERVER_URL: http://host.docker.internal:3000 + OPENFLARE_TUNNEL_TOKEN: 85464eeb72c49abc430569d6b9c77f78 + LOG_LEVEL: "debug" extra_hosts: - "host.docker.internal:host-gateway" \ No newline at end of file diff --git a/openflare_server/common/init.go b/openflare_server/common/init.go index b4a206b0..a28038d9 100644 --- a/openflare_server/common/init.go +++ b/openflare_server/common/init.go @@ -23,12 +23,17 @@ func printHelp() { fmt.Println("Usage: openflare [--port ] [--log-dir ] [--version] [--help]") } -func init() { +// ParseFlags 在命令行参数被任何 import 链上的 init() 误解析之前, +// 由各 binary 的 main() 显式调用一次。openflare_server 与 openflare-relay +// 共用 flag.CommandLine,必须先注册各自的 flag 再调用本函数。 +// 测试场景(go test)下不会执行本函数,单元测试可直接跳过命令行解析。 +func ParseFlags() { executableName := strings.ToLower(filepath.Base(os.Args[0])) isTest := strings.Contains(executableName, ".test") || flag.Lookup("test.v") != nil - if !isTest { - flag.Parse() + if isTest { + return } + flag.Parse() if *PrintVersion { fmt.Println(Version) diff --git a/openflare_server/main.go b/openflare_server/main.go index ffe28526..99f9d5d7 100644 --- a/openflare_server/main.go +++ b/openflare_server/main.go @@ -4,10 +4,6 @@ import ( "context" "embed" "fmt" - "github.com/gin-contrib/sessions" - "github.com/gin-contrib/sessions/cookie" - "github.com/gin-contrib/sessions/redis" - "github.com/gin-gonic/gin" "log/slog" "openflare/common" _ "openflare/docs" @@ -19,6 +15,11 @@ import ( "openflare/utils/geoip" "os" "strconv" + + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/cookie" + "github.com/gin-contrib/sessions/redis" + "github.com/gin-gonic/gin" ) //go:embed all:web/build @@ -41,6 +42,7 @@ var indexPage []byte // @name X-Agent-Token // @description Agent API 使用节点专属 Agent Token 或全局 Discovery Token func main() { + common.ParseFlags() common.SetupGinLog() slog.Info("OpenFlare started", "version", common.Version) if os.Getenv("GIN_MODE") != "debug" {