Inbound rule support

This commit is contained in:
世界
2022-07-02 14:07:50 +08:00
parent 9f4c0ff624
commit 7c57eb70e8
34 changed files with 622 additions and 247 deletions

27
option/address.go Normal file
View File

@@ -0,0 +1,27 @@
package option
import (
"encoding/json"
"net/netip"
)
type ListenAddress netip.Addr
func (a *ListenAddress) MarshalJSON() ([]byte, error) {
value := netip.Addr(*a).String()
return json.Marshal(value)
}
func (a *ListenAddress) UnmarshalJSON(bytes []byte) error {
var value string
err := json.Unmarshal(bytes, &value)
if err != nil {
return err
}
addr, err := netip.ParseAddr(value)
if err != nil {
return err
}
*a = ListenAddress(addr)
return nil
}

14
option/config.go Normal file
View File

@@ -0,0 +1,14 @@
package option
type Options struct {
Log *LogOption `json:"log"`
Inbounds []Inbound `json:"inbounds,omitempty"`
Outbounds []Outbound `json:"outbounds,omitempty"`
Routes []Rule `json:"routes,omitempty"`
}
type LogOption struct {
Disabled bool `json:"disabled,omitempty"`
Level string `json:"level,omitempty"`
Output string `json:"output,omitempty"`
}

115
option/inbound.go Normal file
View File

@@ -0,0 +1,115 @@
package option
import (
"encoding/json"
"github.com/sagernet/sing/common/auth"
E "github.com/sagernet/sing/common/exceptions"
)
var ErrUnknownInboundType = E.New("unknown inbound type")
type _Inbound struct {
Tag string `json:"tag,omitempty"`
Type string `json:"type,omitempty"`
DirectOptions *DirectInboundOptions `json:"directOptions,omitempty"`
SocksOptions *SimpleInboundOptions `json:"socksOptions,omitempty"`
HTTPOptions *SimpleInboundOptions `json:"httpOptions,omitempty"`
MixedOptions *SimpleInboundOptions `json:"mixedOptions,omitempty"`
ShadowsocksOptions *ShadowsocksInboundOptions `json:"shadowsocksOptions,omitempty"`
}
type Inbound _Inbound
func (i *Inbound) MarshalJSON() ([]byte, error) {
var options []byte
var err error
switch i.Type {
case "direct":
options, err = json.Marshal(i.DirectOptions)
case "socks":
options, err = json.Marshal(i.SocksOptions)
case "http":
options, err = json.Marshal(i.HTTPOptions)
case "mixed":
options, err = json.Marshal(i.MixedOptions)
case "shadowsocks":
options, err = json.Marshal(i.ShadowsocksOptions)
default:
return nil, E.Extend(ErrUnknownInboundType, i.Type)
}
if err != nil {
return nil, err
}
var content map[string]any
err = json.Unmarshal(options, &content)
if err != nil {
return nil, err
}
content["tag"] = i.Tag
content["type"] = i.Type
return json.Marshal(content)
}
func (i *Inbound) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Inbound)(i))
if err != nil {
return err
}
switch i.Type {
case "direct":
if i.DirectOptions != nil {
break
}
err = json.Unmarshal(bytes, &i.DirectOptions)
case "socks":
if i.SocksOptions != nil {
break
}
err = json.Unmarshal(bytes, &i.SocksOptions)
case "http":
if i.HTTPOptions != nil {
break
}
err = json.Unmarshal(bytes, &i.HTTPOptions)
case "mixed":
if i.MixedOptions != nil {
break
}
err = json.Unmarshal(bytes, &i.MixedOptions)
case "shadowsocks":
if i.ShadowsocksOptions != nil {
break
}
err = json.Unmarshal(bytes, &i.ShadowsocksOptions)
default:
return E.Extend(ErrUnknownInboundType, i.Type)
}
return err
}
type ListenOptions struct {
Listen ListenAddress `json:"listen"`
Port uint16 `json:"listen_port"`
TCPFastOpen bool `json:"tcpFastOpen,omitempty"`
UDPTimeout int64 `json:"udpTimeout,omitempty"`
}
type SimpleInboundOptions struct {
ListenOptions
Users []auth.User `json:"users,omitempty"`
}
type DirectInboundOptions struct {
ListenOptions
Network NetworkList `json:"network,omitempty"`
OverrideAddress string `json:"overrideAddress,omitempty"`
OverridePort uint16 `json:"overridePort,omitempty"`
}
type ShadowsocksInboundOptions struct {
ListenOptions
Network NetworkList `json:"network,omitempty"`
Method string `json:"method"`
Password string `json:"password"`
}

35
option/network.go Normal file
View File

