log: show proxy info in log, via PROXY

This commit is contained in:
nadoo 2019-09-18 22:08:48 +08:00
parent edcb21236a
commit d6d706b5ee
17 changed files with 124 additions and 110 deletions

View File

@ -73,14 +73,14 @@ func (c *Client) Exchange(reqBytes []byte, clientAddr string, preferTCP bool) ([
} }
} }
dnsServer, network, respBytes, err := c.exchange(req.Question.QNAME, reqBytes, preferTCP) dnsServer, network, dialerAddr, respBytes, err := c.exchange(req.Question.QNAME, reqBytes, preferTCP)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if req.Question.QTYPE != QTypeA && req.Question.QTYPE != QTypeAAAA { if req.Question.QTYPE != QTypeA && req.Question.QTYPE != QTypeAAAA {
log.F("[dns] %s <-> %s(%s), type: %d, %s", log.F("[dns] %s <-> %s(%s) via %s, type: %d, %s",
clientAddr, dnsServer, network, req.Question.QTYPE, req.Question.QNAME) clientAddr, dnsServer, network, dialerAddr, req.Question.QTYPE, req.Question.QNAME)
return respBytes, nil return respBytes, nil
} }
@ -116,14 +116,14 @@ func (c *Client) Exchange(reqBytes []byte, clientAddr string, preferTCP bool) ([
c.cache.Put(getKey(resp.Question), respBytes, ttl) c.cache.Put(getKey(resp.Question), respBytes, ttl)
} }
log.F("[dns] %s <-> %s(%s), type: %d, %s: %s", log.F("[dns] %s <-> %s(%s) via %s, type: %d, %s: %s",
clientAddr, dnsServer, network, resp.Question.QTYPE, resp.Question.QNAME, strings.Join(ips, ",")) clientAddr, dnsServer, network, dialerAddr, resp.Question.QTYPE, resp.Question.QNAME, strings.Join(ips, ","))
return respBytes, nil return respBytes, nil
} }
// exchange choose a upstream dns server based on qname, communicate with it on the network. // exchange choose a upstream dns server based on qname, communicate with it on the network.
func (c *Client) exchange(qname string, reqBytes []byte, preferTCP bool) (server, network string, respBytes []byte, err error) { func (c *Client) exchange(qname string, reqBytes []byte, preferTCP bool) (server, network, dialerAddr string, respBytes []byte, err error) {
// use tcp to connect upstream server default // use tcp to connect upstream server default
network = "tcp" network = "tcp"
dialer := c.proxy.NextDialer(qname + ":53") dialer := c.proxy.NextDialer(qname + ":53")
@ -167,7 +167,7 @@ func (c *Client) exchange(qname string, reqBytes []byte, preferTCP bool) (server
log.F("[dns] failed to exchange with server %v: %v", server, err) log.F("[dns] failed to exchange with server %v: %v", server, err)
} }
return server, network, respBytes, err return server, network, dialer.Addr(), respBytes, err
} }
// exchangeTCP exchange with server over tcp. // exchangeTCP exchange with server over tcp.

View File

