diff --git a/adapter/inbound.go b/adapter/inbound.go index 2092ed4f5..0cdc75d43 100644 --- a/adapter/inbound.go +++ b/adapter/inbound.go @@ -77,6 +77,7 @@ type InboundContext struct { LastInbound string OriginDestination M.Socksaddr RouteOriginalDestination M.Socksaddr + TCPPrefix []byte UDPDisableDomainUnmapping bool UDPConnect bool UDPTimeout time.Duration diff --git a/docs/configuration/route/rule_action.md b/docs/configuration/route/rule_action.md index 55de06f80..016cd6f0b 100644 --- a/docs/configuration/route/rule_action.md +++ b/docs/configuration/route/rule_action.md @@ -2,6 +2,10 @@ icon: material/new-box --- +!!! quote "Changes in sing-box 1.15.0" + + :material-plus: [tcp_prefix](#tcp_prefix) + !!! quote "Changes in sing-box 1.13.0" :material-plus: [bypass](#bypass) @@ -146,6 +150,7 @@ Not available when `method` is set to drop. "override_port": 0, "network_strategy": "", "fallback_delay": "", + "tcp_prefix": "", "udp_disable_domain_unmapping": false, "udp_connect": false, "udp_timeout": "", @@ -186,6 +191,14 @@ See [Dial Fields](/configuration/shared/dial/#fallback_network_type) for details See [Dial Fields](/configuration/shared/dial/#fallback_delay) for details. +#### tcp_prefix + +!!! question "Since sing-box 1.15.0" + +Prepend data to each TCP connection. + +Specify a Base64 string or a byte array. + #### udp_disable_domain_unmapping If enabled, for UDP proxy requests addressed to a domain, diff --git a/docs/configuration/route/rule_action.zh.md b/docs/configuration/route/rule_action.zh.md index fd02469f8..732e219e3 100644 --- a/docs/configuration/route/rule_action.zh.md +++ b/docs/configuration/route/rule_action.zh.md @@ -2,6 +2,10 @@ icon: material/new-box --- +!!! quote "sing-box 1.15.0 中的更改" + + :material-plus: [tcp_prefix](#tcp_prefix) + !!! quote "sing-box 1.13.0 中的更改" :material-plus: [bypass](#bypass) @@ -139,6 +143,7 @@ icon: material/new-box "override_port": 0, "network_strategy": "", "fallback_delay": "", + "tcp_prefix": "", "udp_disable_domain_unmapping": false, "udp_connect": false, "udp_timeout": "", @@ -183,6 +188,14 @@ icon: material/new-box 详情参阅 [拨号字段](/zh/configuration/shared/dial/#fallback_delay)。 +#### tcp_prefix + +!!! question "自 sing-box 1.15.0 起" + +在每个 TCP 连接的开头添加数据。 + +使用 Base64 字符串或字节数组。 + #### udp_disable_domain_unmapping 如果启用,对于地址为域的 UDP 代理请求,将在响应中发送原始包地址而不是映射的域。 diff --git a/docs/schema.json b/docs/schema.json index cba1fd3a2..cbfa52ff9 100644 --- a/docs/schema.json +++ b/docs/schema.json @@ -15830,6 +15830,21 @@ "minimum": 0, "maximum": 4294967295 }, + "tcp_prefix": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + } + ] + }, "udp_disable_domain_unmapping": { "type": "boolean" }, @@ -15890,6 +15905,21 @@ "minimum": 0, "maximum": 4294967295 }, + "tcp_prefix": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + } + ] + }, "udp_disable_domain_unmapping": { "type": "boolean" }, @@ -15957,6 +15987,21 @@ "minimum": 0, "maximum": 4294967295 }, + "tcp_prefix": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "array", + "items": { + "type": "integer", + "minimum": 0, + "maximum": 255 + } + } + ] + }, "udp_disable_domain_unmapping": { "type": "boolean" }, diff --git a/option/rule.go b/option/rule.go index 9af85fa94..abb46f4ae 100644 --- a/option/rule.go +++ b/option/rule.go @@ -262,7 +262,7 @@ func unmarshalDefaultRuleContext(ctx context.Context, data []byte, rule *Default if err != nil { return err } - if depth > 0 && rawAction == "" && routeOptions == (RouteActionOptions{}) { + if depth > 0 && rawAction == "" && reflect.DeepEqual(routeOptions, RouteActionOptions{}) { rule.RuleAction = RuleAction{} } return nil @@ -286,7 +286,7 @@ func unmarshalLogicalRuleContext(ctx context.Context, data []byte, rule *Logical if err != nil { return err } - if depth > 0 && rawAction == "" && routeOptions == (RouteActionOptions{}) { + if depth > 0 && rawAction == "" && reflect.DeepEqual(routeOptions, RouteActionOptions{}) { rule.RuleAction = RuleAction{} } return nil diff --git a/option/rule_action.go b/option/rule_action.go index 33ddd2bdc..f1bb893d2 100644 --- a/option/rule_action.go +++ b/option/rule_action.go @@ -169,6 +169,7 @@ type RawRouteOptionsActionOptions struct { NetworkStrategy *NetworkStrategy `json:"network_strategy,omitempty"` FallbackDelay uint32 `json:"fallback_delay,omitempty"` + TCPPrefix []byte `json:"tcp_prefix,omitempty"` UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"` UDPConnect bool `json:"udp_connect,omitempty"` UDPTimeout badoption.Duration `json:"udp_timeout,omitempty"` @@ -187,7 +188,10 @@ func (r *RouteOptionsActionOptions) UnmarshalJSON(data []byte) error { if err != nil { return err } - if *r == (RouteOptionsActionOptions{}) { + if len(r.TCPPrefix) == 0 { + r.TCPPrefix = nil + } + if reflect.DeepEqual(*r, RouteOptionsActionOptions{}) { return E.New("empty route option action") } if r.TLSFragment && r.TLSRecordFragment { diff --git a/route/conn.go b/route/conn.go index f567096b0..dfcd69639 100644 --- a/route/conn.go +++ b/route/conn.go @@ -133,6 +133,31 @@ func (m *ConnectionManager) NewConnection(ctx context.Context, this N.Dialer, co m.logger.ErrorContext(ctx, err) return } + if len(metadata.TCPPrefix) > 0 { + writeContext, cancel := context.WithTimeout(ctx, C.TCPTimeout) + writeClosed := make(chan struct{}) + stop := context.AfterFunc(writeContext, func() { + remoteConn.Close() + close(writeClosed) + }) + var n int + n, err = remoteConn.Write(metadata.TCPPrefix) + if !stop() { + <-writeClosed + err = writeContext.Err() + } + cancel() + if err == nil && n < len(metadata.TCPPrefix) { + err = io.ErrShortWrite + } + if err != nil { + err = E.Cause(err, "write TCP prefix") + remoteConn.Close() + N.CloseOnHandshakeFailure(conn, onClose, err) + m.logger.ErrorContext(ctx, err) + return + } + } err = N.ReportConnHandshakeSuccess(conn, remoteConn) if err != nil { err = E.Cause(err, "report handshake success") diff --git a/route/route.go b/route/route.go index bd8d121e4..447f50c54 100644 --- a/route/route.go +++ b/route/route.go @@ -394,11 +394,20 @@ func (r *Router) PreMatch(metadata adapter.InboundContext, firstPacket []byte) a r.logger.DebugContext(ctx, "sniffed packet protocol: ", metadata.Protocol) } case *R.RuleActionRouteOptions: + if metadata.Network == N.NetworkTCP && len(action.TCPPrefix) > 0 { + return continueResult + } applyRouteOptionsOverride(&metadata, action) case *R.RuleActionRoute: + if metadata.Network == N.NetworkTCP && len(action.TCPPrefix) > 0 { + return continueResult + } applyRouteOptionsOverride(&metadata, &action.RuleActionRouteOptions) return r.preMatchFlow(ctx, &metadata, packetDestination, currentRule, action.Outbound) case *R.RuleActionBypass: + if metadata.Network == N.NetworkTCP && len(action.TCPPrefix) > 0 { + return continueResult + } applyRouteOptionsOverride(&metadata, &action.RuleActionRouteOptions) if action.Outbound == "" { if metadata.Destination.IsDomain() || metadata.Destination != packetDestination { @@ -658,6 +667,9 @@ match: if routeOptions.FallbackDelay != 0 { metadata.FallbackDelay = routeOptions.FallbackDelay } + if len(routeOptions.TCPPrefix) > 0 { + metadata.TCPPrefix = routeOptions.TCPPrefix + } if routeOptions.UDPDisableDomainUnmapping { metadata.UDPDisableDomainUnmapping = true } diff --git a/route/rule/rule_action.go b/route/rule/rule_action.go index 9a55d2ad9..7051be884 100644 --- a/route/rule/rule_action.go +++ b/route/rule/rule_action.go @@ -32,6 +32,7 @@ func newRuleActionRouteOptions(options option.RawRouteOptionsActionOptions) (Rul OverridePort: options.OverridePort, NetworkStrategy: (*C.NetworkStrategy)(options.NetworkStrategy), FallbackDelay: time.Duration(options.FallbackDelay), + TCPPrefix: options.TCPPrefix, UDPDisableDomainUnmapping: options.UDPDisableDomainUnmapping, UDPConnect: options.UDPConnect, UDPTimeout: time.Duration(options.UDPTimeout), @@ -63,6 +64,9 @@ func NewRuleAction(logger logger.ContextLogger, action option.RuleAction) (adapt } return &routeOptions, nil case C.RuleActionTypeBypass: + if action.BypassOptions.Outbound == "" && len(action.BypassOptions.TCPPrefix) > 0 { + return nil, E.New("tcp_prefix requires an outbound for bypass") + } routeOptions, err := newRuleActionRouteOptions(action.BypassOptions.RawRouteOptionsActionOptions) if err != nil { return nil, err @@ -205,6 +209,7 @@ type RuleActionRouteOptions struct { NetworkType []C.InterfaceType FallbackNetworkType []C.InterfaceType FallbackDelay time.Duration + TCPPrefix []byte UDPDisableDomainUnmapping bool UDPConnect bool UDPTimeout time.Duration @@ -243,6 +248,9 @@ func (r *RuleActionRouteOptions) Descriptions() []string { if r.FallbackDelay > 0 { descriptions = append(descriptions, F.ToString("fallback-delay=", r.FallbackDelay.String())) } + if len(r.TCPPrefix) > 0 { + descriptions = append(descriptions, "tcp-prefix") + } if r.UDPDisableDomainUnmapping { descriptions = append(descriptions, "udp-disable-domain-unmapping") }