From bb503c6954697cb54fe15c1d01780946791b1b1c Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 22 May 2016 22:30:08 +0200 Subject: [PATCH] Static hosts in DNS server --- app/dns/config.go | 3 +++ app/dns/config_json.go | 15 ++++++++++++++- app/dns/server.go | 9 +++++++++ app/dns/server_test.go | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app/dns/config.go b/app/dns/config.go index 863a855a..3ba4ba32 100644 --- a/app/dns/config.go +++ b/app/dns/config.go @@ -1,9 +1,12 @@ package dns import ( + "net" + v2net "github.com/v2ray/v2ray-core/common/net" ) type Config struct { + Hosts map[string]net.IP NameServers []v2net.Destination } diff --git a/app/dns/config_json.go b/app/dns/config_json.go index 6e78336c..bd866598 100644 --- a/app/dns/config_json.go +++ b/app/dns/config_json.go @@ -4,13 +4,16 @@ package dns import ( "encoding/json" + "errors" + "net" v2net "github.com/v2ray/v2ray-core/common/net" ) func (this *Config) UnmarshalJSON(data []byte) error { type JsonConfig struct { - Servers []v2net.AddressJson `json:"servers"` + Servers []v2net.AddressJson `json:"servers"` + Hosts map[string]v2net.AddressJson `json:"hosts"` } jsonConfig := new(JsonConfig) if err := json.Unmarshal(data, jsonConfig); err != nil { @@ -21,5 +24,15 @@ func (this *Config) UnmarshalJSON(data []byte) error { this.NameServers[idx] = v2net.UDPDestination(server.Address, v2net.Port(53)) } + if jsonConfig.Hosts != nil { + this.Hosts = make(map[string]net.IP) + for domain, ip := range jsonConfig.Hosts { + if ip.Address.IsDomain() { + return errors.New(ip.Address.String() + " is not an IP.") + } + this.Hosts[domain] = ip.Address.IP() + } + } + return nil } diff --git a/app/dns/server.go b/app/dns/server.go index 0265b609..de8b2ec7 100644 --- a/app/dns/server.go +++ b/app/dns/server.go @@ -23,6 +23,7 @@ type DomainRecord struct { type CacheServer struct { sync.RWMutex space app.Space + hosts map[string]net.IP records map[string]*DomainRecord servers []NameServer } @@ -31,6 +32,7 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer { server := &CacheServer{ records: make(map[string]*DomainRecord), servers: make([]NameServer, len(config.NameServers)), + hosts: config.Hosts, } space.InitializeApplication(func() error { if !space.HasApp(dispatcher.APP_ID) { @@ -46,6 +48,9 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer { server.servers[idx] = NewUDPNameServer(ns, dispatcher) } } + if len(config.NameServers) == 0 { + server.servers = append(server.servers, &LocalNameServer{}) + } return nil }) return server @@ -67,6 +72,10 @@ func (this *CacheServer) GetCached(domain string) []net.IP { } func (this *CacheServer) Get(domain string) []net.IP { + if ip, found := this.hosts[domain]; found { + return []net.IP{ip} + } + domain = dns.Fqdn(domain) ips := this.GetCached(domain) if ips != nil { diff --git a/app/dns/server_test.go b/app/dns/server_test.go index 9df015e6..ac6cbb4c 100644 --- a/app/dns/server_test.go +++ b/app/dns/server_test.go @@ -22,7 +22,7 @@ func TestDnsAdd(t *testing.T) { space := app.NewSpace() outboundHandlerManager := proxyman.NewDefaultOutboundHandlerManager() - outboundHandlerManager.SetDefaultHandler(&freedom.FreedomConnection{}) + outboundHandlerManager.SetDefaultHandler(freedom.NewFreedomConnection(&freedom.Config{}, space)) space.BindApp(proxyman.APP_ID_OUTBOUND_MANAGER, outboundHandlerManager) space.BindApp(dispatcher.APP_ID, dispatchers.NewDefaultDispatcher(space))