@ -22,8 +22,8 @@ type Server struct {
} }
// NewServer returns a new dns server. // NewServer returns a new dns server.
func NewServer(addr string, proxy proxy.Proxy, config *Config) (*Server, error) { func NewServer(addr string, p proxy.Proxy, config *Config) (*Server, error) {
c, err := NewClient(proxy, config) c, err := NewClient(p, config)
s := &Server{ s := &Server{
addr: addr, addr: addr,
Client: c, Client: c,

View File

@ -21,7 +21,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// HTTP struct // HTTP struct.
type HTTP struct { type HTTP struct {
dialer proxy.Dialer dialer proxy.Dialer
proxy proxy.Proxy proxy proxy.Proxy
@ -36,7 +36,7 @@ func init() {
proxy.RegisterServer("http", NewHTTPServer) proxy.RegisterServer("http", NewHTTPServer)
} }
// NewHTTP returns a http proxy // NewHTTP returns a http proxy.
func NewHTTP(s string, d proxy.Dialer, p proxy.Proxy) (*HTTP, error) { func NewHTTP(s string, d proxy.Dialer, p proxy.Proxy) (*HTTP, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
@ -65,17 +65,17 @@ func NewHTTP(s string, d proxy.Dialer, p proxy.Proxy) (*HTTP, error) {
return h, nil return h, nil
} }
// NewHTTPDialer returns a http proxy dialer // NewHTTPDialer returns a http proxy dialer.
func NewHTTPDialer(s string, d proxy.Dialer) (proxy.Dialer, error) { func NewHTTPDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {
return NewHTTP(s, d, nil) return NewHTTP(s, d, nil)
} }
// NewHTTPServer returns a http proxy server // NewHTTPServer returns a http proxy server.
func NewHTTPServer(s string, p proxy.Proxy) (proxy.Server, error) { func NewHTTPServer(s string, p proxy.Proxy) (proxy.Server, error) {
return NewHTTP(s, nil, p) return NewHTTP(s, nil, p)
} }
// ListenAndServe . // ListenAndServe listens on server's addr and serves connections.
func (s *HTTP) ListenAndServe() { func (s *HTTP) ListenAndServe() {
l, err := net.Listen("tcp", s.addr) l, err := net.Listen("tcp", s.addr)
if err != nil { if err != nil {
@ -97,7 +97,7 @@ func (s *HTTP) ListenAndServe() {
} }
} }
// Serve . // Serve serves a connection.
func (s *HTTP) Serve(c net.Conn) { func (s *HTTP) Serve(c net.Conn) {
defer c.Close() defer c.Close()
@ -147,7 +147,7 @@ func (s *HTTP) Serve(c net.Conn) {
rc, p, err := s.proxy.Dial("tcp", tgt) rc, p, err := s.proxy.Dial("tcp", tgt)
if err != nil { if err != nil {
fmt.Fprintf(c, "%s 502 ERROR\r\n\r\n", proto) fmt.Fprintf(c, "%s 502 ERROR\r\n\r\n", proto)
log.F("[http] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, p) log.F("[http] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, p, err)
return return
} }
defer rc.Close() defer rc.Close()
@ -165,7 +165,7 @@ func (s *HTTP) Serve(c net.Conn) {
// send request to remote server // send request to remote server
rc.Write(reqBuf.Bytes()) rc.Write(reqBuf.Bytes())
// copy the left request bytes to remote server. eg. length specificed or chunked body. // copy the left request bytes to remote server. eg. length specificed or chunked body
go func() { go func() {
if _, err := reqR.Peek(1); err == nil { if _, err := reqR.Peek(1); err == nil {
io.Copy(rc, reqR) io.Copy(rc, reqR)
@ -183,7 +183,7 @@ func (s *HTTP) Serve(c net.Conn) {
respHeader, err := respTP.ReadMIMEHeader() respHeader, err := respTP.ReadMIMEHeader()
if err != nil { if err != nil {
log.F("[http] read header error:%s", err) log.F("[http] %s <-> %s via %s, read header error: %v", c.RemoteAddr(), tgt, p, err)
return return
} }
@ -194,7 +194,7 @@ func (s *HTTP) Serve(c net.Conn) {
writeFirstLine(&respBuf, proto, code, status) writeFirstLine(&respBuf, proto, code, status)
writeHeaders(&respBuf, respHeader) writeHeaders(&respBuf, respHeader)
log.F("[http] %s <-> %s, %s", c.RemoteAddr(), tgt, p) log.F("[http] %s <-> %s via %s", c.RemoteAddr(), tgt, p)
c.Write(respBuf.Bytes()) c.Write(respBuf.Bytes())
io.Copy(c, respR) io.Copy(c, respR)
@ -206,24 +206,24 @@ func (s *HTTP) servHTTPS(method, requestURI, proto string, c net.Conn) {
if err != nil { if err != nil {
c.Write([]byte(proto)) c.Write([]byte(proto))
c.Write([]byte(" 502 ERROR\r\n\r\n")) c.Write([]byte(" 502 ERROR\r\n\r\n"))
log.F("[http] %s <-> %s [c], error in dial: %v", c.RemoteAddr(), requestURI, err) log.F("[http] %s <-> %s [c] via %s, error in dial: %v", c.RemoteAddr(), requestURI, p, err)
return return
} }
c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
log.F("[http] %s <-> %s [c], %s", c.RemoteAddr(), requestURI, p) log.F("[http] %s <-> %s [c] via %s", c.RemoteAddr(), requestURI, p)
_, _, err = conn.Relay(c, rc) _, _, err = conn.Relay(c, rc)
if err != nil { if err != nil {
if err, ok := err.(net.Error); ok && err.Timeout() { if err, ok := err.(net.Error); ok && err.Timeout() {
return // ignore i/o timeout return // ignore i/o timeout
} }
log.F("relay error: %v", err) log.F("[http] relay error: %v", err)
} }
} }
// Addr returns forwarder's address // Addr returns forwarder's address.
func (s *HTTP) Addr() string { func (s *HTTP) Addr() string {
if s.addr == "" { if s.addr == "" {
return s.dialer.Addr() return s.dialer.Addr()
@ -231,7 +231,7 @@ func (s *HTTP) Addr() string {
return s.addr return s.addr
} }
// 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.dialer.Dial(network, s.addr) rc, err := s.dialer.Dial(network, s.addr)
if err != nil { if err != nil {
@ -273,12 +273,12 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) {
return nil, errors.New("[http] can not connect remote address: " + addr + ". error code: " + code) return nil, errors.New("[http] can not connect remote address: " + addr + ". error code: " + code)
} }
// DialUDP connects to the given address via the proxy // DialUDP connects to the given address via the proxy.
func (s *HTTP) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) { func (s *HTTP) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
return nil, nil, errors.New("http client does not support udp") return nil, nil, errors.New("http client does not support udp")
} }
// 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()
if err != nil { if err != nil {

View File

@ -15,7 +15,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// KCP struct // KCP struct.
type KCP struct { type KCP struct {
dialer proxy.Dialer dialer proxy.Dialer
proxy proxy.Proxy proxy proxy.Proxy
@ -36,7 +36,7 @@ func init() {
proxy.RegisterServer("kcp", NewKCPServer) proxy.RegisterServer("kcp", NewKCPServer)
} }
// NewKCP returns a kcp proxy struct // NewKCP returns a kcp proxy struct.
func NewKCP(s string, d proxy.Dialer, p proxy.Proxy) (*KCP, error) { func NewKCP(s string, d proxy.Dialer, p proxy.Proxy) (*KCP, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
@ -124,12 +124,12 @@ func NewKCP(s string, d proxy.Dialer, p proxy.Proxy) (*KCP, error) {
return k, nil return k, nil
} }
// NewKCPDialer returns a kcp proxy dialer // NewKCPDialer returns a kcp proxy dialer.
func NewKCPDialer(s string, d proxy.Dialer) (proxy.Dialer, error) { func NewKCPDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {
return NewKCP(s, d, nil) return NewKCP(s, d, nil)
} }
// NewKCPServer returns a kcp proxy server // NewKCPServer returns a kcp proxy server.
func NewKCPServer(s string, p proxy.Proxy) (proxy.Server, error) { func NewKCPServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",") transport := strings.Split(s, ",")
@ -152,7 +152,7 @@ func NewKCPServer(s string, p proxy.Proxy) (proxy.Server, error) {
return k, nil return k, nil
} }
// ListenAndServe . // ListenAndServe listens on server's addr and serves connections.
func (s *KCP) ListenAndServe() { func (s *KCP) ListenAndServe() {
l, err := kcp.ListenWithOptions(s.addr, s.block, s.dataShards, s.parityShards) l, err := kcp.ListenWithOptions(s.addr, s.block, s.dataShards, s.parityShards)
if err != nil { if err != nil {
@ -182,7 +182,7 @@ func (s *KCP) ListenAndServe() {
} }
} }
// Serve serves requests // Serve serves connections.
func (s *KCP) Serve(c net.Conn) { func (s *KCP) Serve(c net.Conn) {
// we know the internal server will close the connection after serve // we know the internal server will close the connection after serve
// defer c.Close() // defer c.Close()
@ -192,10 +192,15 @@ func (s *KCP) Serve(c net.Conn) {
} }
} }
// Addr returns forwarder's address // Addr returns forwarder's address.
func (s *KCP) Addr() string { return s.addr } func (s *KCP) Addr() string {
if s.addr == "" {
return s.dialer.Addr()
}
return s.addr
}
// 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 *KCP) Dial(network, addr string) (net.Conn, error) { func (s *KCP) Dial(network, addr string) (net.Conn, error) {
// NOTE: kcp uses udp, we should dial remote server directly here // NOTE: kcp uses udp, we should dial remote server directly here
c, err := kcp.DialWithOptions(s.addr, s.block, s.dataShards, s.parityShards) c, err := kcp.DialWithOptions(s.addr, s.block, s.dataShards, s.parityShards)
@ -219,7 +224,7 @@ func (s *KCP) Dial(network, addr string) (net.Conn, error) {
return c, err return c, err
} }
// DialUDP connects to the given address via the proxy // DialUDP connects to the given address via the proxy.
func (s *KCP) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { func (s *KCP) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
return nil, nil, errors.New("kcp client does not support udp now") return nil, nil, errors.New("kcp client does not support udp now")
} }

View File

@ -25,8 +25,8 @@ var httpMethods = [...][]byte{
[]byte("PATCH"), []byte("PATCH"),
} }
// MixedProxy struct // Mixed struct.
type MixedProxy struct { type Mixed struct {
proxy proxy.Proxy proxy proxy.Proxy
addr string addr string
@ -35,18 +35,18 @@ type MixedProxy struct {
} }
func init() { func init() {
proxy.RegisterServer("mixed", NewMixedProxyServer) proxy.RegisterServer("mixed", NewMixedServer)
} }
// NewMixedProxy returns a mixed proxy // NewMixed returns a mixed proxy.
func NewMixedProxy(s string, p proxy.Proxy) (*MixedProxy, error) { func NewMixed(s string, p proxy.Proxy) (*Mixed, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
log.F("parse err: %s", err) log.F("parse err: %s", err)
return nil, err return nil, err
} }
m := &MixedProxy{ m := &Mixed{
proxy: p, proxy: p,
addr: u.Host, addr: u.Host,
} }
@ -57,22 +57,22 @@ func NewMixedProxy(s string, p proxy.Proxy) (*MixedProxy, error) {
return m, nil return m, nil
} }
// NewMixedProxyServer returns a mixed proxy server // NewMixedServer returns a mixed server.
func NewMixedProxyServer(s string, p proxy.Proxy) (proxy.Server, error) { func NewMixedServer(s string, p proxy.Proxy) (proxy.Server, error) {
return NewMixedProxy(s, p) return NewMixed(s, p)
} }
// ListenAndServe . // ListenAndServe listens on server's addr and serves connections.
func (p *MixedProxy) ListenAndServe() { func (m *Mixed) ListenAndServe() {
go p.socks5.ListenAndServeUDP() go m.socks5.ListenAndServeUDP()
l, err := net.Listen("tcp", p.addr) l, err := net.Listen("tcp", m.addr)
if err != nil { if err != nil {
log.F("[mixed] failed to listen on %s: %v", p.addr, err) log.F("[mixed] failed to listen on %s: %v", m.addr, err)
return return
} }
log.F("[mixed] listening TCP on %s", p.addr) log.F("[mixed] listening TCP on %s", m.addr)
for { for {
c, err := l.Accept() c, err := l.Accept()
@ -81,12 +81,12 @@ func (p *MixedProxy) ListenAndServe() {
continue continue
} }
go p.Serve(c) go m.Serve(c)
} }
} }
// Serve . // Serve serves connections.
func (p *MixedProxy) Serve(c net.Conn) { func (m *Mixed) Serve(c net.Conn) {
defer c.Close() defer c.Close()
if c, ok := c.(*net.TCPConn); ok { if c, ok := c.(*net.TCPConn); ok {
@ -95,7 +95,7 @@ func (p *MixedProxy) Serve(c net.Conn) {
cc := conn.NewConn(c) cc := conn.NewConn(c)
if p.socks5 != nil { if m.socks5 != nil {
head, err := cc.Peek(1) head, err := cc.Peek(1)
if err != nil { if err != nil {
// log.F("[mixed] socks5 peek error: %s", err) // log.F("[mixed] socks5 peek error: %s", err)
@ -104,12 +104,12 @@ func (p *MixedProxy) Serve(c net.Conn) {
// check socks5, client send socksversion: 5 as the first byte // check socks5, client send socksversion: 5 as the first byte
if head[0] == socks5.Version { if head[0] == socks5.Version {
p.socks5.Serve(cc) m.socks5.Serve(cc)
return return
} }
} }
if p.http != nil { if m.http != nil {
head, err := cc.Peek(8) head, err := cc.Peek(8)
if err != nil { if err != nil {
log.F("[mixed] http peek error: %s", err) log.F("[mixed] http peek error: %s", err)
@ -118,7 +118,7 @@ func (p *MixedProxy) Serve(c net.Conn) {
for _, method := range httpMethods { for _, method := range httpMethods {
if bytes.HasPrefix(head, method) { if bytes.HasPrefix(head, method) {
p.http.Serve(cc) m.http.Serve(cc)
return return
} }
} }

View File

@ -10,7 +10,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// Obfs struct // Obfs struct.
type Obfs struct { type Obfs struct {
dialer proxy.Dialer dialer proxy.Dialer
addr string addr string
@ -27,7 +27,7 @@ func init() {
proxy.RegisterDialer("simple-obfs", NewObfsDialer) proxy.RegisterDialer("simple-obfs", NewObfsDialer)
} }
// NewObfs returns a proxy struct // NewObfs returns a proxy struct.
func NewObfs(s string, d proxy.Dialer) (*Obfs, error) { func NewObfs(s string, d proxy.Dialer) (*Obfs, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
@ -81,15 +81,20 @@ func NewObfs(s string, d proxy.Dialer) (*Obfs, error) {
return p, nil return p, nil
} }
// NewObfsDialer returns a proxy dialer // NewObfsDialer returns a proxy dialer.
func NewObfsDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) { func NewObfsDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) {
return NewObfs(s, dialer) return NewObfs(s, dialer)
} }
// Addr returns forwarder's address // Addr returns forwarder's address.
func (s *Obfs) Addr() string { return s.addr } func (s *Obfs) Addr() string {
if s.addr == "" {
return s.dialer.Addr()
}
return s.addr
}
// 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 *Obfs) Dial(network, addr string) (net.Conn, error) { func (s *Obfs) Dial(network, addr string) (net.Conn, error) {
c, err := s.dialer.Dial("tcp", s.addr) c, err := s.dialer.Dial("tcp", s.addr)
if err != nil { if err != nil {
@ -97,11 +102,10 @@ func (s *Obfs) Dial(network, addr string) (net.Conn, error) {
return nil, err return nil, err
} }
cc, e := s.obfsConn(c) return s.obfsConn(c)
return cc, e
} }
// DialUDP connects to the given address via the proxy // DialUDP connects to the given address via the proxy.
func (s *Obfs) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { func (s *Obfs) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
return nil, nil, errors.New("obfs client does not support udp now") return nil, nil, errors.New("obfs client does not support udp now")
} }

View File

@ -23,7 +23,7 @@ const (
IP6T_SO_ORIGINAL_DST = 80 IP6T_SO_ORIGINAL_DST = 80
) )
// RedirProxy struct // RedirProxy struct.
type RedirProxy struct { type RedirProxy struct {
proxy proxy.Proxy proxy proxy.Proxy
addr string addr string
@ -63,7 +63,7 @@ func NewRedir6Server(s string, p proxy.Proxy) (proxy.Server, error) {
return NewRedirProxy(s, p, true) return NewRedirProxy(s, p, true)
} }
// ListenAndServe . // ListenAndServe listens on server's addr and serves connections.
func (s *RedirProxy) ListenAndServe() { func (s *RedirProxy) ListenAndServe() {
l, err := net.Listen("tcp", s.addr) l, err := net.Listen("tcp", s.addr)
if err != nil { if err != nil {
@ -84,7 +84,7 @@ func (s *RedirProxy) ListenAndServe() {
} }
} }
// Serve . // Serve serves connections.
func (s *RedirProxy) Serve(c net.Conn) { func (s *RedirProxy) Serve(c net.Conn) {
defer c.Close() defer c.Close()
@ -106,12 +106,12 @@ func (s *RedirProxy) Serve(c net.Conn) {
rc, p, err := s.proxy.Dial("tcp", tgt.String()) rc, p, err := s.proxy.Dial("tcp", tgt.String())
if err != nil { if err != nil {
log.F("[redir] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, p) log.F("[redir] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, p, err)
return return
} }
defer rc.Close() defer rc.Close()
log.F("[redir] %s <-> %s, %s", c.RemoteAddr(), tgt, p) log.F("[redir] %s <-> %s via %s", c.RemoteAddr(), tgt, p)
_, _, err = conn.Relay(c, rc) _, _, err = conn.Relay(c, rc)
if err != nil { if err != nil {

View File

@ -15,7 +15,7 @@ func init() {
proxy.RegisterDialer("reject", NewRejectDialer) proxy.RegisterDialer("reject", NewRejectDialer)
} }
// NewReject returns a reject proxy, reject:// // NewReject returns a reject proxy, reject://.
func NewReject(s string, d proxy.Dialer) (*Reject, error) { func NewReject(s string, d proxy.Dialer) (*Reject, error) {
return &Reject{}, nil return &Reject{}, nil
} }

View File

@ -135,12 +135,12 @@ func (s *Socks5) Serve(c net.Conn) {
rc, p, err := s.proxy.Dial("tcp", tgt.String()) rc, p, err := s.proxy.Dial("tcp", tgt.String())
if err != nil { if err != nil {
log.F("[socks5] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, p) log.F("[socks5] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, p, err)
return return
} }
defer rc.Close() defer rc.Close()
log.F("[socks5] %s <-> %s, %s", c.RemoteAddr(), tgt, p) log.F("[socks5] %s <-> %s via %s", c.RemoteAddr(), tgt, p)
_, _, err = conn.Relay(c, rc) _, _, err = conn.Relay(c, rc)
if err != nil { if err != nil {

View File

@ -151,12 +151,12 @@ func (s *SS) Serve(c net.Conn) {
rc, err := dialer.Dial(network, tgt.String()) rc, err := dialer.Dial(network, tgt.String())
if err != nil { if err != nil {
log.F("[ss] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), tgt, err, dialer.Addr()) log.F("[ss] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), tgt, dialer.Addr(), err)
return return
} }
defer rc.Close() defer rc.Close()
log.F("[ss] %s <-> %s, %s", c.RemoteAddr(), tgt, dialer.Addr()) log.F("[ss] %s <-> %s via %s", c.RemoteAddr(), tgt, dialer.Addr())
_, _, err = conn.Relay(c, rc) _, _, err = conn.Relay(c, rc)
if err != nil { if err != nil {

View File

@ -17,7 +17,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// SSR . // SSR struct.
type SSR struct { type SSR struct {
dialer proxy.Dialer dialer proxy.Dialer
addr string addr string

View File

@ -10,7 +10,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// TCPTun struct // TCPTun struct.
type TCPTun struct { type TCPTun struct {
proxy proxy.Proxy proxy proxy.Proxy
addr string addr string
@ -22,7 +22,7 @@ func init() {
proxy.RegisterServer("tcptun", NewTCPTunServer) proxy.RegisterServer("tcptun", NewTCPTunServer)
} }
// NewTCPTun returns a tcptun proxy // NewTCPTun returns a tcptun proxy.
func NewTCPTun(s string, p proxy.Proxy) (*TCPTun, error) { func NewTCPTun(s string, p proxy.Proxy) (*TCPTun, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
@ -42,12 +42,12 @@ func NewTCPTun(s string, p proxy.Proxy) (*TCPTun, error) {
return t, nil return t, nil
} }
// NewTCPTunServer returns a udp tunnel server // NewTCPTunServer returns a udp tunnel server.
func NewTCPTunServer(s string, p proxy.Proxy) (proxy.Server, error) { func NewTCPTunServer(s string, p proxy.Proxy) (proxy.Server, error) {
return NewTCPTun(s, p) return NewTCPTun(s, p)
} }
// ListenAndServe . // ListenAndServe listens on server's addr and serves connections.
func (s *TCPTun) ListenAndServe() { func (s *TCPTun) ListenAndServe() {
l, err := net.Listen("tcp", s.addr) l, err := net.Listen("tcp", s.addr)
if err != nil { if err != nil {
@ -68,7 +68,7 @@ func (s *TCPTun) ListenAndServe() {
} }
} }
// Serve . // Serve serves a connection.
func (s *TCPTun) Serve(c net.Conn) { func (s *TCPTun) Serve(c net.Conn) {
defer c.Close() defer c.Close()
@ -78,12 +78,12 @@ func (s *TCPTun) Serve(c net.Conn) {
rc, p, err := s.proxy.Dial("tcp", s.raddr) rc, p, err := s.proxy.Dial("tcp", s.raddr)
if err != nil { if err != nil {
log.F("[tcptun] %s <-> %s, %s, error in dial: %v", c.RemoteAddr(), s.addr, err, p) log.F("[tcptun] %s <-> %s via %s, error in dial: %v", c.RemoteAddr(), s.addr, p, err)
return return
} }
defer rc.Close() defer rc.Close()
log.F("[tcptun] %s <-> %s, %s", c.RemoteAddr(), s.raddr, p) log.F("[tcptun] %s <-> %s via %s", c.RemoteAddr(), s.raddr, p)
_, _, err = conn.Relay(c, rc) _, _, err = conn.Relay(c, rc)
if err != nil { if err != nil {

View File

@ -11,7 +11,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// TLS struct // TLS struct.
type TLS struct { type TLS struct {
dialer proxy.Dialer dialer proxy.Dialer
proxy proxy.Proxy proxy proxy.Proxy
@ -34,7 +34,7 @@ func init() {
proxy.RegisterServer("tls", NewTLSServer) proxy.RegisterServer("tls", NewTLSServer)
} }
// NewTLS returns a tls proxy struct // NewTLS returns a tls proxy struct.
func NewTLS(s string, d proxy.Dialer, p proxy.Proxy) (*TLS, error) { func NewTLS(s string, d proxy.Dialer, p proxy.Proxy) (*TLS, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
@ -70,7 +70,7 @@ func NewTLS(s string, d proxy.Dialer, p proxy.Proxy) (*TLS, error) {
return t, nil return t, nil
} }
// NewTLSDialer returns a tls proxy dialer // NewTLSDialer returns a tls proxy dialer.
func NewTLSDialer(s string, d proxy.Dialer) (proxy.Dialer, error) { func NewTLSDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {
p, err := NewTLS(s, d, nil) p, err := NewTLS(s, d, nil)
if err != nil { if err != nil {
@ -87,7 +87,7 @@ func NewTLSDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {
return p, err return p, err
} }
// NewTLSServer returns a tls transport layer before the real server // NewTLSServer returns a tls transport layer before the real server.
func NewTLSServer(s string, p proxy.Proxy) (proxy.Server, error) { func NewTLSServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",") transport := strings.Split(s, ",")
@ -121,7 +121,7 @@ func NewTLSServer(s string, p proxy.Proxy) (proxy.Server, error) {
return t, nil return t, nil
} }
// ListenAndServe . // ListenAndServe listens on server's addr and serves connections.
func (s *TLS) ListenAndServe() { func (s *TLS) ListenAndServe() {
l, err := net.Listen("tcp", s.addr) l, err := net.Listen("tcp", s.addr)
if err != nil { if err != nil {
@ -143,7 +143,7 @@ func (s *TLS) ListenAndServe() {
} }
} }
// Serve serves requests // Serve serves a connection.
func (s *TLS) Serve(c net.Conn) { func (s *TLS) Serve(c net.Conn) {
// we know the internal server will close the connection after serve // we know the internal server will close the connection after serve
// defer c.Close() // defer c.Close()
@ -154,10 +154,15 @@ func (s *TLS) Serve(c net.Conn) {
} }
} }
// Addr returns forwarder's address // Addr returns forwarder's address.
func (s *TLS) Addr() string { return s.addr } func (s *TLS) Addr() string {
if s.addr == "" {
return s.dialer.Addr()
}
return s.addr
}
// 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 *TLS) Dial(network, addr string) (net.Conn, error) { func (s *TLS) Dial(network, addr string) (net.Conn, error) {
cc, err := s.dialer.Dial("tcp", s.addr) cc, err := s.dialer.Dial("tcp", s.addr)
if err != nil { if err != nil {
@ -170,7 +175,7 @@ func (s *TLS) Dial(network, addr string) (net.Conn, error) {
return c, err return c, err
} }
// DialUDP connects to the given address via the proxy // DialUDP connects to the given address via the proxy.
func (s *TLS) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { func (s *TLS) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
return nil, nil, errors.New("tls client does not support udp now") return nil, nil, errors.New("tls client does not support udp now")
} }

View File

@ -17,7 +17,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// TProxy struct // TProxy struct.
type TProxy struct { type TProxy struct {
proxy proxy.Proxy proxy proxy.Proxy
addr string addr string
@ -50,7 +50,7 @@ func NewTProxyServer(s string, p proxy.Proxy) (proxy.Server, error) {
return NewTProxy(s, p) return NewTProxy(s, p)
} }
// ListenAndServe . // ListenAndServe listens on server's addr and serves connections.
func (s *TProxy) ListenAndServe() { func (s *TProxy) ListenAndServe() {
// go s.ListenAndServeTCP() // go s.ListenAndServeTCP()
s.ListenAndServeUDP() s.ListenAndServeUDP()

View File

@ -11,7 +11,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// Unix domain socket struct // Unix domain socket struct.
type Unix struct { type Unix struct {
dialer proxy.Dialer dialer proxy.Dialer
proxy proxy.Proxy proxy proxy.Proxy
@ -25,7 +25,7 @@ func init() {
proxy.RegisterDialer("unix", NewUnixDialer) proxy.RegisterDialer("unix", NewUnixDialer)
} }
// NewUnix returns unix fomain socket proxy // NewUnix returns unix fomain socket proxy.
func NewUnix(s string, d proxy.Dialer, p proxy.Proxy) (*Unix, error) { func NewUnix(s string, d proxy.Dialer, p proxy.Proxy) (*Unix, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {
@ -42,12 +42,12 @@ func NewUnix(s string, d proxy.Dialer, p proxy.Proxy) (*Unix, error) {
return unix, nil return unix, nil
} }
// NewUnixDialer returns a unix domain socket dialer // NewUnixDialer returns a unix domain socket dialer.
func NewUnixDialer(s string, d proxy.Dialer) (proxy.Dialer, error) { func NewUnixDialer(s string, d proxy.Dialer) (proxy.Dialer, error) {
return NewUnix(s, d, nil) return NewUnix(s, d, nil)
} }
// NewUnixServer returns a unix domain socket server // NewUnixServer returns a unix domain socket server.
func NewUnixServer(s string, p proxy.Proxy) (proxy.Server, error) { func NewUnixServer(s string, p proxy.Proxy) (proxy.Server, error) {
transport := strings.Split(s, ",") transport := strings.Split(s, ",")
@ -70,7 +70,7 @@ func NewUnixServer(s string, p proxy.Proxy) (proxy.Server, error) {
return unix, nil return unix, nil
} }
// ListenAndServe serves requests // ListenAndServe serves requests.
func (s *Unix) ListenAndServe() { func (s *Unix) ListenAndServe() {
os.Remove(s.addr) os.Remove(s.addr)
l, err := net.Listen("unix", s.addr) l, err := net.Listen("unix", s.addr)
@ -80,12 +80,12 @@ func (s *Unix) ListenAndServe() {
} }
defer l.Close() defer l.Close()
log.F("[uinx] listening on %s", s.addr) log.F("[unix] listening on %s", s.addr)
for { for {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
log.F("[uinx] failed to accept: %v", err) log.F("[unix] failed to accept: %v", err)
continue continue
} }
@ -93,7 +93,7 @@ func (s *Unix) ListenAndServe() {
} }
} }
// Serve serves requests // Serve serves requests.
func (s *Unix) Serve(c net.Conn) { func (s *Unix) Serve(c net.Conn) {
// we know the internal server will close the connection after serve // we know the internal server will close the connection after serve
// defer c.Close() // defer c.Close()
@ -103,7 +103,7 @@ func (s *Unix) Serve(c net.Conn) {
} }
} }
// Addr returns forwarder's address // Addr returns forwarder's address.
func (s *Unix) Addr() string { func (s *Unix) Addr() string {
if s.addr == "" { if s.addr == "" {
return s.dialer.Addr() return s.dialer.Addr()
@ -117,7 +117,7 @@ func (s *Unix) Dial(network, addr string) (net.Conn, error) {
return net.Dial("unix", s.addr) return net.Dial("unix", s.addr)
} }
// DialUDP connects to the given address via the proxy // DialUDP connects to the given address via the proxy.
func (s *Unix) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { func (s *Unix) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) {
return nil, nil, errors.New("unix domain socket client does not support udp now") return nil, nil, errors.New("unix domain socket client does not support udp now")
} }

View File

@ -99,7 +99,7 @@ func (s *UoTTun) ListenAndServe() {
continue continue
} }
log.F("[uottun] %s <-> %s, %s", clientAddr, s.raddr, p) log.F("[uottun] %s <-> %s via %s", clientAddr, s.raddr, p)
} }
} }

View File

@ -10,7 +10,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// VMess . // VMess struct.
type VMess struct { type VMess struct {
dialer proxy.Dialer dialer proxy.Dialer
addr string addr string
@ -78,7 +78,7 @@ func NewVMessDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) {
return NewVMess(s, dialer) return NewVMess(s, dialer)
} }
// Addr returns forwarder's address // Addr returns forwarder's address.
func (s *VMess) Addr() string { func (s *VMess) Addr() string {
if s.addr == "" { if s.addr == "" {
return s.dialer.Addr() return s.dialer.Addr()