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

@ -2,7 +2,6 @@ package config
import ( import (
"container/list" "container/list"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"net" "net"
@ -54,8 +53,8 @@ type General struct {
GeodataLoader string `json:"geodata-loader"` GeodataLoader string `json:"geodata-loader"`
TCPConcurrent bool `json:"tcp-concurrent"` TCPConcurrent bool `json:"tcp-concurrent"`
EnableProcess bool `json:"enable-process"` EnableProcess bool `json:"enable-process"`
Tun Tun `json:"tun"` Tun LC.Tun `json:"tun"`
TuicServer TuicServer `json:"tuic-server"` TuicServer LC.TuicServer `json:"tuic-server"`
Sniffing bool `json:"sniffing"` Sniffing bool `json:"sniffing"`
EBpf EBpf `json:"-"` EBpf EBpf `json:"-"`
} }
@ -115,56 +114,11 @@ type Profile struct {
StoreFakeIP bool `yaml:"store-fake-ip"` StoreFakeIP bool `yaml:"store-fake-ip"`
} }
type TuicServer struct {
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"`
}
type TLS struct { type TLS struct {
Certificate string `yaml:"certificate"` Certificate string `yaml:"certificate"`
PrivateKey string `yaml:"private-key"` PrivateKey string `yaml:"private-key"`
} }
func (t TuicServer) String() string {
b, _ := json.Marshal(t)
return string(b)
}
// Tun config
type Tun 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 []LC.ListenPrefix `yaml:"inet4-address" json:"inet4-address,omitempty"`
Inet6Address []LC.ListenPrefix `yaml:"inet6-address" json:"inet6-address,omitempty"`
StrictRoute bool `yaml:"strict-route" json:"strict-route,omitempty"`
Inet4RouteAddress []LC.ListenPrefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"`
Inet6RouteAddress []LC.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"`
}
// IPTables config // IPTables config
type IPTables struct { type IPTables struct {
Enable bool `yaml:"enable" json:"enable"` Enable bool `yaml:"enable" json:"enable"`
@ -734,6 +688,7 @@ func parseRuleProviders(cfg *RawConfig) (ruleProviders map[string]providerTypes.
} }
func parseSubRules(cfg *RawConfig, proxies map[string]C.Proxy) (subRules map[string][]C.Rule, err error) { func parseSubRules(cfg *RawConfig, proxies map[string]C.Proxy) (subRules map[string][]C.Rule, err error) {
subRules = map[string][]C.Rule{}
for name, rawRules := range cfg.SubRules { for name, rawRules := range cfg.SubRules {
if len(name) == 0 { if len(name) == 0 {
return nil, fmt.Errorf("sub-rule name is empty") return nil, fmt.Errorf("sub-rule name is empty")
@ -1162,21 +1117,6 @@ func parseAuthentication(rawRecords []string) []auth.AuthUser {
} }
func parseTun(rawTun RawTun, general *General) error { func parseTun(rawTun RawTun, general *General) error {
var dnsHijack []netip.AddrPort
for _, d := range rawTun.DNSHijack {
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 fmt.Errorf("parse dns-hijack url error: %w", err)
}
dnsHijack = append(dnsHijack, addrPort)
}
tunAddressPrefix := T.FakeIPRange() tunAddressPrefix := T.FakeIPRange()
if !tunAddressPrefix.IsValid() { if !tunAddressPrefix.IsValid() {
tunAddressPrefix = netip.MustParsePrefix("198.18.0.1/16") tunAddressPrefix = netip.MustParsePrefix("198.18.0.1/16")
@ -1187,11 +1127,11 @@ func parseTun(rawTun RawTun, general *General) error {
rawTun.Inet6Address = nil rawTun.Inet6Address = nil
} }
general.Tun = Tun{ general.Tun = LC.Tun{
Enable: rawTun.Enable, Enable: rawTun.Enable,
Device: rawTun.Device, Device: rawTun.Device,
Stack: rawTun.Stack, Stack: rawTun.Stack,
DNSHijack: dnsHijack, DNSHijack: rawTun.DNSHijack,
AutoRoute: rawTun.AutoRoute, AutoRoute: rawTun.AutoRoute,
AutoDetectInterface: rawTun.AutoDetectInterface, AutoDetectInterface: rawTun.AutoDetectInterface,
RedirectToTun: rawTun.RedirectToTun, RedirectToTun: rawTun.RedirectToTun,
@ -1217,7 +1157,7 @@ func parseTun(rawTun RawTun, general *General) error {
} }
func parseTuicServer(rawTuic RawTuicServer, general *General) error { func parseTuicServer(rawTuic RawTuicServer, general *General) error {
general.TuicServer = TuicServer{ general.TuicServer = LC.TuicServer{
Enable: rawTuic.Enable, Enable: rawTuic.Enable,
Listen: rawTuic.Listen, Listen: rawTuic.Listen,
Token: rawTuic.Token, Token: rawTuic.Token,

View File

@ -125,8 +125,8 @@ func GetGeneral() *config.General {
LogLevel: log.Level(), LogLevel: log.Level(),
IPv6: !resolver.DisableIPv6, IPv6: !resolver.DisableIPv6,
GeodataLoader: G.LoaderName(), GeodataLoader: G.LoaderName(),
Tun: config.Tun(listener.GetTunConf()), Tun: listener.GetTunConf(),
TuicServer: config.TuicServer(listener.GetTuicConf()), TuicServer: listener.GetTuicConf(),
Interface: dialer.DefaultInterface.Load(), Interface: dialer.DefaultInterface.Load(),
Sniffing: tunnel.IsSniffing(), Sniffing: tunnel.IsSniffing(),
TCPConcurrent: dialer.GetDial(), TCPConcurrent: dialer.GetDial(),

View File

@ -2,7 +2,6 @@ package route
import ( import (
"net/http" "net/http"
"net/netip"
"path/filepath" "path/filepath"
"sync" "sync"
@ -61,7 +60,7 @@ type tunSchema struct {
Enable bool `yaml:"enable" json:"enable"` Enable bool `yaml:"enable" json:"enable"`
Device *string `yaml:"device" json:"device"` Device *string `yaml:"device" json:"device"`
Stack *C.TUNStack `yaml:"stack" json:"stack"` Stack *C.TUNStack `yaml:"stack" json:"stack"`
DNSHijack *[]netip.AddrPort `yaml:"dns-hijack" json:"dns-hijack"` DNSHijack *[]string `yaml:"dns-hijack" json:"dns-hijack"`
AutoRoute *bool `yaml:"auto-route" json:"auto-route"` AutoRoute *bool `yaml:"auto-route" json:"auto-route"`
AutoDetectInterface *bool `yaml:"auto-detect-interface" json:"auto-detect-interface"` AutoDetectInterface *bool `yaml:"auto-detect-interface" json:"auto-detect-interface"`
//RedirectToTun []string `yaml:"-" json:"-"` //RedirectToTun []string `yaml:"-" json:"-"`

View File

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

View File

@ -5,6 +5,7 @@ import (
"net/netip" "net/netip"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -71,27 +72,27 @@ func StringSliceToListenPrefixSlice(ss []string) ([]ListenPrefix, error) {
} }
type Tun struct { type Tun struct {
Enable bool Enable bool `yaml:"enable" json:"enable"`
Device string Device string `yaml:"device" json:"device"`
Stack C.TUNStack Stack C.TUNStack `yaml:"stack" json:"stack"`
DNSHijack []netip.AddrPort DNSHijack []string `yaml:"dns-hijack" json:"dns-hijack"`
AutoRoute bool AutoRoute bool `yaml:"auto-route" json:"auto-route"`
AutoDetectInterface bool AutoDetectInterface bool `yaml:"auto-detect-interface" json:"auto-detect-interface"`
RedirectToTun []string RedirectToTun []string `yaml:"-" json:"-"`
MTU uint32 MTU uint32 `yaml:"mtu" json:"mtu,omitempty"`
Inet4Address []ListenPrefix Inet4Address []ListenPrefix `yaml:"inet4-address" json:"inet4-address,omitempty"`
Inet6Address []ListenPrefix Inet6Address []ListenPrefix `yaml:"inet6-address" json:"inet6-address,omitempty"`
StrictRoute bool StrictRoute bool `yaml:"strict-route" json:"strict-route,omitempty"`
Inet4RouteAddress []ListenPrefix Inet4RouteAddress []ListenPrefix `yaml:"inet4-route-address" json:"inet4-route-address,omitempty"`
Inet6RouteAddress []ListenPrefix Inet6RouteAddress []ListenPrefix `yaml:"inet6-route-address" json:"inet6-route-address,omitempty"`
IncludeUID []uint32 IncludeUID []uint32 `yaml:"include-uid" json:"include-uid,omitempty"`
IncludeUIDRange []string IncludeUIDRange []string `yaml:"include-uid-range" json:"include-uid-range,omitempty"`
ExcludeUID []uint32 ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"`
ExcludeUIDRange []string ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"`
IncludeAndroidUser []int IncludeAndroidUser []int `yaml:"include-android-user" json:"include-android-user,omitempty"`
IncludePackage []string IncludePackage []string `yaml:"include-package" json:"include-package,omitempty"`
ExcludePackage []string ExcludePackage []string `yaml:"exclude-package" json:"exclude-package,omitempty"`
EndpointIndependentNat bool EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint-independent-nat,omitempty"`
UDPTimeout int64 UDPTimeout int64 `yaml:"udp-timeout" json:"udp-timeout,omitempty"`
} }

View File

@ -2,7 +2,6 @@ package inbound
import ( import (
"errors" "errors"
"net/netip"
"strings" "strings"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
@ -56,15 +55,6 @@ func NewTun(options *TunOption) (*Tun, error) {
if !exist { if !exist {
return nil, errors.New("invalid tun stack") 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) inet4Address, err := LC.StringSliceToListenPrefixSlice(options.Inet4Address)
if err != nil { if err != nil {
return nil, err return nil, err
@ -88,7 +78,7 @@ func NewTun(options *TunOption) (*Tun, error) {
Enable: true, Enable: true,
Device: options.Device, Device: options.Device,
Stack: stack, Stack: stack,
DNSHijack: dnsHijack, DNSHijack: options.DNSHijack,
AutoRoute: options.AutoRoute, AutoRoute: options.AutoRoute,
AutoDetectInterface: options.AutoDetectInterface, AutoDetectInterface: options.AutoDetectInterface,
MTU: options.MTU, MTU: options.MTU,

View File

@ -825,7 +825,7 @@ func hasTunConfigChange(tunConf *LC.Tun) bool {
} }
sort.Slice(tunConf.DNSHijack, func(i, j int) 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 { 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 var dnsAdds []netip.AddrPort
for _, d := range options.DNSHijack { 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 { for _, a := range options.Inet4Address {
addrPort := netip.AddrPortFrom(a.Build().Addr().Next(), 53) addrPort := netip.AddrPortFrom(a.Build().Addr().Next(), 53)