Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support dns lookup local hosts file #12

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
localCmd.Flags().StringVarP(&cfg.Local.MixedAddr, "mixed", "M", "", "mixed proxy for SOCKS and HTTP")
localCmd.Flags().StringSliceVar(&cfg.Local.TCPTunAddr, "tcp-tun", nil, "simple tcp tun listening address (format: \"local:port=remote:port\")")
localCmd.Flags().BoolVar(&cfg.Local.SystemProxy, "system-proxy", false, "enable system proxy settings")
localCmd.Flags().BoolVar(&cfg.Local.LookupHostsFile, "lookup-hostsfile", false, "dns lookup local hosts file")

// ssr
localCmd.Flags().StringVarP(&cfg.Server[0].Type, "type", "T", "", "enable shadowsocksr")
Expand Down
25 changes: 15 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ type MitmOption struct {
}

type LocalConfig struct {
SocksAddr string `yaml:"socks_addr,omitempty" json:"socks_addr,omitempty"`
HTTPAddr string `yaml:"http_addr,omitempty" json:"http_addr,omitempty"`
SocksAuth string `yaml:"socks_auth,omitempty" json:"socks_auth,omitempty"`
HTTPAuth string `yaml:"http_auth,omitempty" json:"http_auth,omitempty"`
MixedAddr string `yaml:"mixed_addr,omitempty" json:"mixed_addr,omitempty"`
TCPTunAddr []string `yaml:"tcp_tun_addr,omitempty" json:"tcp_tun_addr,omitempty"`
SystemProxy bool `yaml:"system_proxy,omitempty" json:"system_proxy,omitempty"`
Mitm *MitmOption `yaml:"mitm,omitempty" json:"mitm,omitempty"`
Tun *TunOption `yaml:"tun,omitempty" json:"tun,omitempty"`
DNS *DnsOption `yaml:"dns,omitempty" json:"dns,omitempty"`
SocksAddr string `yaml:"socks_addr,omitempty" json:"socks_addr,omitempty"`
HTTPAddr string `yaml:"http_addr,omitempty" json:"http_addr,omitempty"`
SocksAuth string `yaml:"socks_auth,omitempty" json:"socks_auth,omitempty"`
HTTPAuth string `yaml:"http_auth,omitempty" json:"http_auth,omitempty"`
MixedAddr string `yaml:"mixed_addr,omitempty" json:"mixed_addr,omitempty"`
TCPTunAddr []string `yaml:"tcp_tun_addr,omitempty" json:"tcp_tun_addr,omitempty"`
SystemProxy bool `yaml:"system_proxy,omitempty" json:"system_proxy,omitempty"`
LookupHostsFile bool `yaml:"lookup_hostsfile,omitempty" json:"lookup_hostsfile,omitempty"`
Mitm *MitmOption `yaml:"mitm,omitempty" json:"mitm,omitempty"`
Tun *TunOption `yaml:"tun,omitempty" json:"tun,omitempty"`
DNS *DnsOption `yaml:"dns,omitempty" json:"dns,omitempty"`
}

type Domain struct {
Expand Down Expand Up @@ -468,6 +469,10 @@ func (cfg *Config) BuildLocalOptions() []ss.SSOption {
opts = append(opts, ss.WithSystemProxy())
}

if cfg.Local.LookupHostsFile {
opts = append(opts, ss.WithLookupHostsFile())
}

return opts
}

