chore: listeners support tun
This commit is contained in:
@ -74,7 +74,7 @@ var _ C.InboundListener = (*Base)(nil)
|
||||
type BaseOption struct {
|
||||
NameStr string `inbound:"name"`
|
||||
Listen string `inbound:"listen,omitempty"`
|
||||
Port int `inbound:"port"`
|
||||
Port int `inbound:"port,omitempty"`
|
||||
SpecialRules string `inbound:"rule,omitempty"`
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@ type ShadowSocks struct {
|
||||
*Base
|
||||
config *ShadowSocksOption
|
||||
l C.MultiAddrListener
|
||||
ss LC.ShadowsocksServer
|
||||
}
|
||||
|
||||
func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
|
||||
@ -31,8 +32,13 @@ func NewShadowSocks(options *ShadowSocksOption) (*ShadowSocks, error) {
|
||||
return &ShadowSocks{
|
||||
Base: base,
|
||||
config: options,
|
||||
ss: LC.ShadowsocksServer{
|
||||
Enable: true,
|
||||
Listen: base.RawAddress(),
|
||||
Password: options.Password,
|
||||
Cipher: options.Cipher,
|
||||
},
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// Config implements constant.InboundListener
|
||||
@ -53,17 +59,7 @@ func (s *ShadowSocks) Address() string {
|
||||
// Listen implements constant.InboundListener
|
||||
func (s *ShadowSocks) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
||||
var err error
|
||||
s.l, err = sing_shadowsocks.New(
|
||||
LC.ShadowsocksServer{
|
||||
Enable: true,
|
||||
Listen: s.RawAddress(),
|
||||
Password: s.config.Password,
|
||||
Cipher: s.config.Cipher,
|
||||
},
|
||||
tcpIn,
|
||||
udpIn,
|
||||
s.Additions()...,
|
||||
)
|
||||
s.l, err = sing_shadowsocks.New(s.ss, tcpIn, udpIn, s.Additions()...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ type Tuic struct {
|
||||
*Base
|
||||
config *TuicOption
|
||||
l *tuic.Listener
|
||||
ts LC.TuicServer
|
||||
}
|
||||
|
||||
func NewTuic(options *TuicOption) (*Tuic, error) {
|
||||
@ -37,8 +38,19 @@ func NewTuic(options *TuicOption) (*Tuic, error) {
|
||||
return &Tuic{
|
||||
Base: base,
|
||||
config: options,
|
||||
ts: LC.TuicServer{
|
||||
Enable: true,
|
||||
Listen: base.RawAddress(),
|
||||
Token: options.Token,
|
||||
Certificate: options.Certificate,
|
||||
PrivateKey: options.PrivateKey,
|
||||
CongestionController: options.CongestionController,
|
||||
MaxIdleTime: options.MaxIdleTime,
|
||||
AuthenticationTimeout: options.AuthenticationTimeout,
|
||||
ALPN: options.ALPN,
|
||||
MaxUdpRelayPacketSize: options.MaxUdpRelayPacketSize,
|
||||
},
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// Config implements constant.InboundListener
|
||||
@ -59,23 +71,7 @@ func (t *Tuic) Address() string {
|
||||
// Listen implements constant.InboundListener
|
||||
func (t *Tuic) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
||||
var err error
|
||||
t.l, err = tuic.New(
|
||||
LC.TuicServer{
|
||||
Enable: true,
|
||||
Listen: t.RawAddress(),
|
||||
Token: t.config.Token,
|
||||
Certificate: t.config.Certificate,
|
||||
PrivateKey: t.config.PrivateKey,
|
||||
CongestionController: t.config.CongestionController,
|
||||
MaxIdleTime: t.config.MaxIdleTime,
|
||||
AuthenticationTimeout: t.config.AuthenticationTimeout,
|
||||
ALPN: t.config.ALPN,
|
||||
MaxUdpRelayPacketSize: t.config.MaxUdpRelayPacketSize,
|
||||
},
|
||||
tcpIn,
|
||||
udpIn,
|
||||
t.Additions()...,
|
||||
)
|
||||
t.l, err = tuic.New(t.ts, tcpIn, udpIn, t.Additions()...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
139
listener/inbound/tun.go
Normal file
139
listener/inbound/tun.go
Normal file
@ -0,0 +1,139 @@
|
||||
package inbound
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/netip"
|
||||
"strings"
|
||||
|
||||
C "github.com/Dreamacro/clash/constant"
|
||||
LC "github.com/Dreamacro/clash/listener/config"
|
||||
"github.com/Dreamacro/clash/listener/sing_tun"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
)
|
||||
|
||||
type TunOption struct {
|
||||
BaseOption
|
||||
Device string `inbound:"device,omitempty"`
|
||||
Stack string `inbound:"stack,omitempty"`
|
||||
DNSHijack []string `inbound:"dns-hijack,omitempty"`
|
||||
AutoRoute bool `inbound:"auto-route,omitempty"`
|
||||
AutoDetectInterface bool `inbound:"auto-detect-interface,omitempty"`
|
||||
|
||||
MTU uint32 `inbound:"mtu,omitempty"`
|
||||
Inet4Address []string `inbound:"inet4_address,omitempty"`
|
||||
Inet6Address []string `inbound:"inet6_address,omitempty"`
|
||||
StrictRoute bool `inbound:"strict_route,omitempty"`
|
||||
Inet4RouteAddress []string `inbound:"inet4_route_address,omitempty"`
|
||||
Inet6RouteAddress []string `inbound:"inet6_route_address,omitempty"`
|
||||
IncludeUID []uint32 `inbound:"include_uid,omitempty"`
|
||||
IncludeUIDRange []string `inbound:"include_uid_range,omitempty"`
|
||||
ExcludeUID []uint32 `inbound:"exclude_uid,omitempty"`
|
||||
ExcludeUIDRange []string `inbound:"exclude_uid_range,omitempty"`
|
||||
IncludeAndroidUser []int `inbound:"include_android_user,omitempty"`
|
||||
IncludePackage []string `inbound:"include_package,omitempty"`
|
||||
ExcludePackage []string `inbound:"exclude_package,omitempty"`
|
||||
EndpointIndependentNat bool `inbound:"endpoint_independent_nat,omitempty"`
|
||||
UDPTimeout int64 `inbound:"udp_timeout,omitempty"`
|
||||
}
|
||||
|
||||
func (o TunOption) Equal(config C.InboundConfig) bool {
|
||||
return optionToString(o) == optionToString(config)
|
||||
}
|
||||
|
||||
type Tun struct {
|
||||
*Base
|
||||
config *TunOption
|
||||
l *sing_tun.Listener
|
||||
tun LC.Tun
|
||||
}
|
||||
|
||||
func NewTun(options *TunOption) (*Tun, error) {
|
||||
base, err := NewBase(&options.BaseOption)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
stack, exist := C.StackTypeMapping[strings.ToLower(options.Stack)]
|
||||
if !exist {
|
||||
return nil, errors.New("invalid tun stack")
|
||||
}
|
||||
dnsHijack := make([]netip.AddrPort, 0, len(options.DNSHijack))
|
||||
for _, str := range options.DNSHijack {
|
||||
var a netip.AddrPort
|
||||
err = a.UnmarshalText([]byte(str))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dnsHijack = append(dnsHijack, a)
|
||||
}
|
||||
inet4Address, err := LC.StringSliceToListenPrefixSlice(options.Inet4Address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inet6Address, err := LC.StringSliceToListenPrefixSlice(options.Inet6Address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inet4RouteAddress, err := LC.StringSliceToListenPrefixSlice(options.Inet4RouteAddress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inet6RouteAddress, err := LC.StringSliceToListenPrefixSlice(options.Inet6RouteAddress)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Tun{
|
||||
Base: base,
|
||||
config: options,
|
||||
tun: LC.Tun{
|
||||
Enable: true,
|
||||
Device: options.Device,
|
||||
Stack: stack,
|
||||
DNSHijack: dnsHijack,
|
||||
AutoRoute: options.AutoRoute,
|
||||
AutoDetectInterface: options.AutoDetectInterface,
|
||||
MTU: options.MTU,
|
||||
Inet4Address: inet4Address,
|
||||
Inet6Address: inet6Address,
|
||||
StrictRoute: options.StrictRoute,
|
||||
Inet4RouteAddress: inet4RouteAddress,
|
||||
Inet6RouteAddress: inet6RouteAddress,
|
||||
IncludeUID: options.IncludeUID,
|
||||
IncludeUIDRange: options.IncludeUIDRange,
|
||||
ExcludeUID: options.ExcludeUID,
|
||||
ExcludeUIDRange: options.ExcludeUIDRange,
|
||||
IncludeAndroidUser: options.IncludeAndroidUser,
|
||||
IncludePackage: options.IncludePackage,
|
||||
ExcludePackage: options.ExcludePackage,
|
||||
EndpointIndependentNat: options.EndpointIndependentNat,
|
||||
UDPTimeout: options.UDPTimeout,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Config implements constant.InboundListener
|
||||
func (t *Tun) Config() C.InboundConfig {
|
||||
return t.config
|
||||
}
|
||||
|
||||
// Address implements constant.InboundListener
|
||||
func (t *Tun) Address() string {
|
||||
return t.l.Address()
|
||||
}
|
||||
|
||||
// Listen implements constant.InboundListener
|
||||
func (t *Tun) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) error {
|
||||
var err error
|
||||
t.l, err = sing_tun.New(t.tun, tcpIn, udpIn, t.Additions()...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Infoln("Tun[%s] proxy listening at: %s", t.Name(), t.Address())
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close implements constant.InboundListener
|
||||
func (t *Tun) Close() error {
|
||||
return t.l.Close()
|
||||
}
|
||||
|
||||
var _ C.InboundListener = (*Tun)(nil)
|
@ -55,7 +55,7 @@ func (t *Tunnel) Close() error {
|
||||
if err == nil {
|
||||
err = udpErr
|
||||
} else {
|
||||
return fmt.Errorf("close tcp err: %t, close udp err: %t", err.Error(), udpErr.Error())
|
||||
return fmt.Errorf("close tcp err: %s, close udp err: %s", err.Error(), udpErr.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -82,7 +82,8 @@ func (t *Tunnel) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter
|
||||
return err
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unknow network type: %s", network)
|
||||
log.Warnln("unknown network type: %s, passed", network)
|
||||
continue
|
||||
}
|
||||
log.Infoln("Tunnel[%s](%s/%s)proxy listening at: %s", t.Name(), network, t.config.Target, t.Address())
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ type Vmess struct {
|
||||
*Base
|
||||
config *VmessOption
|
||||
l C.MultiAddrListener
|
||||
vs LC.VmessServer
|
||||
}
|
||||
|
||||
func NewVmess(options *VmessOption) (*Vmess, error) {
|
||||
@ -33,11 +34,23 @@ func NewVmess(options *VmessOption) (*Vmess, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users := make([]LC.VmessUser, len(options.Users))
|
||||
for i, v := range options.Users {
|
||||
users[i] = LC.VmessUser{
|
||||
Username: v.Username,
|
||||
UUID: v.UUID,
|
||||
AlterID: v.AlterID,
|
||||
}
|
||||
}
|
||||
return &Vmess{
|
||||
Base: base,
|
||||
config: options,
|
||||
vs: LC.VmessServer{
|
||||
Enable: true,
|
||||
Listen: base.RawAddress(),
|
||||
Users: users,
|
||||
},
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// Config implements constant.InboundListener
|
||||
@ -66,16 +79,7 @@ func (v *Vmess) Listen(tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter)
|
||||
AlterID: v.AlterID,
|
||||
}
|
||||
}
|
||||
v.l, err = sing_vmess.New(
|
||||
LC.VmessServer{
|
||||
Enable: true,
|
||||
Listen: v.RawAddress(),
|
||||
Users: users,
|
||||
},
|
||||
tcpIn,
|
||||
udpIn,
|
||||
v.Additions()...,
|
||||
)
|
||||
v.l, err = sing_vmess.New(v.vs, tcpIn, udpIn, v.Additions()...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Reference in New Issue
Block a user