From f3db1980cf75f8ca9430c153f03921aaa6b9fc16 Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Thu, 28 Jun 2018 09:49:23 +0800 Subject: [PATCH] general: tidy code --- README.md | 3 -- ipset_linux.go | 56 +++++++++++++---------- proxy/http/http.go | 11 +---- proxy/socks5/packet.go | 99 ++++++++++++++++++++++++++++++++++++++++ proxy/socks5/socks5.go | 101 ++--------------------------------------- proxy/ss/packet.go | 67 +++++++++++++++++++++++++++ proxy/ss/ss.go | 60 ------------------------ proxy/vmess/vmess.go | 26 +++++++---- 8 files changed, 222 insertions(+), 201 deletions(-) create mode 100644 proxy/socks5/packet.go create mode 100644 proxy/ss/packet.go diff --git a/README.md b/README.md index 6ab0949..7019b8d 100644 --- a/README.md +++ b/README.md @@ -50,12 +50,9 @@ General: - Rule proxy based on destinations: [Config Examples](config/examples) TODO: - - [ ] Transparent UDP proxy (iptables tproxy) - [ ] DNS Cache - [ ] TUN/TAP device support -- [ ] Code refactoring: support proxy registering so it can be pluggable -- [ ] Conditional compilation so we can abandon needless proxy type and get a smaller binary size - [ ] IPv6 support - [ ] SSH tunnel support diff --git a/ipset_linux.go b/ipset_linux.go index 67e9222..f3cd02d 100644 --- a/ipset_linux.go +++ b/ipset_linux.go @@ -17,42 +17,52 @@ import ( "github.com/nadoo/glider/common/log" ) -// netfilter netlink message types +// NFNL_SUBSYS_IPSET netfilter netlink message types // https://github.com/torvalds/linux/blob/9e66317d3c92ddaab330c125dfe9d06eee268aff/include/uapi/linux/netfilter/nfnetlink.h#L56 -// NFNL_SUBSYS_IPSET const NFNL_SUBSYS_IPSET = 6 // http://git.netfilter.org/ipset/tree/include/libipset/linux_ip_set.h -// IPSET_PROTOCOL: The protocol version +// IPSET_PROTOCOL The protocol version const IPSET_PROTOCOL = 6 -// IPSET_MAXNAMELEN: The max length of strings including NUL: set and type identifiers +// IPSET_MAXNAMELEN The max length of strings including NUL: set and type identifiers const IPSET_MAXNAMELEN = 32 // Message types and commands -const IPSET_CMD_CREATE = 2 -const IPSET_CMD_FLUSH = 4 -const IPSET_CMD_ADD = 9 -const IPSET_CMD_DEL = 10 +const ( + IPSET_CMD_CREATE = 2 + IPSET_CMD_FLUSH = 4 + IPSET_CMD_ADD = 9 + IPSET_CMD_DEL = 10 +) -/* Attributes at command level */ -const IPSET_ATTR_PROTOCOL = 1 /* 1: Protocol version */ -const IPSET_ATTR_SETNAME = 2 /* 2: Name of the set */ -const IPSET_ATTR_TYPENAME = 3 /* 3: Typename */ -const IPSET_ATTR_REVISION = 4 /* 4: Settype revision */ -const IPSET_ATTR_FAMILY = 5 /* 5: Settype family */ -const IPSET_ATTR_DATA = 7 /* 7: Nested attributes */ +// Attributes at command level +const ( + IPSET_ATTR_PROTOCOL = 1 /* 1: Protocol version */ + IPSET_ATTR_SETNAME = 2 /* 2: Name of the set */ + IPSET_ATTR_TYPENAME = 3 /* 3: Typename */ + IPSET_ATTR_REVISION = 4 /* 4: Settype revision */ + IPSET_ATTR_FAMILY = 5 /* 5: Settype family */ + IPSET_ATTR_DATA = 7 /* 7: Nested attributes */ +) -/* CADT specific attributes */ -const IPSET_ATTR_IP = 1 -const IPSET_ATTR_CIDR = 3 +// CADT specific attributes +const ( + IPSET_ATTR_IP = 1 + IPSET_ATTR_CIDR = 3 +) -/* IP specific attributes */ -const IPSET_ATTR_IPADDR_IPV4 = 1 -const IPSET_ATTR_IPADDR_IPV6 = 2 +// IP specific attributes +const ( + IPSET_ATTR_IPADDR_IPV4 = 1 + IPSET_ATTR_IPADDR_IPV6 = 2 +) -const NLA_F_NESTED = (1 << 15) -const NLA_F_NET_BYTEORDER = (1 << 14) +// ATTR flags +const ( + NLA_F_NESTED = (1 << 15) + NLA_F_NET_BYTEORDER = (1 << 14) +) var nextSeqNr uint32 var nativeEndian binary.ByteOrder diff --git a/proxy/http/http.go b/proxy/http/http.go index 4ef64ed..a013518 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -117,19 +117,10 @@ func (s *HTTP) Serve(c net.Conn) { return } cleanHeaders(reqHeader) + // tell the remote server not to keep alive reqHeader.Set("Connection", "close") - // X-Forwarded-For - // if s.xff { - // if reqHeader.Get("X-Forwarded-For") != "" { - // reqHeader.Add("X-Forwarded-For", ",") - // } - // reqHeader.Add("X-Forwarded-For", c.RemoteAddr().(*net.TCPAddr).IP.String()) - // reqHeader.Add("X-Forwarded-For", ",") - // reqHeader.Add("X-Forwarded-For", s.selfip) - // } - url, err := url.ParseRequestURI(requestURI) if err != nil { log.F("proxy-http parse request url error: %s", err) diff --git a/proxy/socks5/packet.go b/proxy/socks5/packet.go new file mode 100644 index 0000000..289323c --- /dev/null +++ b/proxy/socks5/packet.go @@ -0,0 +1,99 @@ +package socks5 + +import ( + "net" + + "github.com/nadoo/glider/common/log" + "github.com/nadoo/glider/common/socks" +) + +// PktConn . +type PktConn struct { + net.PacketConn + + writeAddr net.Addr // write to and read from addr + + tgtAddr socks.Addr + tgtHeader bool + + ctrlConn net.Conn // tcp control conn +} + +// NewPktConn returns a PktConn +func NewPktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr socks.Addr, tgtHeader bool, ctrlConn net.Conn) *PktConn { + pc := &PktConn{ + PacketConn: c, + writeAddr: writeAddr, + tgtAddr: tgtAddr, + tgtHeader: tgtHeader, + ctrlConn: ctrlConn} + + if ctrlConn != nil { + go func() { + buf := []byte{} + for { + _, err := ctrlConn.Read(buf) + if err, ok := err.(net.Error); ok && err.Timeout() { + continue + } + log.F("proxy-socks5 dialudp udp associate end") + return + } + }() + } + + return pc +} + +// ReadFrom overrides the original function from net.PacketConn +func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) { + if !pc.tgtHeader { + return pc.PacketConn.ReadFrom(b) + } + + buf := make([]byte, len(b)) + n, raddr, err := pc.PacketConn.ReadFrom(buf) + if err != nil { + return n, raddr, err + } + + // https://tools.ietf.org/html/rfc1928#section-7 + // +----+------+------+----------+----------+----------+ + // |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA | + // +----+------+------+----------+----------+----------+ + // | 2 | 1 | 1 | Variable | 2 | Variable | + // +----+------+------+----------+----------+----------+ + tgtAddr := socks.SplitAddr(buf[3:]) + copy(b, buf[3+len(tgtAddr):]) + + //test + if pc.writeAddr == nil { + pc.writeAddr = raddr + } + + if pc.tgtAddr == nil { + pc.tgtAddr = tgtAddr + } + + return n - len(tgtAddr) - 3, raddr, err +} + +// WriteTo overrides the original function from net.PacketConn +func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) { + if !pc.tgtHeader { + return pc.PacketConn.WriteTo(b, addr) + } + + buf := append([]byte{0, 0, 0}, pc.tgtAddr...) + buf = append(buf, b[:]...) + return pc.PacketConn.WriteTo(buf, pc.writeAddr) +} + +// Close . +func (pc *PktConn) Close() error { + if pc.ctrlConn != nil { + pc.ctrlConn.Close() + } + + return pc.PacketConn.Close() +} diff --git a/proxy/socks5/socks5.go b/proxy/socks5/socks5.go index 1768814..2e9c784 100644 --- a/proxy/socks5/socks5.go +++ b/proxy/socks5/socks5.go @@ -166,7 +166,7 @@ func (s *SOCKS5) ListenAndServeUDP() { buf := make([]byte, conn.UDPBufSize) for { - c := NewSocks5PktConn(lc, nil, nil, true, nil) + c := NewPktConn(lc, nil, nil, true, nil) n, raddr, err := c.ReadFrom(buf) if err != nil { @@ -174,7 +174,7 @@ func (s *SOCKS5) ListenAndServeUDP() { continue } - var pc *Socks5PktConn + var pc *PktConn v, ok := nm.Load(raddr.String()) if !ok && v == nil { if c.tgtAddr == nil { @@ -188,7 +188,7 @@ func (s *SOCKS5) ListenAndServeUDP() { continue } - pc = NewSocks5PktConn(lpc, nextHop, nil, false, nil) + pc = NewPktConn(lpc, nextHop, nil, false, nil) nm.Store(raddr.String(), pc) go func() { @@ -198,7 +198,7 @@ func (s *SOCKS5) ListenAndServeUDP() { }() } else { - pc = v.(*Socks5PktConn) + pc = v.(*PktConn) } _, err = pc.WriteTo(buf[:n], pc.writeAddr) @@ -291,7 +291,7 @@ func (s *SOCKS5) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.A return nil, nil, err } - pkc := NewSocks5PktConn(pc, nextHop, dstAddr, true, c) + pkc := NewPktConn(pc, nextHop, dstAddr, true, c) return pkc, nextHop, err } @@ -469,94 +469,3 @@ func (s *SOCKS5) handshake(rw io.ReadWriter) (socks.Addr, error) { return addr, err // skip VER, CMD, RSV fields } - -// Socks5PktConn . -type Socks5PktConn struct { - net.PacketConn - - writeAddr net.Addr // write to and read from addr - - tgtAddr socks.Addr - tgtHeader bool - - ctrlConn net.Conn // tcp control conn -} - -// NewSocks5PktConn returns a Socks5PktConn -func NewSocks5PktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr socks.Addr, tgtHeader bool, ctrlConn net.Conn) *Socks5PktConn { - pc := &Socks5PktConn{ - PacketConn: c, - writeAddr: writeAddr, - tgtAddr: tgtAddr, - tgtHeader: tgtHeader, - ctrlConn: ctrlConn} - - if ctrlConn != nil { - go func() { - buf := []byte{} - for { - _, err := ctrlConn.Read(buf) - if err, ok := err.(net.Error); ok && err.Timeout() { - continue - } - log.F("proxy-socks5 dialudp udp associate end") - return - } - }() - } - - return pc -} - -// ReadFrom overrides the original function from net.PacketConn -func (pc *Socks5PktConn) ReadFrom(b []byte) (int, net.Addr, error) { - if !pc.tgtHeader { - return pc.PacketConn.ReadFrom(b) - } - - buf := make([]byte, len(b)) - n, raddr, err := pc.PacketConn.ReadFrom(buf) - if err != nil { - return n, raddr, err - } - - // https://tools.ietf.org/html/rfc1928#section-7 - // +----+------+------+----------+----------+----------+ - // |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA | - // +----+------+------+----------+----------+----------+ - // | 2 | 1 | 1 | Variable | 2 | Variable | - // +----+------+------+----------+----------+----------+ - tgtAddr := socks.SplitAddr(buf[3:]) - copy(b, buf[3+len(tgtAddr):]) - - //test - if pc.writeAddr == nil { - pc.writeAddr = raddr - } - - if pc.tgtAddr == nil { - pc.tgtAddr = tgtAddr - } - - return n - len(tgtAddr) - 3, raddr, err -} - -// WriteTo overrides the original function from net.PacketConn -func (pc *Socks5PktConn) WriteTo(b []byte, addr net.Addr) (int, error) { - if !pc.tgtHeader { - return pc.PacketConn.WriteTo(b, addr) - } - - buf := append([]byte{0, 0, 0}, pc.tgtAddr...) - buf = append(buf, b[:]...) - return pc.PacketConn.WriteTo(buf, pc.writeAddr) -} - -// Close . -func (pc *Socks5PktConn) Close() error { - if pc.ctrlConn != nil { - pc.ctrlConn.Close() - } - - return pc.PacketConn.Close() -} diff --git a/proxy/ss/packet.go b/proxy/ss/packet.go new file mode 100644 index 0000000..91edf60 --- /dev/null +++ b/proxy/ss/packet.go @@ -0,0 +1,67 @@ +package ss + +import ( + "net" + + "github.com/nadoo/glider/common/socks" +) + +// PktConn . +type PktConn struct { + net.PacketConn + + writeAddr net.Addr // write to and read from addr + + tgtAddr socks.Addr + tgtHeader bool +} + +// NewPktConn returns a PktConn +func NewPktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr socks.Addr, tgtHeader bool) *PktConn { + pc := &PktConn{ + PacketConn: c, + writeAddr: writeAddr, + tgtAddr: tgtAddr, + tgtHeader: tgtHeader} + return pc +} + +// ReadFrom overrides the original function from net.PacketConn +func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) { + if !pc.tgtHeader { + return pc.PacketConn.ReadFrom(b) + } + + buf := make([]byte, len(b)) + n, raddr, err := pc.PacketConn.ReadFrom(buf) + if err != nil { + return n, raddr, err + } + + tgtAddr := socks.SplitAddr(buf) + copy(b, buf[len(tgtAddr):]) + + //test + if pc.writeAddr == nil { + pc.writeAddr = raddr + } + + if pc.tgtAddr == nil { + pc.tgtAddr = tgtAddr + } + + return n - len(tgtAddr), raddr, err +} + +// WriteTo overrides the original function from net.PacketConn +func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) { + if !pc.tgtHeader { + return pc.PacketConn.WriteTo(b, addr) + } + + buf := make([]byte, len(pc.tgtAddr)+len(b)) + copy(buf, pc.tgtAddr) + copy(buf[len(pc.tgtAddr):], b) + + return pc.PacketConn.WriteTo(buf, pc.writeAddr) +} diff --git a/proxy/ss/ss.go b/proxy/ss/ss.go index 383d89e..0df4f46 100644 --- a/proxy/ss/ss.go +++ b/proxy/ss/ss.go @@ -279,63 +279,3 @@ func (s *SS) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { pkc := NewPktConn(s.PacketConn(pc), nextHop, socks.ParseAddr(addr), true) return pkc, nextHop, err } - -// PktConn . -type PktConn struct { - net.PacketConn - - writeAddr net.Addr // write to and read from addr - - tgtAddr socks.Addr - tgtHeader bool -} - -// NewPktConn returns a PktConn -func NewPktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr socks.Addr, tgtHeader bool) *PktConn { - pc := &PktConn{ - PacketConn: c, - writeAddr: writeAddr, - tgtAddr: tgtAddr, - tgtHeader: tgtHeader} - return pc -} - -// ReadFrom overrides the original function from net.PacketConn -func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) { - if !pc.tgtHeader { - return pc.PacketConn.ReadFrom(b) - } - - buf := make([]byte, len(b)) - n, raddr, err := pc.PacketConn.ReadFrom(buf) - if err != nil { - return n, raddr, err - } - - tgtAddr := socks.SplitAddr(buf) - copy(b, buf[len(tgtAddr):]) - - //test - if pc.writeAddr == nil { - pc.writeAddr = raddr - } - - if pc.tgtAddr == nil { - pc.tgtAddr = tgtAddr - } - - return n - len(tgtAddr), raddr, err -} - -// WriteTo overrides the original function from net.PacketConn -func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) { - if !pc.tgtHeader { - return pc.PacketConn.WriteTo(b, addr) - } - - buf := make([]byte, len(pc.tgtAddr)+len(b)) - copy(buf, pc.tgtAddr) - copy(buf[len(pc.tgtAddr):], b) - - return pc.PacketConn.WriteTo(buf, pc.writeAddr) -} diff --git a/proxy/vmess/vmess.go b/proxy/vmess/vmess.go index 39f2eaa..524091f 100644 --- a/proxy/vmess/vmess.go +++ b/proxy/vmess/vmess.go @@ -22,6 +22,7 @@ import ( "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/tls" + // needed _ "v2ray.com/core/app/proxyman/outbound" _ "v2ray.com/core/transport/internet/tcp" ) @@ -31,10 +32,12 @@ type VMess struct { dialer proxy.Dialer addr string - uuid string - alertID uint32 - network string - security string + uuid string + alertID uint32 + + outboundSecurity string + streamProtocol string + streamSecurity string config *core.Config instance *core.Instance @@ -120,10 +123,12 @@ func NewVMess(s string, dialer proxy.Dialer) (*VMess, error) { dialer: dialer, addr: addr, - uuid: uuid, - alertID: uint32(alertID), - network: "tcp", - security: "tls", + uuid: uuid, + alertID: uint32(alertID), + + outboundSecurity: "auto", + streamProtocol: "tcp", + streamSecurity: "tls", config: config, instance: v, @@ -157,7 +162,10 @@ func (s *VMess) Dial(network, addr string) (net.Conn, error) { } // TODO: does not support upstream dialer now - c, err := core.Dial(context.Background(), s.instance, v2net.TCPDestination(v2net.ParseAddress(host), v2net.Port(port))) + c, err := core.Dial(context.Background(), + s.instance, + v2net.TCPDestination(v2net.ParseAddress(host), v2net.Port(port))) + if err != nil { log.F("proxy-vmess dial to %s error: %s", s.addr, err) return nil, err