sing-box/option/outbound.go

79 lines
1.9 KiB
Go
Raw Normal View History

2022-07-02 06:07:50 +00:00
package option
2022-06-30 13:27:56 +00:00
import (
"encoding/json"
E "github.com/sagernet/sing/common/exceptions"
2022-07-02 06:07:50 +00:00
M "github.com/sagernet/sing/common/metadata"
2022-06-30 13:27:56 +00:00
)
type _Outbound struct {
2022-07-02 17:57:04 +00:00
Tag string `json:"tag,omitempty"`
Type string `json:"type,omitempty"`
DirectOptions DirectOutboundOptions `json:"-"`
ShadowsocksOptions ShadowsocksOutboundOptions `json:"-"`
2022-06-30 13:27:56 +00:00
}
type Outbound _Outbound
2022-07-02 19:42:57 +00:00
func (h Outbound) 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-06-30 13:27:56 +00:00
case "direct":
2022-07-02 19:42:57 +00:00
v = h.DirectOptions
2022-06-30 13:27:56 +00:00
case "shadowsocks":
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 outbound type: ", h.Type)
2022-06-30 13:27:56 +00:00
}
2022-07-02 19:42:57 +00:00
return MarshallObjects((_Outbound)(h), v)
2022-06-30 13:27:56 +00:00
}
2022-07-02 19:42:57 +00:00
func (h *Outbound) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Outbound)(h))
2022-07-02 17:57:04 +00:00
if err != nil {
2022-06-30 13:27:56 +00:00
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-06-30 13:27:56 +00:00
case "direct":
2022-07-02 19:42:57 +00:00
v = &h.DirectOptions
2022-06-30 13:27:56 +00:00
case "shadowsocks":
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-02 17:57:04 +00:00
return json.Unmarshal(bytes, v)
2022-06-30 13:27:56 +00:00
}
type DialerOptions struct {
Detour string `json:"detour,omitempty"`
BindInterface string `json:"bind_interface,omitempty"`
RoutingMark int `json:"routing_mark,omitempty"`
ReuseAddr bool `json:"reuse_addr,omitempty"`
ConnectTimeout int `json:"connect_timeout,omitempty"`
TCPFastOpen bool `json:"tcp_fast_open,omitempty"`
}
type DirectOutboundOptions struct {
DialerOptions
OverrideAddress string `json:"override_address,omitempty"`
OverridePort uint16 `json:"override_port,omitempty"`
}
2022-07-02 06:07:50 +00:00
type ServerOptions struct {
2022-06-30 13:27:56 +00:00
Server string `json:"server"`
ServerPort uint16 `json:"server_port"`
2022-07-02 06:07:50 +00:00
}
func (o ServerOptions) Build() M.Socksaddr {
return M.ParseSocksaddrHostPort(o.Server, o.ServerPort)
}
type ShadowsocksOutboundOptions struct {
DialerOptions
ServerOptions
Method string `json:"method"`
Password string `json:"password"`
2022-06-30 13:27:56 +00:00
}