Przeglądaj źródła

Static hosts in DNS server

v2ray 9 lat temu
rodzic
commit
bb503c6954
4 zmienionych plików z 27 dodań i 2 usunięć
  1. 3 0
      app/dns/config.go
  2. 14 1
      app/dns/config_json.go
  3. 9 0
      app/dns/server.go
  4. 1 1
      app/dns/server_test.go

+ 3 - 0
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
 }

+ 14 - 1
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
 }

+ 9 - 0
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 {

+ 1 - 1
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))