Expand Down
1 change: 1 addition & 0 deletions example-configs/simple-client-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ local:
mixed_addr: :10088
# socks_auth: "123:123"
# http_auth: '123:123'
lookup_hostsfile: true
log:
color: true
log_level: trace
Expand Down
20 changes: 14 additions & 6 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ type nameserverExt struct {
type Resolver struct {
*fakeIPResolver
// UDP/TCP/DoT/DoH
nameservers []nameserverExt
clients map[string]*DnsClient
lookupGroup singleflight.Group
nameservers []nameserverExt
clients map[string]*DnsClient
lookupGroup singleflight.Group
lookupHostPref bool
}

func parseNameserver(nameservers []string) []nameserverExt {
Expand Down Expand Up @@ -112,7 +113,7 @@ func parseNameserver(nameservers []string) []nameserverExt {
return list
}

func NewDnsResolver(nameservers []string) *Resolver {
func NewDnsResolver(nameservers []string, lookupHostsFile bool) *Resolver {
nameserver := parseNameserver(nameservers)
// default system config dns
fallback := parseNameserver(dnsutil.GetLocalDnsList())
Expand All @@ -121,8 +122,9 @@ func NewDnsResolver(nameservers []string) *Resolver {
}

resolver := &Resolver{
clients: make(map[string]*DnsClient),
nameservers: append(nameserver, fallback...),
clients: make(map[string]*DnsClient),
nameservers: append(nameserver, fallback...),
lookupHostPref: lookupHostsFile,
}

for _, ns := range resolver.nameservers {
Expand Down Expand Up @@ -157,6 +159,12 @@ func (r *Resolver) LookupHost(ctx context.Context, host string) netip.Addr {
}

func (r *Resolver) LookupIP(ctx context.Context, host string) ([]netip.Addr, error) {
if r.lookupHostPref {
ip := hostsutil.LookupIP(host)
if ip.IsValid() {
return []netip.Addr{ip}, nil
}
}
ipsCh := make(chan []netip.Addr, 1)
// lookup ipv6 address
go func() {
Expand Down
2 changes: 1 addition & 1 deletion resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestDnsResolver(t *testing.T) {
"https://dns.alidns.com/dns-query",
}
logger.Logger = logger.LogContext.Copy().WithCaller(false, true, false, true).BuildConsoleLogger(logx.LevelTrace)
r := NewDnsResolver(nameservers)
r := NewDnsResolver(nameservers, false)

for _, ns := range nameservers {
for i := 0; i < 2; i++ {
Expand Down
27 changes: 17 additions & 10 deletions ss/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ type serverOptions struct {
}

type localOptions struct {
socksAddr string
httpAddr string
mixedAddr string
socksAuth *Auth
httpAuth *Auth
tcpTunAddr [][]string
systemProxy bool
enableTun bool
enhancerConfig enhancer.EnhancerConfig
mitmConfig proxy.MimtOption
socksAddr string
httpAddr string
mixedAddr string
socksAuth *Auth
httpAuth *Auth
tcpTunAddr [][]string
systemProxy bool
enableTun bool
lookupHostsFile bool
enhancerConfig enhancer.EnhancerConfig
mitmConfig proxy.MimtOption
}

type ssOptions struct {
Expand Down Expand Up @@ -257,6 +258,12 @@ func WithSystemProxy() SSOption {
})
}

func WithLookupHostsFile() SSOption {
return ssOptionFunc(func(so *ssOptions) {
so.localOpts.lookupHostsFile = true
})
}

func WithSocksUserInfo(username, password string) SSOption {
return ssOptionFunc(func(so *ssOptions) {
so.localOpts.socksAuth = NewAuth(username, password)
Expand Down
2 changes: 1 addition & 1 deletion ss/ss_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewShadowsocksClient(opts ...SSOption) *ShadowsocksClient {
}

// init the global default dns resolver
resolver.DefaultResolver = resolver.NewDnsResolver(resolver.DefaultDnsNameservers)
resolver.DefaultResolver = resolver.NewDnsResolver(resolver.DefaultDnsNameservers, s.Opts.localOpts.lookupHostsFile)

// only one proxy node with command line
if len(s.Opts.serverOpts) == 1 && s.Opts.serverOpts[0].name == "" && rule.MatchRuler.GlobalTo == "" {
Expand Down
2 changes: 1 addition & 1 deletion ss/ss_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewShadowsocksServer(opts ...SSOption) *ShadowsocksServer {
if len(s.Opts.serverOpts) == 0 {
logger.Logger.Fatal("ss-server need configuration")
}
resolver.DefaultResolver = resolver.NewDnsResolver(nil)
resolver.DefaultResolver = resolver.NewDnsResolver(nil, false)
for _, opt := range s.Opts.serverOpts {
if err := s.initServerHandler(&opt); err != nil {
logger.Logger.Error("init server failed", logx.Error("error", err))
Expand Down
6 changes: 4 additions & 2 deletions util/hostsutil/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ func initHostsMap() {
}
for ip, hosts := range mp {
for _, host := range hosts {
logger.Logger.Infof("read hosts record: [%s]->[%s]", ip, host)
hostsMap[host] = append(hostsMap[host], netip.MustParseAddr(ip))
if addr, err := netip.ParseAddr(ip); err == nil {
logger.Logger.Infof("read hosts record: [%s]->[%s]", host, ip)
hostsMap[host] = append(hostsMap[host], addr)
}
}
}
}
Expand Down
Loading