2022-07-02 06:07:50 +00:00
|
|
|
package option
|
2022-06-30 13:27:56 +00:00
|
|
|
|
|
|
|
import (
|
2022-07-03 03:28:15 +00:00
|
|
|
"github.com/goccy/go-json"
|
2022-07-03 15:23:18 +00:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
2022-07-02 17:57:04 +00:00
|
|
|
"github.com/sagernet/sing/common"
|
2022-06-30 13:27:56 +00:00
|
|
|
"github.com/sagernet/sing/common/auth"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type _Inbound struct {
|
2022-07-02 17:57:04 +00:00
|
|
|
Type string `json:"type"`
|
2022-07-03 15:23:18 +00:00
|
|
|
Tag string `json:"tag,omitempty"`
|
2022-07-02 17:57:04 +00:00
|
|
|
DirectOptions DirectInboundOptions `json:"-"`
|
|
|
|
SocksOptions SimpleInboundOptions `json:"-"`
|
|
|
|
HTTPOptions SimpleInboundOptions `json:"-"`
|
|
|
|
MixedOptions SimpleInboundOptions `json:"-"`
|
|
|
|
ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Inbound _Inbound
|
|
|
|
|
2022-07-02 19:42:57 +00:00
|
|
|
func (h Inbound) Equals(other Inbound) bool {
|
|
|
|
return h.Type == other.Type &&
|
|
|
|
h.Tag == other.Tag &&
|
2022-07-03 15:23:18 +00:00
|
|
|
h.DirectOptions == other.DirectOptions &&
|
|
|
|
h.SocksOptions.Equals(other.SocksOptions) &&
|
|
|
|
h.HTTPOptions.Equals(other.HTTPOptions) &&
|
|
|
|
h.MixedOptions.Equals(other.MixedOptions) &&
|
2022-07-04 07:34:43 +00:00
|
|
|
h.ShadowsocksOptions.Equals(other.ShadowsocksOptions)
|
2022-07-02 17:57:04 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 19:42:57 +00:00
|
|
|
func (h Inbound) MarshalJSON() ([]byte, error) {
|
2022-07-02 17:57:04 +00:00
|
|
|
var v any
|
2022-07-02 19:42:57 +00:00
|
|
|
switch h.Type {
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeDirect:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = h.DirectOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeSocks:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = h.SocksOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeHTTP:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = h.HTTPOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeMixed:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = h.MixedOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeShadowsocks:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = h.ShadowsocksOptions
|
2022-06-30 13:27:56 +00:00
|
|
|
default:
|
2022-07-02 19:42:57 +00:00
|
|
|
return nil, E.New("unknown inbound type: ", h.Type)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 19:42:57 +00:00
|
|
|
return MarshallObjects((_Inbound)(h), v)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-02 19:42:57 +00:00
|
|
|
func (h *Inbound) UnmarshalJSON(bytes []byte) error {
|
|
|
|
err := json.Unmarshal(bytes, (*_Inbound)(h))
|
2022-06-30 13:27:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-02 17:57:04 +00:00
|
|
|
var v any
|
2022-07-02 19:42:57 +00:00
|
|
|
switch h.Type {
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeDirect:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = &h.DirectOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeSocks:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = &h.SocksOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeHTTP:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = &h.HTTPOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeMixed:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = &h.MixedOptions
|
2022-07-03 15:23:18 +00:00
|
|
|
case C.TypeShadowsocks:
|
2022-07-02 19:42:57 +00:00
|
|
|
v = &h.ShadowsocksOptions
|
2022-06-30 13:27:56 +00:00
|
|
|
default:
|
2022-07-02 17:57:04 +00:00
|
|
|
return nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-03 03:28:15 +00:00
|
|
|
err = UnmarshallExcluded(bytes, (*_Inbound)(h), v)
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "inbound options")
|
|
|
|
}
|
|
|
|
return nil
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListenOptions struct {
|
2022-07-06 04:39:44 +00:00
|
|
|
Listen ListenAddress `json:"listen"`
|
|
|
|
Port uint16 `json:"listen_port"`
|
|
|
|
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
|
|
|
|
UDPTimeout int64 `json:"udp_timeout,omitempty"`
|
|
|
|
SniffEnabled bool `json:"sniff,omitempty"`
|
|
|
|
SniffOverrideDestination bool `json:"sniff_override_destination,omitempty"`
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SimpleInboundOptions struct {
|
|
|
|
ListenOptions
|
|
|
|
Users []auth.User `json:"users,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-07-02 17:57:04 +00:00
|
|
|
func (o SimpleInboundOptions) Equals(other SimpleInboundOptions) bool {
|
|
|
|
return o.ListenOptions == other.ListenOptions &&
|
|
|
|
common.ComparableSliceEquals(o.Users, other.Users)
|
|
|
|
}
|
|
|
|
|
2022-06-30 13:27:56 +00:00
|
|
|
type DirectInboundOptions struct {
|
|
|
|
ListenOptions
|
|
|
|
Network NetworkList `json:"network,omitempty"`
|
2022-07-02 17:57:04 +00:00
|
|
|
OverrideAddress string `json:"override_address,omitempty"`
|
|
|
|
OverridePort uint16 `json:"override_port,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-06-30 13:27:56 +00:00
|
|
|
type ShadowsocksInboundOptions struct {
|
|
|
|
ListenOptions
|
2022-07-04 07:34:43 +00: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 13:27:56 +00:00
|
|
|
}
|