mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
udptun: add DialUDP to forwarders
This commit is contained in:
parent
12f15be1dc
commit
31930f2955
@ -14,6 +14,9 @@ type Dialer interface {
|
|||||||
// Dial connects to the given address via the proxy.
|
// Dial connects to the given address via the proxy.
|
||||||
Dial(network, addr string) (c net.Conn, err error)
|
Dial(network, addr string) (c net.Conn, err error)
|
||||||
|
|
||||||
|
// DialUDP connects to the given address via the proxy.
|
||||||
|
DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error)
|
||||||
|
|
||||||
// Get the dialer by dstAddr
|
// Get the dialer by dstAddr
|
||||||
NextDialer(dstAddr string) Dialer
|
NextDialer(dstAddr string) Dialer
|
||||||
}
|
}
|
||||||
|
21
direct.go
21
direct.go
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
// direct proxy
|
// direct proxy
|
||||||
type direct struct{}
|
type direct struct{}
|
||||||
@ -16,10 +18,27 @@ func (d *direct) Dial(network, addr string) (net.Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c, err := net.Dial(network, addr)
|
c, err := net.Dial(network, addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if c, ok := c.(*net.TCPConn); ok {
|
if c, ok := c.(*net.TCPConn); ok {
|
||||||
c.SetKeepAlive(true)
|
c.SetKeepAlive(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialUDP connects to the given address via the proxy.
|
||||||
|
func (d *direct) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||||
|
pc, err = net.ListenPacket(network, "")
|
||||||
|
if err != nil {
|
||||||
|
logf("ListenPacket error: %s", err)
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
uAddr, err := net.ResolveUDPAddr("udp", addr)
|
||||||
|
return pc, uAddr, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (d *direct) NextDialer(dstAddr string) Dialer { return d }
|
func (d *direct) NextDialer(dstAddr string) Dialer { return d }
|
||||||
|
7
http.go
7
http.go
@ -212,7 +212,7 @@ func (s *HTTP) servHTTPS(method, requestURI, proto string, c net.Conn) {
|
|||||||
|
|
||||||
// Dial connects to the address addr on the network net via the proxy.
|
// Dial connects to the address addr on the network net via the proxy.
|
||||||
func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
|
func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
|
||||||
rc, err := s.cDialer.Dial("tcp", s.addr)
|
rc, err := s.cDialer.Dial(network, s.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("dial to %s error: %s", s.addr, err)
|
logf("dial to %s error: %s", s.addr, err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -247,6 +247,11 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return nil, errors.New("proxy-http cound not connect remote address: " + addr + ". error code: " + code)
|
return nil, errors.New("proxy-http cound not connect remote address: " + addr + ". error code: " + code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialUDP connects to the given address via the proxy.
|
||||||
|
func (s *HTTP) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||||
|
return nil, nil, errors.New("DialUDP not supported")
|
||||||
|
}
|
||||||
|
|
||||||
// parseFirstLine parses "GET /foo HTTP/1.1" OR "HTTP/1.1 200 OK" into its three parts.
|
// parseFirstLine parses "GET /foo HTTP/1.1" OR "HTTP/1.1 200 OK" into its three parts.
|
||||||
func parseFirstLine(tp *textproto.Reader) (r1, r2, r3 string, ok bool) {
|
func parseFirstLine(tp *textproto.Reader) (r1, r2, r3 string, ok bool) {
|
||||||
line, err := tp.ReadLine()
|
line, err := tp.ReadLine()
|
||||||
|
4
rule.go
4
rule.go
@ -113,6 +113,10 @@ func (rd *RuleDialer) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return rd.NextDialer(addr).Dial(network, addr)
|
return rd.NextDialer(addr).Dial(network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rd *RuleDialer) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||||
|
return rd.NextDialer(addr).DialUDP(network, addr)
|
||||||
|
}
|
||||||
|
|
||||||
// AddDomainIP used to update ipMap rules according to domainMap rule
|
// AddDomainIP used to update ipMap rules according to domainMap rule
|
||||||
func (rd *RuleDialer) AddDomainIP(domain, ip string) error {
|
func (rd *RuleDialer) AddDomainIP(domain, ip string) error {
|
||||||
if ip != "" {
|
if ip != "" {
|
||||||
|
@ -156,6 +156,11 @@ func (s *SOCKS5) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DialUDP connects to the given address via the proxy.
|
||||||
|
func (s *SOCKS5) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||||
|
return nil, nil, errors.New("DialUDP not supported")
|
||||||
|
}
|
||||||
|
|
||||||
// connect takes an existing connection to a socks5 proxy server,
|
// connect takes an existing connection to a socks5 proxy server,
|
||||||
// and commands the server to extend that connection to target,
|
// and commands the server to extend that connection to target,
|
||||||
// which must be a canonical address with a host and port.
|
// which must be a canonical address with a host and port.
|
||||||
|
85
ss.go
85
ss.go
@ -225,8 +225,8 @@ func (s *SS) Dial(network, addr string) (net.Conn, error) {
|
|||||||
case "uot":
|
case "uot":
|
||||||
target[0] = target[0] | 0x8
|
target[0] = target[0] | 0x8
|
||||||
return s.dialTCP(target)
|
return s.dialTCP(target)
|
||||||
case "udp":
|
// case "udp":
|
||||||
return s.dialUDP(target)
|
// return s.dialUDP(target)
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("Unknown schema: " + network)
|
return nil, errors.New("Unknown schema: " + network)
|
||||||
}
|
}
|
||||||
@ -254,34 +254,51 @@ func (s *SS) dialTCP(target Addr) (net.Conn, error) {
|
|||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: support forwarder chain
|
// DialUDP connects to the given address via the proxy.
|
||||||
func (s *SS) dialUDP(target Addr) (net.Conn, error) {
|
func (s *SS) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||||
c, err := net.ListenPacket("udp", "")
|
// TODO: check forward chain
|
||||||
|
pc, nextHop, err := s.cDialer.DialUDP(network, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-ss dialudp failed to listen packet: %v", err)
|
logf("proxy-ss dialudp to %s error: %s", addr, err)
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
sUDPAddr, err := net.ResolveUDPAddr("udp", s.Addr())
|
nextHopAddr := ParseAddr(nextHop.String())
|
||||||
suc := &ssUDPConn{
|
writeTo, err = net.ResolveUDPAddr("udp", s.Addr())
|
||||||
PacketConn: s.PacketConn(c),
|
|
||||||
addr: sUDPAddr,
|
pkc := NewPktConn(s.PacketConn(pc), writeTo, nextHopAddr, true)
|
||||||
target: target,
|
|
||||||
|
return pkc, writeTo, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return suc, err
|
// PktConn wraps a net.PacketConn and support Write method like net.Conn
|
||||||
}
|
type PktConn struct {
|
||||||
|
|
||||||
type ssUDPConn struct {
|
|
||||||
net.PacketConn
|
net.PacketConn
|
||||||
|
|
||||||
addr net.Addr
|
addr net.Addr // write to and read from addr
|
||||||
target Addr
|
target Addr
|
||||||
|
tgtHeader bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPktConn returns a PktConn
|
||||||
|
func NewPktConn(c net.PacketConn, addr net.Addr, target Addr, tgtHeader bool) *PktConn {
|
||||||
|
pc := &PktConn{
|
||||||
|
PacketConn: c,
|
||||||
|
addr: addr,
|
||||||
|
target: target,
|
||||||
|
tgtHeader: tgtHeader}
|
||||||
|
return pc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *PktConn) Read(b []byte) (int, error) {
|
||||||
|
|
||||||
|
if !pc.tgtHeader {
|
||||||
|
n, _, err := pc.PacketConn.ReadFrom(b)
|
||||||
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ssUDPConn) Read(b []byte) (int, error) {
|
|
||||||
buf := make([]byte, len(b))
|
buf := make([]byte, len(b))
|
||||||
n, raddr, err := uc.PacketConn.ReadFrom(buf)
|
n, raddr, err := pc.PacketConn.ReadFrom(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
@ -292,20 +309,32 @@ func (uc *ssUDPConn) Read(b []byte) (int, error) {
|
|||||||
return n - len(srcAddr), err
|
return n - len(srcAddr), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ssUDPConn) Write(b []byte) (int, error) {
|
func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
buf := make([]byte, len(uc.target)+len(b))
|
|
||||||
copy(buf, uc.target)
|
n, err := pc.Read(b)
|
||||||
copy(buf[len(uc.target):], b)
|
|
||||||
|
// TODO: Addr
|
||||||
|
return n, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pc *PktConn) Write(b []byte) (int, error) {
|
||||||
|
if !pc.tgtHeader {
|
||||||
|
return pc.PacketConn.WriteTo(b, pc.addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, len(pc.target)+len(b))
|
||||||
|
copy(buf, pc.target)
|
||||||
|
copy(buf[len(pc.target):], b)
|
||||||
|
|
||||||
// logf("Write: \n%s", hex.Dump(buf))
|
// logf("Write: \n%s", hex.Dump(buf))
|
||||||
|
|
||||||
return uc.PacketConn.WriteTo(buf, uc.addr)
|
return pc.PacketConn.WriteTo(buf, pc.addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ssUDPConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||||
return 0, errors.New("not available")
|
return pc.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *ssUDPConn) RemoteAddr() net.Addr {
|
func (pc *PktConn) RemoteAddr() net.Addr {
|
||||||
return uc.addr
|
return pc.addr
|
||||||
}
|
}
|
||||||
|
15
strategy.go
15
strategy.go
@ -67,6 +67,10 @@ func (rr *rrDialer) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return rr.NextDialer(addr).Dial(network, addr)
|
return rr.NextDialer(addr).Dial(network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (rr *rrDialer) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||||
|
return rr.NextDialer(addr).DialUDP(network, addr)
|
||||||
|
}
|
||||||
|
|
||||||
func (rr *rrDialer) NextDialer(dstAddr string) Dialer {
|
func (rr *rrDialer) NextDialer(dstAddr string) Dialer {
|
||||||
n := len(rr.dialers)
|
n := len(rr.dialers)
|
||||||
if n == 1 {
|
if n == 1 {
|
||||||
@ -158,3 +162,14 @@ func (ha *haDialer) Dial(network, addr string) (net.Conn, error) {
|
|||||||
|
|
||||||
return d.Dial(network, addr)
|
return d.Dial(network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ha *haDialer) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||||
|
d := ha.dialers[ha.idx]
|
||||||
|
|
||||||
|
result, ok := ha.status.Load(ha.idx)
|
||||||
|
if ok && !result.(bool) {
|
||||||
|
d = ha.NextDialer(addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return d.DialUDP(network, addr)
|
||||||
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Glider Service (%i)
|
Description=Glider Service (%i)
|
||||||
After=network.target
|
|
||||||
Before=iptables.service ip6tables.service
|
Before=iptables.service ip6tables.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
|
@ -42,20 +42,20 @@ func (s *UDPTun) ListenAndServe() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
rc, err := s.sDialer.Dial("udp", s.raddr)
|
rc, wt, err := s.sDialer.DialUDP("udp", s.raddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-udptun failed to connect to server %v: %v", s.raddr, err)
|
logf("proxy-udptun failed to connect to server %v: %v", s.raddr, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err = rc.Write(buf[:n])
|
n, err = rc.WriteTo(buf[:n], wt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-udptun rc.Write error: %v", err)
|
logf("proxy-udptun rc.Write error: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = make([]byte, udpBufSize)
|
buf = make([]byte, udpBufSize)
|
||||||
n, err = rc.Read(buf)
|
n, _, err = rc.ReadFrom(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-udptun rc.Read error: %v", err)
|
logf("proxy-udptun rc.Read error: %v", err)
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user