Darien Raymond 7 years ago
parent
commit
15cd999ed3
4 changed files with 22 additions and 4 deletions
  1. 7 3
      app/router/condition_geoip.go
  2. 4 0
      app/router/config.go
  3. 9 1
      app/router/config.pb.go
  4. 2 0
      app/router/config.proto

+ 7 - 3
app/router/condition_geoip.go

@@ -153,6 +153,7 @@ func (m *GeoIPMatcher) match6(ip ipv6) bool {
 	return normalize6(ip, m.prefix6[l]) == m.ip6[l]
 	return normalize6(ip, m.prefix6[l]) == m.ip6[l]
 }
 }
 
 
+// Match returns true if the given ip is included by the GeoIP.
 func (m *GeoIPMatcher) Match(ip net.IP) bool {
 func (m *GeoIPMatcher) Match(ip net.IP) bool {
 	switch len(ip) {
 	switch len(ip) {
 	case 4:
 	case 4:
@@ -167,11 +168,14 @@ func (m *GeoIPMatcher) Match(ip net.IP) bool {
 	}
 	}
 }
 }
 
 
-type GlobalGeoIPContainer struct {
+// GeoIPMatcherContainer is a container for GeoIPMatchers. It keeps unique copies of GeoIPMatcher by country code.
+type GeoIPMatcherContainer struct {
 	matchers []*GeoIPMatcher
 	matchers []*GeoIPMatcher
 }
 }
 
 
-func (c *GlobalGeoIPContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
+// Add adds a new GeoIP set into the container.
+// If the country code of GeoIP is not empty, GeoIPMatcherContainer will try to find an existing one, instead of adding a new one.
+func (c *GeoIPMatcherContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
 	if len(geoip.CountryCode) > 0 {
 	if len(geoip.CountryCode) > 0 {
 		for _, m := range c.matchers {
 		for _, m := range c.matchers {
 			if m.countryCode == geoip.CountryCode {
 			if m.countryCode == geoip.CountryCode {
@@ -193,5 +197,5 @@ func (c *GlobalGeoIPContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) {
 }
 }
 
 
 var (
 var (
-	globalGeoIPContainer GlobalGeoIPContainer
+	globalGeoIPContainer GeoIPMatcherContainer
 )
 )

+ 4 - 0
app/router/config.go

@@ -6,12 +6,15 @@ import (
 	"v2ray.com/core/common/net"
 	"v2ray.com/core/common/net"
 )
 )
 
 
+// CIDRList is an alias of []*CIDR to provide sort.Interface.
 type CIDRList []*CIDR
 type CIDRList []*CIDR
 
 
+// Len implements sort.Interface.
 func (l *CIDRList) Len() int {
 func (l *CIDRList) Len() int {
 	return len(*l)
 	return len(*l)
 }
 }
 
 
+// Less implements sort.Interface.
 func (l *CIDRList) Less(i int, j int) bool {
 func (l *CIDRList) Less(i int, j int) bool {
 	ci := (*l)[i]
 	ci := (*l)[i]
 	cj := (*l)[j]
 	cj := (*l)[j]
@@ -37,6 +40,7 @@ func (l *CIDRList) Less(i int, j int) bool {
 	return ci.Prefix < cj.Prefix
 	return ci.Prefix < cj.Prefix
 }
 }
 
 
+// Swap implements sort.Interface.
 func (l *CIDRList) Swap(i int, j int) {
 func (l *CIDRList) Swap(i int, j int) {
 	(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
 	(*l)[i], (*l)[j] = (*l)[j], (*l)[i]
 }
 }

+ 9 - 1
app/router/config.pb.go

@@ -362,17 +362,25 @@ func (m *GeoSiteList) GetEntry() []*GeoSite {
 }
 }
 
 
 type RoutingRule struct {
 type RoutingRule struct {
-	Tag    string    `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
+	// Tag of outbound that this rule is pointing to.
+	Tag string `protobuf:"bytes,1,opt,name=tag,proto3" json:"tag,omitempty"`
+	// List of domains for target domain matching.
 	Domain []*Domain `protobuf:"bytes,2,rep,name=domain,proto3" json:"domain,omitempty"`
 	Domain []*Domain `protobuf:"bytes,2,rep,name=domain,proto3" json:"domain,omitempty"`
 	// List of CIDRs for target IP address matching.
 	// List of CIDRs for target IP address matching.
+	// The list must be sorted beforehand.
 	Cidr []*CIDR `protobuf:"bytes,3,rep,name=cidr,proto3" json:"cidr,omitempty"` // Deprecated: Do not use.
 	Cidr []*CIDR `protobuf:"bytes,3,rep,name=cidr,proto3" json:"cidr,omitempty"` // Deprecated: Do not use.
 	// List of GeoIPs for target IP address matching. If this entry exists, the cidr above will have no effect.
 	// List of GeoIPs for target IP address matching. If this entry exists, the cidr above will have no effect.
+	// GeoIP fields with the same country code are supposed to contain exactly same content. They will be merged during runtime.
+	// For customized GeoIPs, please leave country code empty.
+	// The CIDR list in the GeoIP must be sorted beforehand.
 	Geoip       []*GeoIP         `protobuf:"bytes,10,rep,name=geoip,proto3" json:"geoip,omitempty"`
 	Geoip       []*GeoIP         `protobuf:"bytes,10,rep,name=geoip,proto3" json:"geoip,omitempty"`
 	PortRange   *net.PortRange   `protobuf:"bytes,4,opt,name=port_range,json=portRange,proto3" json:"port_range,omitempty"`
 	PortRange   *net.PortRange   `protobuf:"bytes,4,opt,name=port_range,json=portRange,proto3" json:"port_range,omitempty"`
 	NetworkList *net.NetworkList `protobuf:"bytes,5,opt,name=network_list,json=networkList,proto3" json:"network_list,omitempty"`
 	NetworkList *net.NetworkList `protobuf:"bytes,5,opt,name=network_list,json=networkList,proto3" json:"network_list,omitempty"`
 	// List of CIDRs for source IP address matching.
 	// List of CIDRs for source IP address matching.
+	// The list must be sorted beforehand.
 	SourceCidr []*CIDR `protobuf:"bytes,6,rep,name=source_cidr,json=sourceCidr,proto3" json:"source_cidr,omitempty"` // Deprecated: Do not use.
 	SourceCidr []*CIDR `protobuf:"bytes,6,rep,name=source_cidr,json=sourceCidr,proto3" json:"source_cidr,omitempty"` // Deprecated: Do not use.
 	// List of GeoIPs for source IP address matching. If this entry exists, the source_cidr above will have no effect.
 	// List of GeoIPs for source IP address matching. If this entry exists, the source_cidr above will have no effect.
+	// The CIDR list in the GeoIP must be sorted beforehand.
 	SourceGeoip          []*GeoIP `protobuf:"bytes,11,rep,name=source_geoip,json=sourceGeoip,proto3" json:"source_geoip,omitempty"`
 	SourceGeoip          []*GeoIP `protobuf:"bytes,11,rep,name=source_geoip,json=sourceGeoip,proto3" json:"source_geoip,omitempty"`
 	UserEmail            []string `protobuf:"bytes,7,rep,name=user_email,json=userEmail,proto3" json:"user_email,omitempty"`
 	UserEmail            []string `protobuf:"bytes,7,rep,name=user_email,json=userEmail,proto3" json:"user_email,omitempty"`
 	InboundTag           []string `protobuf:"bytes,8,rep,name=inbound_tag,json=inboundTag,proto3" json:"inbound_tag,omitempty"`
 	InboundTag           []string `protobuf:"bytes,8,rep,name=inbound_tag,json=inboundTag,proto3" json:"inbound_tag,omitempty"`

+ 2 - 0
app/router/config.proto

@@ -60,6 +60,8 @@ message GeoSiteList{
 message RoutingRule {
 message RoutingRule {
   // Tag of outbound that this rule is pointing to.
   // Tag of outbound that this rule is pointing to.
   string tag = 1;
   string tag = 1;
+
+  // List of domains for target domain matching.
   repeated Domain domain = 2;
   repeated Domain domain = 2;
 
 
   // List of CIDRs for target IP address matching.
   // List of CIDRs for target IP address matching.