mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
dns: use pool buffer if possible
This commit is contained in:
1 parent
45f4240f73
commit
76fddcc29c
8 files changed
+63
-35
No files matched your search
+16
-14
@@ -12,6 +12,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
@@ -66,12 +67,12 @@ func (s *SOCKS4) Dial(network, addr string) (net.Conn, error) {
|
||||
switch network {
|
||||
case "tcp", "tcp4":
|
||||
default:
|
||||
return nil, errors.New("[socks4]: no support for connection type " + network)
|
||||
return nil, errors.New("[socks4] no support for connection type " + network)
|
||||
}
|
||||
|
||||
c, err := s.dialer.Dial(network, s.addr)
|
||||
if err != nil {
|
||||
log.F("[socks4]: dial to %s error: %s", s.addr, err)
|
||||
log.F("[socks4] dial to %s error: %s", s.addr, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ func (s *SOCKS4) Dial(network, addr string) (net.Conn, error) {
|
||||
|
||||
// DialUDP connects to the given address via the proxy.
|
||||
func (s *SOCKS4) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
||||
return nil, nil, errors.New("DialUDP are not supported by Socks4")
|
||||
return nil, nil, errors.New("[socks4] DialUDP are not supported by Socks4")
|
||||
}
|
||||
|
||||
func (s *SOCKS4) lookupIP(host string) (ip net.IP, err error) {
|
||||
@@ -94,12 +95,12 @@ func (s *SOCKS4) lookupIP(host string) (ip net.IP, err error) {
|
||||
return
|
||||
}
|
||||
if len(ips) == 0 {
|
||||
err = errors.New("Cannot resolve host: " + host)
|
||||
err = errors.New("[socks4] Cannot resolve host: " + host)
|
||||
return
|
||||
}
|
||||
ip = ips[0].To4()
|
||||
if len(ip) != net.IPv4len {
|
||||
err = errors.New("IPv6 is not supported by socks4")
|
||||
err = errors.New("[socks4] IPv6 is not supported by socks4")
|
||||
return
|
||||
}
|
||||
return
|
||||
@@ -116,10 +117,10 @@ func (s *SOCKS4) connect(conn net.Conn, target string) error {
|
||||
|
||||
port, err := strconv.Atoi(portStr)
|
||||
if err != nil {
|
||||
return errors.New("proxy: failed to parse port number: " + portStr)
|
||||
return errors.New("[socks4] failed to parse port number: " + portStr)
|
||||
}
|
||||
if port < 1 || port > 0xffff {
|
||||
return errors.New("proxy: port number out of range: " + portStr)
|
||||
return errors.New("[socks4] port number out of range: " + portStr)
|
||||
}
|
||||
|
||||
ip, err := s.lookupIP(host)
|
||||
@@ -137,27 +138,28 @@ func (s *SOCKS4) connect(conn net.Conn, target string) error {
|
||||
0, // user id
|
||||
}
|
||||
|
||||
resp := make([]byte, 8)
|
||||
resp := pool.GetBuffer(8)
|
||||
defer pool.PutBuffer(resp)
|
||||
|
||||
if _, err := conn.Write(buf); err != nil {
|
||||
return errors.New("proxy: failed to write greeting to socks4 proxy at " + s.addr + ": " + err.Error())
|
||||
return errors.New("[socks4] failed to write greeting to socks4 proxy at " + s.addr + ": " + err.Error())
|
||||
}
|
||||
|
||||
if _, err := io.ReadFull(conn, resp); err != nil {
|
||||
return errors.New("proxy: failed to read greeting from socks4 proxy at " + s.addr + ": " + err.Error())
|
||||
return errors.New("[socks4] failed to read greeting from socks4 proxy at " + s.addr + ": " + err.Error())
|
||||
}
|
||||
|
||||
switch resp[1] {
|
||||
case 0x5a:
|
||||
// request granted
|
||||
case 0x5b:
|
||||
err = errors.New("Socks connection request rejected or failed")
|
||||
err = errors.New("[socks4] connection request rejected or failed")
|
||||
case 0x5c:
|
||||
err = errors.New("Socks connection request request failed because client is not running identd (or not reachable from the server)")
|
||||
err = errors.New("[socks4] connection request request failed because client is not running identd (or not reachable from the server)")
|
||||
case 0x5d:
|
||||
err = errors.New("Socks connection request request failed because client's identd could not confirm the user ID in the request")
|
||||
err = errors.New("[socks4] connection request request failed because client's identd could not confirm the user ID in the request")
|
||||
default:
|
||||
err = errors.New("Socks connection request failed, unknown error")
|
||||
err = errors.New("[socks4] connection request failed, unknown error")
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
+13
-4
@@ -5,6 +5,7 @@ import (
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
)
|
||||
|
||||
@@ -31,7 +32,8 @@ func NewPktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr socks.Addr, tgtHea
|
||||
|
||||
if ctrlConn != nil {
|
||||
go func() {
|
||||
buf := make([]byte, 1)
|
||||
buf := pool.GetBuffer(1)
|
||||
defer pool.PutBuffer(buf)
|
||||
for {
|
||||
_, err := ctrlConn.Read(buf)
|
||||
if err, ok := err.(net.Error); ok && err.Timeout() {
|
||||
@@ -52,7 +54,9 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
return pc.PacketConn.ReadFrom(b)
|
||||
}
|
||||
|
||||
buf := make([]byte, len(b))
|
||||
buf := pool.GetBuffer(len(b))
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
n, raddr, err := pc.PacketConn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
return n, raddr, err
|
||||
@@ -92,8 +96,13 @@ func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||
return pc.PacketConn.WriteTo(b, addr)
|
||||
}
|
||||
|
||||
buf := append([]byte{0, 0, 0}, pc.tgtAddr...)
|
||||
buf = append(buf, b[:]...)
|
||||
buf := pool.GetBuffer(3 + len(pc.tgtAddr) + len(b))
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
copy(buf, []byte{0, 0, 0})
|
||||
copy(buf[3:], pc.tgtAddr)
|
||||
copy(buf[3+len(pc.tgtAddr):], b)
|
||||
|
||||
return pc.PacketConn.WriteTo(buf, pc.writeAddr)
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
@@ -114,7 +115,8 @@ func (s *Socks5) Serve(c net.Conn) {
|
||||
if err != nil {
|
||||
// UDP: keep the connection until disconnect then free the UDP socket
|
||||
if err == socks.Errors[9] {
|
||||
buf := make([]byte, 1)
|
||||
buf := pool.GetBuffer(1)
|
||||
defer pool.PutBuffer(buf)
|
||||
// block here
|
||||
for {
|
||||
_, err := c.Read(buf)
|
||||
@@ -252,7 +254,9 @@ func (s *Socks5) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.A
|
||||
// send VER, NMETHODS, METHODS
|
||||
c.Write([]byte{Version, 1, 0})
|
||||
|
||||
buf := make([]byte, socks.MaxAddrLen)
|
||||
buf := pool.GetBuffer(socks.MaxAddrLen)
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
// read VER METHOD
|
||||
if _, err := io.ReadFull(c, buf[:2]); err != nil {
|
||||
return nil, nil, err
|
||||
|
||||
+7
-2
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
)
|
||||
|
||||
@@ -33,7 +34,9 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
return pc.PacketConn.ReadFrom(b)
|
||||
}
|
||||
|
||||
buf := make([]byte, len(b))
|
||||
buf := pool.GetBuffer(len(b))
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
n, raddr, err := pc.PacketConn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
return n, raddr, err
|
||||
@@ -63,7 +66,9 @@ func (pc *PktConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
||||
return pc.PacketConn.WriteTo(b, addr)
|
||||
}
|
||||
|
||||
buf := make([]byte, len(pc.tgtAddr)+len(b))
|
||||
buf := pool.GetBuffer(len(pc.tgtAddr) + len(b))
|
||||
pool.PutBuffer(buf)
|
||||
|
||||
copy(buf, pc.tgtAddr)
|
||||
copy(buf[len(pc.tgtAddr):], b)
|
||||
|
||||
|
||||
+6
-4
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
@@ -121,17 +122,18 @@ func (s *SS) Serve(c net.Conn) {
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
req := make([]byte, conn.UDPBufSize)
|
||||
n, err := c.Read(req)
|
||||
buf := pool.GetBuffer(conn.UDPBufSize)
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
n, err := c.Read(buf)
|
||||
if err != nil {
|
||||
log.F("[ss-uottun] error in ioutil.ReadAll: %s\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
tgtAddr, _ := net.ResolveUDPAddr("udp", tgt.String())
|
||||
rc.WriteTo(req[:n], tgtAddr)
|
||||
rc.WriteTo(buf[:n], tgtAddr)
|
||||
|
||||
buf := make([]byte, conn.UDPBufSize)
|
||||
n, _, err = rc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
log.F("[ss-uottun] read error: %v", err)
|
||||
|
||||
Reference in new issue
Block a user