@@ -0,0 +1,35 @@
package option
import (
"encoding/json"
"strings"
C "github.com/sagernet/sing-box/constant"
E "github.com/sagernet/sing/common/exceptions"
)
type NetworkList []string
func (v *NetworkList) UnmarshalJSON(data []byte) error {
var networkList string
err := json.Unmarshal(data, &networkList)
if err != nil {
return err
}
for _, networkName := range strings.Split(networkList, ",") {
switch networkName {
case "tcp", "udp":
*v = append(*v, networkName)
default:
return E.New("unknown network: " + networkName)
}
}
return nil
}
func (v *NetworkList) Build() []string {
if len(*v) == 0 {
return []string{C.NetworkTCP, C.NetworkUDP}
}
return *v
}

99
option/outbound.go Normal file
View File

@@ -0,0 +1,99 @@
package option
import (
"encoding/json"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
)
var ErrUnknownOutboundType = E.New("unknown outbound type")
type _Outbound struct {
Tag string `json:"tag,omitempty"`
Type string `json:"type,omitempty"`
DirectOptions *DirectOutboundOptions `json:"directOptions,omitempty"`
ShadowsocksOptions *ShadowsocksOutboundOptions `json:"shadowsocksOptions,omitempty"`
}
type Outbound _Outbound
func (i *Outbound) MarshalJSON() ([]byte, error) {
var options []byte
var err error
switch i.Type {
case "direct":
options, err = json.Marshal(i.DirectOptions)
case "shadowsocks":
options, err = json.Marshal(i.ShadowsocksOptions)
default:
return nil, E.Extend(ErrUnknownOutboundType, i.Type)
}
if err != nil {
return nil, err
}
var content map[string]any
err = json.Unmarshal(options, &content)
if err != nil {
return nil, err
}
content["tag"] = i.Tag
content["type"] = i.Type
return json.Marshal(content)
}
func (i *Outbound) UnmarshalJSON(bytes []byte) error {
if err := json.Unmarshal(bytes, (*_Outbound)(i)); err != nil {
return err
}
switch i.Type {
case "direct":
if i.DirectOptions != nil {
break
}
if err := json.Unmarshal(bytes, &i.DirectOptions); err != nil {
return err
}
case "shadowsocks":
if i.ShadowsocksOptions != nil {
break
}
if err := json.Unmarshal(bytes, &i.ShadowsocksOptions); err != nil {
return err
}
default:
return E.Extend(ErrUnknownOutboundType, i.Type)
}
return nil
}
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"`
}
type ServerOptions struct {
Server string `json:"server"`
ServerPort uint16 `json:"server_port"`
}
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"`
}

80
option/route.go Normal file
View File

@@ -0,0 +1,80 @@
package option
import (
"encoding/json"
C "github.com/sagernet/sing-box/constant"
E "github.com/sagernet/sing/common/exceptions"
)
var ErrUnknownRuleType = E.New("unknown rule type")
type _Rule struct {
Type string `json:"type"`
DefaultOptions DefaultRule `json:"default_options,omitempty"`
LogicalOptions LogicalRule `json:"logical_options,omitempty"`
}
type Rule _Rule
func (r *Rule) MarshalJSON() ([]byte, error) {
var content map[string]any
switch r.Type {
case "", C.RuleTypeDefault:
return json.Marshal(r.DefaultOptions)
case C.RuleTypeLogical:
options, err := json.Marshal(r.LogicalOptions)
if err != nil {
return nil, err
}
err = json.Unmarshal(options, &content)
if err != nil {
return nil, err
}
content["type"] = r.Type
return json.Marshal(content)
default:
return nil, E.Extend(ErrUnknownRuleType, r.Type)
}
}
func (r *Rule) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_Rule)(r))
if err != nil {
return err
}
switch r.Type {
case "", C.RuleTypeDefault:
err = json.Unmarshal(bytes, &r.DefaultOptions)
case C.RuleTypeLogical:
err = json.Unmarshal(bytes, &r.LogicalOptions)
default:
err = E.Extend(ErrUnknownRuleType, r.Type)
}
return err
}
type DefaultRule struct {
Inbound []string `json:"inbound,omitempty"`
IPVersion []int `json:"ip_version,omitempty"`
Network []string `json:"network,omitempty"`
Protocol []string `json:"protocol,omitempty"`
Domain []string `json:"domain,omitempty"`
DomainSuffix []string `json:"domain_suffix,omitempty"`
DomainKeyword []string `json:"domain_keyword,omitempty"`
SourceGeoIP []string `json:"source_geoip,omitempty"`
GeoIP []string `json:"geoip,omitempty"`
SourceIPCIDR []string `json:"source_ipcidr,omitempty"`
SourcePort []string `json:"source_port,omitempty"`
IPCIDR []string `json:"destination_ipcidr,omitempty"`
Port []string `json:"destination_port,omitempty"`
ProcessName []string `json:"process_name,omitempty"`
ProcessPath []string `json:"process_path,omitempty"`
Outbound string `json:"outbound,omitempty"`
}
type LogicalRule struct {
Mode string `json:"mode"`
Rules []DefaultRule `json:"rules,omitempty"`
Outbound string `json:"outbound,omitempty"`
}