proxy: added the ability to log forwarder

This commit is contained in:
nadoo committed 2019-09-18 12:53:04 +08:00
1 parent c2425e67de
commit 063dc1bc01
22 files changed
+138 -141

No files matched your search

+1 -1
View File
@@ -15,7 +15,7 @@ type Dialer interface {
Addr() string
// 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, proxy string, err error)
// DialUDP connects to the given address via the proxy.
DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error)
+2 -2
View File
@@ -39,7 +39,7 @@ func NewDirect(intface string) (*Direct, error) {
func (d *Direct) Addr() string { return "DIRECT" }
// Dial connects to the address addr on the network net
func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
func (d *Direct) Dial(network, addr string) (c net.Conn, p string, err error) {
if d.iface == nil || d.ip != nil {
c, err = dial(network, addr, d.ip)
if err == nil {
@@ -60,7 +60,7 @@ func (d *Direct) Dial(network, addr string) (c net.Conn, err error) {
err = errors.New("dial failed, maybe the interface link is down, please check it")
}
return
return c, "DIRECT", err
}
func dial(network, addr string, localIP net.IP) (net.Conn, error) {
-174
View File
@@ -1,174 +0,0 @@
package proxy
import (
"net"
"net/url"
"strconv"
"strings"
"sync/atomic"
"github.com/nadoo/glider/common/log"
)
// StatusHandler function will be called when the forwarder's status changed
type StatusHandler func(*Forwarder)
// Forwarder is a forwarder
type Forwarder struct {
Dialer
addr string
priority uint32
maxFailures uint32 // maxfailures to set to Disabled
disabled uint32
failures uint32
latency int64
intface string // local interface or ip address
handlers []StatusHandler
}
// ForwarderFromURL parses `forward=` command value and returns a new forwarder
func ForwarderFromURL(s, intface string) (f *Forwarder, err error) {
f = &Forwarder{}
ss := strings.Split(s, "#")
if len(ss) > 1 {
err = f.parseOption(ss[1])
}
iface := intface
if f.intface != "" && f.intface != intface {
iface = f.intface
}
var d Dialer
d, err = NewDirect(iface)
if err != nil {
return nil, err
}
for _, url := range strings.Split(ss[0], ",") {
d, err = DialerFromURL(url, d)
if err != nil {
return nil, err
}
}
f.Dialer = d
f.addr = d.Addr()
// set forwarder to disabled by default
// TODO: check here
f.Disable()
return f, err
}
func (f *Forwarder) parseOption(option string) error {
query, err := url.ParseQuery(option)
if err != nil {
return err
}
var priority uint64
p := query.Get("priority")
if p != "" {
priority, err = strconv.ParseUint(p, 10, 32)
}
f.SetPriority(uint32(priority))
f.intface = query.Get("interface")
return err
}
// Addr .
func (f *Forwarder) Addr() string {
return f.addr
}
// Dial .
func (f *Forwarder) Dial(network, addr string) (c net.Conn, err error) {
c, err = f.Dialer.Dial(network, addr)
if err != nil {
f.IncFailures()
if f.Failures() >= f.MaxFailures() && f.Enabled() {
f.Disable()
log.F("[forwarder] %s reaches maxfailures.", f.addr)
}
}
return c, err
}
// Failures returns the failuer count of forwarder
func (f *Forwarder) Failures() uint32 {
return atomic.LoadUint32(&f.failures)
}
// IncFailures increase the failuer count by 1
func (f *Forwarder) IncFailures() {
atomic.AddUint32(&f.failures, 1)
}
// AddHandler adds a custom handler to handle the status change event
func (f *Forwarder) AddHandler(h StatusHandler) {
f.handlers = append(f.handlers, h)
}
// Enable the forwarder
func (f *Forwarder) Enable() {
if atomic.CompareAndSwapUint32(&f.disabled, 1, 0) {
for _, h := range f.handlers {
h(f)
}
}
atomic.StoreUint32(&f.failures, 0)
}
// Disable the forwarder
func (f *Forwarder) Disable() {
if atomic.CompareAndSwapUint32(&f.disabled, 0, 1) {
for _, h := range f.handlers {
h(f)
}
}
}
// Enabled returns the status of forwarder
func (f *Forwarder) Enabled() bool {
return !isTrue(atomic.LoadUint32(&f.disabled))
}
func isTrue(n uint32) bool {
return n&1 == 1
}
// Priority returns the priority of forwarder
func (f *Forwarder) Priority() uint32 {
return atomic.LoadUint32(&f.priority)
}
// SetPriority sets the priority of forwarder
func (f *Forwarder) SetPriority(l uint32) {
atomic.StoreUint32(&f.priority, l)
}
// MaxFailures returns the maxFailures of forwarder
func (f *Forwarder) MaxFailures() uint32 {
return atomic.LoadUint32(&f.maxFailures)
}
// SetMaxFailures sets the maxFailures of forwarder
func (f *Forwarder) SetMaxFailures(l uint32) {
atomic.StoreUint32(&f.maxFailures, l)
}
// Latency returns the latency of forwarder
func (f *Forwarder) Latency() int64 {
return atomic.LoadInt64(&f.latency)
}
// SetLatency sets the latency of forwarder
func (f *Forwarder) SetLatency(l int64) {
atomic.StoreInt64(&f.latency, l)
}
+11 -13
View File
@@ -142,10 +142,10 @@ func (s *HTTP) Serve(c net.Conn) {
tgt += ":80"
}
rc, err := s.dialer.Dial("tcp", tgt)
rc, p, err := s.dialer.Dial("tcp", tgt)
if err != nil {
fmt.Fprintf(c, "%s 502 ERROR\r\n\r\n", proto)
log.F("[http] %s <-> %s, error in dial: %v", c.RemoteAddr(), tgt, err)
log.F("[http] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, p)
return
}
defer rc.Close()
@@ -192,7 +192,7 @@ func (s *HTTP) Serve(c net.Conn) {
writeFirstLine(&respBuf, proto, code, status)
writeHeaders(&respBuf, respHeader)
log.F("[http] %s <-> %s", c.RemoteAddr(), tgt)
log.F("[http] %s <-> %s, %s", c.RemoteAddr(), tgt, p)
c.Write(respBuf.Bytes())
io.Copy(c, respR)
@@ -200,7 +200,7 @@ func (s *HTTP) Serve(c net.Conn) {
}
func (s *HTTP) servHTTPS(method, requestURI, proto string, c net.Conn) {
rc, err := s.dialer.Dial("tcp", requestURI)
rc, p, err := s.dialer.Dial("tcp", requestURI)
if err != nil {
c.Write([]byte(proto))
c.Write([]byte(" 502 ERROR\r\n\r\n"))
@@ -210,7 +210,7 @@ func (s *HTTP) servHTTPS(method, requestURI, proto string, c net.Conn) {
c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
log.F("[http] %s <-> %s [c]", c.RemoteAddr(), requestURI)
log.F("[http] %s <-> %s [c], %s", c.RemoteAddr(), requestURI, p)
_, _, err = conn.Relay(c, rc)
if err != nil {
@@ -233,11 +233,11 @@ func (s *HTTP) Addr() string {
func (s *HTTP) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy
func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
rc, err := s.dialer.Dial(network, s.addr)
func (s *HTTP) Dial(network, addr string) (net.Conn, string, error) {
rc, p, err := s.dialer.Dial(network, s.addr)
if err != nil {
log.F("[http] dial to %s error: %s", s.addr, err)
return nil, err
return nil, p, err
}
var buf bytes.Buffer
@@ -254,7 +254,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
buf.Write([]byte("\r\n"))
_, err = rc.Write(buf.Bytes())
if err != nil {
return nil, err
return nil, p, err
}
c := conn.NewConn(rc)
@@ -262,7 +262,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
_, code, _, ok := parseFirstLine(tpr)
if ok && code == "200" {
tpr.ReadMIMEHeader()
return c, err
return c, p, err
}
if code == "407" {
@@ -271,7 +271,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
log.F("[http] 'CONNECT' method not allowed by proxy %s", s.addr)
}
return nil, errors.New("[http] can not connect remote address: " + addr + ". error code: " + code)
return nil, p, errors.New("[http] can not connect remote address: " + addr + ". error code: " + code)
}
// DialUDP connects to the given address via the proxy
@@ -282,9 +282,7 @@ func (s *HTTP) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Add
// 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) {
line, err := tp.ReadLine()
// log.F("first line: %s", line)
if err != nil {
// log.F("[http] read first line error:%s", err)
return
}
+3 -3
View File
@@ -197,12 +197,12 @@ func (s *KCP) Addr() string { return s.addr }
func (s *KCP) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy
func (s *KCP) Dial(network, addr string) (net.Conn, error) {
func (s *KCP) Dial(network, addr string) (net.Conn, string, error) {
// NOTE: kcp uses udp, we should dial remote server directly here
c, err := kcp.DialWithOptions(s.addr, s.block, s.dataShards, s.parityShards)
if err != nil {
log.F("[tls] dial to %s error: %s", s.addr, err)
return nil, err
return nil, "", err
}
// TODO: change them to customizable later?
@@ -217,7 +217,7 @@ func (s *KCP) Dial(network, addr string) (net.Conn, error) {
c.SetReadBuffer(4194304)
c.SetWriteBuffer(4194304)
return c, err
return c, "", err
}
// DialUDP connects to the given address via the proxy
+5 -4
View File
@@ -93,14 +93,15 @@ func (s *Obfs) Addr() string { return s.addr }
func (s *Obfs) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy
func (s *Obfs) Dial(network, addr string) (net.Conn, error) {
c, err := s.dialer.Dial("tcp", s.addr)
func (s *Obfs) Dial(network, addr string) (net.Conn, string, error) {
c, p, err := s.dialer.Dial("tcp", s.addr)
if err != nil {
log.F("[obfs] dial to %s error: %s", s.addr, err)
return nil, err
return nil, p, err
}
return s.obfsConn(c)
cc, e := s.obfsConn(c)
return cc, p, e
}
// DialUDP connects to the given address via the proxy
+3 -3
View File
@@ -104,14 +104,14 @@ func (s *RedirProxy) Serve(c net.Conn) {
return
}
rc, err := s.dialer.Dial("tcp", tgt.String())
rc, p, err := s.dialer.Dial("tcp", tgt.String())
if err != nil {
log.F("[redir] %s <-> %s, error in dial: %v", c.RemoteAddr(), tgt, err)
log.F("[redir] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, p)
return
}
defer rc.Close()
log.F("[redir] %s <-> %s", c.RemoteAddr(), tgt)
log.F("[redir] %s <-> %s, %s", c.RemoteAddr(), tgt, p)
_, _, err = conn.Relay(c, rc)
if err != nil {
+3 -1
View File
@@ -32,7 +32,9 @@ func (s *Reject) Addr() string { return "REJECT" }
func (s *Reject) NextDialer(dstAddr string) proxy.Dialer { return s }
// Dial connects to the address addr on the network net via the proxy.
func (s *Reject) Dial(network, addr string) (net.Conn, error) { return nil, errors.New("REJECT") }
func (s *Reject) Dial(network, addr string) (net.Conn, string, error) {
return nil, "REJECT", errors.New("REJECT")
}
// DialUDP connects to the given address via the proxy.
func (s *Reject) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
+10 -10
View File
@@ -131,14 +131,14 @@ func (s *SOCKS5) Serve(c net.Conn) {
return
}
rc, err := s.dialer.Dial("tcp", tgt.String())
rc, p, err := s.dialer.Dial("tcp", tgt.String())
if err != nil {
log.F("[socks5] %s <-> %s, error in dial: %v", c.RemoteAddr(), tgt, err)
log.F("[socks5] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, p)
return
}
defer rc.Close()
log.F("[socks5] %s <-> %s", c.RemoteAddr(), tgt)
log.F("[socks5] %s <-> %s, %s", c.RemoteAddr(), tgt, p)
_, _, err = conn.Relay(c, rc)
if err != nil {
@@ -224,30 +224,30 @@ func (s *SOCKS5) Addr() string {
func (s *SOCKS5) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the SOCKS5 proxy.
func (s *SOCKS5) Dial(network, addr string) (net.Conn, error) {
func (s *SOCKS5) Dial(network, addr string) (net.Conn, string, error) {
switch network {
case "tcp", "tcp6", "tcp4":
default:
return nil, errors.New("[socks5]: no support for connection type " + network)
return nil, "", errors.New("[socks5]: no support for connection type " + network)
}
c, err := s.dialer.Dial(network, s.addr)
c, p, err := s.dialer.Dial(network, s.addr)
if err != nil {
log.F("[socks5]: dial to %s error: %s", s.addr, err)
return nil, err
return nil, p, err
}
if err := s.connect(c, addr); err != nil {
c.Close()
return nil, err
return nil, p, err
}
return c, nil
return c, p, 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) {
c, err := s.dialer.Dial("tcp", s.addr)
c, _, err := s.dialer.Dial("tcp", s.addr)
if err != nil {
log.F("[socks5] dialudp dial tcp to %s error: %s", s.addr, err)
return nil, nil, err
+9 -9
View File
@@ -147,14 +147,14 @@ func (s *SS) Serve(c net.Conn) {
network = "udp"
}
rc, err := dialer.Dial(network, tgt.String())
rc, p, err := dialer.Dial(network, tgt.String())
if err != nil {
log.F("[ss] %s <-> %s, error in dial: %v", c.RemoteAddr(), tgt, err)
log.F("[ss] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, p)
return
}
defer rc.Close()
log.F("[ss] %s <-> %s", c.RemoteAddr(), tgt)
log.F("[ss] %s <-> %s, %s", c.RemoteAddr(), tgt, p)
_, _, err = conn.Relay(c, rc)
if err != nil {
@@ -242,29 +242,29 @@ func (s *SS) Addr() string {
func (s *SS) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// 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, string, error) {
target := socks.ParseAddr(addr)
if target == nil {
return nil, errors.New("[ss] unable to parse address: " + addr)
return nil, "", errors.New("[ss] unable to parse address: " + addr)
}
if network == "uot" {
target[0] = target[0] | 0x8
}
c, err := s.dialer.Dial("tcp", s.addr)
c, p, err := s.dialer.Dial("tcp", s.addr)
if err != nil {
log.F("[ss] dial to %s error: %s", s.addr, err)
return nil, err
return nil, p, err
}
c = s.StreamConn(c)
if _, err = c.Write(target); err != nil {
c.Close()
return nil, err
return nil, p, err
}
return c, err
return c, p, err
}
+10 -10
View File
@@ -81,26 +81,26 @@ func (s *SSR) Addr() string {
func (s *SSR) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy.
func (s *SSR) Dial(network, addr string) (net.Conn, error) {
func (s *SSR) Dial(network, addr string) (net.Conn, string, error) {
target := socks.ParseAddr(addr)
if target == nil {
return nil, errors.New("[ssr] unable to parse address: " + addr)
return nil, "", errors.New("[ssr] unable to parse address: " + addr)
}
cipher, err := shadowsocksr.NewStreamCipher(s.EncryptMethod, s.EncryptPassword)
if err != nil {
return nil, err
return nil, "", err
}
c, err := s.dialer.Dial("tcp", s.addr)
c, p, err := s.dialer.Dial("tcp", s.addr)
if err != nil {
log.F("[ssr] dial to %s error: %s", s.addr, err)
return nil, err
return nil, p, err
}
ssrconn := shadowsocksr.NewSSTCPConn(c, cipher)
if ssrconn.Conn == nil || ssrconn.RemoteAddr() == nil {
return nil, errors.New("[ssr] nil connection")
return nil, p, errors.New("[ssr] nil connection")
}
// should initialize obfs/protocol now
@@ -109,7 +109,7 @@ func (s *SSR) Dial(network, addr string) (net.Conn, error) {
ssrconn.IObfs = obfs.NewObfs(s.Obfs)
if ssrconn.IObfs == nil {
return nil, errors.New("[ssr] unsupported obfs type: " + s.Obfs)
return nil, p, errors.New("[ssr] unsupported obfs type: " + s.Obfs)
}
obfsServerInfo := &ssr.ServerInfoForObfs{
@@ -122,7 +122,7 @@ func (s *SSR) Dial(network, addr string) (net.Conn, error) {
ssrconn.IProtocol = protocol.NewProtocol(s.Protocol)
if ssrconn.IProtocol == nil {
return nil, errors.New("[ssr] unsupported protocol type: " + s.Protocol)
return nil, p, errors.New("[ssr] unsupported protocol type: " + s.Protocol)
}
protocolServerInfo := &ssr.ServerInfoForObfs{
@@ -145,10 +145,10 @@ func (s *SSR) Dial(network, addr string) (net.Conn, error) {
if _, err := ssrconn.Write(target); err != nil {
ssrconn.Close()
return nil, err
return nil, p, err
}
return ssrconn, err
return ssrconn, p, err
}
// DialUDP connects to the given address via the proxy.
+3 -3
View File
@@ -76,14 +76,14 @@ func (s *TCPTun) Serve(c net.Conn) {
c.SetKeepAlive(true)
}
rc, err := s.dialer.Dial("tcp", s.raddr)
rc, p, err := s.dialer.Dial("tcp", s.raddr)
if err != nil {
log.F("[tcptun] %s <-> %s, error in dial: %v", c.RemoteAddr(), s.addr, err)
log.F("[tcptun] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), s.addr, err, p)
return
}
defer rc.Close()
log.F("[tcptun] %s <-> %s", c.RemoteAddr(), s.raddr)
log.F("[tcptun] %s <-> %s, %s", c.RemoteAddr(), s.raddr, p)
_, _, err = conn.Relay(c, rc)
if err != nil {
+4 -4
View File
@@ -160,16 +160,16 @@ func (s *TLS) Addr() string { return s.addr }
func (s *TLS) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy
func (s *TLS) Dial(network, addr string) (net.Conn, error) {
cc, err := s.dialer.Dial("tcp", s.addr)
func (s *TLS) Dial(network, addr string) (net.Conn, string, error) {
cc, p, err := s.dialer.Dial("tcp", s.addr)
if err != nil {
log.F("[tls] dial to %s error: %s", s.addr, err)
return nil, err
return nil, p, err
}
c := stdtls.Client(cc, s.tlsConfig)
err = c.Handshake()
return c, err
return c, p, err
}
// DialUDP connects to the given address via the proxy
+3 -3
View File
@@ -113,14 +113,14 @@ func (s *Unix) Addr() string {
func (s *Unix) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy.
func (s *Unix) Dial(network, addr string) (net.Conn, error) {
func (s *Unix) Dial(network, addr string) (net.Conn, string, error) {
// NOTE: must be the first dialer in a chain
rc, err := net.Dial("unix", s.addr)
if err != nil {
return nil, err
return nil, "", err
}
return rc, err
return rc, "", err
}
// DialUDP connects to the given address via the proxy
+2 -2
View File
@@ -69,7 +69,7 @@ func (s *UoTTun) ListenAndServe() {
continue
}
rc, err := s.dialer.Dial("uot", s.raddr)
rc, p, err := s.dialer.Dial("uot", s.raddr)
if err != nil {
log.F("[uottun] failed to connect to server %v: %v", s.raddr, err)
continue
@@ -99,7 +99,7 @@ func (s *UoTTun) ListenAndServe() {
continue
}
log.F("[uottun] %s <-> %s", clientAddr, s.raddr)
log.F("[uottun] %s <-> %s, %s", clientAddr, s.raddr, p)
}
}
+5 -4
View File
@@ -90,13 +90,14 @@ func (s *VMess) Addr() string {
func (s *VMess) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy.
func (s *VMess) Dial(network, addr string) (net.Conn, error) {
rc, err := s.dialer.Dial("tcp", s.addr)
func (s *VMess) Dial(network, addr string) (net.Conn, string, error) {
rc, p, err := s.dialer.Dial("tcp", s.addr)
if err != nil {
return nil, err
return nil, p, err
}
return s.client.NewConn(rc, addr)
cc, e := s.client.NewConn(rc, addr)
return cc, p, e
}
// DialUDP connects to the given address via the proxy.
+5 -4
View File
@@ -76,13 +76,14 @@ func (s *WS) Addr() string {
func (s *WS) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) }
// Dial connects to the address addr on the network net via the proxy.
func (s *WS) Dial(network, addr string) (net.Conn, error) {
rc, err := s.dialer.Dial("tcp", s.addr)
func (s *WS) Dial(network, addr string) (net.Conn, string, error) {
rc, p, err := s.dialer.Dial("tcp", s.addr)
if err != nil {
return nil, err
return nil, p, err
}
return s.client.NewConn(rc, addr)
cc, e := s.client.NewConn(rc, addr)
return cc, p, e
}
// DialUDP connects to the given address via the proxy.