Skip to content

Commit

Permalink
rename IP to CIDR in router
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Oct 18, 2016
1 parent 2af4b16 commit e13c97d
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 127 deletions.
7 changes: 5 additions & 2 deletions app/dns/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions app/router/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,18 @@ func (this *RoutingRule) BuildCondition() (Condition, error) {
conds.Add(anyCond)
}

if len(this.Ip) > 0 {
ipv4Net := make(map[uint32]byte)
if len(this.Cidr) > 0 {
ipv4Net := v2net.NewIPNet()
ipv6Cond := NewAnyCondition()
hasIpv6 := false

for _, ip := range this.Ip {
for _, ip := range this.Cidr {
switch len(ip.Ip) {
case net.IPv4len:
k := (uint32(ip.Ip[0]) << 24) + (uint32(ip.Ip[1]) << 16) + (uint32(ip.Ip[2]) << 8) + uint32(ip.Ip[3])
ipv4Net[k] = byte(32 - ip.UnmatchingBits)
ipv4Net.AddIP(ip.Ip, byte(ip.Prefix))
case net.IPv6len:
hasIpv6 = true
matcher, err := NewCIDRMatcher(ip.Ip, uint32(32)-ip.UnmatchingBits)
matcher, err := NewCIDRMatcher(ip.Ip, ip.Prefix)
if err != nil {
return nil, err
}
Expand All @@ -57,13 +56,13 @@ func (this *RoutingRule) BuildCondition() (Condition, error) {
}
}

if len(ipv4Net) > 0 && hasIpv6 {
if !ipv4Net.IsEmpty() && hasIpv6 {
cond := NewAnyCondition()
cond.Add(NewIPv4Matcher(v2net.NewIPNetInitialValue(ipv4Net)))
cond.Add(NewIPv4Matcher(ipv4Net))
cond.Add(ipv6Cond)
conds.Add(cond)
} else if len(ipv4Net) > 0 {
conds.Add(NewIPv4Matcher(v2net.NewIPNetInitialValue(ipv4Net)))
} else if !ipv4Net.IsEmpty() {
conds.Add(NewIPv4Matcher(ipv4Net))
} else if hasIpv6 {
conds.Add(ipv6Cond)
}
Expand Down
95 changes: 48 additions & 47 deletions app/router/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions app/router/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ message Domain {
string value = 2;
}

// IP for routing decision.
message IP {
// IP for routing decision, in CIDR form.
message CIDR {
// IP address, should be either 4 or 16 bytes.
bytes ip = 1;

// Number of right-most bits in IP matching that is allowed.
// Single IP address like 127.0.0.1 should use unmatching_bits = 0.
// CIDR 10.0.0.0/8 should use unmatching_bits = 32-8 = 24.
uint32 unmatching_bits = 2;
// Number of leading ones in the network mask.
uint32 prefix = 2;
}

message RoutingRule {
string tag = 1;
repeated Domain domain = 2;
repeated IP ip = 3;
repeated CIDR cidr = 3;
v2ray.core.common.net.PortRange port_range = 4;
v2ray.core.common.net.NetworkList network_list = 5;
}
Expand Down
5 changes: 4 additions & 1 deletion common/loader/type.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 10 additions & 14 deletions common/net/ipnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ type IPNet struct {
}

func NewIPNet() *IPNet {
return NewIPNetInitialValue(make(map[uint32]byte, 1024))
}

func NewIPNetInitialValue(data map[uint32]byte) *IPNet {
return &IPNet{
cache: data,
cache: make(map[uint32]byte, 1024),
}
}

Expand All @@ -45,11 +41,15 @@ func (this *IPNet) Add(ipNet *net.IPNet) {
// For now, we don't support IPv6
return
}
value := ipToUint32(ipv4)
mask := ipMaskToByte(ipNet.Mask)
existing, found := this.cache[value]
this.AddIP(ipv4, mask)
}

func (this *IPNet) AddIP(ip []byte, mask byte) {
k := ipToUint32(ip)
existing, found := this.cache[k]
if !found || existing > mask {
this.cache[value] = mask
this.cache[k] = mask
}
}

Expand Down Expand Up @@ -80,12 +80,8 @@ func (this *IPNet) Contains(ip net.IP) bool {
return false
}

func (this *IPNet) Serialize() []uint32 {
content := make([]uint32, 0, 2*len(this.cache))
for key, value := range this.cache {
content = append(content, uint32(key), uint32(value))
}
return content
func (this *IPNet) IsEmpty() bool {
return len(this.cache) == 0
}

func init() {
Expand Down
6 changes: 4 additions & 2 deletions common/net/network.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions common/protocol/user.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e13c97d

Please sign in to comment.