fix: config crash

This commit is contained in:
wwqgtxx
2022-12-06 09:04:30 +08:00
parent f7fb5840cf
commit b5b06ea49c
8 changed files with 69 additions and 130 deletions

View File

@ -5,16 +5,16 @@ import (
)
type TuicServer struct {
Enable bool
Listen string
Token []string
Certificate string
PrivateKey string
CongestionController string
MaxIdleTime int
AuthenticationTimeout int
ALPN []string
MaxUdpRelayPacketSize int
Enable bool `yaml:"enable" json:"enable"`
Listen string `yaml:"listen" json:"listen"`
Token []string `yaml:"token" json:"token"`
Certificate string `yaml:"certificate" json:"certificate"`
PrivateKey string `yaml:"private-key" json:"private-key"`
CongestionController string `yaml:"congestion-controller" json:"congestion-controller,omitempty"`
MaxIdleTime int `yaml:"max-idle-time" json:"max-idle-time,omitempty"`
AuthenticationTimeout int `yaml:"authentication-timeout" json:"authentication-timeout,omitempty"`
ALPN []string `yaml:"alpn" json:"alpn,omitempty"`
MaxUdpRelayPacketSize int `yaml:"max-udp-relay-packet-size" json:"max-udp-relay-packet-size,omitempty"`
}
func (t TuicServer) String() string {

View File

@ -5,6 +5,7 @@ import (
"net/netip"
C "github.com/Dreamacro/clash/constant"
"gopkg.in/yaml.v3"
)
@ -71,27 +72,27 @@ func StringSliceToListenPrefixSlice(ss []string) ([]ListenPrefix, error) {
}
type Tun struct {
Enable bool
Device string
Stack C.TUNStack
DNSHijack []netip.AddrPort
AutoRoute bool
AutoDetectInterface bool
RedirectToTun []string
Enable bool `yaml:"enable" json:"enable"`
Device string `yaml:"device" json:"device"`
Stack C.TUNStack `yaml:"stack" json:"stack"`
DNSHijack []string `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
Inet4Address []ListenPrefix
Inet6Address []ListenPrefix
StrictRoute bool
Inet4RouteAddress []ListenPrefix
Inet6RouteAddress []ListenPrefix
IncludeUID []uint32
IncludeUIDRange []string
ExcludeUID []uint32
ExcludeUIDRange []string
IncludeAndroidUser []int
IncludePackage []string
ExcludePackage []string
EndpointIndependentNat bool
UDPTimeout int64
MTU uint32 `yaml:"mtu" json:"mtu,omitempty"`
Inet4Address []ListenPrefix `yaml:"inet4-address" json:"inet4-address,omitempty"`
Inet6Address []ListenPrefix `yaml:"inet6-address" json:"inet6-address,omitempty"`
StrictRoute bool `yaml:"strict-route" json:"strict-route,omitempty"`
Inet4RouteAddress []ListenPrefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"`
Inet6RouteAddress []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"`
}

View File

@ -2,7 +2,6 @@ package inbound
import (
"errors"
"net/netip"
"strings"
C "github.com/Dreamacro/clash/constant"
@ -56,15 +55,6 @@ func NewTun(options *TunOption) (*Tun, error) {
if !exist {
return nil, errors.New("invalid tun stack")
}
dnsHijack := make([]netip.AddrPort, 0, len(options.DNSHijack))
for _, str := range options.DNSHijack {
var a netip.AddrPort
err = a.UnmarshalText([]byte(str))
if err != nil {
return nil, err
}
dnsHijack = append(dnsHijack, a)
}
inet4Address, err := LC.StringSliceToListenPrefixSlice(options.Inet4Address)
if err != nil {
return nil, err
@ -88,7 +78,7 @@ func NewTun(options *TunOption) (*Tun, error) {
Enable: true,
Device: options.Device,
Stack: stack,
DNSHijack: dnsHijack,
DNSHijack: options.DNSHijack,
AutoRoute: options.AutoRoute,
AutoDetectInterface: options.AutoDetectInterface,
MTU: options.MTU,

View File

@ -825,7 +825,7 @@ func hasTunConfigChange(tunConf *LC.Tun) bool {
}
sort.Slice(tunConf.DNSHijack, func(i, j int) bool {
return tunConf.DNSHijack[i].Addr().Less(tunConf.DNSHijack[j].Addr())
return tunConf.DNSHijack[i] < tunConf.DNSHijack[j]
})
sort.Slice(tunConf.Inet4Address, func(i, j int) bool {

View File

@ -109,7 +109,16 @@ func New(options LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapte
var dnsAdds []netip.AddrPort
for _, d := range options.DNSHijack {
dnsAdds = append(dnsAdds, d)
if _, after, ok := strings.Cut(d, "://"); ok {
d = after
}
d = strings.Replace(d, "any", "0.0.0.0", 1)
addrPort, err := netip.ParseAddrPort(d)
if err != nil {
return nil, fmt.Errorf("parse dns-hijack url error: %w", err)
}
dnsAdds = append(dnsAdds, addrPort)
}
for _, a := range options.Inet4Address {
addrPort := netip.AddrPortFrom(a.Build().Addr().Next(), 53)