forked from WhoJave/clash
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
190 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cidr | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
"unsafe" | ||
|
||
"go4.org/netipx" | ||
) | ||
|
||
type IpCidrSet struct { | ||
// must same with netipx.IPSet | ||
rr []netipx.IPRange | ||
} | ||
|
||
func NewIpCidrSet() *IpCidrSet { | ||
return &IpCidrSet{} | ||
} | ||
|
||
func (set *IpCidrSet) AddIpCidrForString(ipCidr string) error { | ||
prefix, err := netip.ParsePrefix(ipCidr) | ||
if err != nil { | ||
return err | ||
} | ||
return set.AddIpCidr(prefix) | ||
} | ||
|
||
func (set *IpCidrSet) AddIpCidr(ipCidr netip.Prefix) (err error) { | ||
if r := netipx.RangeOfPrefix(ipCidr); r.IsValid() { | ||
set.rr = append(set.rr, r) | ||
} else { | ||
err = fmt.Errorf("not valid ipcidr range: %s", ipCidr) | ||
} | ||
return | ||
} | ||
|
||
func (set *IpCidrSet) IsContainForString(ipString string) bool { | ||
ip, err := netip.ParseAddr(ipString) | ||
if err != nil { | ||
return false | ||
} | ||
return set.IsContain(ip) | ||
} | ||
|
||
func (set *IpCidrSet) IsContain(ip netip.Addr) bool { | ||
return set.toIPSet().Contains(ip.WithZone("")) | ||
} | ||
|
||
func (set *IpCidrSet) Merge() error { | ||
var b netipx.IPSetBuilder | ||
b.AddSet(set.toIPSet()) | ||
i, err := b.IPSet() | ||
if err != nil { | ||
return err | ||
} | ||
set.fromIPSet(i) | ||
return nil | ||
} | ||
|
||
func (set *IpCidrSet) toIPSet() *netipx.IPSet { | ||
return (*netipx.IPSet)(unsafe.Pointer(set)) | ||
} | ||
|
||
func (set *IpCidrSet) fromIPSet(i *netipx.IPSet) { | ||
*set = *(*IpCidrSet)(unsafe.Pointer(i)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package cidr | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIpv4(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ipCidr string | ||
ip string | ||
expected bool | ||
}{ | ||
{ | ||
name: "Test Case 1", | ||
ipCidr: "149.154.160.0/20", | ||
ip: "149.154.160.0", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Test Case 2", | ||
ipCidr: "192.168.0.0/16", | ||
ip: "10.0.0.1", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
set := &IpCidrSet{} | ||
set.AddIpCidrForString(test.ipCidr) | ||
|
||
result := set.IsContainForString(test.ip) | ||
if result != test.expected { | ||
t.Errorf("Expected result: %v, got: %v", test.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIpv6(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ipCidr string | ||
ip string | ||
expected bool | ||
}{ | ||
{ | ||
name: "Test Case 1", | ||
ipCidr: "2409:8000::/20", | ||
ip: "2409:8087:1e03:21::27", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Test Case 2", | ||
ipCidr: "240e::/16", | ||
ip: "240e:964:ea02:100:1800::71", | ||
expected: true, | ||
}, | ||
} | ||
// Add more test cases as needed | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
set := &IpCidrSet{} | ||
set.AddIpCidrForString(test.ipCidr) | ||
|
||
result := set.IsContainForString(test.ip) | ||
if result != test.expected { | ||
t.Errorf("Expected result: %v, got: %v", test.expected, result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestMerge(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
ipCidr1 string | ||
ipCidr2 string | ||
ipCidr3 string | ||
expectedLen int | ||
}{ | ||
{ | ||
name: "Test Case 1", | ||
ipCidr1: "2409:8000::/20", | ||
ipCidr2: "2409:8000::/21", | ||
ipCidr3: "2409:8000::/48", | ||
expectedLen: 1, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
set := &IpCidrSet{} | ||
set.AddIpCidrForString(test.ipCidr1) | ||
set.AddIpCidrForString(test.ipCidr2) | ||
set.Merge() | ||
|
||
rangesLen := len(set.rr) | ||
|
||
if rangesLen != test.expectedLen { | ||
t.Errorf("Expected len: %v, got: %v", test.expectedLen, rangesLen) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters