chore: try to let tun's restful patch api work

This commit is contained in:
wwqgtxx
2022-11-04 08:52:30 +08:00
parent 9a5c0a4b6d
commit e20d01a679
4 changed files with 172 additions and 36 deletions

View File

@ -1,14 +1,16 @@
package route
import (
"github.com/Dreamacro/clash/component/dialer"
"net/http"
"net/netip"
"path/filepath"
"sync"
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/config"
"github.com/Dreamacro/clash/constant"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/hub/executor"
P "github.com/Dreamacro/clash/listener"
"github.com/Dreamacro/clash/log"
@ -38,7 +40,7 @@ type configSchema struct {
RedirPort *int `json:"redir-port"`
TProxyPort *int `json:"tproxy-port"`
MixedPort *int `json:"mixed-port"`
Tun *config.Tun `json:"tun"`
Tun *tunSchema `json:"tun"`
AllowLan *bool `json:"allow-lan"`
BindAddress *string `json:"bind-address"`
Mode *tunnel.TunnelMode `json:"mode"`
@ -49,6 +51,32 @@ type configSchema struct {
InterfaceName *string `json:"interface-name"`
}
type tunSchema struct {
Enable bool `yaml:"enable" json:"enable"`
Device *string `yaml:"device" json:"device"`
Stack *C.TUNStack `yaml:"stack" json:"stack"`
DNSHijack *[]netip.AddrPort `yaml:"dns-hijack" json:"dns-hijack"`
AutoRoute *bool `yaml:"auto-route" json:"auto-route"`
AutoDetectInterface *bool `yaml:"auto-detect-interface" json:"auto-detect-interface"`
//RedirectToTun []string `yaml:"-" json:"-"`
MTU *uint32 `yaml:"mtu" json:"mtu,omitempty"`
//Inet4Address *[]config.ListenPrefix `yaml:"inet4-address" json:"inet4_address,omitempty"`
Inet6Address *[]config.ListenPrefix `yaml:"inet6-address" json:"inet6_address,omitempty"`
StrictRoute *bool `yaml:"strict-route" json:"strict_route,omitempty"`
Inet4RouteAddress *[]config.ListenPrefix `yaml:"inet4_route_address" json:"inet4_route_address,omitempty"`
Inet6RouteAddress *[]config.ListenPrefix `yaml:"inet6_route_address" json:"inet6_route_address,omitempty"`
IncludeUID *[]uint32 `yaml:"include-uid" json:"include_uid,omitempty"`
IncludeUIDRange *[]string `yaml:"include-uid-range" json:"include_uid_range,omitempty"`
ExcludeUID *[]uint32 `yaml:"exclude-uid" json:"exclude_uid,omitempty"`
ExcludeUIDRange *[]string `yaml:"exclude-uid-range" json:"exclude_uid_range,omitempty"`
IncludeAndroidUser *[]int `yaml:"include-android-user" json:"include_android_user,omitempty"`
IncludePackage *[]string `yaml:"include-package" json:"include_package,omitempty"`
ExcludePackage *[]string `yaml:"exclude-package" json:"exclude_package,omitempty"`
EndpointIndependentNat *bool `yaml:"endpoint-independent-nat" json:"endpoint_independent_nat,omitempty"`
UDPTimeout *int64 `yaml:"udp-timeout" json:"udp_timeout,omitempty"`
}
func getConfigs(w http.ResponseWriter, r *http.Request) {
general := executor.GetGeneral()
render.JSON(w, r, general)
@ -62,6 +90,64 @@ func pointerOrDefault(p *int, def int) int {
return def
}
func pointerOrDefaultTun(p *tunSchema, def config.Tun) config.Tun {
if p != nil {
def.Enable = p.Enable
if p.Device != nil {
def.Device = *p.Device
}
if p.Stack != nil {
def.Stack = *p.Stack
}
if p.DNSHijack != nil {
def.DNSHijack = *p.DNSHijack
}
if p.AutoRoute != nil {
def.AutoRoute = *p.AutoRoute
}
if p.AutoDetectInterface != nil {
def.AutoDetectInterface = *p.AutoDetectInterface
}
if p.MTU != nil {
def.MTU = *p.MTU
}
//if p.Inet4Address != nil {
// def.Inet4Address = *p.Inet4Address
//}
if p.Inet6Address != nil {
def.Inet6Address = *p.Inet6Address
}
if p.IncludeUID != nil {
def.IncludeUID = *p.IncludeUID
}
if p.IncludeUIDRange != nil {
def.IncludeUIDRange = *p.IncludeUIDRange
}
if p.ExcludeUID != nil {
def.ExcludeUID = *p.ExcludeUID
}
if p.ExcludeUIDRange != nil {
def.ExcludeUIDRange = *p.ExcludeUIDRange
}
if p.IncludeAndroidUser != nil {
def.IncludeAndroidUser = *p.IncludeAndroidUser
}
if p.IncludePackage != nil {
def.IncludePackage = *p.IncludePackage
}
if p.ExcludePackage != nil {
def.ExcludePackage = *p.ExcludePackage
}
if p.EndpointIndependentNat != nil {
def.EndpointIndependentNat = *p.EndpointIndependentNat
}
if p.UDPTimeout != nil {
def.UDPTimeout = *p.UDPTimeout
}
}
return def
}
func patchConfigs(w http.ResponseWriter, r *http.Request) {
general := &configSchema{}
if err := render.DecodeJSON(r.Body, general); err != nil {
@ -100,6 +186,7 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn)
P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn)
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tcpIn, udpIn)
P.ReCreateTun(pointerOrDefaultTun(general.Tun, P.LastTunConf), tcpIn, udpIn)
if general.Mode != nil {
tunnel.SetMode(*general.Mode)