Quellcode durchsuchen

proto for dns config

Darien Raymond vor 9 Jahren
Ursprung
Commit
d38e62932d

+ 12 - 4
app/dns/config.go

@@ -3,10 +3,18 @@ package dns
 import (
 	"net"
 
-	v2net "v2ray.com/core/common/net"
+	"v2ray.com/core/common/log"
 )
 
-type Config struct {
-	Hosts       map[string]net.IP
-	NameServers []v2net.Destination
+func (this *Config) GetInternalHosts() map[string]net.IP {
+	hosts := make(map[string]net.IP)
+	for domain, addressPB := range this.GetHosts() {
+		address := addressPB.AsAddress()
+		if address.Family().IsDomain() {
+			log.Warning("DNS: Ignoring domain address in static hosts: ", address.Domain())
+			continue
+		}
+		hosts[domain] = address.IP()
+	}
+	return hosts
 }

+ 82 - 0
app/dns/config.pb.go

@@ -0,0 +1,82 @@
+// Code generated by protoc-gen-go.
+// source: v2ray.com/core/app/dns/config.proto
+// DO NOT EDIT!
+
+/*
+Package dns is a generated protocol buffer package.
+
+It is generated from these files:
+	v2ray.com/core/app/dns/config.proto
+
+It has these top-level messages:
+	Config
+*/
+package dns
+
+import proto "github.com/golang/protobuf/proto"
+import fmt "fmt"
+import math "math"
+import com_v2ray_core_common_net "v2ray.com/core/common/net"
+import com_v2ray_core_common_net2 "v2ray.com/core/common/net"
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
+
+type Config struct {
+	NameServers []*com_v2ray_core_common_net2.DestinationPB     `protobuf:"bytes,1,rep,name=NameServers,json=nameServers" json:"NameServers,omitempty"`
+	Hosts       map[string]*com_v2ray_core_common_net.AddressPB `protobuf:"bytes,2,rep,name=Hosts,json=hosts" json:"Hosts,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
+}
+
+func (m *Config) Reset()                    { *m = Config{} }
+func (m *Config) String() string            { return proto.CompactTextString(m) }
+func (*Config) ProtoMessage()               {}
+func (*Config) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
+
+func (m *Config) GetNameServers() []*com_v2ray_core_common_net2.DestinationPB {
+	if m != nil {
+		return m.NameServers
+	}
+	return nil
+}
+
+func (m *Config) GetHosts() map[string]*com_v2ray_core_common_net.AddressPB {
+	if m != nil {
+		return m.Hosts
+	}
+	return nil
+}
+
+func init() {
+	proto.RegisterType((*Config)(nil), "com.v2ray.core.app.dns.Config")
+}
+
+func init() { proto.RegisterFile("v2ray.com/core/app/dns/config.proto", fileDescriptor0) }
+
+var fileDescriptor0 = []byte{
+	// 263 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x7c, 0x90, 0x41, 0x4b, 0xf4, 0x30,
+	0x10, 0x86, 0x49, 0x4b, 0x17, 0xbe, 0xf4, 0xf2, 0xd1, 0x83, 0x94, 0x9e, 0x8a, 0x0a, 0x56, 0x84,
+	0x09, 0xd4, 0x8b, 0xec, 0x45, 0xac, 0x0a, 0xe2, 0x41, 0x96, 0x7a, 0xf3, 0x20, 0xc4, 0x66, 0xd4,
+	0xa2, 0x9d, 0x84, 0x24, 0x16, 0xfa, 0xc3, 0xbd, 0xcb, 0x36, 0x48, 0x61, 0x51, 0x6f, 0x43, 0x78,
+	0xde, 0x67, 0x26, 0x2f, 0x3f, 0x18, 0x6b, 0x2b, 0x27, 0xe8, 0xf4, 0x20, 0x3a, 0x6d, 0x51, 0x48,
+	0x63, 0x84, 0x22, 0x27, 0x3a, 0x4d, 0xcf, 0xfd, 0x0b, 0x18, 0xab, 0xbd, 0xce, 0xf6, 0x3a, 0x3d,
+	0xc0, 0x37, 0x68, 0x11, 0xa4, 0x31, 0xa0, 0xc8, 0x15, 0x47, 0x3b, 0xe1, 0x4e, 0x0f, 0x83, 0x26,
+	0x41, 0xe8, 0x85, 0x54, 0xca, 0xa2, 0x73, 0x41, 0x50, 0x9c, 0xfc, 0x0e, 0x2a, 0x74, 0xbe, 0x27,
+	0xe9, 0x7b, 0x4d, 0x01, 0xde, 0xff, 0x64, 0x7c, 0x75, 0x39, 0xaf, 0xcf, 0x6e, 0x79, 0x7a, 0x27,
+	0x07, 0xbc, 0x47, 0x3b, 0xa2, 0x75, 0x39, 0x2b, 0xe3, 0x2a, 0xad, 0x2b, 0xd8, 0x39, 0x27, 0xd8,
+	0x80, 0xd0, 0xc3, 0xd5, 0x62, 0xdb, 0x34, 0x6d, 0x4a, 0x4b, 0x38, 0x3b, 0xe7, 0xc9, 0x8d, 0x76,
+	0xde, 0xe5, 0xd1, 0x6c, 0x39, 0x86, 0x9f, 0x3f, 0x05, 0x61, 0x35, 0xcc, 0xec, 0x35, 0x79, 0x3b,
+	0xb5, 0xc9, 0xeb, 0x76, 0x2e, 0x1e, 0x39, 0x5f, 0x1e, 0xb3, 0xff, 0x3c, 0x7e, 0xc3, 0x29, 0x67,
+	0x25, 0xab, 0xfe, 0xb5, 0xdb, 0x31, 0x5b, 0xf3, 0x64, 0x94, 0xef, 0x1f, 0x98, 0x47, 0x25, 0xab,
+	0xd2, 0xfa, 0xf0, 0x8f, 0x33, 0x2f, 0x42, 0x3b, 0x9b, 0xa6, 0x0d, 0x91, 0x75, 0x74, 0xc6, 0x9a,
+	0xe4, 0x21, 0x56, 0xe4, 0x9e, 0x56, 0x73, 0x0b, 0xa7, 0x5f, 0x01, 0x00, 0x00, 0xff, 0xff, 0xfb,
+	0x3a, 0xf6, 0xb1, 0x9a, 0x01, 0x00, 0x00,
+}

+ 12 - 0
app/dns/config.proto

@@ -0,0 +1,12 @@
+syntax = "proto3";
+
+package com.v2ray.core.app.dns;
+option go_package = "dns";
+
+import "v2ray.com/core/common/net/address.proto";
+import "v2ray.com/core/common/net/destination.proto";
+
+message Config {
+  repeated com.v2ray.core.common.net.DestinationPB NameServers = 1;
+  map<string, com.v2ray.core.common.net.AddressPB> Hosts = 2;
+}

+ 9 - 14
app/dns/config_json.go

@@ -4,35 +4,30 @@ package dns
 
 import (
 	"encoding/json"
-	"errors"
-	"net"
 
 	v2net "v2ray.com/core/common/net"
 )
 
 func (this *Config) UnmarshalJSON(data []byte) error {
 	type JsonConfig struct {
-		Servers []v2net.AddressPB          `json:"servers"`
-		Hosts   map[string]v2net.AddressPB `json:"hosts"`
+		Servers []*v2net.AddressPB          `json:"servers"`
+		Hosts   map[string]*v2net.AddressPB `json:"hosts"`
 	}
 	jsonConfig := new(JsonConfig)
 	if err := json.Unmarshal(data, jsonConfig); err != nil {
 		return err
 	}
-	this.NameServers = make([]v2net.Destination, len(jsonConfig.Servers))
+	this.NameServers = make([]*v2net.DestinationPB, len(jsonConfig.Servers))
 	for idx, server := range jsonConfig.Servers {
-		this.NameServers[idx] = v2net.UDPDestination(server.AsAddress(), v2net.Port(53))
+		this.NameServers[idx] = &v2net.DestinationPB{
+			Network: v2net.Network_UDP,
+			Address: server,
+			Port:    53,
+		}
 	}
 
 	if jsonConfig.Hosts != nil {
-		this.Hosts = make(map[string]net.IP)
-		for domain, ipOrDomain := range jsonConfig.Hosts {
-			ip := ipOrDomain.GetIp()
-			if ip == nil {
-				return errors.New(ipOrDomain.AsAddress().String() + " is not an IP.")
-			}
-			this.Hosts[domain] = net.IP(ip)
-		}
+		this.Hosts = jsonConfig.Hosts
 	}
 
 	return nil

+ 4 - 3
app/dns/config_json_test.go

@@ -22,7 +22,8 @@ func TestConfigParsing(t *testing.T) {
 	err := json.Unmarshal([]byte(rawJson), config)
 	assert.Error(err).IsNil()
 	assert.Int(len(config.NameServers)).Equals(1)
-	assert.Destination(config.NameServers[0]).IsUDP()
-	assert.Address(config.NameServers[0].Address).Equals(v2net.IPAddress([]byte{8, 8, 8, 8}))
-	assert.Port(config.NameServers[0].Port).Equals(v2net.Port(53))
+	dest := config.NameServers[0].AsDestination()
+	assert.Destination(dest).IsUDP()
+	assert.Address(dest.Address).Equals(v2net.IPAddress([]byte{8, 8, 8, 8}))
+	assert.Port(dest.Port).Equals(v2net.Port(53))
 }

+ 12 - 4
app/dns/server.go

@@ -8,6 +8,7 @@ import (
 	"v2ray.com/core/app"
 	"v2ray.com/core/app/dispatcher"
 	"v2ray.com/core/common/log"
+	v2net "v2ray.com/core/common/net"
 
 	"github.com/miekg/dns"
 )
@@ -32,7 +33,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,
+		hosts:   config.GetInternalHosts(),
 	}
 	space.InitializeApplication(func() error {
 		if !space.HasApp(dispatcher.APP_ID) {
@@ -41,11 +42,18 @@ func NewCacheServer(space app.Space, config *Config) *CacheServer {
 		}
 
 		dispatcher := space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
-		for idx, ns := range config.NameServers {
-			if ns.Address.Family().IsDomain() && ns.Address.Domain() == "localhost" {
+		for idx, destPB := range config.NameServers {
+			address := destPB.Address.AsAddress()
+			if address.Family().IsDomain() && address.Domain() == "localhost" {
 				server.servers[idx] = &LocalNameServer{}
 			} else {
-				server.servers[idx] = NewUDPNameServer(ns, dispatcher)
+				dest := destPB.AsDestination()
+				if dest.Network == v2net.Network_Unknown {
+					dest.Network = v2net.Network_UDP
+				}
+				if dest.Network == v2net.Network_UDP {
+					server.servers[idx] = NewUDPNameServer(dest, dispatcher)
+				}
 			}
 		}
 		if len(config.NameServers) == 0 {

+ 9 - 3
app/dns/server_test.go

@@ -37,9 +37,15 @@ func TestDnsAdd(t *testing.T) {
 
 	domain := "local.v2ray.com"
 	server := NewCacheServer(space, &Config{
-		NameServers: []v2net.Destination{
-			v2net.UDPDestination(v2net.IPAddress([]byte{8, 8, 8, 8}), v2net.Port(53)),
-		},
+		NameServers: []*v2net.DestinationPB{{
+			Network: v2net.Network_UDP,
+			Address: &v2net.AddressPB{
+				Address: &v2net.AddressPB_Ip{
+					Ip: []byte{8, 8, 8, 8},
+				},
+			},
+			Port: 53,
+		}},
 	})
 	space.BindApp(APP_ID, server)
 	space.Initialize()

+ 8 - 0
common/net/destination.go

@@ -51,3 +51,11 @@ func (this Destination) String() string {
 func (this Destination) Equals(another Destination) bool {
 	return this.Network == another.Network && this.Port == another.Port && this.Address.Equals(another.Address)
 }
+
+func (this *DestinationPB) AsDestination() Destination {
+	return Destination{
+		Network: this.Network,
+		Address: this.Address.AsAddress(),
+		Port:    Port(this.Port),
+	}
+}

+ 6 - 3
proxy/freedom/freedom_test.go

@@ -1,7 +1,6 @@
 package freedom_test
 
 import (
-	"net"
 	"testing"
 
 	"v2ray.com/core/app"
@@ -90,8 +89,12 @@ func TestIPResolution(t *testing.T) {
 	r, _ := router.CreateRouter("rules", &rules.RouterRuleConfig{}, space)
 	space.BindApp(router.APP_ID, r)
 	dnsServer := dns.NewCacheServer(space, &dns.Config{
-		Hosts: map[string]net.IP{
-			"v2ray.com": net.IP([]byte{127, 0, 0, 1}),
+		Hosts: map[string]*v2net.AddressPB{
+			"v2ray.com": &v2net.AddressPB{
+				Address: &v2net.AddressPB_Ip{
+					Ip: []byte{127, 0, 0, 1},
+				},
+			},
 		},
 	})
 	space.BindApp(dns.APP_ID, dnsServer)

+ 9 - 3
shell/point/config_json.go

@@ -55,9 +55,15 @@ func (this *Config) UnmarshalJSON(data []byte) error {
 	this.OutboundDetours = jsonConfig.OutboundDetours
 	if jsonConfig.DNSConfig == nil {
 		jsonConfig.DNSConfig = &dns.Config{
-			NameServers: []v2net.Destination{
-				v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
-			},
+			NameServers: []*v2net.DestinationPB{{
+				Network: v2net.Network_UDP,
+				Address: &v2net.AddressPB{
+					Address: &v2net.AddressPB_Domain{
+						Domain: "localhost",
+					},
+				},
+				Port: 53,
+			}},
 		}
 	}
 	this.DNSConfig = jsonConfig.DNSConfig