forked from MetaCubeX/mihomo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eg. IP-SUFFIX,0.0.0.124/6,匹配ip二进制后四位(IP-CIDR的倒序),支持ipv6
- Loading branch information
adlyq
committed
Jun 2, 2022
1 parent
1af39cb
commit 04e5d02
Showing
5 changed files
with
100 additions
and
0 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
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,79 @@ | ||
package common | ||
|
||
import ( | ||
C "github.com/Dreamacro/clash/constant" | ||
"net/netip" | ||
) | ||
|
||
type IPSuffix struct { | ||
*Base | ||
ipBytes []byte | ||
bits int | ||
payload string | ||
adapter string | ||
isSourceIP bool | ||
noResolveIP bool | ||
} | ||
|
||
func (is *IPSuffix) RuleType() C.RuleType { | ||
if is.isSourceIP { | ||
return C.SrcIPSuffix | ||
} | ||
return C.IPSuffix | ||
} | ||
|
||
func (is *IPSuffix) Match(metadata *C.Metadata) bool { | ||
ip := metadata.DstIP | ||
if is.isSourceIP { | ||
ip = metadata.SrcIP | ||
} | ||
|
||
mIPBytes := ip.AsSlice() | ||
if len(is.ipBytes) != len(mIPBytes) { | ||
return false | ||
} | ||
|
||
size := len(mIPBytes) | ||
bits := is.bits | ||
|
||
for i := bits / 8; i > 0; i-- { | ||
if is.ipBytes[size-i] != mIPBytes[size-i] { | ||
return false | ||
} | ||
} | ||
|
||
if (is.ipBytes[size-bits/8-1] << (8 - bits%8)) != (mIPBytes[size-bits/8-1] << (8 - bits%8)) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (is *IPSuffix) Adapter() string { | ||
return is.adapter | ||
} | ||
|
||
func (is *IPSuffix) Payload() string { | ||
return is.payload | ||
} | ||
|
||
func (is *IPSuffix) ShouldResolveIP() bool { | ||
return !is.noResolveIP | ||
} | ||
|
||
func NewIPSuffix(payload, adapter string, isSrc, noResolveIP bool) (*IPSuffix, error) { | ||
ipnet, err := netip.ParsePrefix(payload) | ||
if err != nil { | ||
return nil, errPayload | ||
} | ||
|
||
return &IPSuffix{ | ||
Base: &Base{}, | ||
payload: payload, | ||
ipBytes: ipnet.Addr().AsSlice(), | ||
bits: ipnet.Bits(), | ||
adapter: adapter, | ||
isSourceIP: isSrc, | ||
noResolveIP: noResolveIP, | ||
}, nil | ||
} |
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