github.com/metacubex/mihomo@v1.18.5/adapter/inbound/ipfilter.go (about)

     1  package inbound
     2  
     3  import (
     4  	"net"
     5  	"net/netip"
     6  
     7  	C "github.com/metacubex/mihomo/constant"
     8  )
     9  
    10  var lanAllowedIPs []netip.Prefix
    11  var lanDisAllowedIPs []netip.Prefix
    12  
    13  func SetAllowedIPs(prefixes []netip.Prefix) {
    14  	lanAllowedIPs = prefixes
    15  }
    16  
    17  func SetDisAllowedIPs(prefixes []netip.Prefix) {
    18  	lanDisAllowedIPs = prefixes
    19  }
    20  
    21  func AllowedIPs() []netip.Prefix {
    22  	return lanAllowedIPs
    23  }
    24  
    25  func DisAllowedIPs() []netip.Prefix {
    26  	return lanDisAllowedIPs
    27  }
    28  
    29  func IsRemoteAddrDisAllowed(addr net.Addr) bool {
    30  	m := C.Metadata{}
    31  	if err := m.SetRemoteAddr(addr); err != nil {
    32  		return false
    33  	}
    34  	return isAllowed(m.AddrPort().Addr().Unmap()) && !isDisAllowed(m.AddrPort().Addr().Unmap())
    35  }
    36  
    37  func isAllowed(addr netip.Addr) bool {
    38  	if addr.IsValid() {
    39  		for _, prefix := range lanAllowedIPs {
    40  			if prefix.Contains(addr) {
    41  				return true
    42  			}
    43  		}
    44  	}
    45  	return false
    46  }
    47  
    48  func isDisAllowed(addr netip.Addr) bool {
    49  	if addr.IsValid() {
    50  		for _, prefix := range lanDisAllowedIPs {
    51  			if prefix.Contains(addr) {
    52  				return true
    53  			}
    54  		}
    55  	}
    56  	return false
    57  }