From 44d87ed642f930edbf267da8e85d98e5191d3df1 Mon Sep 17 00:00:00 2001
From: Darien Raymond <admin@v2ray.com>
Date: Wed, 16 Jan 2019 20:32:41 +0100
Subject: [PATCH] inbound tag for dns requests

---
 app/dns/config.pb.go | 88 ++++++++++++++++++++++++--------------------
 app/dns/config.proto |  3 ++
 app/dns/server.go    |  8 ++++
 app/dns/udpns.go     |  6 ++-
 4 files changed, 65 insertions(+), 40 deletions(-)

diff --git a/app/dns/config.pb.go b/app/dns/config.pb.go
index 3be30dd6..232c6cc6 100644
--- a/app/dns/config.pb.go
+++ b/app/dns/config.pb.go
@@ -153,11 +153,13 @@ type Config struct {
 	// Deprecated. Use static_hosts.
 	Hosts map[string]*net.IPOrDomain `protobuf:"bytes,2,rep,name=Hosts,proto3" json:"Hosts,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` // Deprecated: Do not use.
 	// Client IP for EDNS client subnet. Must be 4 bytes (IPv4) or 16 bytes (IPv6).
-	ClientIp             []byte                `protobuf:"bytes,3,opt,name=client_ip,json=clientIp,proto3" json:"client_ip,omitempty"`
-	StaticHosts          []*Config_HostMapping `protobuf:"bytes,4,rep,name=static_hosts,json=staticHosts,proto3" json:"static_hosts,omitempty"`
-	XXX_NoUnkeyedLiteral struct{}              `json:"-"`
-	XXX_unrecognized     []byte                `json:"-"`
-	XXX_sizecache        int32                 `json:"-"`
+	ClientIp    []byte                `protobuf:"bytes,3,opt,name=client_ip,json=clientIp,proto3" json:"client_ip,omitempty"`
+	StaticHosts []*Config_HostMapping `protobuf:"bytes,4,rep,name=static_hosts,json=staticHosts,proto3" json:"static_hosts,omitempty"`
+	// Tag is the inbound tag of DNS client.
+	Tag                  string   `protobuf:"bytes,6,opt,name=tag,proto3" json:"tag,omitempty"`
+	XXX_NoUnkeyedLiteral struct{} `json:"-"`
+	XXX_unrecognized     []byte   `json:"-"`
+	XXX_sizecache        int32    `json:"-"`
 }
 
 func (m *Config) Reset()         { *m = Config{} }
@@ -222,6 +224,13 @@ func (m *Config) GetStaticHosts() []*Config_HostMapping {
 	return nil
 }
 
