mihomo/adapter/inbound/auth.go

46 lines
813 B
Go
Raw Normal View History

2023-10-10 19:43:26 +08:00
package inbound
import (
"net"
"net/netip"
2023-10-11 22:54:19 +08:00
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
2023-10-10 19:43:26 +08:00
)
var skipAuthPrefixes []netip.Prefix
func SetSkipAuthPrefixes(prefixes []netip.Prefix) {
skipAuthPrefixes = prefixes
}
func SkipAuthPrefixes() []netip.Prefix {
return skipAuthPrefixes
}
func SkipAuthRemoteAddr(addr net.Addr) bool {
2023-10-11 22:54:19 +08:00
m := C.Metadata{}
if err := m.SetRemoteAddr(addr); err != nil {
return false
}
return skipAuth(m.AddrPort().Addr())
}
func SkipAuthRemoteAddress(addr string) bool {
m := C.Metadata{}
if err := m.SetRemoteAddress(addr); err != nil {
return false
}
return skipAuth(m.AddrPort().Addr())
}
func skipAuth(addr netip.Addr) bool {
if addr.IsValid() {
2023-10-10 19:43:26 +08:00
for _, prefix := range skipAuthPrefixes {
2023-10-11 22:54:19 +08:00
if prefix.Contains(addr.Unmap()) {
2023-10-10 19:43:26 +08:00
return true
}
}
}
return false
}