Merge from remote branch

This commit is contained in:
yaling888
2022-02-23 01:00:27 +08:00
79 changed files with 1071 additions and 542 deletions

View File

@ -3,6 +3,7 @@ package proxy
import (
"fmt"
"net"
"os"
"runtime"
"strconv"
"sync"
@ -80,38 +81,50 @@ func SetBindAddress(host string) {
bindAddress = host
}
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) error {
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
httpMux.Lock()
defer httpMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start HTTP server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
if httpListener != nil {
if httpListener.RawAddress() == addr {
return nil
return
}
httpListener.Close()
httpListener = nil
}
if portIsZero(addr) {
return nil
return
}
var err error
httpListener, err = http.New(addr, tcpIn)
if err != nil {
return err
return
}
log.Infoln("HTTP proxy listening at: %s", httpListener.Address())
return nil
}
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
socksMux.Lock()
defer socksMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start SOCKS server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
shouldTCPIgnore := false
@ -136,40 +149,46 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
}
if shouldTCPIgnore && shouldUDPIgnore {
return nil
return
}
if portIsZero(addr) {
return nil
return
}
tcpListener, err := socks.New(addr, tcpIn)
if err != nil {
return err
return
}
udpListener, err := socks.NewUDP(addr, udpIn)
if err != nil {
tcpListener.Close()
return err
return
}
socksListener = tcpListener
socksUDPListener = udpListener
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
return nil
}
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
redirMux.Lock()
defer redirMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start Redir server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
if redirListener != nil {
if redirListener.RawAddress() == addr {
return nil
return
}
redirListener.Close()
redirListener = nil
@ -177,20 +196,19 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
if redirUDPListener != nil {
if redirUDPListener.RawAddress() == addr {
return nil
return
}
redirUDPListener.Close()
redirUDPListener = nil
}
if portIsZero(addr) {
return nil
return
}
var err error
redirListener, err = redir.New(addr, tcpIn)
if err != nil {
return err
return
}
redirUDPListener, err = tproxy.NewUDP(addr, udpIn)
@ -199,18 +217,24 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
}
log.Infoln("Redirect proxy listening at: %s", redirListener.Address())
return nil
}
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
tproxyMux.Lock()
defer tproxyMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start TProxy server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
if tproxyListener != nil {
if tproxyListener.RawAddress() == addr {
return nil
return
}
tproxyListener.Close()
tproxyListener = nil
@ -218,20 +242,19 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
if tproxyUDPListener != nil {
if tproxyUDPListener.RawAddress() == addr {
return nil
return
}
tproxyUDPListener.Close()
tproxyUDPListener = nil
}
if portIsZero(addr) {
return nil
return
}
var err error
tproxyListener, err = tproxy.New(addr, tcpIn)
if err != nil {
return err
return
}
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn)
@ -240,13 +263,19 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
}
log.Infoln("TProxy server listening at: %s", tproxyListener.Address())
return nil
}
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
mixedMux.Lock()
defer mixedMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start Mixed(http+socks) server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
shouldTCPIgnore := false
@ -270,46 +299,52 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
}
if shouldTCPIgnore && shouldUDPIgnore {
return nil
return
}
if portIsZero(addr) {
return nil
return
}
var err error
mixedListener, err = mixed.New(addr, tcpIn)
if err != nil {
return err
return
}
mixedUDPLister, err = socks.NewUDP(addr, udpIn)
if err != nil {
mixedListener.Close()
return err
return
}
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
return nil
}
func ReCreateTun(conf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateTun(conf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
tunMux.Lock()
defer tunMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start TUN interface error: %s", err.Error())
os.Exit(2)
}
}()
if tunAdapter != nil {
tunAdapter.Close()
tunAdapter = nil
}
if !conf.Enable {
return nil
return
}
var err error
tunAdapter, err = tun.New(conf, tcpIn, udpIn)
return err
if err != nil {
log.Warnln("Failed to start TUN interface: %s", err.Error())
}
}
// GetPorts return the ports of proxy servers

View File

@ -49,7 +49,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
}
go func() {
for {
buf := pool.Get(pool.RelayBufferSize)
buf := pool.Get(pool.UDPBufferSize)
n, remoteAddr, err := l.ReadFrom(buf)
if err != nil {
pool.Put(buf)

View File

@ -57,7 +57,7 @@ func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error)
go func() {
oob := make([]byte, 1024)
for {
buf := pool.Get(pool.RelayBufferSize)
buf := pool.Get(pool.UDPBufferSize)
n, oobn, _, lAddr, err := c.ReadMsgUDP(buf, oob)
if err != nil {
pool.Put(buf)

View File

@ -14,10 +14,10 @@ import (
"syscall"
"unsafe"
"github.com/Dreamacro/clash/common/pool"
"golang.org/x/net/ipv6"
"golang.org/x/sys/unix"
"github.com/Dreamacro/clash/common/pool"
)
const (

View File

@ -2,6 +2,7 @@ package commons
import (
"github.com/Dreamacro/clash/component/resolver"
D "github.com/miekg/dns"
)

View File

@ -17,6 +17,7 @@ import (
"github.com/Dreamacro/clash/listener/tun/ipstack"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/transport/socks5"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
"gvisor.dev/gvisor/pkg/tcpip/buffer"

View File

@ -6,6 +6,7 @@ import (
"github.com/Dreamacro/clash/dns"
"github.com/Dreamacro/clash/log"
D "github.com/miekg/dns"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"

View File

@ -5,6 +5,7 @@ import (
"net"
"github.com/Dreamacro/clash/component/resolver"
"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/buffer"
"gvisor.dev/gvisor/pkg/tcpip/header"

View File

@ -7,6 +7,7 @@ import (
"time"
D "github.com/Dreamacro/clash/listener/tun/ipstack/commons"
"github.com/kr328/tun2socket/binding"
"github.com/kr328/tun2socket/redirect"
)

View File

@ -4,10 +4,10 @@ import (
"net"
"strconv"
"github.com/kr328/tun2socket/binding"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/context"
"github.com/kr328/tun2socket/binding"
)
func handleTCP(conn net.Conn, endpoint *binding.Endpoint, tcpIn chan<- C.ConnContext) {

View File

@ -11,6 +11,7 @@ import (
"github.com/Dreamacro/clash/listener/tun/dev"
"github.com/Dreamacro/clash/listener/tun/ipstack"
"github.com/Dreamacro/clash/log"
"github.com/kr328/tun2socket"
"github.com/kr328/tun2socket/binding"
"github.com/kr328/tun2socket/redirect"

View File

@ -8,6 +8,7 @@ import (
"github.com/Dreamacro/clash/common/pool"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/kr328/tun2socket/binding"
"github.com/kr328/tun2socket/redirect"
)