Feature: add inbounds for flexible binding inbound (#2818)

This commit is contained in:
fuyun
2023-08-03 22:30:08 +08:00
committed by GitHub
parent 10f4d5375a
commit 9e78137768
25 changed files with 552 additions and 361 deletions

View File

@ -1,7 +1,92 @@
package constant
import (
"fmt"
"net"
"net/url"
"strconv"
)
type Listener interface {
RawAddress() string
Address() string
Close() error
}
type InboundType string
const (
InboundTypeSocks InboundType = "socks"
InboundTypeRedir InboundType = "redir"
InboundTypeTproxy InboundType = "tproxy"
InboundTypeHTTP InboundType = "http"
InboundTypeMixed InboundType = "mixed"
)
var supportInboundTypes = map[InboundType]bool{
InboundTypeSocks: true,
InboundTypeRedir: true,
InboundTypeTproxy: true,
InboundTypeHTTP: true,
InboundTypeMixed: true,
}
type inbound struct {
Type InboundType `json:"type" yaml:"type"`
BindAddress string `json:"bind-address" yaml:"bind-address"`
IsFromPortCfg bool `json:"-" yaml:"-"`
}
// Inbound
type Inbound inbound
// UnmarshalYAML implements yaml.Unmarshaler
func (i *Inbound) UnmarshalYAML(unmarshal func(any) error) error {
var tp string
if err := unmarshal(&tp); err != nil {
var inner inbound
if err := unmarshal(&inner); err != nil {
return err
}
*i = Inbound(inner)
return nil
}
inner, err := parseInbound(tp)
if err != nil {
return err
}
*i = Inbound(*inner)
if !supportInboundTypes[i.Type] {
return fmt.Errorf("not support inbound type: %s", i.Type)
}
_, portStr, err := net.SplitHostPort(i.BindAddress)
if err != nil {
return fmt.Errorf("bind address parse error. addr:%s, err:%v", i.BindAddress, err)
}
port, err := strconv.Atoi(portStr)
if err != nil {
return fmt.Errorf("port not a number. addr:%s", i.BindAddress)
}
if port == 0 {
return fmt.Errorf("invalid bind port. addr:%s", i.BindAddress)
}
return nil
}
func parseInbound(alias string) (*inbound, error) {
u, err := url.Parse(alias)
if err != nil {
return nil, err
}
listenerType := InboundType(u.Scheme)
return &inbound{
Type: listenerType,
BindAddress: u.Host,
}, nil
}
func (i *Inbound) ToAlias() string {
return string(i.Type) + "://" + i.BindAddress
}

View File

@ -69,6 +69,7 @@ type Metadata struct {
DstIP net.IP `json:"destinationIP"`
SrcPort string `json:"sourcePort"`
DstPort string `json:"destinationPort"`
InboundPort string `json:"inboundPort"`
Host string `json:"host"`
DNSMode DNSMode `json:"dnsMode"`
ProcessPath string `json:"processPath"`

View File

@ -10,6 +10,7 @@ const (
SrcIPCIDR
SrcPort
DstPort
InboundPort
Process
ProcessPath
IPSet
@ -36,6 +37,8 @@ func (rt RuleType) String() string {
return "SrcPort"
case DstPort:
return "DstPort"
case InboundPort:
return "InboundPort"
case Process:
return "Process"
case ProcessPath: