From bacc75393b0a5db81e5e843a451433e019d306bf Mon Sep 17 00:00:00 2001 From: karin0 Date: Sat, 24 Aug 2024 18:45:50 +0800 Subject: [PATCH] feat: support external controller and its override settings --- core/src/foss/golang/go.mod | 4 ++++ core/src/foss/golang/go.sum | 8 ++++++++ core/src/main/golang/go.mod | 4 ++++ core/src/main/golang/go.sum | 8 ++++++++ core/src/main/golang/native/config/load.go | 12 ++++++++++++ core/src/main/golang/native/config/process.go | 1 - .../clash/core/model/ConfigurationOverride.kt | 6 ++++++ .../kr328/clash/design/OverrideSettingsDesign.kt | 16 ++++++++++++++++ design/src/main/res/values/strings.xml | 2 ++ 9 files changed, 60 insertions(+), 1 deletion(-) diff --git a/core/src/foss/golang/go.mod b/core/src/foss/golang/go.mod index df201b63c..b79a292d7 100644 --- a/core/src/foss/golang/go.mod +++ b/core/src/foss/golang/go.mod @@ -11,6 +11,7 @@ require ( github.com/Kr328/tun2socket v0.0.0-20220414050025-d07c78d06d34 // indirect github.com/RyuaNerin/go-krypto v1.2.4 // indirect github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 // indirect + github.com/ajg/form v1.5.1 // indirect github.com/andybalholm/brotli v1.0.6 // indirect github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/buger/jsonparser v1.1.1 // indirect @@ -23,6 +24,9 @@ require ( github.com/ericlagergren/subtle v0.0.0-20220507045147-890d697da010 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gaukas/godicttls v0.0.4 // indirect + github.com/go-chi/chi/v5 v5.1.0 // indirect + github.com/go-chi/cors v1.2.1 // indirect + github.com/go-chi/render v1.0.3 // indirect github.com/go-ole/go-ole v1.3.0 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/gobwas/httphead v0.1.0 // indirect diff --git a/core/src/foss/golang/go.sum b/core/src/foss/golang/go.sum index 5c35e659d..23e05d641 100644 --- a/core/src/foss/golang/go.sum +++ b/core/src/foss/golang/go.sum @@ -7,6 +7,8 @@ github.com/RyuaNerin/go-krypto v1.2.4 h1:mXuNdK6M317aPV0llW6Xpjbo4moOlPF7Yxz4tb4 github.com/RyuaNerin/go-krypto v1.2.4/go.mod h1:QqCYkoutU3yInyD9INt2PGolVRsc3W4oraQadVGXJ/8= github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 h1:cDVUiFo+npB0ZASqnw4q90ylaVAbnYyx0JYqK4YcGok= github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344/go.mod h1:9pIqrY6SXNL8vjRQE5Hd/OL5GyK/9MrGUWs87z/eFfk= +github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU= +github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= @@ -39,6 +41,12 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gaukas/godicttls v0.0.4 h1:NlRaXb3J6hAnTmWdsEKb9bcSBD6BvcIjdGdeb0zfXbk= github.com/gaukas/godicttls v0.0.4/go.mod h1:l6EenT4TLWgTdwslVb4sEMOCf7Bv0JAK67deKr9/NCI= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= +github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= +github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= +github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= diff --git a/core/src/main/golang/go.mod b/core/src/main/golang/go.mod index 0502a5bca..3c85007de 100644 --- a/core/src/main/golang/go.mod +++ b/core/src/main/golang/go.mod @@ -20,6 +20,7 @@ require ( github.com/3andne/restls-client-go v0.1.6 // indirect github.com/RyuaNerin/go-krypto v1.2.4 // indirect github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 // indirect + github.com/ajg/form v1.5.1 // indirect github.com/andybalholm/brotli v1.0.6 // indirect github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/buger/jsonparser v1.1.1 // indirect @@ -31,6 +32,9 @@ require ( github.com/ericlagergren/subtle v0.0.0-20220507045147-890d697da010 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gaukas/godicttls v0.0.4 // indirect + github.com/go-chi/chi/v5 v5.1.0 // indirect + github.com/go-chi/cors v1.2.1 // indirect + github.com/go-chi/render v1.0.3 // indirect github.com/go-ole/go-ole v1.3.0 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/gobwas/httphead v0.1.0 // indirect diff --git a/core/src/main/golang/go.sum b/core/src/main/golang/go.sum index 5c35e659d..23e05d641 100644 --- a/core/src/main/golang/go.sum +++ b/core/src/main/golang/go.sum @@ -7,6 +7,8 @@ github.com/RyuaNerin/go-krypto v1.2.4 h1:mXuNdK6M317aPV0llW6Xpjbo4moOlPF7Yxz4tb4 github.com/RyuaNerin/go-krypto v1.2.4/go.mod h1:QqCYkoutU3yInyD9INt2PGolVRsc3W4oraQadVGXJ/8= github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 h1:cDVUiFo+npB0ZASqnw4q90ylaVAbnYyx0JYqK4YcGok= github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344/go.mod h1:9pIqrY6SXNL8vjRQE5Hd/OL5GyK/9MrGUWs87z/eFfk= +github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU= +github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk= @@ -39,6 +41,12 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gaukas/godicttls v0.0.4 h1:NlRaXb3J6hAnTmWdsEKb9bcSBD6BvcIjdGdeb0zfXbk= github.com/gaukas/godicttls v0.0.4/go.mod h1:l6EenT4TLWgTdwslVb4sEMOCf7Bv0JAK67deKr9/NCI= +github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= +github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= +github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= +github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= +github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= diff --git a/core/src/main/golang/native/config/load.go b/core/src/main/golang/native/config/load.go index 8b5114657..88efc176b 100644 --- a/core/src/main/golang/native/config/load.go +++ b/core/src/main/golang/native/config/load.go @@ -13,6 +13,7 @@ import ( "github.com/metacubex/mihomo/config" "github.com/metacubex/mihomo/hub/executor" + "github.com/metacubex/mihomo/hub/route" ) func logDns(cfg *config.RawConfig) { @@ -76,6 +77,17 @@ func Load(path string) error { return err } + // Start the external controller like in hub.Parse(), but we have set its + // default override value to end with ":0" for security. + // + // It would be difficult to update configurations for external controller + // inbound, so changes will require a restart after a profile is loaded. + if cfg.Controller.ExternalController != "" && !strings.HasSuffix(cfg.Controller.ExternalController, ":0") { + go route.Start(cfg.Controller.ExternalController, cfg.Controller.ExternalControllerTLS, + cfg.Controller.Secret, cfg.TLS.Certificate, cfg.TLS.PrivateKey, cfg.Controller.ExternalDohServer, + cfg.General.LogLevel == log.DEBUG) + } + executor.ApplyConfig(cfg, true) app.ApplySubtitlePattern(rawCfg.ClashForAndroid.UiSubtitlePattern) diff --git a/core/src/main/golang/native/config/process.go b/core/src/main/golang/native/config/process.go index f4f8be228..545f12770 100644 --- a/core/src/main/golang/native/config/process.go +++ b/core/src/main/golang/native/config/process.go @@ -41,7 +41,6 @@ func patchOverride(cfg *config.RawConfig, _ string) error { func patchGeneral(cfg *config.RawConfig, _ string) error { cfg.Interface = "" cfg.ExternalUI = "" - cfg.ExternalController = "" return nil } diff --git a/core/src/main/java/com/github/kr328/clash/core/model/ConfigurationOverride.kt b/core/src/main/java/com/github/kr328/clash/core/model/ConfigurationOverride.kt index a64baac84..8f87a6985 100644 --- a/core/src/main/java/com/github/kr328/clash/core/model/ConfigurationOverride.kt +++ b/core/src/main/java/com/github/kr328/clash/core/model/ConfigurationOverride.kt @@ -41,6 +41,12 @@ data class ConfigurationOverride( @SerialName("ipv6") var ipv6: Boolean? = null, + @SerialName("external-controller") + var externalController: String? = "127.0.0.1:0", + + @SerialName("secret") + var secret: String? = null, + @SerialName("hosts") var hosts: Map? = null, diff --git a/design/src/main/java/com/github/kr328/clash/design/OverrideSettingsDesign.kt b/design/src/main/java/com/github/kr328/clash/design/OverrideSettingsDesign.kt index 8622cbaa4..80426ff4d 100644 --- a/design/src/main/java/com/github/kr328/clash/design/OverrideSettingsDesign.kt +++ b/design/src/main/java/com/github/kr328/clash/design/OverrideSettingsDesign.kt @@ -185,6 +185,22 @@ class OverrideSettingsDesign( empty = R.string.default_ ) + editableText( + value = configuration::externalController, + adapter = NullableTextAdapter.String, + title = R.string.external_controller, + placeholder = R.string.dont_modify, + empty = R.string.default_ + ) + + editableText( + value = configuration::secret, + adapter = NullableTextAdapter.String, + title = R.string.secret, + placeholder = R.string.dont_modify, + empty = R.string.default_ + ) + selectableList( value = configuration::mode, values = arrayOf( diff --git a/design/src/main/res/values/strings.xml b/design/src/main/res/values/strings.xml index a6c87acda..cba90dcfa 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -144,6 +144,8 @@ Mode Log Level IPv6 + External Controller + Secret Hosts Sideload GEOIP External GEOIP database