sing-box/option/inbound.go

213 lines
5.9 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 (
2022-07-08 23:03:57 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-03 01:57:04 +08:00
"github.com/sagernet/sing/common"
2022-06-30 21:27:56 +08:00
"github.com/sagernet/sing/common/auth"
E "github.com/sagernet/sing/common/exceptions"
2022-07-06 15:01:09 +08:00
"github.com/goccy/go-json"
2022-06-30 21:27:56 +08:00
)
type _Inbound struct {
2022-07-03 01:57:04 +08:00
Type string `json:"type"`
Tag string `json:"tag,omitempty"`
2022-07-18 12:32:31 +08:00
TunOptions TunInboundOptions `json:"-"`
RedirectOptions RedirectInboundOptions `json:"-"`
TProxyOptions TProxyInboundOptions `json:"-"`
2022-07-03 01:57:04 +08:00
DirectOptions DirectInboundOptions `json:"-"`
SocksOptions SocksInboundOptions `json:"-"`
HTTPOptions HTTPMixedInboundOptions `json:"-"`
MixedOptions HTTPMixedInboundOptions `json:"-"`
2022-07-03 01:57:04 +08:00
ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
2022-07-18 12:32:31 +08:00
VMessOptions VMessInboundOptions `json:"-"`
2022-06-30 21:27:56 +08:00
}
type Inbound _Inbound
2022-07-03 03:42:57 +08:00
func (h Inbound) Equals(other Inbound) bool {
return h.Type == other.Type &&
h.Tag == other.Tag &&
2022-07-18 12:32:31 +08:00
h.TunOptions == other.TunOptions &&
h.RedirectOptions == other.RedirectOptions &&
h.TProxyOptions == other.TProxyOptions &&
h.DirectOptions == other.DirectOptions &&
h.SocksOptions.Equals(other.SocksOptions) &&
h.HTTPOptions.Equals(other.HTTPOptions) &&
h.MixedOptions.Equals(other.MixedOptions) &&
2022-07-09 19:18:37 +08:00
h.ShadowsocksOptions.Equals(other.ShadowsocksOptions) &&
2022-07-18 12:32:31 +08:00
h.VMessOptions.Equals(other.VMessOptions)
2022-07-03 01:57:04 +08:00
}
2022-07-03 03:42:57 +08:00
func (h Inbound) MarshalJSON() ([]byte, error) {
2022-07-03 01:57:04 +08:00
var v any
2022-07-03 03:42:57 +08:00
switch h.Type {
2022-07-18 12:32:31 +08:00
case C.TypeTun:
v = h.TunOptions
case C.TypeRedirect:
v = h.RedirectOptions
case C.TypeTProxy:
v = h.TProxyOptions
case C.TypeDirect:
2022-07-03 03:42:57 +08:00
v = h.DirectOptions
case C.TypeSocks:
2022-07-03 03:42:57 +08:00
v = h.SocksOptions
case C.TypeHTTP:
2022-07-03 03:42:57 +08:00
v = h.HTTPOptions
case C.TypeMixed:
2022-07-03 03:42:57 +08:00
v = h.MixedOptions
case C.TypeShadowsocks:
2022-07-03 03:42:57 +08:00
v = h.ShadowsocksOptions
2022-07-18 12:32:31 +08:00
case C.TypeVMess:
v = h.VMessOptions
2022-06-30 21:27:56 +08:00
default:
2022-07-03 03:42:57 +08:00
return nil, E.New("unknown inbound type: ", h.Type)
2022-06-30 21:27:56 +08:00
}
2022-07-03 03:42:57 +08:00
return MarshallObjects((_Inbound)(h), v)
2022-06-30 21:27:56 +08:00
}
2022-07-03 03:42:57 +08:00
func (h *Inbound) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Inbound)(h))
2022-06-30 21:27:56 +08:00
if err != nil {
return err
}
2022-07-03 01:57:04 +08:00
var v any
2022-07-03 03:42:57 +08:00
switch h.Type {
2022-07-18 12:32:31 +08:00
case C.TypeTun:
v = &h.TunOptions
case C.TypeRedirect:
v = &h.RedirectOptions
case C.TypeTProxy:
v = &h.TProxyOptions
case C.TypeDirect:
2022-07-03 03:42:57 +08:00
v = &h.DirectOptions
case C.TypeSocks:
2022-07-03 03:42:57 +08:00
v = &h.SocksOptions
case C.TypeHTTP:
2022-07-03 03:42:57 +08:00
v = &h.HTTPOptions
case C.TypeMixed:
2022-07-03 03:42:57 +08:00
v = &h.MixedOptions
case C.TypeShadowsocks:
2022-07-03 03:42:57 +08:00
v = &h.ShadowsocksOptions
2022-07-18 12:32:31 +08:00
case C.TypeVMess:
v = &h.VMessOptions
2022-06-30 21:27:56 +08:00
default:
2022-07-18 12:32:31 +08:00
return E.New("unknown inbound type: ", h.Type)
2022-06-30 21:27:56 +08:00
}
err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
if err != nil {
return E.Cause(err, "inbound options")
}
return nil
2022-06-30 21:27:56 +08:00
}
type InboundOptions struct {
2022-07-07 23:36:32 +08:00
SniffEnabled bool `json:"sniff,omitempty"`
SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
DomainStrategy DomainStrategy `json:"domain_strategy,omitempty"`
2022-06-30 21:27:56 +08:00
}
type ListenOptions struct {
Listen ListenAddress `json:"listen"`
ListenPort uint16 `json:"listen_port"`
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
UDPTimeout int64 `json:"udp_timeout,omitempty"`
InboundOptions
}
type SocksInboundOptions struct {
2022-06-30 21:27:56 +08:00
ListenOptions
Users []auth.User `json:"users,omitempty"`
}
func (o SocksInboundOptions) Equals(other SocksInboundOptions) bool {
2022-07-03 01:57:04 +08:00
return o.ListenOptions == other.ListenOptions &&
common.ComparableSliceEquals(o.Users, other.Users)
}
type HTTPMixedInboundOptions struct {
ListenOptions
Users []auth.User `json:"users,omitempty"`
SetSystemProxy bool `json:"set_system_proxy,omitempty"`
}
func (o HTTPMixedInboundOptions) Equals(other HTTPMixedInboundOptions) bool {
return o.ListenOptions == other.ListenOptions &&
common.ComparableSliceEquals(o.Users, other.Users) &&
o.SetSystemProxy == other.SetSystemProxy
}
2022-06-30 21:27:56 +08:00
type DirectInboundOptions struct {
ListenOptions
Network NetworkList `json:"network,omitempty"`
2022-07-03 01:57:04 +08:00
OverrideAddress string `json:"override_address,omitempty"`
OverridePort uint16 `json:"override_port,omitempty"`
}
2022-06-30 21:27:56 +08:00
type ShadowsocksInboundOptions struct {
ListenOptions
2022-07-04 15:34:43 +08:00
Network NetworkList `json:"network,omitempty"`
Method string `json:"method"`
Password string `json:"password"`
Users []ShadowsocksUser `json:"users,omitempty"`
Destinations []ShadowsocksDestination `json:"destinations,omitempty"`
}
func (o ShadowsocksInboundOptions) Equals(other ShadowsocksInboundOptions) bool {
return o.ListenOptions == other.ListenOptions &&
o.Network == other.Network &&
o.Method == other.Method &&
o.Password == other.Password &&
common.ComparableSliceEquals(o.Users, other.Users) &&
common.ComparableSliceEquals(o.Destinations, other.Destinations)
}
type ShadowsocksUser struct {
Name string `json:"name"`
Password string `json:"password"`
}
type ShadowsocksDestination struct {
Name string `json:"name"`
Password string `json:"password"`
ServerOptions
2022-06-30 21:27:56 +08:00
}
2022-07-09 19:18:37 +08:00
2022-07-18 12:32:31 +08:00
type VMessInboundOptions struct {
ListenOptions
Users []VMessUser `json:"users,omitempty"`
}
func (o VMessInboundOptions) Equals(other VMessInboundOptions) bool {
return o.ListenOptions == other.ListenOptions &&
common.ComparableSliceEquals(o.Users, other.Users)
}
type VMessUser struct {
Name string `json:"name"`
UUID string `json:"uuid"`
}
2022-07-09 19:18:37 +08:00
type TunInboundOptions struct {
2022-07-10 14:22:28 +08:00
InterfaceName string `json:"interface_name,omitempty"`
MTU uint32 `json:"mtu,omitempty"`
2022-07-10 14:22:28 +08:00
Inet4Address *ListenPrefix `json:"inet4_address,omitempty"`
Inet6Address *ListenPrefix `json:"inet6_address,omitempty"`
AutoRoute bool `json:"auto_route,omitempty"`
InboundOptions
2022-07-09 19:18:37 +08:00
}
2022-07-15 08:42:02 +08:00
type RedirectInboundOptions struct {
ListenOptions
}
type TProxyInboundOptions struct {
ListenOptions
Network NetworkList `json:"network,omitempty"`
}
type DNSInboundOptions struct {
ListenOptions
Network NetworkList `json:"network,omitempty"`
}