This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LAN IP whitelist and blacklist to inbound
configuration
- Loading branch information
1 parent
93dc2ba
commit de5b09a
Showing
7 changed files
with
103 additions
and
1 deletion.
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,65 @@ | ||
package inbound | ||
|
||
import ( | ||
"net" | ||
"net/netip" | ||
|
||
C "github.com/MerlinKodo/clash-rev/constant" | ||
) | ||
|
||
var lanAllowedIPs []netip.Prefix | ||
var lanDisAllowedIPs []netip.Prefix | ||
|
||
func SetAllowedIPs(prefixes []netip.Prefix) { | ||
lanAllowedIPs = prefixes | ||
} | ||
|
||
func SetDisAllowedIPs(prefixes []netip.Prefix) { | ||
lanDisAllowedIPs = prefixes | ||
} | ||
|
||
func AllowedIPs() []netip.Prefix { | ||
return lanAllowedIPs | ||
} | ||
|
||
func DisAllowedIPs() []netip.Prefix { | ||
return lanDisAllowedIPs | ||
} | ||
|
||
func IsRemoteAddrAllowed(addr net.Addr) bool { | ||
m := C.Metadata{} | ||
if err := m.SetRemoteAddr(addr); err != nil { | ||
return false | ||
} | ||
return isAllowed(m.AddrPort().Addr()) && !isDisAllowed(m.AddrPort().Addr()) | ||
} | ||
|
||
func IsRemoteAddressAllowed(addr string) bool { | ||
m := C.Metadata{} | ||
if err := m.SetRemoteAddress(addr); err != nil { | ||
return false | ||
} | ||
return isAllowed(m.AddrPort().Addr()) && !isDisAllowed(m.AddrPort().Addr()) | ||
} | ||
|
||
func isAllowed(addr netip.Addr) bool { | ||
if addr.IsValid() { | ||
for _, prefix := range lanAllowedIPs { | ||
if prefix.Contains(addr.Unmap()) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func isDisAllowed(addr netip.Addr) bool { | ||
if addr.IsValid() { | ||
for _, prefix := range lanDisAllowedIPs { | ||
if prefix.Contains(addr.Unmap()) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} |
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
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