Style: code style

This commit is contained in:
Dreamacro 2021-06-13 17:23:10 +08:00
parent bcfc15e398
commit 6091fcdfec
27 changed files with 171 additions and 186 deletions

View file

@ -18,9 +18,9 @@ import (
"github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/dns"
P "github.com/Dreamacro/clash/listener"
authStore "github.com/Dreamacro/clash/listener/auth"
"github.com/Dreamacro/clash/log"
P "github.com/Dreamacro/clash/proxy"
authStore "github.com/Dreamacro/clash/proxy/auth"
"github.com/Dreamacro/clash/tunnel"
)
@ -187,23 +187,26 @@ func updateGeneral(general *config.General, force bool) {
bindAddress := general.BindAddress
P.SetBindAddress(bindAddress)
if err := P.ReCreateHTTP(general.Port); err != nil {
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
if err := P.ReCreateHTTP(general.Port, tcpIn); err != nil {
log.Errorln("Start HTTP server error: %s", err.Error())
}
if err := P.ReCreateSocks(general.SocksPort); err != nil {
if err := P.ReCreateSocks(general.SocksPort, tcpIn, udpIn); err != nil {
log.Errorln("Start SOCKS5 server error: %s", err.Error())
}
if err := P.ReCreateRedir(general.RedirPort); err != nil {
if err := P.ReCreateRedir(general.RedirPort, tcpIn, udpIn); err != nil {
log.Errorln("Start Redir server error: %s", err.Error())
}
if err := P.ReCreateTProxy(general.TProxyPort); err != nil {
if err := P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn); err != nil {
log.Errorln("Start TProxy server error: %s", err.Error())
}
if err := P.ReCreateMixed(general.MixedPort); err != nil {
if err := P.ReCreateMixed(general.MixedPort, tcpIn, udpIn); err != nil {
log.Errorln("Start Mixed(http and socks5) server error: %s", err.Error())
}
}

View file

@ -7,8 +7,8 @@ import (
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/config"
"github.com/Dreamacro/clash/hub/executor"
P "github.com/Dreamacro/clash/listener"
"github.com/Dreamacro/clash/log"
P "github.com/Dreamacro/clash/proxy"
"github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi/v5"
@ -66,11 +66,15 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
}
ports := P.GetPorts()
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port))
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort))
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort))
P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort))
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort))
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
P.ReCreateHTTP(pointerOrDefault(general.Port, ports.Port), tcpIn)
P.ReCreateSocks(pointerOrDefault(general.SocksPort, ports.SocksPort), tcpIn, udpIn)
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort), tcpIn, udpIn)
P.ReCreateTProxy(pointerOrDefault(general.TProxyPort, ports.TProxyPort), tcpIn, udpIn)
P.ReCreateMixed(pointerOrDefault(general.MixedPort, ports.MixedPort), tcpIn, udpIn)
if general.Mode != nil {
tunnel.SetMode(*general.Mode)

View file

@ -11,28 +11,25 @@ import (
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/common/cache"
"github.com/Dreamacro/clash/component/auth"
C "github.com/Dreamacro/clash/constant"
authStore "github.com/Dreamacro/clash/listener/auth"
"github.com/Dreamacro/clash/log"
authStore "github.com/Dreamacro/clash/proxy/auth"
"github.com/Dreamacro/clash/tunnel"
)
type HTTPListener struct {
type Listener struct {
net.Listener
address string
closed bool
cache *cache.Cache
}
func NewHTTPProxy(addr string) (*HTTPListener, error) {
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
hl := &HTTPListener{l, addr, false, cache.New(30 * time.Second)}
hl := &Listener{l, addr, false, cache.New(30 * time.Second)}
go func() {
log.Infoln("HTTP proxy listening at: %s", addr)
for {
c, err := hl.Accept()
if err != nil {
@ -41,19 +38,19 @@ func NewHTTPProxy(addr string) (*HTTPListener, error) {
}
continue
}
go HandleConn(c, hl.cache)
go HandleConn(c, in, hl.cache)
}
}()
return hl, nil
}
func (l *HTTPListener) Close() {
func (l *Listener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *HTTPListener) Address() string {
func (l *Listener) Address() string {
return l.address
}
@ -70,7 +67,7 @@ func canActivate(loginStr string, authenticator auth.Authenticator, cache *cache
return
}
func HandleConn(conn net.Conn, cache *cache.Cache) {
func HandleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
br := bufio.NewReader(conn)
keepAlive:
@ -106,9 +103,9 @@ keepAlive:
conn.Close()
return
}
tunnel.Add(inbound.NewHTTPS(request, conn))
in <- inbound.NewHTTPS(request, conn)
return
}
tunnel.Add(inbound.NewHTTP(request, conn))
in <- inbound.NewHTTP(request, conn)
}

View file

@ -6,26 +6,29 @@ import (
"strconv"
"sync"
"github.com/Dreamacro/clash/adapter/inbound"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/http"
"github.com/Dreamacro/clash/listener/mixed"
"github.com/Dreamacro/clash/listener/redir"
"github.com/Dreamacro/clash/listener/socks"
"github.com/Dreamacro/clash/listener/tproxy"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/proxy/http"
"github.com/Dreamacro/clash/proxy/mixed"
"github.com/Dreamacro/clash/proxy/redir"
"github.com/Dreamacro/clash/proxy/socks"
)
var (
allowLan = false
bindAddress = "*"
socksListener *socks.SockListener
socksUDPListener *socks.SockUDPListener
httpListener *http.HTTPListener
redirListener *redir.RedirListener
redirUDPListener *redir.RedirUDPListener
tproxyListener *redir.TProxyListener
tproxyUDPListener *redir.RedirUDPListener
mixedListener *mixed.MixedListener
mixedUDPLister *socks.SockUDPListener
socksListener *socks.Listener
socksUDPListener *socks.UDPListener
httpListener *http.Listener
redirListener *redir.Listener
redirUDPListener *tproxy.UDPListener
tproxyListener *tproxy.Listener
tproxyUDPListener *tproxy.UDPListener
mixedListener *mixed.Listener
mixedUDPLister *socks.UDPListener
// lock for recreate function
socksMux sync.Mutex
@ -59,7 +62,7 @@ func SetBindAddress(host string) {
bindAddress = host
}
func ReCreateHTTP(port int) error {
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) error {
httpMux.Lock()
defer httpMux.Unlock()
@ -78,15 +81,16 @@ func ReCreateHTTP(port int) error {
}
var err error
httpListener, err = http.NewHTTPProxy(addr)
httpListener, err = http.New(addr, tcpIn)
if err != nil {
return err
}
log.Infoln("HTTP proxy listening at: %s", httpListener.Address())
return nil
}
func ReCreateSocks(port int) error {
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
socksMux.Lock()
defer socksMux.Unlock()
@ -121,12 +125,12 @@ func ReCreateSocks(port int) error {
return nil
}
tcpListener, err := socks.NewSocksProxy(addr)
tcpListener, err := socks.New(addr, tcpIn)
if err != nil {
return err
}
udpListener, err := socks.NewSocksUDPProxy(addr)
udpListener, err := socks.NewUDP(addr, udpIn)
if err != nil {
tcpListener.Close()
return err
@ -135,10 +139,11 @@ func ReCreateSocks(port int) error {
socksListener = tcpListener
socksUDPListener = udpListener
log.Infoln("SOCKS5 proxy listening at: %s", socksListener.Address())
return nil
}
func ReCreateRedir(port int) error {
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
redirMux.Lock()
defer redirMux.Unlock()
@ -165,20 +170,21 @@ func ReCreateRedir(port int) error {
}
var err error
redirListener, err = redir.NewRedirProxy(addr)
redirListener, err = redir.New(addr, tcpIn)
if err != nil {
return err
}
redirUDPListener, err = redir.NewRedirUDPProxy(addr)
redirUDPListener, err = tproxy.NewUDP(addr, udpIn)
if err != nil {
log.Warnln("Failed to start Redir UDP Listener: %s", err)
}
log.Infoln("Redirect proxy listening at: %s", redirListener.Address())
return nil
}
func ReCreateTProxy(port int) error {
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
tproxyMux.Lock()
defer tproxyMux.Unlock()
@ -205,20 +211,21 @@ func ReCreateTProxy(port int) error {
}
var err error
tproxyListener, err = redir.NewTProxy(addr)
tproxyListener, err = tproxy.New(addr, tcpIn)
if err != nil {
return err
}
tproxyUDPListener, err = redir.NewRedirUDPProxy(addr)
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn)
if err != nil {
log.Warnln("Failed to start TProxy UDP Listener: %s", err)
}
log.Infoln("TProxy server listening at: %s", tproxyListener.Address())
return nil
}
func ReCreateMixed(port int) error {
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
mixedMux.Lock()
defer mixedMux.Unlock()
@ -253,17 +260,18 @@ func ReCreateMixed(port int) error {
}
var err error
mixedListener, err = mixed.NewMixedProxy(addr)
mixedListener, err = mixed.New(addr, tcpIn)
if err != nil {
return err
}
mixedUDPLister, err = socks.NewSocksUDPProxy(addr)
mixedUDPLister, err = socks.NewUDP(addr, udpIn)
if err != nil {
mixedListener.Close()
return err
}
log.Infoln("Mixed(http+socks5) proxy listening at: %s", mixedListener.Address())
return nil
}

View file

@ -5,29 +5,27 @@ import (
"time"
"github.com/Dreamacro/clash/common/cache"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/proxy/http"
"github.com/Dreamacro/clash/proxy/socks"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/http"
"github.com/Dreamacro/clash/listener/socks"
"github.com/Dreamacro/clash/transport/socks5"
)
type MixedListener struct {
type Listener struct {
net.Listener
address string
closed bool
cache *cache.Cache
}
func NewMixedProxy(addr string) (*MixedListener, error) {
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
ml := &MixedListener{l, addr, false, cache.New(30 * time.Second)}
ml := &Listener{l, addr, false, cache.New(30 * time.Second)}
go func() {
log.Infoln("Mixed(http+socks5) proxy listening at: %s", addr)
for {
c, err := ml.Accept()
if err != nil {
@ -36,23 +34,23 @@ func NewMixedProxy(addr string) (*MixedListener, error) {
}
continue
}
go handleConn(c, ml.cache)
go handleConn(c, in, ml.cache)
}
}()
return ml, nil
}
func (l *MixedListener) Close() {
func (l *Listener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *MixedListener) Address() string {
func (l *Listener) Address() string {
return l.address
}
func handleConn(conn net.Conn, cache *cache.Cache) {
func handleConn(conn net.Conn, in chan<- C.ConnContext, cache *cache.Cache) {
bufConn := NewBufferedConn(conn)
head, err := bufConn.Peek(1)
if err != nil {
@ -60,9 +58,9 @@ func handleConn(conn net.Conn, cache *cache.Cache) {
}
if head[0] == socks5.Version {
socks.HandleSocks(bufConn)
socks.HandleSocks(bufConn, in)
return
}
http.HandleConn(bufConn, cache)
http.HandleConn(bufConn, in, cache)
}

View file

@ -5,25 +5,22 @@ import (
"github.com/Dreamacro/clash/adapter/inbound"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel"
)
type RedirListener struct {
type Listener struct {
net.Listener
address string
closed bool
}
func NewRedirProxy(addr string) (*RedirListener, error) {
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
rl := &RedirListener{l, addr, false}
rl := &Listener{l, addr, false}
go func() {
log.Infoln("Redir proxy listening at: %s", addr)
for {
c, err := l.Accept()
if err != nil {
@ -32,28 +29,28 @@ func NewRedirProxy(addr string) (*RedirListener, error) {
}
continue
}
go handleRedir(c)
go handleRedir(c, in)
}
}()
return rl, nil
}
func (l *RedirListener) Close() {
func (l *Listener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *RedirListener) Address() string {
func (l *Listener) Address() string {
return l.address
}
func handleRedir(conn net.Conn) {
func handleRedir(conn net.Conn, in chan<- C.ConnContext) {
target, err := parserPacket(conn)
if err != nil {
conn.Close()
return
}
conn.(*net.TCPConn).SetKeepAlive(true)
tunnel.Add(inbound.NewSocket(target, conn, C.REDIR))
in <- inbound.NewSocket(target, conn, C.REDIR)
}

View file

@ -7,27 +7,24 @@ import (
"github.com/Dreamacro/clash/adapter/inbound"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
authStore "github.com/Dreamacro/clash/proxy/auth"
authStore "github.com/Dreamacro/clash/listener/auth"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/Dreamacro/clash/tunnel"
)
type SockListener struct {
type Listener struct {
net.Listener
address string
closed bool
}
func NewSocksProxy(addr string) (*SockListener, error) {
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
sl := &SockListener{l, addr, false}
sl := &Listener{l, addr, false}
go func() {
log.Infoln("SOCKS proxy listening at: %s", addr)
for {
c, err := l.Accept()
if err != nil {
@ -36,23 +33,23 @@ func NewSocksProxy(addr string) (*SockListener, error) {
}
continue
}
go HandleSocks(c)
go HandleSocks(c, in)
}
}()
return sl, nil
}
func (l *SockListener) Close() {
func (l *Listener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *SockListener) Address() string {
func (l *Listener) Address() string {
return l.address
}
func HandleSocks(conn net.Conn) {
func HandleSocks(conn net.Conn, in chan<- C.ConnContext) {
target, command, err := socks5.ServerHandshake(conn, authStore.Authenticator())
if err != nil {
conn.Close()
@ -66,5 +63,5 @@ func HandleSocks(conn net.Conn) {
io.Copy(ioutil.Discard, conn)
return
}
tunnel.Add(inbound.NewSocket(target, conn, C.SOCKS))
in <- inbound.NewSocket(target, conn, C.SOCKS)
}

View file

@ -9,27 +9,25 @@ import (
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/Dreamacro/clash/tunnel"
)
type SockUDPListener struct {
type UDPListener struct {
net.PacketConn
address string
closed bool
}
func NewSocksUDPProxy(addr string) (*SockUDPListener, error) {
func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
}
err = sockopt.UDPReuseaddr(l.(*net.UDPConn))
if err != nil {
if err := sockopt.UDPReuseaddr(l.(*net.UDPConn)); err != nil {
log.Warnln("Failed to Reuse UDP Address: %s", err)
}
sl := &SockUDPListener{l, addr, false}
sl := &UDPListener{l, addr, false}
go func() {
for {
buf := pool.Get(pool.RelayBufferSize)
@ -41,23 +39,23 @@ func NewSocksUDPProxy(addr string) (*SockUDPListener, error) {
}
continue
}
handleSocksUDP(l, buf[:n], remoteAddr)
handleSocksUDP(l, in, buf[:n], remoteAddr)
}
}()
return sl, nil
}
func (l *SockUDPListener) Close() error {
func (l *UDPListener) Close() error {
l.closed = true
return l.PacketConn.Close()
}
func (l *SockUDPListener) Address() string {
func (l *UDPListener) Address() string {
return l.address
}
func handleSocksUDP(pc net.PacketConn, buf []byte, addr net.Addr) {
func handleSocksUDP(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, addr net.Addr) {
target, payload, err := socks5.DecodeUDPPacket(buf)
if err != nil {
// Unresolved UDP packet, return buffer to the pool
@ -70,5 +68,8 @@ func handleSocksUDP(pc net.PacketConn, buf []byte, addr net.Addr) {
payload: payload,
bufRef: buf,
}
tunnel.AddPacket(inbound.NewPacket(target, packet, C.SOCKS))
select {
case in <- inbound.NewPacket(target, packet, C.TPROXY):
default:
}
}

View file

@ -1,4 +1,4 @@
package redir
package tproxy
import (
"net"

View file

@ -1,6 +1,6 @@
// +build linux
package redir
package tproxy
import (
"net"

View file

@ -1,6 +1,6 @@
// +build !linux
package redir
package tproxy
import (
"errors"

View file

@ -1,22 +1,20 @@
package redir
package tproxy
import (
"net"
"github.com/Dreamacro/clash/adapter/inbound"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/Dreamacro/clash/tunnel"
)
type TProxyListener struct {
type Listener struct {
net.Listener
address string
closed bool
}
func NewTProxy(addr string) (*TProxyListener, error) {
func New(addr string, in chan<- C.ConnContext) (*Listener, error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
@ -33,13 +31,12 @@ func NewTProxy(addr string) (*TProxyListener, error) {
return nil, err
}
rl := &TProxyListener{
rl := &Listener{
Listener: l,
address: addr,
}
go func() {
log.Infoln("TProxy server listening at: %s", addr)
for {
c, err := l.Accept()
if err != nil {
@ -48,24 +45,24 @@ func NewTProxy(addr string) (*TProxyListener, error) {
}
continue
}
go rl.handleTProxy(c)
go rl.handleTProxy(c, in)
}
}()
return rl, nil
}
func (l *TProxyListener) Close() {
func (l *Listener) Close() {
l.closed = true
l.Listener.Close()
}
func (l *TProxyListener) Address() string {
func (l *Listener) Address() string {
return l.address
}
func (l *TProxyListener) handleTProxy(conn net.Conn) {
func (l *Listener) handleTProxy(conn net.Conn, in chan<- C.ConnContext) {
target := socks5.ParseAddrToSocksAddr(conn.LocalAddr())
conn.(*net.TCPConn).SetKeepAlive(true)
tunnel.Add(inbound.NewSocket(target, conn, C.TPROXY))
in <- inbound.NewSocket(target, conn, C.TPROXY)
}

View file

@ -1,4 +1,4 @@
package redir
package tproxy
import (
"net"
@ -7,22 +7,21 @@ import (
"github.com/Dreamacro/clash/common/pool"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/transport/socks5"
"github.com/Dreamacro/clash/tunnel"
)
type RedirUDPListener struct {
type UDPListener struct {
net.PacketConn
address string
closed bool
}
func NewRedirUDPProxy(addr string) (*RedirUDPListener, error) {
func NewUDP(addr string, in chan<- *inbound.PacketAdapter) (*UDPListener, error) {
l, err := net.ListenPacket("udp", addr)
if err != nil {
return nil, err
}
rl := &RedirUDPListener{l, addr, false}
rl := &UDPListener{l, addr, false}
c := l.(*net.UDPConn)
@ -53,27 +52,30 @@ func NewRedirUDPProxy(addr string) (*RedirUDPListener, error) {
if err != nil {
continue
}
handleRedirUDP(l, buf[:n], lAddr, rAddr)
handlePacketConn(l, in, buf[:n], lAddr, rAddr)
}
}()
return rl, nil
}
func (l *RedirUDPListener) Close() error {
func (l *UDPListener) Close() error {
l.closed = true
return l.PacketConn.Close()
}
func (l *RedirUDPListener) Address() string {
func (l *UDPListener) Address() string {
return l.address
}
func handleRedirUDP(pc net.PacketConn, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {
func handlePacketConn(pc net.PacketConn, in chan<- *inbound.PacketAdapter, buf []byte, lAddr *net.UDPAddr, rAddr *net.UDPAddr) {
target := socks5.ParseAddrToSocksAddr(rAddr)
pkt := &packet{
lAddr: lAddr,
buf: buf,
}
tunnel.AddPacket(inbound.NewPacket(target, pkt, C.TPROXY))
select {
case in <- inbound.NewPacket(target, pkt, C.TPROXY):
default:
}
}

View file

@ -1,8 +1,10 @@
// +build linux
package redir
package tproxy
import (
"encoding/binary"
"errors"
"fmt"
"net"
"os"
@ -10,6 +12,11 @@ import (
"syscall"
)
const (
IPV6_TRANSPARENT = 0x4b
IPV6_RECVORIGDSTADDR = 0x4a
)
// dialUDP acts like net.DialUDP for transparent proxy.
// It binds to a non-local address(`lAddr`).
func dialUDP(network string, lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, error) {
@ -94,3 +101,24 @@ func udpAddrFamily(net string, lAddr, rAddr *net.UDPAddr) int {
}
return syscall.AF_INET6
}
func getOrigDst(oob []byte, oobn int) (*net.UDPAddr, error) {
msgs, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return nil, err
}
for _, msg := range msgs {
if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_RECVORIGDSTADDR {
ip := net.IP(msg.Data[4:8])
port := binary.BigEndian.Uint16(msg.Data[2:4])
return &net.UDPAddr{IP: ip, Port: int(port)}, nil
} else if msg.Header.Level == syscall.SOL_IPV6 && msg.Header.Type == IPV6_RECVORIGDSTADDR {
ip := net.IP(msg.Data[8:24])
port := binary.BigEndian.Uint16(msg.Data[2:4])
return &net.UDPAddr{IP: ip, Port: int(port)}, nil
}
}
return nil, errors.New("cannot find origDst")
}

View file

@ -1,12 +1,16 @@
// +build !linux
package redir
package tproxy
import (
"errors"
"net"
)
func getOrigDst(oob []byte, oobn int) (*net.UDPAddr, error) {
return nil, errors.New("UDP redir not supported on current platform")
}
func dialUDP(network string, lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, error) {
return nil, errors.New("UDP redir not supported on current platform")
}

View file

@ -1,36 +0,0 @@
// +build linux
package redir
import (
"encoding/binary"
"errors"
"net"
"syscall"
)
const (
IPV6_TRANSPARENT = 0x4b
IPV6_RECVORIGDSTADDR = 0x4a
)
func getOrigDst(oob []byte, oobn int) (*net.UDPAddr, error) {
msgs, err := syscall.ParseSocketControlMessage(oob[:oobn])
if err != nil {
return nil, err
}
for _, msg := range msgs {
if msg.Header.Level == syscall.SOL_IP && msg.Header.Type == syscall.IP_RECVORIGDSTADDR {
ip := net.IP(msg.Data[4:8])
port := binary.BigEndian.Uint16(msg.Data[2:4])
return &net.UDPAddr{IP: ip, Port: int(port)}, nil
} else if msg.Header.Level == syscall.SOL_IPV6 && msg.Header.Type == IPV6_RECVORIGDSTADDR {
ip := net.IP(msg.Data[8:24])
port := binary.BigEndian.Uint16(msg.Data[2:4])
return &net.UDPAddr{IP: ip, Port: int(port)}, nil
}
}
return nil, errors.New("cannot find origDst")
}

View file

@ -1,12 +0,0 @@
// +build !linux
package redir
import (
"errors"
"net"
)
func getOrigDst(oob []byte, oobn int) (*net.UDPAddr, error) {
return nil, errors.New("UDP redir not supported on current platform")
}

View file

@ -37,17 +37,14 @@ func init() {
go process()
}
// Add request to queue
func Add(ctx C.ConnContext) {
tcpQueue <- ctx
// TCPIn return fan-in queue
func TCPIn() chan<- C.ConnContext {
return tcpQueue
}
// AddPacket add udp Packet to queue
func AddPacket(packet *inbound.PacketAdapter) {
select {
case udpQueue <- packet:
default:
}
// UDPIn return fan-in udp queue
func UDPIn() chan<- *inbound.PacketAdapter {
return udpQueue
}
// Rules return all rules