mirror of
https://github.com/nadoo/glider.git
synced 2025-02-24 01:45:39 +08:00
udptun: use net.Conn instead of net.PacketConn
This commit is contained in:
parent
b33127fba5
commit
c51cc70b0b
@ -14,9 +14,6 @@ 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 returns a UDP PacketConn
|
|
||||||
DialUDP(network, addr string) (c net.PacketConn, err error)
|
|
||||||
|
|
||||||
// Get the dialer by dstAddr
|
// Get the dialer by dstAddr
|
||||||
NextDialer(dstAddr string) Dialer
|
NextDialer(dstAddr string) Dialer
|
||||||
}
|
}
|
||||||
|
17
direct.go
17
direct.go
@ -3,8 +3,7 @@ package main
|
|||||||
import "net"
|
import "net"
|
||||||
|
|
||||||
// direct proxy
|
// direct proxy
|
||||||
type direct struct {
|
type direct struct{}
|
||||||
}
|
|
||||||
|
|
||||||
// Direct proxy
|
// Direct proxy
|
||||||
var Direct = &direct{}
|
var Direct = &direct{}
|
||||||
@ -12,6 +11,10 @@ var Direct = &direct{}
|
|||||||
func (d *direct) Addr() string { return "DIRECT" }
|
func (d *direct) Addr() string { return "DIRECT" }
|
||||||
|
|
||||||
func (d *direct) Dial(network, addr string) (net.Conn, error) {
|
func (d *direct) Dial(network, addr string) (net.Conn, error) {
|
||||||
|
if network == "uot" {
|
||||||
|
network = "udp"
|
||||||
|
}
|
||||||
|
|
||||||
c, err := net.Dial(network, addr)
|
c, err := net.Dial(network, addr)
|
||||||
if c, ok := c.(*net.TCPConn); ok {
|
if c, ok := c.(*net.TCPConn); ok {
|
||||||
c.SetKeepAlive(true)
|
c.SetKeepAlive(true)
|
||||||
@ -19,14 +22,4 @@ func (d *direct) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *direct) DialUDP(network, addr string) (net.PacketConn, error) {
|
|
||||||
uAddr, err := net.ResolveUDPAddr(network, addr)
|
|
||||||
if err != nil {
|
|
||||||
logf("ResolveUDPAddr error: %s", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return net.DialUDP("udp", nil, uAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *direct) NextDialer(dstAddr string) Dialer { return d }
|
func (d *direct) NextDialer(dstAddr string) Dialer { return d }
|
||||||
|
5
http.go
5
http.go
@ -236,11 +236,6 @@ 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 .
|
|
||||||
func (s *HTTP) DialUDP(network, addr string) (net.PacketConn, error) {
|
|
||||||
return nil, errors.New("proxy-http udp not supported by http proxy now")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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()
|
||||||
|
5
rule.go
5
rule.go
@ -113,11 +113,6 @@ func (rd *RuleDialer) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return rd.NextDialer(addr).Dial(network, addr)
|
return rd.NextDialer(addr).Dial(network, addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialUDP .
|
|
||||||
func (rd *RuleDialer) DialUDP(network, addr string) (net.PacketConn, 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,11 +156,6 @@ func (s *SOCKS5) Dial(network, addr string) (net.Conn, error) {
|
|||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialUDP .
|
|
||||||
func (s *SOCKS5) DialUDP(network, addr string) (net.PacketConn, error) {
|
|
||||||
return nil, errors.New("udp not supported by socks5 proxy now")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
||||||
|
79
ss.go
79
ss.go
@ -207,6 +207,11 @@ func (s *SS) ListenAndServeUDP() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListCipher .
|
||||||
|
func ListCipher() string {
|
||||||
|
return strings.Join(core.ListCipher(), " ")
|
||||||
|
}
|
||||||
|
|
||||||
// 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 *SS) Dial(network, addr string) (net.Conn, error) {
|
func (s *SS) Dial(network, addr string) (net.Conn, error) {
|
||||||
target := ParseAddr(addr)
|
target := ParseAddr(addr)
|
||||||
@ -216,12 +221,12 @@ func (s *SS) Dial(network, addr string) (net.Conn, error) {
|
|||||||
|
|
||||||
switch network {
|
switch network {
|
||||||
case "tcp":
|
case "tcp":
|
||||||
return s.DialTCP(target)
|
return s.dialTCP(target)
|
||||||
case "udp":
|
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)
|
||||||
}
|
}
|
||||||
@ -229,7 +234,7 @@ func (s *SS) Dial(network, addr string) (net.Conn, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DialTCP connects to the address addr via the proxy.
|
// DialTCP connects to the address addr via the proxy.
|
||||||
func (s *SS) DialTCP(target Addr) (net.Conn, error) {
|
func (s *SS) dialTCP(target Addr) (net.Conn, error) {
|
||||||
c, err := s.cDialer.Dial("tcp", s.addr)
|
c, err := s.cDialer.Dial("tcp", 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)
|
||||||
@ -249,20 +254,58 @@ func (s *SS) DialTCP(target Addr) (net.Conn, error) {
|
|||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialUDP .
|
// TODO: support forwarder chain
|
||||||
func (s *SS) DialUDP(network, addr string) (net.PacketConn, error) {
|
func (s *SS) dialUDP(target Addr) (net.Conn, error) {
|
||||||
target := ParseAddr(addr)
|
c, err := net.ListenPacket("udp", "")
|
||||||
if target == nil {
|
if err != nil {
|
||||||
return nil, errors.New("Unable to parse address: " + addr)
|
logf("proxy-ss dialudp failed to listen packet: %v", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c, _ := net.ListenPacket(network, "")
|
sUDPAddr, err := net.ResolveUDPAddr("udp", s.Addr())
|
||||||
c = s.PacketConn(c)
|
suc := &ssUDPConn{
|
||||||
|
PacketConn: s.PacketConn(c),
|
||||||
return c, nil
|
addr: sUDPAddr,
|
||||||
|
target: target,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListCipher .
|
return suc, err
|
||||||
func ListCipher() string {
|
}
|
||||||
return strings.Join(core.ListCipher(), " ")
|
|
||||||
|
type ssUDPConn struct {
|
||||||
|
net.PacketConn
|
||||||
|
|
||||||
|
addr net.Addr
|
||||||
|
target Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *ssUDPConn) Read(b []byte) (int, error) {
|
||||||
|
buf := make([]byte, len(b))
|
||||||
|
n, raddr, err := uc.PacketConn.ReadFrom(buf)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
srcAddr := ParseAddr(raddr.String())
|
||||||
|
copy(b, buf[len(srcAddr):])
|
||||||
|
|
||||||
|
return n - len(srcAddr), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *ssUDPConn) Write(b []byte) (int, error) {
|
||||||
|
buf := make([]byte, len(uc.target)+len(b))
|
||||||
|
copy(buf, uc.target)
|
||||||
|
copy(buf[len(uc.target):], b)
|
||||||
|
|
||||||
|
// logf("Write: \n%s", hex.Dump(buf))
|
||||||
|
|
||||||
|
return uc.PacketConn.WriteTo(buf, uc.addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *ssUDPConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||||
|
return 0, errors.New("not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (uc *ssUDPConn) RemoteAddr() net.Addr {
|
||||||
|
return uc.addr
|
||||||
}
|
}
|
||||||
|
@ -67,10 +67,6 @@ 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) (net.PacketConn, 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 {
|
||||||
|
63
udptun.go
63
udptun.go
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UDPTun struct
|
// UDPTun struct
|
||||||
@ -35,18 +34,7 @@ func (s *UDPTun) ListenAndServe() {
|
|||||||
|
|
||||||
logf("proxy-udptun listening UDP on %s", s.addr)
|
logf("proxy-udptun listening UDP on %s", s.addr)
|
||||||
|
|
||||||
if s.sDialer.Addr() == "DIRECT" {
|
|
||||||
s.ServeDirect(c)
|
|
||||||
} else {
|
|
||||||
s.ServeSS(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeDirect .
|
|
||||||
func (s *UDPTun) ServeDirect(c net.PacketConn) {
|
|
||||||
buf := make([]byte, udpBufSize)
|
buf := make([]byte, udpBufSize)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, clientAddr, err := c.ReadFrom(buf)
|
n, clientAddr, err := c.ReadFrom(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -57,57 +45,26 @@ func (s *UDPTun) ServeDirect(c net.PacketConn) {
|
|||||||
rc, err := s.sDialer.Dial("udp", s.raddr)
|
rc, err := s.sDialer.Dial("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)
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if urc, ok := rc.(*net.UDPConn); ok {
|
|
||||||
urc.Write(buf[:n])
|
|
||||||
go func() {
|
|
||||||
timedCopy(c, clientAddr, urc, 5*time.Minute, false)
|
|
||||||
urc.Close()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
logf("proxy-udptun %s <-> %s", clientAddr, s.raddr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeSS .
|
|
||||||
func (s *UDPTun) ServeSS(c net.PacketConn) {
|
|
||||||
// var nm sync.Map
|
|
||||||
buf := make([]byte, udpBufSize)
|
|
||||||
tgt := ParseAddr(s.raddr)
|
|
||||||
copy(buf, tgt)
|
|
||||||
|
|
||||||
for {
|
|
||||||
n, clientAddr, err := c.ReadFrom(buf[len(tgt):])
|
|
||||||
if err != nil {
|
|
||||||
logf("proxy-udptun read error: %v", err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
n, err = rc.Write(buf[:n])
|
||||||
rc, 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 rc.Write error: %v", err)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check here, get the correct sDialer's addr
|
buf = make([]byte, udpBufSize)
|
||||||
sUDPAddr, err := net.ResolveUDPAddr("udp", s.sDialer.Addr())
|
n, err = rc.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-udptun failed to ResolveUDPAddr %s", s.sDialer.Addr())
|
logf("proxy-udptun rc.Read error: %v", err)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
rc.WriteTo(buf[:len(tgt)+n], sUDPAddr)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
timedCopy(c, clientAddr, rc, 5*time.Minute, false)
|
|
||||||
rc.Close()
|
rc.Close()
|
||||||
}()
|
|
||||||
|
|
||||||
|
// logf("rc resp: \n%s", hex.Dump(buf[:n]))
|
||||||
|
|
||||||
|
c.WriteTo(buf[:n], clientAddr)
|
||||||
logf("proxy-udptun %s <-> %s", clientAddr, s.raddr)
|
logf("proxy-udptun %s <-> %s", clientAddr, s.raddr)
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,7 @@ func (s *UoTTun) ListenAndServe() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
rc, err := s.sDialer.Dial("uot", s.raddr)
|
||||||
rc, err := s.sDialer.Dial("udp", s.raddr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logf("proxy-uottun failed to connect to server %v: %v", s.raddr, err)
|
logf("proxy-uottun failed to connect to server %v: %v", s.raddr, err)
|
||||||
return
|
return
|
||||||
@ -73,6 +72,5 @@ func (s *UoTTun) ListenAndServe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
logf("proxy-uottun %s <-> %s", clientAddr, s.raddr)
|
logf("proxy-uottun %s <-> %s", clientAddr, s.raddr)
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user