sing-box/option/inbound.go

107 lines
3.3 KiB
Go
Raw Normal View History

2022-07-02 14:07:50 +08:00
package option
2022-06-30 21:27:56 +08:00
import (
2024-11-02 00:39:02 +08:00
"context"
"time"
2022-06-30 21:27:56 +08:00
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/json"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing/common/json/badjson"
2024-11-18 18:55:34 +08:00
"github.com/sagernet/sing/common/json/badoption"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing/service"
2022-06-30 21:27:56 +08:00
)
2024-11-02 00:39:02 +08:00
type InboundOptionsRegistry interface {
CreateOptions(outboundType string) (any, bool)
}
2022-06-30 21:27:56 +08:00
type _Inbound struct {
2024-11-02 00:39:02 +08:00
Type string `json:"type"`
Tag string `json:"tag,omitempty"`
Options any `json:"-"`
2022-06-30 21:27:56 +08:00
}
type Inbound _Inbound
2024-11-02 00:39:02 +08:00
func (h *Inbound) MarshalJSONContext(ctx context.Context) ([]byte, error) {
return badjson.MarshallObjectsContext(ctx, (*_Inbound)(h), h.Options)
}
2024-11-02 00:39:02 +08:00
func (h *Inbound) UnmarshalJSONContext(ctx context.Context, content []byte) error {
2024-11-21 18:10:41 +08:00
err := json.UnmarshalContext(ctx, content, (*_Inbound)(h))
2022-06-30 21:27:56 +08:00
if err != nil {
return err
}
2024-11-02 00:39:02 +08:00
registry := service.FromContext[InboundOptionsRegistry](ctx)
if registry == nil {
2024-11-06 19:02:55 +08:00
return E.New("missing Inbound fields registry in context")
2022-06-30 21:27:56 +08:00
}
2024-11-02 00:39:02 +08:00
options, loaded := registry.CreateOptions(h.Type)
if !loaded {
return E.New("unknown inbound type: ", h.Type)
}
err = badjson.UnmarshallExcludedContext(ctx, content, (*_Inbound)(h), options)
if err != nil {
return err
}
2024-11-02 00:39:02 +08:00
h.Options = options
return nil
2022-06-30 21:27:56 +08:00
}
2024-10-21 23:38:34 +08:00
// Deprecated: Use rule action instead
type InboundOptions struct {
2024-11-18 18:55:34 +08:00
SniffEnabled bool `json:"sniff,omitempty"`
SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
SniffTimeout badoption.Duration `json:"sniff_timeout,omitempty"`
DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
UDPDisableDomainUnmapping bool `json:"udp_disable_domain_unmapping,omitempty"`
Detour string `json:"detour,omitempty"`
2022-06-30 21:27:56 +08:00
}
type ListenOptions struct {
2024-11-18 18:55:34 +08:00
Listen *badoption.Addr `json:"listen,omitempty"`
ListenPort uint16 `json:"listen_port,omitempty"`
TCPKeepAlive badoption.Duration `json:"tcp_keep_alive,omitempty"`
TCPKeepAliveInterval badoption.Duration `json:"tcp_keep_alive_interval,omitempty"`
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
TCPMultiPath bool `json:"tcp_multi_path,omitempty"`
UDPFragment *bool `json:"udp_fragment,omitempty"`
UDPFragmentDefault bool `json:"-"`
UDPTimeout UDPTimeoutCompat `json:"udp_timeout,omitempty"`
2024-11-02 00:39:02 +08:00
// Deprecated: removed
ProxyProtocol bool `json:"proxy_protocol,omitempty"`
// Deprecated: removed
ProxyProtocolAcceptNoHeader bool `json:"proxy_protocol_accept_no_header,omitempty"`
InboundOptions
}
2024-11-18 18:55:34 +08:00
type UDPTimeoutCompat badoption.Duration
2023-12-26 10:51:39 +08:00
func (c UDPTimeoutCompat) MarshalJSON() ([]byte, error) {
return json.Marshal((time.Duration)(c).String())
}
func (c *UDPTimeoutCompat) UnmarshalJSON(data []byte) error {
var valueNumber int64
err := json.Unmarshal(data, &valueNumber)
if err == nil {
2023-12-26 10:51:39 +08:00
*c = UDPTimeoutCompat(time.Second * time.Duration(valueNumber))
return nil
}
2024-11-18 18:55:34 +08:00
return json.Unmarshal(data, (*badoption.Duration)(c))
}
type ListenOptionsWrapper interface {
TakeListenOptions() ListenOptions
ReplaceListenOptions(options ListenOptions)
}
func (o *ListenOptions) TakeListenOptions() ListenOptions {
return *o
}
func (o *ListenOptions) ReplaceListenOptions(options ListenOptions) {
*o = options
}