+func (m *Config) GetTag() string {
+	if m != nil {
+		return m.Tag
+	}
+	return ""
+}
+
 type Config_HostMapping struct {
 	Type                 DomainMatchingType `protobuf:"varint,1,opt,name=type,proto3,enum=v2ray.core.app.dns.DomainMatchingType" json:"type,omitempty"`
 	Domain               string             `protobuf:"bytes,2,opt,name=domain,proto3" json:"domain,omitempty"`
@@ -291,38 +300,39 @@ func init() {
 }
 
 var fileDescriptor_ed5695198e3def8f = []byte{
-	// 523 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x93, 0xd1, 0x6e, 0xd3, 0x30,
-	0x18, 0x85, 0x49, 0xd2, 0x76, 0xeb, 0x9f, 0x52, 0x15, 0x5f, 0x4c, 0x55, 0x91, 0xa0, 0x0c, 0x31,
-	0x2a, 0x10, 0x8e, 0x14, 0x90, 0x80, 0xdd, 0x4c, 0x6c, 0x2b, 0xa2, 0x42, 0x83, 0xca, 0x43, 0x5c,
-	0x00, 0x52, 0xe5, 0x25, 0xa6, 0xb3, 0x68, 0x6c, 0x63, 0xbb, 0x85, 0xf0, 0x06, 0x88, 0x37, 0xe1,
-	0x29, 0x51, 0xed, 0x8e, 0x16, 0xd6, 0x01, 0x37, 0xdc, 0xd5, 0xee, 0xf9, 0xfe, 0x73, 0x72, 0xfe,
-	0x04, 0x6e, 0xce, 0x52, 0x4d, 0x4b, 0x9c, 0xc9, 0x22, 0xc9, 0xa4, 0x66, 0x09, 0x55, 0x2a, 0xc9,
-	0x85, 0x49, 0x32, 0x29, 0xde, 0xf3, 0x31, 0x56, 0x5a, 0x5a, 0x89, 0xd0, 0x99, 0x48, 0x33, 0x4c,
-	0x95, 0xc2, 0xb9, 0x30, 0x9d, 0xdb, 0xbf, 0x81, 0x99, 0x2c, 0x0a, 0x29, 0x12,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0x44, 0xb9, 0x30, 0xdf, 0x43, 0xf4, 0x3a, 0x25, 0xb4, 0xc4, 0x07,
-	0xf3, 0xff, 0x9e, 0x28, 0x85, 0x0f, 0x85, 0x39, 0xa9, 0xb9, 0xaf, 0xf3, 0xfe, 0x8f, 0x00, 0x00,
-	0x00, 0xff, 0xff, 0x32, 0xd5, 0xea, 0x6d, 0x2e, 0x04, 0x00, 0x00,
+	// 530 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x53, 0xdb, 0x6e, 0xd3, 0x30,
+	0x18, 0x26, 0x49, 0xdb, 0xad, 0x7f, 0xc6, 0x54, 0x7c, 0x31, 0x45, 0x45, 0x82, 0x32, 0xc4, 0xa8,
+	0x40, 0x38, 0x52, 0x40, 0x02, 0x76, 0x33, 0xb1, 0xad, 0x88, 0x0a, 0x0d, 0x2a, 0x0f, 0x71, 0x01,
+	0x48, 0x95, 0x97, 0x98, 0xce, 0xa2, 0xb1, 0x8d, 0xed, 0x16, 0xc2, 0x2b, 0xf0, 0x08, 0xbc, 0x01,
+	0x4f, 0x89, 0x6a, 0x77, 0xb4, 0xb0, 0x0e, 0xb8, 0xe1, 0xce, 0x87, 0xef, 0x94, 0xef, 0x77, 0xe0,
+	0xe6, 0x34, 0xd3, 0xb4, 0xc2, 0xb9, 0x2c, 0xd3, 0x5c, 0x6a, 0x96, 0x52, 0xa5, 0xd2, 0x42, 0x98,
+	0x34, 0x97, 0xe2, 0x3d, 0x1f, 0x61, 0xa5, 0xa5, 0x95, 0x08, 0x9d, 0x81, 0x34, 0xc3, 0x54, 0x29,
+	0x5c, 0x08, 0xd3, 0xbe, 0xfd, 0x1b, 0x31, 0x97, 0x65, 0x29, 0x45, 0x2a, 0x98, 0x4d, 0x69, 0x51,
+	0x68, 0x66, 0x8c, 0x27, 0xb7, 0xef, 0x5e, 0x0c, 0x2c, 0x98, 0xb1, 0x5c, 0x50, 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, 0x07, 0xb3, 0xbb, 0x27, 0x4a, 0xe1, 0x43, 0x61, 0x4e,
+	0x1a, 0xee, 0x7f, 0xbd, 0xff, 0x23, 0x00, 0x00, 0xff, 0xff, 0x83, 0x2b, 0x1c, 0x4c, 0x40, 0x04,
+	0x00, 0x00,
 }
diff --git a/app/dns/config.proto b/app/dns/config.proto
index a17ca734..b060a5e6 100644
--- a/app/dns/config.proto
+++ b/app/dns/config.proto
@@ -49,4 +49,7 @@ message Config {
   }
 
   repeated HostMapping static_hosts = 4;
+
+  // Tag is the inbound tag of DNS client.
+  string tag = 6;
 }
diff --git a/app/dns/server.go b/app/dns/server.go
index e25315ed..f74f004f 100644
--- a/app/dns/server.go
+++ b/app/dns/server.go
@@ -10,6 +10,7 @@ import (
 	"v2ray.com/core"
 	"v2ray.com/core/common"
 	"v2ray.com/core/common/net"
+	"v2ray.com/core/common/session"
 	"v2ray.com/core/common/strmatcher"
 	"v2ray.com/core/features"
 	"v2ray.com/core/features/dns"
@@ -24,12 +25,14 @@ type Server struct {
 	clientIP       net.IP
 	domainMatcher  strmatcher.IndexMatcher
 	domainIndexMap map[uint32]uint32
+	tag            string
 }
 
 // New creates a new DNS server with given configuration.
 func New(ctx context.Context, config *Config) (*Server, error) {
 	server := &Server{
 		clients: make([]Client, 0, len(config.NameServers)+len(config.NameServer)),
+		tag:     config.Tag,
 	}
 	if len(config.ClientIp) > 0 {
 		if len(config.ClientIp) != 4 && len(config.ClientIp) != 16 {
@@ -118,6 +121,11 @@ func (s *Server) Close() error {
 
 func (s *Server) queryIPTimeout(client Client, domain string, option IPOption) ([]net.IP, error) {
 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
+	if len(s.tag) > 0 {
+		ctx = session.ContextWithInbound(ctx, &session.Inbound{
+			Tag: s.tag,
+		})
+	}
 	ips, err := client.QueryIP(ctx, domain, option)
 	cancel()
 	return ips, err
diff --git a/app/dns/udpns.go b/app/dns/udpns.go
index 817cd38a..22e91b3f 100644
--- a/app/dns/udpns.go
+++ b/app/dns/udpns.go
@@ -303,7 +303,11 @@ func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string, option
 	for _, msg := range msgs {
 		b, err := dns.PackMessage(msg)
 		common.Must(err)
-		s.udpServer.Dispatch(context.Background(), s.address, b)
+		udpCtx := context.Background()
+		if inbound := session.InboundFromContext(ctx); inbound != nil {
+			udpCtx = session.ContextWithInbound(udpCtx, inbound)
+		}
+		s.udpServer.Dispatch(udpCtx, s.address, b)
 	}
 }