mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
general: tidy code
This commit is contained in:
parent
fde0572593
commit
f3db1980cf
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
99
proxy/socks5/packet.go
Normal file
99
proxy/socks5/packet.go
Normal file
@ -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()
|
||||
}
|
@ -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()
|
||||
}
|
||||
|
67
proxy/ss/packet.go
Normal file
67
proxy/ss/packet.go
Normal file
@ -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)
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -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"
|
||||
)
|
||||
@ -33,8 +34,10 @@ type VMess struct {
|
||||
|
||||
uuid string
|
||||
alertID uint32
|
||||
network string
|
||||
security string
|
||||
|
||||
outboundSecurity string
|
||||
streamProtocol string
|
||||
streamSecurity string
|
||||
|
||||
config *core.Config
|
||||
instance *core.Instance
|
||||
@ -122,8 +125,10 @@ func NewVMess(s string, dialer proxy.Dialer) (*VMess, error) {
|
||||
|
||||
uuid: uuid,
|
||||
alertID: uint32(alertID),
|
||||
network: "tcp",
|
||||
security: "tls",
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user