Merge branch 'dev' of https://github.com/Dreamacro/clash into Alpha
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
package proxy
|
||||
package listener
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Dreamacro/clash/adapter/inbound"
|
||||
@ -24,6 +25,8 @@ import (
|
||||
"github.com/Dreamacro/clash/listener/tuic"
|
||||
"github.com/Dreamacro/clash/listener/tunnel"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -39,11 +42,11 @@ var (
|
||||
tproxyUDPListener *tproxy.UDPListener
|
||||
mixedListener *mixed.Listener
|
||||
mixedUDPLister *socks.UDPListener
|
||||
tunnelTCPListeners = map[string]*tunnel.Listener{}
|
||||
tunnelUDPListeners = map[string]*tunnel.PacketConn{}
|
||||
tunLister *sing_tun.Listener
|
||||
shadowSocksListener C.AdvanceListener
|
||||
vmessListener *sing_vmess.Listener
|
||||
tcpTunListener *tunnel.Listener
|
||||
udpTunListener *tunnel.UdpListener
|
||||
tuicListener *tuic.Listener
|
||||
autoRedirListener *autoredir.Listener
|
||||
autoRedirProgram *ebpf.TcEBpfProgram
|
||||
@ -55,11 +58,10 @@ var (
|
||||
redirMux sync.Mutex
|
||||
tproxyMux sync.Mutex
|
||||
mixedMux sync.Mutex
|
||||
tunnelMux sync.Mutex
|
||||
tunMux sync.Mutex
|
||||
ssMux sync.Mutex
|
||||
vmessMux sync.Mutex
|
||||
tcpTunMux sync.Mutex
|
||||
udpTunMux sync.Mutex
|
||||
tuicMux sync.Mutex
|
||||
autoRedirMux sync.Mutex
|
||||
tcMux sync.Mutex
|
||||
@ -76,8 +78,6 @@ type Ports struct {
|
||||
MixedPort int `json:"mixed-port"`
|
||||
ShadowSocksConfig string `json:"ss-config"`
|
||||
VmessConfig string `json:"vmess-config"`
|
||||
TcpTunConfig string `json:"tcptun-config"`
|
||||
UdpTunConfig string `json:"udptun-config"`
|
||||
}
|
||||
|
||||
func GetTunConf() config.Tun {
|
||||
@ -331,76 +331,6 @@ func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<-
|
||||
return
|
||||
}
|
||||
|
||||
func ReCreateTcpTun(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
||||
tcpTunMux.Lock()
|
||||
defer tcpTunMux.Unlock()
|
||||
shouldIgnore := false
|
||||
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
log.Errorln("Start TcpTun server error: %s", err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
if tcpTunListener != nil {
|
||||
if tcpTunListener.Config() != config {
|
||||
tcpTunListener.Close()
|
||||
tcpTunListener = nil
|
||||
} else {
|
||||
shouldIgnore = true
|
||||
}
|
||||
}
|
||||
|
||||
if shouldIgnore {
|
||||
return
|
||||
}
|
||||
|
||||
tcpListener, err := tunnel.New(config, tcpIn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tcpTunListener = tcpListener
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReCreateUdpTun(config string, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
||||
udpTunMux.Lock()
|
||||
defer udpTunMux.Unlock()
|
||||
shouldIgnore := false
|
||||
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
log.Errorln("Start UdpTun server error: %s", err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
if udpTunListener != nil {
|
||||
if udpTunListener.Config() != config {
|
||||
udpTunListener.Close()
|
||||
udpTunListener = nil
|
||||
} else {
|
||||
shouldIgnore = true
|
||||
}
|
||||
}
|
||||
|
||||
if shouldIgnore {
|
||||
return
|
||||
}
|
||||
|
||||
udpListener, err := tunnel.NewUdp(config, udpIn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
udpTunListener = udpListener
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReCreateTuic(config config.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
||||
tuicMux.Lock()
|
||||
defer func() {
|
||||
@ -664,6 +594,95 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<-
|
||||
log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs())
|
||||
}
|
||||
|
||||
func PatchTunnel(tunnels []config.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
||||
tunnelMux.Lock()
|
||||
defer tunnelMux.Unlock()
|
||||
|
||||
type addrProxy struct {
|
||||
network string
|
||||
addr string
|
||||
target string
|
||||
proxy string
|
||||
}
|
||||
|
||||
tcpOld := lo.Map(
|
||||
lo.Keys(tunnelTCPListeners),
|
||||
func(key string, _ int) addrProxy {
|
||||
parts := strings.Split(key, "/")
|
||||
return addrProxy{
|
||||
network: "tcp",
|
||||
addr: parts[0],
|
||||
target: parts[1],
|
||||
proxy: parts[2],
|
||||
}
|
||||
},
|
||||
)
|
||||
udpOld := lo.Map(
|
||||
lo.Keys(tunnelUDPListeners),
|
||||
func(key string, _ int) addrProxy {
|
||||
parts := strings.Split(key, "/")
|
||||
return addrProxy{
|
||||
network: "udp",
|
||||
addr: parts[0],
|
||||
target: parts[1],
|
||||
proxy: parts[2],
|
||||
}
|
||||
},
|
||||
)
|
||||
oldElm := lo.Union(tcpOld, udpOld)
|
||||
|
||||
newElm := lo.FlatMap(
|
||||
tunnels,
|
||||
func(tunnel config.Tunnel, _ int) []addrProxy {
|
||||
return lo.Map(
|
||||
tunnel.Network,
|
||||
func(network string, _ int) addrProxy {
|
||||
return addrProxy{
|
||||
network: network,
|
||||
addr: tunnel.Address,
|
||||
target: tunnel.Target,
|
||||
proxy: tunnel.Proxy,
|
||||
}
|
||||
},
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
needClose, needCreate := lo.Difference(oldElm, newElm)
|
||||
|
||||
for _, elm := range needClose {
|
||||
key := fmt.Sprintf("%s/%s/%s", elm.addr, elm.target, elm.proxy)
|
||||
if elm.network == "tcp" {
|
||||
tunnelTCPListeners[key].Close()
|
||||
delete(tunnelTCPListeners, key)
|
||||
} else {
|
||||
tunnelUDPListeners[key].Close()
|
||||
delete(tunnelUDPListeners, key)
|
||||
}
|
||||
}
|
||||
|
||||
for _, elm := range needCreate {
|
||||
key := fmt.Sprintf("%s/%s/%s", elm.addr, elm.target, elm.proxy)
|
||||
if elm.network == "tcp" {
|
||||
l, err := tunnel.New(elm.addr, elm.target, elm.proxy, tcpIn)
|
||||
if err != nil {
|
||||
log.Errorln("Start tunnel %s error: %s", elm.target, err.Error())
|
||||
continue
|
||||
}
|
||||
tunnelTCPListeners[key] = l
|
||||
log.Infoln("Tunnel(tcp/%s) proxy %s listening at: %s", elm.target, elm.proxy, tunnelTCPListeners[key].Address())
|
||||
} else {
|
||||
l, err := tunnel.NewUDP(elm.addr, elm.target, elm.proxy, udpIn)
|
||||
if err != nil {
|
||||
log.Errorln("Start tunnel %s error: %s", elm.target, err.Error())
|
||||
continue
|
||||
}
|
||||
tunnelUDPListeners[key] = l
|
||||
log.Infoln("Tunnel(udp/%s) proxy %s listening at: %s", elm.target, elm.proxy, tunnelUDPListeners[key].Address())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GetPorts return the ports of proxy servers
|
||||
func GetPorts() *Ports {
|
||||
ports := &Ports{}
|
||||
@ -706,14 +725,6 @@ func GetPorts() *Ports {
|
||||
ports.VmessConfig = vmessListener.Config()
|
||||
}
|
||||
|
||||
if tcpTunListener != nil {
|
||||
ports.TcpTunConfig = tcpTunListener.Config()
|
||||
}
|
||||
|
||||
if udpTunListener != nil {
|
||||
ports.UdpTunConfig = udpTunListener.Config()
|
||||
}
|
||||
|
||||
return ports
|
||||
}
|
||||
|
||||
|
35
listener/tunnel/packet.go
Normal file
35
listener/tunnel/packet.go
Normal file
@ -0,0 +1,35 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
)
|
||||
|
||||
type packet struct {
|
||||
pc net.PacketConn
|
||||
rAddr net.Addr
|
||||
payload []byte
|
||||
}
|
||||
|
||||
func (c *packet) Data() []byte {
|
||||
return c.payload
|
||||
}
|
||||
|
||||
// WriteBack write UDP packet with source(ip, port) = `addr`
|
||||
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
||||
return c.pc.WriteTo(b, c.rAddr)
|
||||
}
|
||||
|
||||
// LocalAddr returns the source IP/Port of UDP Packet
|
||||
func (c *packet) LocalAddr() net.Addr {
|
||||
return c.rAddr
|
||||
}
|
||||
|
||||
func (c *packet) Drop() {
|
||||
pool.Put(c.payload)
|
||||
}
|
||||
|
||||
func (c *packet) InAddr() net.Addr {
|
||||
return c.pc.LocalAddr()
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/adapter/inbound"
|
||||
@ -10,59 +11,68 @@ import (
|
||||
)
|
||||
|
||||
type Listener struct {
|
||||
closed bool
|
||||
config string
|
||||
listeners []net.Listener
|
||||
listener net.Listener
|
||||
addr string
|
||||
target socks5.Addr
|
||||
proxy string
|
||||
closed bool
|
||||
}
|
||||
|
||||
func New(config string, in chan<- C.ConnContext) (*Listener, error) {
|
||||
tl := &Listener{false, config, nil}
|
||||
pl := PairList{}
|
||||
err := pl.Set(config)
|
||||
// RawAddress implements C.Listener
|
||||
func (l *Listener) RawAddress() string {
|
||||
return l.addr
|
||||
}
|
||||
|
||||
// Address implements C.Listener
|
||||
func (l *Listener) Address() string {
|
||||
return l.listener.Addr().String()
|
||||
}
|
||||
|
||||
// Close implements C.Listener
|
||||
func (l *Listener) Close() error {
|
||||
l.closed = true
|
||||
return l.listener.Close()
|
||||
}
|
||||
|
||||
func (l *Listener) handleTCP(conn net.Conn, in chan<- C.ConnContext) {
|
||||
conn.(*net.TCPConn).SetKeepAlive(true)
|
||||
ctx := inbound.NewSocket(l.target, conn, C.TUNNEL)
|
||||
ctx.Metadata().SpecialProxy = l.proxy
|
||||
in <- ctx
|
||||
}
|
||||
|
||||
func New(addr, target, proxy string, in chan<- C.ConnContext) (*Listener, error) {
|
||||
l, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, p := range pl {
|
||||
addr := p[0]
|
||||
target := p[1]
|
||||
go func() {
|
||||
tgt := socks5.ParseAddr(target)
|
||||
if tgt == nil {
|
||||
log.Errorln("invalid target address %q", target)
|
||||
return
|
||||
}
|
||||
l, err := inbound.Listen("tcp", addr)
|
||||
targetAddr := socks5.ParseAddr(target)
|
||||
if targetAddr == nil {
|
||||
return nil, fmt.Errorf("invalid target address %s", target)
|
||||
}
|
||||
|
||||
log.Infoln("TCP tunnel %s <-> %s", l.Addr().String(), target)
|
||||
|
||||
rl := &Listener{
|
||||
listener: l,
|
||||
target: targetAddr,
|
||||
proxy: proxy,
|
||||
addr: addr,
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tl.listeners = append(tl.listeners, l)
|
||||
log.Infoln("TCP tunnel %s <-> %s", l.Addr().String(), target)
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
if tl.closed {
|
||||
break
|
||||
}
|
||||
continue
|
||||
if rl.closed {
|
||||
break
|
||||
}
|
||||
_ = c.(*net.TCPConn).SetKeepAlive(true)
|
||||
|
||||
in <- inbound.NewSocket(tgt, c, C.TCPTUN)
|
||||
continue
|
||||
}
|
||||
}()
|
||||
}
|
||||
go rl.handleTCP(c, in)
|
||||
}
|
||||
}()
|
||||
|
||||
return tl, nil
|
||||
}
|
||||
|
||||
func (l *Listener) Close() {
|
||||
l.closed = true
|
||||
for _, lis := range l.listeners {
|
||||
_ = lis.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Listener) Config() string {
|
||||
return l.config
|
||||
return rl, nil
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/Dreamacro/clash/adapter/inbound"
|
||||
@ -10,70 +11,78 @@ import (
|
||||
"github.com/Dreamacro/clash/transport/socks5"
|
||||
)
|
||||
|
||||
type UdpListener struct {
|
||||
closed bool
|
||||
config string
|
||||
listeners []net.PacketConn
|
||||
type PacketConn struct {
|
||||
conn net.PacketConn
|
||||
addr string
|
||||
target socks5.Addr
|
||||
proxy string
|
||||
closed bool
|
||||
}
|
||||
|
||||
func NewUdp(config string, in chan<- *inbound.PacketAdapter) (*UdpListener, error) {
|
||||
ul := &UdpListener{false, config, nil}
|
||||
pl := PairList{}
|
||||
err := pl.Set(config)
|
||||
// RawAddress implements C.Listener
|
||||
func (l *PacketConn) RawAddress() string {
|
||||
return l.addr
|
||||
}
|
||||
|
||||
// Address implements C.Listener
|
||||
func (l *PacketConn) Address() string {
|
||||
return l.conn.LocalAddr().String()
|
||||
}
|
||||
|
||||
// Close implements C.Listener
|
||||
func (l *PacketConn) Close() error {
|
||||
l.closed = true
|
||||
return l.conn.Close()
|
||||
}
|
||||
|
||||
func NewUDP(addr, target, proxy string, in chan<- *inbound.PacketAdapter) (*PacketConn, error) {
|
||||
l, err := net.ListenPacket("udp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, p := range pl {
|
||||
addr := p[0]
|
||||
target := p[1]
|
||||
go func() {
|
||||
tgt := socks5.ParseAddr(target)
|
||||
if tgt == nil {
|
||||
log.Errorln("invalid target address %q", target)
|
||||
return
|
||||
}
|
||||
l, err := net.ListenPacket("udp", addr)
|
||||
targetAddr := socks5.ParseAddr(target)
|
||||
if targetAddr == nil {
|
||||
return nil, fmt.Errorf("invalid target address %s", target)
|
||||
}
|
||||
|
||||
log.Infoln("Udp tunnel %s <-> %s", l.LocalAddr().String(), target)
|
||||
|
||||
sl := &PacketConn{
|
||||
conn: l,
|
||||
target: targetAddr,
|
||||
proxy: proxy,
|
||||
addr: addr,
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
buf := pool.Get(pool.UDPBufferSize)
|
||||
n, remoteAddr, err := l.ReadFrom(buf)
|
||||
if err != nil {
|
||||
return
|
||||
pool.Put(buf)
|
||||
if sl.closed {
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
ul.listeners = append(ul.listeners, l)
|
||||
log.Infoln("Udp tunnel %s <-> %s", l.LocalAddr().String(), target)
|
||||
for {
|
||||
buf := pool.Get(pool.RelayBufferSize)
|
||||
n, remoteAddr, err := l.ReadFrom(buf)
|
||||
if err != nil {
|
||||
pool.Put(buf)
|
||||
if ul.closed {
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
packet := &packet{
|
||||
pc: l,
|
||||
rAddr: remoteAddr,
|
||||
payload: buf[:n],
|
||||
bufRef: buf,
|
||||
}
|
||||
select {
|
||||
case in <- inbound.NewPacket(tgt, packet, C.UDPTUN):
|
||||
default:
|
||||
}
|
||||
sl.handleUDP(l, in, buf[:n], remoteAddr)
|
||||
}
|
||||
}()
|
||||
|
||||
}
|
||||
}()
|
||||
return sl, nil
|
||||
}
|
||||
|
||||
func (l *PacketConn) handleUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, addr net.Addr) {
|
||||
packet := &packet{
|
||||
pc: pc,
|
||||
rAddr: addr,
|
||||
payload: buf,
|
||||
}
|
||||
|
||||
return ul, nil
|
||||
}
|
||||
|
||||
func (l *UdpListener) Close() {
|
||||
l.closed = true
|
||||
for _, lis := range l.listeners {
|
||||
_ = lis.Close()
|
||||
ctx := inbound.NewPacket(l.target, packet, C.TUNNEL)
|
||||
ctx.Metadata().SpecialProxy = l.proxy
|
||||
select {
|
||||
case in <- ctx:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UdpListener) Config() string {
|
||||
return l.config
|
||||
}
|
||||
|
@ -1,63 +0,0 @@
|
||||
package tunnel
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/Dreamacro/clash/common/pool"
|
||||
)
|
||||
|
||||
type PairList [][2]string // key1=val1,key2=val2,...
|
||||
|
||||
func (l PairList) String() string {
|
||||
s := make([]string, len(l))
|
||||
for i, pair := range l {
|
||||
s[i] = pair[0] + "=" + pair[1]
|
||||
}
|
||||
return strings.Join(s, ",")
|
||||
}
|
||||
func (l *PairList) Set(s string) error {
|
||||
for _, item := range strings.Split(s, ",") {
|
||||
pair := strings.Split(item, "=")
|
||||
if len(pair) != 2 {
|
||||
return nil
|
||||
}
|
||||
*l = append(*l, [2]string{pair[0], pair[1]})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type packet struct {
|
||||
pc net.PacketConn
|
||||
rAddr net.Addr
|
||||
payload []byte
|
||||
bufRef []byte
|
||||
}
|
||||
|
||||
func (c *packet) Data() []byte {
|
||||
return c.payload
|
||||
}
|
||||
|
||||
// WriteBack wirtes UDP packet with source(ip, port) = `addr`
|
||||
func (c *packet) WriteBack(b []byte, addr net.Addr) (n int, err error) {
|
||||
if addr == nil {
|
||||
err = errors.New("address is invalid")
|
||||
return
|
||||
}
|
||||
packet := b
|
||||
return c.pc.WriteTo(packet, c.rAddr)
|
||||
}
|
||||
|
||||
// LocalAddr returns the source IP/Port of UDP Packet
|
||||
func (c *packet) LocalAddr() net.Addr {
|
||||
return c.rAddr
|
||||
}
|
||||
|
||||
func (c *packet) Drop() {
|
||||
pool.Put(c.bufRef)
|
||||
}
|
||||
|
||||
func (c *packet) InAddr() net.Addr {
|
||||
return c.pc.LocalAddr()
|
||||
}
|
Reference in New Issue
Block a user