From 768f97bead32b51b7f6312a010ad655c47745ba2 Mon Sep 17 00:00:00 2001 From: "rui.zheng" Date: Wed, 14 Sep 2016 22:53:06 +0800 Subject: [PATCH] fix log title --- conn.go | 42 +++++++++++++++++------- forward.go | 94 ++++++++++++++++++++++++------------------------------ socks.go | 32 +++++++++---------- 3 files changed, 88 insertions(+), 80 deletions(-) diff --git a/conn.go b/conn.go index 9f91681a..cec23381 100644 --- a/conn.go +++ b/conn.go @@ -87,16 +87,16 @@ func listenAndServe(arg Args) error { } func listenAndServeTcpForward(arg Args) error { - ln, err := net.Listen("tcp", arg.Addr) + raddr, err := net.ResolveTCPAddr("tcp", arg.Remote) if err != nil { return err } - defer ln.Close() - raddr, err := net.ResolveTCPAddr("tcp", arg.Remote) + ln, err := net.Listen("tcp", arg.Addr) if err != nil { return err } + defer ln.Close() for { conn, err := ln.Accept() @@ -121,7 +121,7 @@ func listenAndServeUdpForward(arg Args) error { conn, err := net.ListenUDP("udp", laddr) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", laddr, raddr, err) return err } defer conn.Close() @@ -132,7 +132,7 @@ func listenAndServeUdpForward(arg Args) error { n, addr, err := conn.ReadFromUDP(b) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", laddr, raddr, err) continue } go func() { @@ -149,7 +149,7 @@ func listenAndServeUdpForward(arg Args) error { b := make([]byte, 32*1024) n, addr, err := conn.ReadFromUDP(b) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", laddr, raddr, err) return } @@ -166,11 +166,11 @@ func listenAndServeUdpForward(arg Args) error { dgram := <-wChan addr, err := net.ResolveUDPAddr("udp", dgram.Header.Addr.String()) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s <- %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s <- %s : %s", laddr, raddr, err) continue // drop silently } if _, err = conn.WriteToUDP(dgram.Data, addr); err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s <- %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s <- %s : %s", laddr, raddr, err) return } } @@ -186,6 +186,15 @@ func serveRTcpForward(arg Args) error { return errors.New("rtcp: at least one -F must be assigned") } + laddr, err := net.ResolveTCPAddr("tcp", arg.Addr) + if err != nil { + return err + } + raddr, err := net.ResolveTCPAddr("tcp", arg.Remote) + if err != nil { + return err + } + retry := 0 for { conn, _, err := forwardChain(forwardArgs...) @@ -199,9 +208,9 @@ func serveRTcpForward(arg Args) error { } retry = 0 - if err := connectRTcpForward(conn, arg); err != nil { + if err := connectRTcpForward(conn, laddr, raddr); err != nil { conn.Close() - time.Sleep(10 * time.Second) + time.Sleep(6 * time.Second) } } } @@ -211,6 +220,15 @@ func serveRUdpForward(arg Args) error { return errors.New("rudp: at least one -F must be assigned") } + laddr, err := net.ResolveUDPAddr("udp", arg.Addr) + if err != nil { + return err + } + raddr, err := net.ResolveUDPAddr("udp", arg.Remote) + if err != nil { + return err + } + retry := 0 for { conn, _, err := forwardChain(forwardArgs...) @@ -224,9 +242,9 @@ func serveRUdpForward(arg Args) error { } retry = 0 - if err := connectRUdpForward(conn, arg); err != nil { + if err := connectRUdpForward(conn, laddr, raddr); err != nil { conn.Close() - time.Sleep(10 * time.Second) + time.Sleep(6 * time.Second) } } } diff --git a/forward.go b/forward.go index d35e6d84..bf807b04 100644 --- a/forward.go +++ b/forward.go @@ -12,45 +12,45 @@ import ( func handleTcpForward(conn net.Conn, raddr net.Addr) { defer conn.Close() - glog.V(LINFO).Infof("[tcp-forward] %s - %s", conn.RemoteAddr(), raddr) + glog.V(LINFO).Infof("[tcp] %s - %s", conn.RemoteAddr(), raddr) c, err := Connect(raddr.String()) if err != nil { - glog.V(LWARNING).Infof("[tcp-forward] %s -> %s : %s", conn.RemoteAddr(), raddr, err) + glog.V(LWARNING).Infof("[tcp] %s -> %s : %s", conn.RemoteAddr(), raddr, err) return } defer c.Close() - glog.V(LINFO).Infof("[tcp-forward] %s <-> %s", conn.RemoteAddr(), raddr) + glog.V(LINFO).Infof("[tcp] %s <-> %s", conn.RemoteAddr(), raddr) Transport(conn, c) - glog.V(LINFO).Infof("[tcp-forward] %s >-< %s", conn.RemoteAddr(), raddr) + glog.V(LINFO).Infof("[tcp] %s >-< %s", conn.RemoteAddr(), raddr) } func handleUdpForwardLocal(conn *net.UDPConn, laddr, raddr *net.UDPAddr, data []byte) { lconn, err := net.ListenUDP("udp", nil) if err != nil { - glog.V(LWARNING).Infof("[udp-forward] %s -> %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", laddr, raddr, err) return } defer lconn.Close() if _, err := lconn.WriteToUDP(data, raddr); err != nil { - glog.V(LWARNING).Infof("[udp-forward] %s -> %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", laddr, raddr, err) return } - glog.V(LDEBUG).Infof("[udp-forward] %s >>> %s length %d", laddr, raddr, len(data)) + glog.V(LDEBUG).Infof("[udp] %s >>> %s length %d", laddr, raddr, len(data)) b := udpPool.Get().([]byte) defer udpPool.Put(b) lconn.SetReadDeadline(time.Now().Add(time.Second * 60)) n, addr, err := lconn.ReadFromUDP(b) if err != nil { - glog.V(LWARNING).Infof("[udp-forward] %s <- %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s <- %s : %s", laddr, raddr, err) return } - glog.V(LDEBUG).Infof("[udp-forward] %s <<< %s length %d", laddr, addr, n) + glog.V(LDEBUG).Infof("[udp] %s <<< %s length %d", laddr, addr, n) if _, err := conn.WriteToUDP(b[:n], laddr); err != nil { - glog.V(LWARNING).Infof("[udp-forward] %s <- %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s <- %s : %s", laddr, raddr, err) } return } @@ -91,7 +91,7 @@ func handleUdpForwardTunnel(laddr, raddr *net.UDPAddr, rChan, wChan chan *gosock for { tun, err = prepareUdpConnectTunnel(raddr) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", laddr, raddr, err) time.Sleep((1 << uint(retry)) * time.Second) if retry < 5 { retry++ @@ -101,7 +101,7 @@ func handleUdpForwardTunnel(laddr, raddr *net.UDPAddr, rChan, wChan chan *gosock break } - glog.V(LINFO).Infof("[udp-connect] %s <-> %s", laddr, raddr) + glog.V(LINFO).Infof("[udp] %s <-> %s", laddr, raddr) rExit := make(chan interface{}) rErr, wErr := make(chan error, 1), make(chan error, 1) @@ -111,7 +111,7 @@ func handleUdpForwardTunnel(laddr, raddr *net.UDPAddr, rChan, wChan chan *gosock select { case dgram := <-rChan: if err := dgram.Write(tun); err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", laddr, raddr, err) rErr <- err return } @@ -126,7 +126,7 @@ func handleUdpForwardTunnel(laddr, raddr *net.UDPAddr, rChan, wChan chan *gosock for { dgram, err := gosocks5.ReadUDPDatagram(tun) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s <- %s : %s", laddr, raddr, err) + glog.V(LWARNING).Infof("[udp] %s <- %s : %s", laddr, raddr, err) close(rExit) wErr <- err return @@ -146,17 +146,15 @@ func handleUdpForwardTunnel(laddr, raddr *net.UDPAddr, rChan, wChan chan *gosock case <-wErr: //log.Println("r exit", err) } - glog.V(LINFO).Infof("[udp-connect] %s >-< %s", laddr, raddr) + glog.V(LINFO).Infof("[udp] %s >-< %s", laddr, raddr) } -func connectRTcpForward(conn net.Conn, arg Args) error { - glog.V(LINFO).Infof("[rtcp] %s - %s", arg.Addr, arg.Remote) +func connectRTcpForward(conn net.Conn, laddr, raddr net.Addr) error { + glog.V(LINFO).Infof("[rtcp] %s - %s", laddr, raddr) - addr, _ := net.ResolveTCPAddr("tcp", arg.Addr) - req := gosocks5.NewRequest(gosocks5.CmdBind, ToSocksAddr(addr)) - bindAddr := req.Addr + req := gosocks5.NewRequest(gosocks5.CmdBind, ToSocksAddr(laddr)) if err := req.Write(conn); err != nil { - glog.V(LWARNING).Infof("[rtcp] %s -> %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rtcp] %s -> %s : %s", laddr, raddr, err) return err } @@ -164,33 +162,33 @@ func connectRTcpForward(conn net.Conn, arg Args) error { conn.SetReadDeadline(time.Now().Add(90 * time.Second)) rep, err := gosocks5.ReadReply(conn) if err != nil { - glog.V(LWARNING).Infof("[rtcp] %s -> %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rtcp] %s -> %s : %s", laddr, raddr, err) return err } conn.SetReadDeadline(time.Time{}) if rep.Rep != gosocks5.Succeeded { - glog.V(LWARNING).Infof("[rtcp] %s -> %s : bind on %s failure", bindAddr, arg.Remote, arg.Addr) - return errors.New("Bind on " + arg.Addr + " failure") + glog.V(LWARNING).Infof("[rtcp] %s -> %s : bind on %s failure", laddr, raddr, laddr) + return errors.New("Bind on " + laddr.String() + " failure") } - glog.V(LINFO).Infof("[rtcp] %s - %s BIND ON %s OK", bindAddr, arg.Remote, rep.Addr) + glog.V(LINFO).Infof("[rtcp] %s - %s BIND ON %s OK", laddr, raddr, rep.Addr) // second reply, peer connection rep, err = gosocks5.ReadReply(conn) if err != nil { - glog.V(LWARNING).Infof("[rtcp] %s -> %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rtcp] %s -> %s : %s", laddr, raddr, err) return err } if rep.Rep != gosocks5.Succeeded { - glog.V(LWARNING).Infof("[rtcp] %s -> %s : peer connect failure", bindAddr, arg.Remote) + glog.V(LWARNING).Infof("[rtcp] %s -> %s : peer connect failure", laddr, raddr) return errors.New("peer connect failure") } - glog.V(LINFO).Infof("[rtcp] %s -> %s PEER %s CONNECTED", bindAddr, arg.Remote, rep.Addr) + glog.V(LINFO).Infof("[rtcp] %s -> %s PEER %s CONNECTED", laddr, raddr, rep.Addr) go func() { defer conn.Close() - lconn, err := net.Dial("tcp", arg.Remote) + lconn, err := net.DialTimeout("tcp", raddr.String(), time.Second*180) if err != nil { glog.V(LWARNING).Infof("[rtcp] %s -> %s : %s", rep.Addr, lconn.RemoteAddr(), err) return @@ -205,15 +203,13 @@ func connectRTcpForward(conn net.Conn, arg Args) error { return nil } -func connectRUdpForward(conn net.Conn, arg Args) error { - glog.V(LINFO).Infof("[rudp] %s - %s", arg.Addr, arg.Remote) +func connectRUdpForward(conn net.Conn, laddr, raddr *net.UDPAddr) error { + glog.V(LINFO).Infof("[rudp] %s - %s", laddr, raddr) - addr, _ := net.ResolveUDPAddr("udp", arg.Addr) - req := gosocks5.NewRequest(CmdUdpTun, ToSocksAddr(addr)) - bindAddr := req.Addr + req := gosocks5.NewRequest(CmdUdpTun, ToSocksAddr(laddr)) conn.SetWriteDeadline(time.Now().Add(time.Second * 90)) if err := req.Write(conn); err != nil { - glog.V(LWARNING).Infof("[rudp] %s -> %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rudp] %s -> %s : %s", laddr, raddr, err) return err } conn.SetWriteDeadline(time.Time{}) @@ -221,28 +217,22 @@ func connectRUdpForward(conn net.Conn, arg Args) error { conn.SetReadDeadline(time.Now().Add(90 * time.Second)) rep, err := gosocks5.ReadReply(conn) if err != nil { - glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", laddr, raddr, err) return err } conn.SetReadDeadline(time.Time{}) if rep.Rep != gosocks5.Succeeded { - glog.V(LWARNING).Infof("[rudp] %s <- %s : bind on %s failure", bindAddr, arg.Remote, arg.Addr) - return errors.New(fmt.Sprintf("Bind on %s failure", bindAddr)) + glog.V(LWARNING).Infof("[rudp] %s <- %s : bind on %s failure", laddr, raddr, laddr) + return errors.New(fmt.Sprintf("Bind on %s failure", laddr)) } - glog.V(LINFO).Infof("[rudp] %s - %s BIND ON %s OK", bindAddr, arg.Remote, rep.Addr) - - raddr, err := net.ResolveUDPAddr("udp", arg.Remote) - if err != nil { - glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", bindAddr, arg.Remote, err) - return err - } + glog.V(LINFO).Infof("[rudp] %s - %s BIND ON %s OK", laddr, raddr, rep.Addr) for { dgram, err := gosocks5.ReadUDPDatagram(conn) if err != nil { - glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", laddr, raddr, err) return err } @@ -252,30 +242,30 @@ func connectRUdpForward(conn net.Conn, arg Args) error { relay, err := net.DialUDP("udp", nil, raddr) if err != nil { - glog.V(LWARNING).Infof("[rudp] %s -> %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rudp] %s -> %s : %s", laddr, raddr, err) return } defer relay.Close() if _, err := relay.Write(dgram.Data); err != nil { - glog.V(LWARNING).Infof("[rudp] %s -> %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rudp] %s -> %s : %s", laddr, raddr, err) return } - glog.V(LDEBUG).Infof("[rudp] %s <<< %s length: %d", arg.Remote, bindAddr, len(dgram.Data)) + glog.V(LDEBUG).Infof("[rudp] %s >>> %s length: %d", laddr, raddr, len(dgram.Data)) relay.SetReadDeadline(time.Now().Add(time.Second * 60)) n, err := relay.Read(b) if err != nil { - glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", laddr, raddr, err) return } relay.SetReadDeadline(time.Time{}) - glog.V(LDEBUG).Infof("[rudp] %s >>> %s length: %d", arg.Remote, bindAddr, n) + glog.V(LDEBUG).Infof("[rudp] %s <<< %s length: %d", laddr, raddr, n) conn.SetWriteDeadline(time.Now().Add(time.Second * 90)) if err := gosocks5.NewUDPDatagram(gosocks5.NewUDPHeader(uint16(n), 0, dgram.Header.Addr), b[:n]).Write(conn); err != nil { - glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", bindAddr, arg.Remote, err) + glog.V(LWARNING).Infof("[rudp] %s <- %s : %s", laddr, raddr, err) return } conn.SetWriteDeadline(time.Time{}) diff --git a/socks.go b/socks.go index 9165d50b..3dab9e40 100644 --- a/socks.go +++ b/socks.go @@ -220,7 +220,7 @@ func handleSocks5Request(req *gosocks5.Request, conn net.Conn) { glog.V(LINFO).Infof("[socks5-bind] %s >-< %s", conn.RemoteAddr(), fconn.RemoteAddr()) case CmdUdpConnect: - glog.V(LINFO).Infof("[udp-connect] %s - %s", conn.RemoteAddr(), req.Addr) + glog.V(LINFO).Infof("[udp] %s - %s", conn.RemoteAddr(), req.Addr) udpConnect(req, conn) case gosocks5.CmdUdp: @@ -249,43 +249,43 @@ func udpConnect(req *gosocks5.Request, conn net.Conn) error { if len(forwardArgs) > 0 { // direct forwarding fconn, _, err := forwardChain(forwardArgs...) if err != nil { - glog.V(LINFO).Infof("[udp-connect] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) + glog.V(LINFO).Infof("[udp] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) return err } defer fconn.Close() if err := req.Write(fconn); err != nil { - glog.V(LINFO).Infof("[udp-connect] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) + glog.V(LINFO).Infof("[udp] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) return err } - glog.V(LINFO).Infof("[udp-connect] %s <-> %s", conn.RemoteAddr(), req.Addr) + glog.V(LINFO).Infof("[udp] %s <-> %s", conn.RemoteAddr(), req.Addr) err = Transport(conn, fconn) - glog.V(LINFO).Infof("[udp-connect] %s >-< %s", conn.RemoteAddr(), req.Addr) + glog.V(LINFO).Infof("[udp] %s >-< %s", conn.RemoteAddr(), req.Addr) return err } raddr, err := net.ResolveUDPAddr("udp", req.Addr.String()) if err != nil { - glog.V(LINFO).Infof("[udp-connect] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) + glog.V(LINFO).Infof("[udp] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) return err } if err := gosocks5.NewReply(gosocks5.Succeeded, nil).Write(conn); err != nil { - glog.V(LINFO).Infof("[udp-connect] %s <- %s : %s", conn.RemoteAddr(), req.Addr, err) + glog.V(LINFO).Infof("[udp] %s <- %s : %s", conn.RemoteAddr(), req.Addr, err) return err } - glog.V(LINFO).Infof("[udp-connect] %s <-> %s", conn.RemoteAddr(), raddr) - defer glog.V(LINFO).Infof("[udp-connect] %s >-< %s", conn.RemoteAddr(), raddr) + glog.V(LINFO).Infof("[udp] %s <-> %s", conn.RemoteAddr(), raddr) + defer glog.V(LINFO).Infof("[udp] %s >-< %s", conn.RemoteAddr(), raddr) for { dgram, err := gosocks5.ReadUDPDatagram(conn) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", conn.RemoteAddr(), req.Addr, err) return err } @@ -295,30 +295,30 @@ func udpConnect(req *gosocks5.Request, conn net.Conn) error { relay, err := net.DialUDP("udp", nil, raddr) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", conn.RemoteAddr(), raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", conn.RemoteAddr(), raddr, err) return } defer relay.Close() if _, err := relay.Write(dgram.Data); err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s -> %s : %s", conn.RemoteAddr(), raddr, err) + glog.V(LWARNING).Infof("[udp] %s -> %s : %s", conn.RemoteAddr(), raddr, err) return } - glog.V(LDEBUG).Infof("[udp-connect] %s >>> %s length: %d", conn.RemoteAddr(), raddr, len(dgram.Data)) + glog.V(LDEBUG).Infof("[udp-tun] %s >>> %s length: %d", conn.RemoteAddr(), raddr, len(dgram.Data)) relay.SetReadDeadline(time.Now().Add(time.Second * 60)) n, err := relay.Read(b) if err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s <- %s : %s", conn.RemoteAddr(), raddr, err) + glog.V(LWARNING).Infof("[udp] %s <- %s : %s", conn.RemoteAddr(), raddr, err) return } relay.SetReadDeadline(time.Time{}) - glog.V(LDEBUG).Infof("[udp-connect] %s <<< %s length: %d", conn.RemoteAddr(), raddr, n) + glog.V(LDEBUG).Infof("[udp-tun] %s <<< %s length: %d", conn.RemoteAddr(), raddr, n) conn.SetWriteDeadline(time.Now().Add(time.Second * 90)) if err := gosocks5.NewUDPDatagram(gosocks5.NewUDPHeader(uint16(n), 0, dgram.Header.Addr), b[:n]).Write(conn); err != nil { - glog.V(LWARNING).Infof("[udp-connect] %s <- %s : %s", conn.RemoteAddr(), raddr, err) + glog.V(LWARNING).Infof("[udp] %s <- %s : %s", conn.RemoteAddr(), raddr, err) return } conn.SetWriteDeadline(time.Time{})