configs.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package route
  2. import (
  3. "net/http"
  4. "net/netip"
  5. "path/filepath"
  6. "github.com/metacubex/mihomo/adapter/inbound"
  7. "github.com/metacubex/mihomo/component/dialer"
  8. "github.com/metacubex/mihomo/component/resolver"
  9. "github.com/metacubex/mihomo/component/updater"
  10. "github.com/metacubex/mihomo/config"
  11. C "github.com/metacubex/mihomo/constant"
  12. "github.com/metacubex/mihomo/hub/executor"
  13. P "github.com/metacubex/mihomo/listener"
  14. LC "github.com/metacubex/mihomo/listener/config"
  15. "github.com/metacubex/mihomo/log"
  16. "github.com/metacubex/mihomo/tunnel"
  17. "github.com/go-chi/chi/v5"
  18. "github.com/go-chi/render"
  19. )
  20. func configRouter() http.Handler {
  21. r := chi.NewRouter()
  22. r.Get("/", getConfigs)
  23. r.Put("/", updateConfigs)
  24. r.Post("/geo", updateGeoDatabases)
  25. r.Patch("/", patchConfigs)
  26. return r
  27. }
  28. type configSchema struct {
  29. Port *int `json:"port"`
  30. SocksPort *int `json:"socks-port"`
  31. RedirPort *int `json:"redir-port"`
  32. TProxyPort *int `json:"tproxy-port"`
  33. MixedPort *int `json:"mixed-port"`
  34. Tun *tunSchema `json:"tun"`
  35. TuicServer *tuicServerSchema `json:"tuic-server"`
  36. ShadowSocksConfig *string `json:"ss-config"`
  37. VmessConfig *string `json:"vmess-config"`
  38. TcptunConfig *string `json:"tcptun-config"`
  39. UdptunConfig *string `json:"udptun-config"`
  40. AllowLan *bool `json:"allow-lan"`
  41. SkipAuthPrefixes *[]netip.Prefix `json:"skip-auth-prefixes"`
  42. LanAllowedIPs *[]netip.Prefix `json:"lan-allowed-ips"`
  43. LanDisAllowedIPs *[]netip.Prefix `json:"lan-disallowed-ips"`
  44. BindAddress *string `json:"bind-address"`
  45. Mode *tunnel.TunnelMode `json:"mode"`
  46. LogLevel *log.LogLevel `json:"log-level"`
  47. IPv6 *bool `json:"ipv6"`
  48. Sniffing *bool `json:"sniffing"`
  49. TcpConcurrent *bool `json:"tcp-concurrent"`
  50. InterfaceName *string `json:"interface-name"`
  51. }
  52. type tunSchema struct {
  53. Enable bool `yaml:"enable" json:"enable"`
  54. Device *string `yaml:"device" json:"device"`
  55. Stack *C.TUNStack `yaml:"stack" json:"stack"`
  56. DNSHijack *[]string `yaml:"dns-hijack" json:"dns-hijack"`
  57. AutoRoute *bool `yaml:"auto-route" json:"auto-route"`
  58. AutoDetectInterface *bool `yaml:"auto-detect-interface" json:"auto-detect-interface"`
  59. //RedirectToTun []string `yaml:"-" json:"-"`
  60. MTU *uint32 `yaml:"mtu" json:"mtu,omitempty"`
  61. GSO *bool `yaml:"gso" json:"gso,omitempty"`
  62. GSOMaxSize *uint32 `yaml:"gso-max-size" json:"gso-max-size,omitempty"`
  63. //Inet4Address *[]netip.Prefix `yaml:"inet4-address" json:"inet4-address,omitempty"`
  64. Inet6Address *[]netip.Prefix `yaml:"inet6-address" json:"inet6-address,omitempty"`
  65. IPRoute2TableIndex *int `yaml:"iproute2-table-index" json:"iproute2_table_index,omitempty"`
  66. IPRoute2RuleIndex *int `yaml:"iproute2-rule-index" json:"iproute2_rule_index,omitempty"`
  67. AutoRedirect *bool `yaml:"auto-redirect" json:"auto_redirect,omitempty"`
  68. AutoRedirectInputMark *uint32 `yaml:"auto-redirect-input-mark" json:"auto_redirect_input_mark,omitempty"`
  69. AutoRedirectOutputMark *uint32 `yaml:"auto-redirect-output-mark" json:"auto_redirect_output_mark,omitempty"`
  70. StrictRoute *bool `yaml:"strict-route" json:"strict-route,omitempty"`
  71. RouteAddress *[]netip.Prefix `yaml:"route-address" json:"route_address,omitempty"`
  72. RouteAddressSet *[]string `yaml:"route-address-set" json:"route_address_set,omitempty"`
  73. RouteExcludeAddress *[]netip.Prefix `yaml:"route-exclude-address" json:"route_exclude_address,omitempty"`
  74. RouteExcludeAddressSet *[]string `yaml:"route-exclude-address-set" json:"route_exclude_address_set,omitempty"`
  75. IncludeInterface *[]string `yaml:"include-interface" json:"include-interface,omitempty"`
  76. ExcludeInterface *[]string `yaml:"exclude-interface" json:"exclude-interface,omitempty"`
  77. IncludeUID *[]uint32 `yaml:"include-uid" json:"include-uid,omitempty"`
  78. IncludeUIDRange *[]string `yaml:"include-uid-range" json:"include-uid-range,omitempty"`
  79. ExcludeUID *[]uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"`
  80. ExcludeUIDRange *[]string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"`
  81. IncludeAndroidUser *[]int `yaml:"include-android-user" json:"include-android-user,omitempty"`
  82. IncludePackage *[]string `yaml:"include-package" json:"include-package,omitempty"`
  83. ExcludePackage *[]string `yaml:"exclude-package" json:"exclude-package,omitempty"`
  84. EndpointIndependentNat *bool `yaml:"endpoint-independent-nat" json:"endpoint-independent-nat,omitempty"`
  85. UDPTimeout *int64 `yaml:"udp-timeout" json:"udp-timeout,omitempty"`
  86. FileDescriptor *int `yaml:"file-descriptor" json:"file-descriptor"`
  87. Inet4RouteAddress *[]netip.Prefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"`
  88. Inet6RouteAddress *[]netip.Prefix `yaml:"inet6-route-address" json:"inet6-route-address,omitempty"`
  89. Inet4RouteExcludeAddress *[]netip.Prefix `yaml:"inet4-route-exclude-address" json:"inet4-route-exclude-address,omitempty"`
  90. Inet6RouteExcludeAddress *[]netip.Prefix `yaml:"inet6-route-exclude-address" json:"inet6-route-exclude-address,omitempty"`
  91. }
  92. type tuicServerSchema struct {
  93. Enable bool `yaml:"enable" json:"enable"`
  94. Listen *string `yaml:"listen" json:"listen"`
  95. Token *[]string `yaml:"token" json:"token"`
  96. Users *map[string]string `yaml:"users" json:"users,omitempty"`
  97. Certificate *string `yaml:"certificate" json:"certificate"`
  98. PrivateKey *string `yaml:"private-key" json:"private-key"`
  99. CongestionController *string `yaml:"congestion-controller" json:"congestion-controller,omitempty"`
  100. MaxIdleTime *int `yaml:"max-idle-time" json:"max-idle-time,omitempty"`
  101. AuthenticationTimeout *int `yaml:"authentication-timeout" json:"authentication-timeout,omitempty"`
  102. ALPN *[]string `yaml:"alpn" json:"alpn,omitempty"`
  103. MaxUdpRelayPacketSize *int `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"`
  104. CWND *int `yaml:"cwnd" json:"cwnd,omitempty"`
  105. }
  106. func getConfigs(w http.ResponseWriter, r *http.Request) {
  107. general := executor.GetGeneral()
  108. render.JSON(w, r, general)
  109. }
  110. func pointerOrDefault(p *int, def int) int {
  111. if p != nil {
  112. return *p
  113. }
  114. return def
  115. }
  116. func pointerOrDefaultString(p *string, def string) string {
  117. if p != nil {
  118. return *p
  119. }
  120. return def
  121. }
  122. func pointerOrDefaultTun(p *tunSchema, def LC.Tun) LC.Tun {
  123. if p != nil {
  124. def.Enable = p.Enable
  125. if p.Device != nil {
  126. def.Device = *p.Device
  127. }
  128. if p.Stack != nil {
  129. def.Stack = *p.Stack
  130. }
  131. if p.DNSHijack != nil {
  132. def.DNSHijack = *p.DNSHijack
  133. }
  134. if p.AutoRoute != nil {
  135. def.AutoRoute = *p.AutoRoute
  136. }
  137. if p.AutoDetectInterface != nil {
  138. def.AutoDetectInterface = *p.AutoDetectInterface
  139. }
  140. if p.MTU != nil {
  141. def.MTU = *p.MTU
  142. }
  143. if p.GSO != nil {
  144. def.GSO = *p.GSO
  145. }
  146. if p.GSOMaxSize != nil {
  147. def.GSOMaxSize = *p.GSOMaxSize
  148. }
  149. //if p.Inet4Address != nil {
  150. // def.Inet4Address = *p.Inet4Address
  151. //}
  152. if p.Inet6Address != nil {
  153. def.Inet6Address = *p.Inet6Address
  154. }
  155. if p.IPRoute2TableIndex != nil {
  156. def.IPRoute2TableIndex = *p.IPRoute2TableIndex
  157. }
  158. if p.IPRoute2RuleIndex != nil {
  159. def.IPRoute2RuleIndex = *p.IPRoute2RuleIndex
  160. }
  161. if p.AutoRedirect != nil {
  162. def.AutoRedirect = *p.AutoRedirect
  163. }
  164. if p.AutoRedirectInputMark != nil {
  165. def.AutoRedirectInputMark = *p.AutoRedirectInputMark
  166. }
  167. if p.AutoRedirectOutputMark != nil {
  168. def.AutoRedirectOutputMark = *p.AutoRedirectOutputMark
  169. }
  170. if p.StrictRoute != nil {
  171. def.StrictRoute = *p.StrictRoute
  172. }
  173. if p.RouteAddress != nil {
  174. def.RouteAddress = *p.RouteAddress
  175. }
  176. if p.RouteAddressSet != nil {
  177. def.RouteAddressSet = *p.RouteAddressSet
  178. }
  179. if p.RouteExcludeAddress != nil {
  180. def.RouteExcludeAddress = *p.RouteExcludeAddress
  181. }
  182. if p.RouteExcludeAddressSet != nil {
  183. def.RouteExcludeAddressSet = *p.RouteExcludeAddressSet
  184. }
  185. if p.Inet4RouteAddress != nil {
  186. def.Inet4RouteAddress = *p.Inet4RouteAddress
  187. }
  188. if p.Inet6RouteAddress != nil {
  189. def.Inet6RouteAddress = *p.Inet6RouteAddress
  190. }
  191. if p.Inet4RouteExcludeAddress != nil {
  192. def.Inet4RouteExcludeAddress = *p.Inet4RouteExcludeAddress
  193. }
  194. if p.Inet6RouteExcludeAddress != nil {
  195. def.Inet6RouteExcludeAddress = *p.Inet6RouteExcludeAddress
  196. }
  197. if p.IncludeInterface != nil {
  198. def.IncludeInterface = *p.IncludeInterface
  199. }
  200. if p.ExcludeInterface != nil {
  201. def.ExcludeInterface = *p.ExcludeInterface
  202. }
  203. if p.IncludeUID != nil {
  204. def.IncludeUID = *p.IncludeUID
  205. }
  206. if p.IncludeUIDRange != nil {
  207. def.IncludeUIDRange = *p.IncludeUIDRange
  208. }
  209. if p.ExcludeUID != nil {
  210. def.ExcludeUID = *p.ExcludeUID
  211. }
  212. if p.ExcludeUIDRange != nil {
  213. def.ExcludeUIDRange = *p.ExcludeUIDRange
  214. }
  215. if p.IncludeAndroidUser != nil {
  216. def.IncludeAndroidUser = *p.IncludeAndroidUser
  217. }
  218. if p.IncludePackage != nil {
  219. def.IncludePackage = *p.IncludePackage
  220. }
  221. if p.ExcludePackage != nil {
  222. def.ExcludePackage = *p.ExcludePackage
  223. }
  224. if p.EndpointIndependentNat != nil {
  225. def.EndpointIndependentNat = *p.EndpointIndependentNat
  226. }
  227. if p.UDPTimeout != nil {
  228. def.UDPTimeout = *p.UDPTimeout
  229. }
  230. if p.FileDescriptor != nil {
  231. def.FileDescriptor = *p.FileDescriptor
  232. }
  233. }
  234. return def
  235. }
  236. func pointerOrDefaultTuicServer(p *tuicServerSchema, def LC.TuicServer) LC.TuicServer {
  237. if p != nil {
  238. def.Enable = p.Enable
  239. if p.Listen != nil {
  240. def.Listen = *p.Listen
  241. }
  242. if p.Token != nil {
  243. def.Token = *p.Token
  244. }
  245. if p.Users != nil {
  246. def.Users = *p.Users
  247. }
  248. if p.Certificate != nil {
  249. def.Certificate = *p.Certificate
  250. }
  251. if p.PrivateKey != nil {
  252. def.PrivateKey = *p.PrivateKey
  253. }
  254. if p.CongestionController != nil {
  255. def.CongestionController = *p.CongestionController
  256. }
  257. if p.MaxIdleTime != nil {
  258. def.MaxIdleTime = *p.MaxIdleTime
  259. }
  260. if p.AuthenticationTimeout != nil {
  261. def.AuthenticationTimeout = *p.AuthenticationTimeout
  262. }
  263. if p.ALPN != nil {
  264. def.ALPN = *p.ALPN
  265. }
  266. if p.MaxUdpRelayPacketSize != nil {
  267. def.MaxUdpRelayPacketSize = *p.MaxUdpRelayPacketSize
  268. }
  269. if p.CWND != nil {
  270. def.CWND = *p.CWND
  271. }
  272. }
  273. return def
  274. }
  275. func patchConfigs(w http.ResponseWriter, r *http.Request) {
  276. general := &configSchema{}
  277. if err := render.DecodeJSON(r.Body, &general); err != nil {
  278. render.Status(r, http.StatusBadRequest)
  279. render.JSON(w, r, ErrBadRequest)
  280. return
  281. }
  282. if general.AllowLan != nil {
  283. P.SetAllowLan(*general.AllowLan)
  284. }
  285. if general.SkipAuthPrefixes != nil {
  286. inbound.SetSkipAuthPrefixes(*general.SkipAuthPrefixes)
  287. }
  288. if general.LanAllowedIPs != nil {
  289. inbound.SetAllowedIPs(*general.LanAllowedIPs)
  290. }
  291. if general.LanDisAllowedIPs != nil {
  292. inbound.SetDisAllowedIPs(*general.LanDisAllowedIPs)
  293. }
  294. if general.BindAddress != nil {
  295. P.SetBindAddress(*general.BindAddress)
  296. }
  297. if general.Sniffing != nil {
  298. tunnel.SetSniffing(*general.Sniffing)
  299. }
  300. if general.TcpConcurrent != nil {
  301. dialer.SetTcpConcurrent(*general.TcpConcurrent)
  302. }
  303. if general.InterfaceName != nil {
  304. dialer.DefaultInterface.Store(*general.InterfaceName)
  305. }
  306. ports := P.GetPorts()
  307. P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port), tunnel.Tunnel)
  308. P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort), tunnel.Tunnel)
  309. P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tunnel.Tunnel)
  310. P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tunnel.Tunnel)
  311. P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tunnel.Tunnel)
  312. P.ReCreateTun(pointerOrDefaultTun(general.Tun, P.LastTunConf), tunnel.Tunnel)
  313. P.ReCreateShadowSocks(pointerOrDefaultString(general.ShadowSocksConfig, ports.ShadowSocksConfig), tunnel.Tunnel)
  314. P.ReCreateVmess(pointerOrDefaultString(general.VmessConfig, ports.VmessConfig), tunnel.Tunnel)
  315. P.ReCreateTuic(pointerOrDefaultTuicServer(general.TuicServer, P.LastTuicConf), tunnel.Tunnel)
  316. if general.Mode != nil {
  317. tunnel.SetMode(*general.Mode)
  318. }
  319. if general.LogLevel != nil {
  320. log.SetLevel(*general.LogLevel)
  321. }
  322. if general.IPv6 != nil {
  323. resolver.DisableIPv6 = !*general.IPv6
  324. }
  325. render.NoContent(w, r)
  326. }
  327. func updateConfigs(w http.ResponseWriter, r *http.Request) {
  328. req := struct {
  329. Path string `json:"path"`
  330. Payload string `json:"payload"`
  331. }{}
  332. if err := render.DecodeJSON(r.Body, &req); err != nil {
  333. render.Status(r, http.StatusBadRequest)
  334. render.JSON(w, r, ErrBadRequest)
  335. return
  336. }
  337. force := r.URL.Query().Get("force") == "true"
  338. var cfg *config.Config
  339. var err error
  340. if req.Payload != "" {
  341. cfg, err = executor.ParseWithBytes([]byte(req.Payload))
  342. if err != nil {
  343. render.Status(r, http.StatusBadRequest)
  344. render.JSON(w, r, newError(err.Error()))
  345. return
  346. }
  347. } else {
  348. if req.Path == "" {
  349. req.Path = C.Path.Config()
  350. }
  351. if !filepath.IsAbs(req.Path) {
  352. render.Status(r, http.StatusBadRequest)
  353. render.JSON(w, r, newError("path is not a absolute path"))
  354. return
  355. }
  356. cfg, err = executor.ParseWithPath(req.Path)
  357. if err != nil {
  358. render.Status(r, http.StatusBadRequest)
  359. render.JSON(w, r, newError(err.Error()))
  360. return
  361. }
  362. }
  363. executor.ApplyConfig(cfg, force)
  364. render.NoContent(w, r)
  365. }
  366. func updateGeoDatabases(w http.ResponseWriter, r *http.Request) {
  367. err := updater.UpdateGeoDatabases()
  368. if err != nil {
  369. log.Errorln("[REST-API] update GEO databases failed: %v", err)
  370. render.Status(r, http.StatusInternalServerError)
  371. render.JSON(w, r, newError(err.Error()))
  372. return
  373. }
  374. cfg, err := executor.ParseWithPath(C.Path.Config())
  375. if err != nil {
  376. log.Errorln("[REST-API] update GEO databases failed: %v", err)
  377. render.Status(r, http.StatusInternalServerError)
  378. render.JSON(w, r, newError("Error parsing configuration"))
  379. return
  380. }
  381. log.Warnln("[GEO] update GEO databases success, applying config")
  382. executor.ApplyConfig(cfg, false)
  383. render.NoContent(w, r)
  384. }