2022-07-02 14:07:50 +08:00
|
|
|
package option
|
2022-06-30 21:27:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type _Inbound struct {
|
2022-07-03 01:57:04 +08:00
|
|
|
Tag string `json:"tag,omitempty"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
DirectOptions DirectInboundOptions `json:"-"`
|
|
|
|
SocksOptions SimpleInboundOptions `json:"-"`
|
|
|
|
HTTPOptions SimpleInboundOptions `json:"-"`
|
|
|
|
MixedOptions SimpleInboundOptions `json:"-"`
|
|
|
|
ShadowsocksOptions ShadowsocksInboundOptions `json:"-"`
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type Inbound _Inbound
|
|
|
|
|
2022-07-03 01:57:04 +08:00
|
|
|
func (i Inbound) Equals(other Inbound) bool {
|
|
|
|
return i.Type == other.Type &&
|
|
|
|
i.Tag == other.Tag &&
|
|
|
|
common.Equals(i.DirectOptions, other.DirectOptions) &&
|
|
|
|
common.Equals(i.SocksOptions, other.SocksOptions) &&
|
|
|
|
common.Equals(i.HTTPOptions, other.HTTPOptions) &&
|
|
|
|
common.Equals(i.MixedOptions, other.MixedOptions) &&
|
|
|
|
common.Equals(i.ShadowsocksOptions, other.ShadowsocksOptions)
|
|
|
|
}
|
|
|
|
|
2022-06-30 21:27:56 +08:00
|
|
|
func (i *Inbound) MarshalJSON() ([]byte, error) {
|
2022-07-03 01:57:04 +08:00
|
|
|
var v any
|
2022-06-30 21:27:56 +08:00
|
|
|
switch i.Type {
|
|
|
|
case "direct":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = i.DirectOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "socks":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = i.SocksOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "http":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = i.HTTPOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "mixed":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = i.MixedOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "shadowsocks":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = i.ShadowsocksOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
default:
|
2022-07-03 01:57:04 +08:00
|
|
|
return nil, E.New("unknown inbound type: ", i.Type)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-03 01:57:04 +08:00
|
|
|
return MarshallObjects(i, v)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Inbound) UnmarshalJSON(bytes []byte) error {
|
|
|
|
err := json.Unmarshal(bytes, (*_Inbound)(i))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-03 01:57:04 +08:00
|
|
|
var v any
|
2022-06-30 21:27:56 +08:00
|
|
|
switch i.Type {
|
|
|
|
case "direct":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = &i.DirectOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "socks":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = &i.SocksOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "http":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = &i.HTTPOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "mixed":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = &i.MixedOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
case "shadowsocks":
|
2022-07-03 01:57:04 +08:00
|
|
|
v = &i.ShadowsocksOptions
|
2022-06-30 21:27:56 +08:00
|
|
|
default:
|
2022-07-03 01:57:04 +08:00
|
|
|
return nil
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
2022-07-03 01:57:04 +08:00
|
|
|
return json.Unmarshal(bytes, v)
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ListenOptions struct {
|
|
|
|
Listen ListenAddress `json:"listen"`
|
|
|
|
Port uint16 `json:"listen_port"`
|
2022-07-03 01:57:04 +08:00
|
|
|
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
|
|
|
|
UDPTimeout int64 `json:"udp_timeout,omitempty"`
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type SimpleInboundOptions struct {
|
|
|
|
ListenOptions
|
|
|
|
Users []auth.User `json:"users,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-07-03 01:57:04 +08:00
|
|
|
func (o SimpleInboundOptions) Equals(other SimpleInboundOptions) bool {
|
|
|
|
return o.ListenOptions == other.ListenOptions &&
|
|
|
|
common.ComparableSliceEquals(o.Users, other.Users)
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o DirectInboundOptions) Equals(other DirectInboundOptions) bool {
|
|
|
|
return o.ListenOptions == other.ListenOptions &&
|
|
|
|
common.ComparableSliceEquals(o.Network, other.Network) &&
|
|
|
|
o.OverrideAddress == other.OverrideAddress &&
|
|
|
|
o.OverridePort == other.OverridePort
|
2022-06-30 21:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ShadowsocksInboundOptions struct {
|
|
|
|
ListenOptions
|
|
|
|
Network NetworkList `json:"network,omitempty"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
2022-07-03 01:57:04 +08:00
|
|
|
|
|
|
|
func (o ShadowsocksInboundOptions) Equals(other ShadowsocksInboundOptions) bool {
|
|
|
|
return o.ListenOptions == other.ListenOptions &&
|
|
|
|
common.ComparableSliceEquals(o.Network, other.Network) &&
|
|
|
|
o.Method == other.Method &&
|
|
|
|
o.Password == other.Password
|
|
|
|
}
|