diff --git a/app/router/condition_geoip.go b/app/router/condition_geoip.go index 5f35bded1..bf09c46d3 100644 --- a/app/router/condition_geoip.go +++ b/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] } +// Match returns true if the given ip is included by the GeoIP. func (m *GeoIPMatcher) Match(ip net.IP) bool { switch len(ip) { 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 } -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 { for _, m := range c.matchers { if m.countryCode == geoip.CountryCode { @@ -193,5 +197,5 @@ func (c *GlobalGeoIPContainer) Add(geoip *GeoIP) (*GeoIPMatcher, error) { } var ( - globalGeoIPContainer GlobalGeoIPContainer + globalGeoIPContainer GeoIPMatcherContainer ) diff --git a/app/router/config.go b/app/router/config.go index 455547b08..23d268eeb 100644 --- a/app/router/config.go +++ b/app/router/config.go @@ -6,12 +6,15 @@ import ( "v2ray.com/core/common/net" ) +// CIDRList is an alias of []*CIDR to provide sort.Interface. type CIDRList []*CIDR +// Len implements sort.Interface. func (l *CIDRList) Len() int { return len(*l) } +// Less implements sort.Interface. func (l *CIDRList) Less(i int, j int) bool { ci := (*l)[i] cj := (*l)[j] @@ -37,6 +40,7 @@ func (l *CIDRList) Less(i int, j int) bool { return ci.Prefix < cj.Prefix } +// Swap implements sort.Interface. func (l *CIDRList) Swap(i int, j int) { (*l)[i], (*l)[j] = (*l)[j], (*l)[i] } diff --git a/app/router/config.pb.go b/app/router/config.pb.go index 2a5db6fdf..0739b0d9a 100644 --- a/app/router/config.pb.go +++ b/app/router/config.pb.go @@ -362,17 +362,25 @@ func (m *GeoSiteList) GetEntry() []*GeoSite { } 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"` // 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. // 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"` 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"` // 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. // 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"` 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"` diff --git a/app/router/config.proto b/app/router/config.proto index 69b109ac2..631ded8c9 100644 --- a/app/router/config.proto +++ b/app/router/config.proto @@ -60,6 +60,8 @@ message GeoSiteList{ message RoutingRule { // Tag of outbound that this rule is pointing to. string tag = 1; + + // List of domains for target domain matching. repeated Domain domain = 2; // List of CIDRs for target IP address matching.