mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
Independent source_ip_is_private
and ip_is_private
rules
This commit is contained in:
parent
ee086ea186
commit
f61ea0da89
@ -5,6 +5,7 @@ icon: material/alert-decagram
|
|||||||
!!! quote "Changes in sing-box 1.8.0"
|
!!! quote "Changes in sing-box 1.8.0"
|
||||||
|
|
||||||
:material-plus: [rule_set](#rule_set)
|
:material-plus: [rule_set](#rule_set)
|
||||||
|
:material-plus: [source_ip_is_private](#source_ip_is_private)
|
||||||
:material-delete-clock: [geoip](#geoip)
|
:material-delete-clock: [geoip](#geoip)
|
||||||
:material-delete-clock: [geosite](#geosite)
|
:material-delete-clock: [geosite](#geosite)
|
||||||
|
|
||||||
@ -56,6 +57,7 @@ icon: material/alert-decagram
|
|||||||
"10.0.0.0/24",
|
"10.0.0.0/24",
|
||||||
"192.168.0.1"
|
"192.168.0.1"
|
||||||
],
|
],
|
||||||
|
"source_ip_is_private": false,
|
||||||
"source_port": [
|
"source_port": [
|
||||||
12345
|
12345
|
||||||
],
|
],
|
||||||
@ -198,6 +200,12 @@ Match source geoip.
|
|||||||
|
|
||||||
Match source IP CIDR.
|
Match source IP CIDR.
|
||||||
|
|
||||||
|
#### source_ip_is_private
|
||||||
|
|
||||||
|
!!! question "Since sing-box 1.8.0"
|
||||||
|
|
||||||
|
Match non-public source IP.
|
||||||
|
|
||||||
#### source_port
|
#### source_port
|
||||||
|
|
||||||
Match source port.
|
Match source port.
|
||||||
|
@ -5,6 +5,7 @@ icon: material/alert-decagram
|
|||||||
!!! quote "sing-box 1.8.0 中的更改"
|
!!! quote "sing-box 1.8.0 中的更改"
|
||||||
|
|
||||||
:material-plus: [rule_set](#rule_set)
|
:material-plus: [rule_set](#rule_set)
|
||||||
|
:material-plus: [source_ip_is_private](#source_ip_is_private)
|
||||||
:material-delete-clock: [geoip](#geoip)
|
:material-delete-clock: [geoip](#geoip)
|
||||||
:material-delete-clock: [geosite](#geosite)
|
:material-delete-clock: [geosite](#geosite)
|
||||||
|
|
||||||
@ -55,6 +56,7 @@ icon: material/alert-decagram
|
|||||||
"source_ip_cidr": [
|
"source_ip_cidr": [
|
||||||
"10.0.0.0/24"
|
"10.0.0.0/24"
|
||||||
],
|
],
|
||||||
|
"source_ip_is_private": false,
|
||||||
"source_port": [
|
"source_port": [
|
||||||
12345
|
12345
|
||||||
],
|
],
|
||||||
@ -195,6 +197,12 @@ DNS 查询类型。值可以为整数或者类型名称字符串。
|
|||||||
|
|
||||||
匹配源 IP CIDR。
|
匹配源 IP CIDR。
|
||||||
|
|
||||||
|
#### source_ip_is_private
|
||||||
|
|
||||||
|
!!! question "自 sing-box 1.8.0 起"
|
||||||
|
|
||||||
|
匹配非公开源 IP。
|
||||||
|
|
||||||
#### source_port
|
#### source_port
|
||||||
|
|
||||||
匹配源端口。
|
匹配源端口。
|
||||||
|
@ -6,6 +6,8 @@ icon: material/alert-decagram
|
|||||||
|
|
||||||
:material-plus: [rule_set](#rule_set)
|
:material-plus: [rule_set](#rule_set)
|
||||||
:material-plus: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
|
:material-plus: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
|
||||||
|
:material-plus: [source_ip_is_private](#source_ip_is_private)
|
||||||
|
:material-plus: [ip_is_private](#ip_is_private)
|
||||||
:material-delete-clock: [source_geoip](#source_geoip)
|
:material-delete-clock: [source_geoip](#source_geoip)
|
||||||
:material-delete-clock: [geoip](#geoip)
|
:material-delete-clock: [geoip](#geoip)
|
||||||
:material-delete-clock: [geosite](#geosite)
|
:material-delete-clock: [geosite](#geosite)
|
||||||
@ -58,10 +60,12 @@ icon: material/alert-decagram
|
|||||||
"10.0.0.0/24",
|
"10.0.0.0/24",
|
||||||
"192.168.0.1"
|
"192.168.0.1"
|
||||||
],
|
],
|
||||||
|
"source_ip_is_private": false,
|
||||||
"ip_cidr": [
|
"ip_cidr": [
|
||||||
"10.0.0.0/24",
|
"10.0.0.0/24",
|
||||||
"192.168.0.1"
|
"192.168.0.1"
|
||||||
],
|
],
|
||||||
|
"ip_is_private": false,
|
||||||
"source_port": [
|
"source_port": [
|
||||||
12345
|
12345
|
||||||
],
|
],
|
||||||
@ -202,10 +206,22 @@ Match geoip.
|
|||||||
|
|
||||||
Match source IP CIDR.
|
Match source IP CIDR.
|
||||||
|
|
||||||
|
#### ip_is_private
|
||||||
|
|
||||||
|
!!! question "Since sing-box 1.8.0"
|
||||||
|
|
||||||
|
Match non-public IP.
|
||||||
|
|
||||||
#### ip_cidr
|
#### ip_cidr
|
||||||
|
|
||||||
Match IP CIDR.
|
Match IP CIDR.
|
||||||
|
|
||||||
|
#### source_ip_is_private
|
||||||
|
|
||||||
|
!!! question "Since sing-box 1.8.0"
|
||||||
|
|
||||||
|
Match non-public source IP.
|
||||||
|
|
||||||
#### source_port
|
#### source_port
|
||||||
|
|
||||||
Match source port.
|
Match source port.
|
||||||
|
@ -6,6 +6,8 @@ icon: material/alert-decagram
|
|||||||
|
|
||||||
:material-plus: [rule_set](#rule_set)
|
:material-plus: [rule_set](#rule_set)
|
||||||
:material-plus: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
|
:material-plus: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
|
||||||
|
:material-plus: [source_ip_is_private](#source_ip_is_private)
|
||||||
|
:material-plus: [ip_is_private](#ip_is_private)
|
||||||
:material-delete-clock: [source_geoip](#source_geoip)
|
:material-delete-clock: [source_geoip](#source_geoip)
|
||||||
:material-delete-clock: [geoip](#geoip)
|
:material-delete-clock: [geoip](#geoip)
|
||||||
:material-delete-clock: [geosite](#geosite)
|
:material-delete-clock: [geosite](#geosite)
|
||||||
@ -57,9 +59,11 @@ icon: material/alert-decagram
|
|||||||
"source_ip_cidr": [
|
"source_ip_cidr": [
|
||||||
"10.0.0.0/24"
|
"10.0.0.0/24"
|
||||||
],
|
],
|
||||||
|
"source_ip_is_private": false,
|
||||||
"ip_cidr": [
|
"ip_cidr": [
|
||||||
"10.0.0.0/24"
|
"10.0.0.0/24"
|
||||||
],
|
],
|
||||||
|
"ip_is_private": false,
|
||||||
"source_port": [
|
"source_port": [
|
||||||
12345
|
12345
|
||||||
],
|
],
|
||||||
@ -200,10 +204,22 @@ icon: material/alert-decagram
|
|||||||
|
|
||||||
匹配源 IP CIDR。
|
匹配源 IP CIDR。
|
||||||
|
|
||||||
|
#### source_ip_is_private
|
||||||
|
|
||||||
|
!!! question "自 sing-box 1.8.0 起"
|
||||||
|
|
||||||
|
匹配非公开源 IP。
|
||||||
|
|
||||||
#### ip_cidr
|
#### ip_cidr
|
||||||
|
|
||||||
匹配 IP CIDR。
|
匹配 IP CIDR。
|
||||||
|
|
||||||
|
#### ip_is_private
|
||||||
|
|
||||||
|
!!! question "自 sing-box 1.8.0 起"
|
||||||
|
|
||||||
|
匹配非公开 IP。
|
||||||
|
|
||||||
#### source_port
|
#### source_port
|
||||||
|
|
||||||
匹配源端口。
|
匹配源端口。
|
||||||
|
@ -68,6 +68,10 @@ icon: material/arrange-bring-forward
|
|||||||
{
|
{
|
||||||
"route": {
|
"route": {
|
||||||
"rules": [
|
"rules": [
|
||||||
|
{
|
||||||
|
"geoip": "private",
|
||||||
|
"outbound": "direct"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"geoip": "cn",
|
"geoip": "cn",
|
||||||
"outbound": "direct"
|
"outbound": "direct"
|
||||||
@ -90,6 +94,10 @@ icon: material/arrange-bring-forward
|
|||||||
{
|
{
|
||||||
"route": {
|
"route": {
|
||||||
"rules": [
|
"rules": [
|
||||||
|
{
|
||||||
|
"ip_is_private": true,
|
||||||
|
"outbound": "direct"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"rule_set": "geoip-cn",
|
"rule_set": "geoip-cn",
|
||||||
"outbound": "direct"
|
"outbound": "direct"
|
||||||
|
@ -78,7 +78,9 @@ type DefaultRule struct {
|
|||||||
SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
|
SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
|
||||||
GeoIP Listable[string] `json:"geoip,omitempty"`
|
GeoIP Listable[string] `json:"geoip,omitempty"`
|
||||||
SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
|
SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
|
||||||
|
SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
|
||||||
IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
|
IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
|
||||||
|
IPIsPrivate bool `json:"ip_is_private,omitempty"`
|
||||||
SourcePort Listable[uint16] `json:"source_port,omitempty"`
|
SourcePort Listable[uint16] `json:"source_port,omitempty"`
|
||||||
SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
|
SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
|
||||||
Port Listable[uint16] `json:"port,omitempty"`
|
Port Listable[uint16] `json:"port,omitempty"`
|
||||||
|
@ -78,6 +78,7 @@ type DefaultDNSRule struct {
|
|||||||
Geosite Listable[string] `json:"geosite,omitempty"`
|
Geosite Listable[string] `json:"geosite,omitempty"`
|
||||||
SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
|
SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
|
||||||
SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
|
SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
|
||||||
|
SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
|
||||||
SourcePort Listable[uint16] `json:"source_port,omitempty"`
|
SourcePort Listable[uint16] `json:"source_port,omitempty"`
|
||||||
SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
|
SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
|
||||||
Port Listable[uint16] `json:"port,omitempty"`
|
Port Listable[uint16] `json:"port,omitempty"`
|
||||||
|
@ -120,6 +120,11 @@ func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options opt
|
|||||||
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
||||||
rule.allItems = append(rule.allItems, item)
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
|
if options.SourceIPIsPrivate {
|
||||||
|
item := NewIPIsPrivateItem(true)
|
||||||
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
|
}
|
||||||
if len(options.IPCIDR) > 0 {
|
if len(options.IPCIDR) > 0 {
|
||||||
item, err := NewIPCIDRItem(false, options.IPCIDR)
|
item, err := NewIPCIDRItem(false, options.IPCIDR)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -128,6 +133,11 @@ func NewDefaultRule(router adapter.Router, logger log.ContextLogger, options opt
|
|||||||
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
||||||
rule.allItems = append(rule.allItems, item)
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
|
if options.IPIsPrivate {
|
||||||
|
item := NewIPIsPrivateItem(false)
|
||||||
|
rule.destinationAddressItems = append(rule.destinationAddressItems, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
|
}
|
||||||
if len(options.SourcePort) > 0 {
|
if len(options.SourcePort) > 0 {
|
||||||
item := NewPortItem(true, options.SourcePort)
|
item := NewPortItem(true, options.SourcePort)
|
||||||
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
||||||
|
@ -119,6 +119,11 @@ func NewDefaultDNSRule(router adapter.Router, logger log.ContextLogger, options
|
|||||||
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
||||||
rule.allItems = append(rule.allItems, item)
|
rule.allItems = append(rule.allItems, item)
|
||||||
}
|
}
|
||||||
|
if options.SourceIPIsPrivate {
|
||||||
|
item := NewIPIsPrivateItem(true)
|
||||||
|
rule.sourceAddressItems = append(rule.sourceAddressItems, item)
|
||||||
|
rule.allItems = append(rule.allItems, item)
|
||||||
|
}
|
||||||
if len(options.SourcePort) > 0 {
|
if len(options.SourcePort) > 0 {
|
||||||
item := NewPortItem(true, options.SourcePort)
|
item := NewPortItem(true, options.SourcePort)
|
||||||
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
rule.sourcePortItems = append(rule.sourcePortItems, item)
|
||||||
|
44
route/rule_item_ip_is_private.go
Normal file
44
route/rule_item_ip_is_private.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package route
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
|
||||||
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ RuleItem = (*IPIsPrivateItem)(nil)
|
||||||
|
|
||||||
|
type IPIsPrivateItem struct {
|
||||||
|
isSource bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIPIsPrivateItem(isSource bool) *IPIsPrivateItem {
|
||||||
|
return &IPIsPrivateItem{isSource}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *IPIsPrivateItem) Match(metadata *adapter.InboundContext) bool {
|
||||||
|
var destination netip.Addr
|
||||||
|
if r.isSource {
|
||||||
|
destination = metadata.Source.Addr
|
||||||
|
} else {
|
||||||
|
destination = metadata.Destination.Addr
|
||||||
|
}
|
||||||
|
if destination.IsValid() && !N.IsPublicAddr(destination) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
for _, destinationAddress := range metadata.DestinationAddresses {
|
||||||
|
if !N.IsPublicAddr(destinationAddress) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *IPIsPrivateItem) String() string {
|
||||||
|
if r.isSource {
|
||||||
|
return "source_ip_is_private=true"
|
||||||
|
} else {
|
||||||
|
return "ip_is_private=true"
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user