mirror of
https://github.com/nadoo/glider.git
synced 2026-11-19 07:09:42 +08:00
proxy: move conn to proxy package
This commit is contained in:
1 parent
47e20cf7f4
commit
bd358e4ae6
14 files changed
+33
-42
No files matched your search
+191
@@ -0,0 +1,191 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
)
|
||||
|
||||
const (
|
||||
// TCPBufSize is the size of tcp buffer.
|
||||
TCPBufSize = 16 << 10
|
||||
|
||||
// UDPBufSize is the size of udp buffer.
|
||||
UDPBufSize = 64 << 10
|
||||
)
|
||||
|
||||
// Conn is a connection with buffered reader.
|
||||
type Conn struct {
|
||||
r *bufio.Reader
|
||||
net.Conn
|
||||
}
|
||||
|
||||
// NewConn returns a new conn.
|
||||
func NewConn(c net.Conn) *Conn {
|
||||
return &Conn{bufio.NewReader(c), c}
|
||||
}
|
||||
|
||||
// Peek returns the next n bytes without advancing the reader.
|
||||
func (c *Conn) Peek(n int) ([]byte, error) {
|
||||
return c.r.Peek(n)
|
||||
}
|
||||
|
||||
func (c *Conn) Read(p []byte) (int, error) {
|
||||
return c.r.Read(p)
|
||||
}
|
||||
|
||||
// Reader returns the internal bufio.Reader.
|
||||
func (c *Conn) Reader() *bufio.Reader {
|
||||
return c.r
|
||||
}
|
||||
|
||||
// Relay relays between left and right.
|
||||
func Relay(left, right net.Conn) error {
|
||||
var err, err1 error
|
||||
var wg sync.WaitGroup
|
||||
var wait = 5 * time.Second
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, err1 = Copy(right, left)
|
||||
right.SetReadDeadline(time.Now().Add(wait)) // unblock read on right
|
||||
}()
|
||||
|
||||
_, err = Copy(left, right)
|
||||
left.SetReadDeadline(time.Now().Add(wait)) // unblock read on left
|
||||
wg.Wait()
|
||||
|
||||
if err1 != nil && !errors.Is(err1, os.ErrDeadlineExceeded) { // requires Go 1.15+
|
||||
return err1
|
||||
}
|
||||
|
||||
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func worthReadFrom(src io.Reader) bool {
|
||||
switch v := src.(type) {
|
||||
case *net.TCPConn:
|
||||
return true
|
||||
case *net.UnixConn:
|
||||
return true
|
||||
case *os.File:
|
||||
fi, err := v.Stat()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return fi.Mode().IsRegular()
|
||||
case *io.LimitedReader:
|
||||
return worthReadFrom(v.R)
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Copy copies from src to dst.
|
||||
// it will try to avoid memory allocating by using WriteTo or ReadFrom method,
|
||||
// if both failed, then it'll fallback to call CopyBuffer method.
|
||||
func Copy(dst io.Writer, src io.Reader) (written int64, err error) {
|
||||
if wt, ok := src.(io.WriterTo); ok {
|
||||
return wt.WriteTo(dst)
|
||||
}
|
||||
|
||||
if rt, ok := dst.(io.ReaderFrom); ok && worthReadFrom(src) {
|
||||
return rt.ReadFrom(src)
|
||||
}
|
||||
|
||||
return CopyBuffer(dst, src)
|
||||
}
|
||||
|
||||
// CopyN copies n bytes (or until an error) from src to dst.
|
||||
func CopyN(dst io.Writer, src io.Reader, n int64) (written int64, err error) {
|
||||
written, err = Copy(dst, io.LimitReader(src, n))
|
||||
if written == n {
|
||||
return n, nil
|
||||
}
|
||||
if written < n && err == nil {
|
||||
// src stopped early; must have been EOF.
|
||||
err = io.EOF
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// CopyBuffer copies from src to dst with a userspace buffer.
|
||||
func CopyBuffer(dst io.Writer, src io.Reader) (written int64, err error) {
|
||||
size := TCPBufSize
|
||||
if l, ok := src.(*io.LimitedReader); ok && int64(size) > l.N {
|
||||
if l.N < 1 {
|
||||
size = 1
|
||||
} else {
|
||||
size = int(l.N)
|
||||
}
|
||||
}
|
||||
|
||||
buf := pool.GetBuffer(size)
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
for {
|
||||
nr, er := src.Read(buf)
|
||||
if nr > 0 {
|
||||
nw, ew := dst.Write(buf[0:nr])
|
||||
if nw > 0 {
|
||||
written += int64(nw)
|
||||
}
|
||||
if ew != nil {
|
||||
err = ew
|
||||
break
|
||||
}
|
||||
if nr != nw {
|
||||
err = io.ErrShortWrite
|
||||
break
|
||||
}
|
||||
}
|
||||
if er != nil {
|
||||
if er != io.EOF {
|
||||
err = er
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return written, err
|
||||
}
|
||||
|
||||
// RelayUDP copys from src to dst at target with read timeout.
|
||||
func RelayUDP(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout time.Duration) error {
|
||||
b := pool.GetBuffer(UDPBufSize)
|
||||
defer pool.PutBuffer(b)
|
||||
|
||||
for {
|
||||
src.SetReadDeadline(time.Now().Add(timeout))
|
||||
n, _, err := src.ReadFrom(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = dst.WriteTo(b[:n], target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// OutboundIP returns preferred outbound ip of this machine.
|
||||
func OutboundIP() string {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
return conn.LocalAddr().(*net.UDPAddr).IP.String()
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"net"
|
||||
"net/textproto"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
@@ -51,7 +50,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := conn.NewConn(rc)
|
||||
c := proxy.NewConn(rc)
|
||||
tpr := textproto.NewReader(c.Reader())
|
||||
line, err := tpr.ReadLine()
|
||||
if err != nil {
|
||||
|
||||
+9
-10
@@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
@@ -46,15 +45,15 @@ func (s *HTTP) ListenAndServe() {
|
||||
func (s *HTTP) Serve(cc net.Conn) {
|
||||
defer cc.Close()
|
||||
|
||||
var c *conn.Conn
|
||||
var c *proxy.Conn
|
||||
switch cc := cc.(type) {
|
||||
case *conn.Conn:
|
||||
case *proxy.Conn:
|
||||
c = cc
|
||||
case *net.TCPConn:
|
||||
cc.SetKeepAlive(true)
|
||||
c = conn.NewConn(cc)
|
||||
c = proxy.NewConn(cc)
|
||||
default:
|
||||
c = conn.NewConn(cc)
|
||||
c = proxy.NewConn(cc)
|
||||
}
|
||||
|
||||
req, err := parseRequest(c.Reader())
|
||||
@@ -72,7 +71,7 @@ func (s *HTTP) Serve(cc net.Conn) {
|
||||
s.servRequest(req, c)
|
||||
}
|
||||
|
||||
func (s *HTTP) servRequest(req *request, c *conn.Conn) {
|
||||
func (s *HTTP) servRequest(req *request, c *proxy.Conn) {
|
||||
// Auth
|
||||
if s.user != "" && s.password != "" {
|
||||
if user, pass, ok := extractUserPass(req.auth); !ok || user != s.user || pass != s.password {
|
||||
@@ -103,7 +102,7 @@ func (s *HTTP) servHTTPS(r *request, c net.Conn) {
|
||||
|
||||
log.F("[http] %s <-> %s [c] via %s", c.RemoteAddr(), r.uri, dialer.Addr())
|
||||
|
||||
if err = conn.Relay(c, rc); err != nil {
|
||||
if err = proxy.Relay(c, rc); err != nil {
|
||||
log.F("[http] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), r.uri, dialer.Addr(), err)
|
||||
// record remote conn failure only
|
||||
if !strings.Contains(err.Error(), s.addr) {
|
||||
@@ -112,7 +111,7 @@ func (s *HTTP) servHTTPS(r *request, c net.Conn) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *HTTP) servHTTP(req *request, c *conn.Conn) {
|
||||
func (s *HTTP) servHTTP(req *request, c *proxy.Conn) {
|
||||
rc, dialer, err := s.proxy.Dial("tcp", req.target)
|
||||
if err != nil {
|
||||
fmt.Fprintf(c, "%s 502 ERROR\r\n\r\n", req.proto)
|
||||
@@ -134,7 +133,7 @@ func (s *HTTP) servHTTP(req *request, c *conn.Conn) {
|
||||
// copy the left request bytes to remote server. eg. length specificed or chunked body.
|
||||
go func() {
|
||||
if _, err := c.Reader().Peek(1); err == nil {
|
||||
conn.Copy(rc, c)
|
||||
proxy.Copy(rc, c)
|
||||
rc.SetDeadline(time.Now())
|
||||
c.SetDeadline(time.Now())
|
||||
}
|
||||
@@ -168,5 +167,5 @@ func (s *HTTP) servHTTP(req *request, c *conn.Conn) {
|
||||
log.F("[http] %s <-> %s via %s", c.RemoteAddr(), req.target, dialer.Addr())
|
||||
c.Write(buf.Bytes())
|
||||
|
||||
conn.Copy(c, r)
|
||||
proxy.Copy(c, r)
|
||||
}
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"net"
|
||||
"net/url"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
"github.com/nadoo/glider/proxy/http"
|
||||
@@ -87,7 +86,7 @@ func (m *Mixed) Serve(c net.Conn) {
|
||||
c.SetKeepAlive(true)
|
||||
}
|
||||
|
||||
cc := conn.NewConn(c)
|
||||
cc := proxy.NewConn(c)
|
||||
head, err := cc.Peek(1)
|
||||
if err != nil {
|
||||
// log.F("[mixed] socks5 peek error: %s", err)
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
@@ -114,7 +113,7 @@ func (s *RedirProxy) Serve(c net.Conn) {
|
||||
|
||||
log.F("[redir] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())
|
||||
|
||||
if err = conn.Relay(c, rc); err != nil {
|
||||
if err = proxy.Relay(c, rc); err != nil {
|
||||
log.F("[redir] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
|
||||
// record remote conn failure only
|
||||
if !strings.Contains(err.Error(), s.addr) {
|
||||
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
@@ -142,7 +141,7 @@ func (s *Socks5) Serve(c net.Conn) {
|
||||
|
||||
log.F("[socks5] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())
|
||||
|
||||
if err = conn.Relay(c, rc); err != nil {
|
||||
if err = proxy.Relay(c, rc); err != nil {
|
||||
log.F("[socks5] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
|
||||
// record remote conn failure only
|
||||
if !strings.Contains(err.Error(), s.addr) {
|
||||
@@ -163,7 +162,7 @@ func (s *Socks5) ListenAndServeUDP() {
|
||||
log.F("[socks5-udp] listening UDP on %s", s.addr)
|
||||
|
||||
var nm sync.Map
|
||||
buf := make([]byte, conn.UDPBufSize)
|
||||
buf := make([]byte, proxy.UDPBufSize)
|
||||
|
||||
for {
|
||||
c := NewPktConn(lc, nil, nil, true, nil)
|
||||
@@ -192,7 +191,7 @@ func (s *Socks5) ListenAndServeUDP() {
|
||||
nm.Store(raddr.String(), pc)
|
||||
|
||||
go func() {
|
||||
conn.RelayUDP(c, raddr, pc, 2*time.Minute)
|
||||
proxy.RelayUDP(c, raddr, pc, 2*time.Minute)
|
||||
pc.Close()
|
||||
nm.Delete(raddr.String())
|
||||
}()
|
||||
|
||||
+4
-5
@@ -10,7 +10,6 @@ import (
|
||||
|
||||
"github.com/nadoo/go-shadowsocks2/core"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
@@ -122,7 +121,7 @@ func (s *SS) Serve(c net.Conn) {
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
buf := pool.GetBuffer(conn.UDPBufSize)
|
||||
buf := pool.GetBuffer(proxy.UDPBufSize)
|
||||
defer pool.PutBuffer(buf)
|
||||
|
||||
n, err := c.Read(buf)
|
||||
@@ -160,7 +159,7 @@ func (s *SS) Serve(c net.Conn) {
|
||||
|
||||
log.F("[ss] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())
|
||||
|
||||
if err = conn.Relay(c, rc); err != nil {
|
||||
if err = proxy.Relay(c, rc); err != nil {
|
||||
log.F("[ss] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
|
||||
// record remote conn failure only
|
||||
if !strings.Contains(err.Error(), s.addr) {
|
||||
@@ -183,7 +182,7 @@ func (s *SS) ListenAndServeUDP() {
|
||||
log.F("[ss-udp] listening UDP on %s", s.addr)
|
||||
|
||||
var nm sync.Map
|
||||
buf := make([]byte, conn.UDPBufSize)
|
||||
buf := make([]byte, proxy.UDPBufSize)
|
||||
|
||||
for {
|
||||
c := NewPktConn(lc, nil, nil, true)
|
||||
@@ -207,7 +206,7 @@ func (s *SS) ListenAndServeUDP() {
|
||||
nm.Store(raddr.String(), pc)
|
||||
|
||||
go func() {
|
||||
conn.RelayUDP(c, raddr, pc, 2*time.Minute)
|
||||
proxy.RelayUDP(c, raddr, pc, 2*time.Minute)
|
||||
pc.Close()
|
||||
nm.Delete(raddr.String())
|
||||
}()
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
@@ -90,7 +89,7 @@ func (s *TCPTun) Serve(c net.Conn) {
|
||||
|
||||
log.F("[tcptun] %s <-> %s via %s", c.RemoteAddr(), s.raddr, dialer.Addr())
|
||||
|
||||
if err = conn.Relay(c, rc); err != nil {
|
||||
if err = proxy.Relay(c, rc); err != nil {
|
||||
log.F("[tcptun] %s <-> %s via %s, relay error: %v", c.RemoteAddr(), s.raddr, dialer.Addr(), err)
|
||||
// record remote conn failure only
|
||||
if !strings.Contains(err.Error(), s.addr) {
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/common/socks"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
// PktConn .
|
||||
@@ -41,7 +41,7 @@ func (pc *PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
}
|
||||
|
||||
length := int(binary.BigEndian.Uint16(b[:2]))
|
||||
if length > conn.UDPBufSize {
|
||||
if length > proxy.UDPBufSize {
|
||||
return 0, nil, errors.New("packet invalid")
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
@@ -66,7 +65,7 @@ func (s *UDPTun) ListenAndServe() {
|
||||
log.F("[udptun] listening UDP on %s", s.addr)
|
||||
|
||||
var nm sync.Map
|
||||
buf := make([]byte, conn.UDPBufSize)
|
||||
buf := make([]byte, proxy.UDPBufSize)
|
||||
|
||||
for {
|
||||
n, raddr, err := c.ReadFrom(buf)
|
||||
@@ -88,7 +87,7 @@ func (s *UDPTun) ListenAndServe() {
|
||||
nm.Store(raddr.String(), pc)
|
||||
|
||||
go func(c, pc net.PacketConn, raddr net.Addr) {
|
||||
conn.RelayUDP(c, raddr, pc, 2*time.Minute)
|
||||
proxy.RelayUDP(c, raddr, pc, 2*time.Minute)
|
||||
pc.Close()
|
||||
nm.Delete(raddr.String())
|
||||
}(c, pc, raddr)
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/log"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
@@ -64,7 +63,7 @@ func (s *UoTTun) ListenAndServe() {
|
||||
|
||||
log.F("[uottun] listening UDP on %s", s.addr)
|
||||
|
||||
buf := make([]byte, conn.UDPBufSize)
|
||||
buf := make([]byte, proxy.UDPBufSize)
|
||||
|
||||
for {
|
||||
n, clientAddr, err := c.ReadFrom(buf)
|
||||
@@ -82,7 +81,7 @@ func (s *UoTTun) ListenAndServe() {
|
||||
go func() {
|
||||
// no remote forwarder, just a local udp forwarder
|
||||
if urc, ok := rc.(*net.UDPConn); ok {
|
||||
conn.RelayUDP(c, clientAddr, urc, 2*time.Minute)
|
||||
proxy.RelayUDP(c, clientAddr, urc, 2*time.Minute)
|
||||
urc.Close()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/nadoo/glider/common/conn"
|
||||
"github.com/nadoo/glider/common/pool"
|
||||
"github.com/nadoo/glider/proxy"
|
||||
)
|
||||
|
||||
const Version byte = 0
|
||||
@@ -69,7 +69,7 @@ func (c *Conn) Read(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
if addLen := int64(buf[1]); addLen > 0 {
|
||||
conn.CopyN(ioutil.Discard, c.Conn, addLen)
|
||||
proxy.CopyN(ioutil.Discard, c.Conn, addLen)
|
||||
}
|
||||
c.rcved = true
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user