【问题标题】:How to reuse listener/connection ? Golang如何重用监听器/连接?戈朗
【发布时间】:2014-12-12 07:00:56
【问题描述】:

我正在尝试通过第 3 方服务器(也称为反向连接)将 NAT 后面的计算机与互联网连接。我在两个端口上监听。一个端口 (dstNet) 连接 NAT 后面的机器,另一个端口连接互联网客户端。 问题是我不知道如何处理 NAT 后面的机器断开连接。即使机器再次连接,流量也不再处理发送/写入......我得到[DEBUG] socks: Copied 0 bytes to client,这当然是我的警告。下面是代码。很长,但我找不到要修剪的地方。

// Make a bridge between dstNet which is
// usually behind NAT and srcNet which is usually a client
// which wants to route the traffic though the NAT machine.
package main

import (
    "bufio"
    "errors"
    log "github.com/golang/glog"
    "io"
    "net"
    "time"
)

const (
    // listen on the dstNet so that we can
    // create a connection with the NAT client
    dstNet = "0.0.0.0:9000"
    // listen on srcNet so that we can get traffic
    // to forward to dstNet
    srcNet = "0.0.0.0:9001"
)

var errCh = make(chan error, 1)

// make a channel to send the reverse connections
var lrCh = make(chan net.Conn, 1)
func listenDst() {
    // Listen on the dstNet
    lr, err := net.Listen("tcp", dstNet)
    if err != nil {
        log.Error(err)
        errCh <- err
        return
    }
    // accept the connection
    for {
        lrConn, err := lr.Accept()
        if err != nil {
            log.Error(err)
            errCh <- err
            return
        }
            log.Errorf("sent connection")
        //  lrConn.SetReadDeadline(time.Now().Add(10 * time.Second))
            lrCh <- lrConn

    }

}

func main() {

    go func() {
        for err := range errCh {
            if err != nil {
                panic(err)
            }
        }
    }()
    // listen for the nat server
    go listenDst()

    // listen for clients to connect
    l, err := net.Listen("tcp", srcNet)
    if err != nil {
        log.Error(err)
        panic(err)
    }
    // accept the connection
    for {
        conn, err := l.Accept()
        if err != nil {
            log.Error(err)
            panic(err)
        }
        // serve the connection
        go func(conn net.Conn) {
            defer conn.Close()
            bufConn := bufio.NewReader(conn)
            dst := <-lrCh
            defer dst.Close()

            // Start proxying
            errCh2 := make(chan error, 2)
            go proxy("target", dst, bufConn, errCh2)
            go proxy("client", conn, dst, errCh2)

            // Wait
            var ei int
            for err = range errCh2 {
                switch {
                case err != nil && err.Error() == "no byte":
                    log.Error(err)
                case err != nil && err.Error() == "use of closed network connection":
                    // if the connection is closed we restart it.
                    log.Error(err)
                    // BUG() attempt to write again the bytes
                case err != nil:
                    log.Error(err)
                    errCh <- err
                }
                if ei == 1 {
                    log.Errorf("done with errors")
                    close(errCh2)
                }
                ei++

            }
        }(conn)

    }
}

// proxy is used to suffle data from src to destination, and sends errors
// down a dedicated channel
func proxy(name string, dst io.Writer, src io.Reader, errCh2 chan error) {
    n, err := io.Copy(dst, src)
    // Log, and sleep. This is jank but allows the otherside
    // to finish a pending copy
    log.Errorf("[DEBUG] socks: Copied %d bytes to %s", n, name)
    time.Sleep(10 * time.Millisecond)
    // Send any errors
    switch {
    case err != nil:
        log.Error(err)
        errCh2 <- err
    case n < 1:
        errCh2 <- errors.New("no byte")
    default:
        errCh2 <- nil
    }
    return
}

【问题讨论】:

    标签: go network-programming


    【解决方案1】:

    出现错误后唯一可以重用连接的情况是 if 是临时条件。

    if err, ok := err.(net.Error); ok && err.Temporary() {
    }
    

    如果您尝试代理 TCP 连接,并且存在 任何 其他错误(检查 Temporary 甚至可能没有那么有用),您需要放弃整个事情并重新开始。您不知道远程服务器的状态是什么,有多少数据包正在传输或丢失,而且您越努力,它只会导致更难的错误。 (提示:不要用sleep 隐藏并发或计时问题。从长远来看,这只会让事情变得更难)

    如果你想引用它,这里有一个更简单的代理模式: https://gist.github.com/jbardin/821d08cb64c01c84b81a

    func Proxy(srvConn, cliConn *net.TCPConn) {
        // channels to wait on the close event for each connection
        serverClosed := make(chan struct{}, 1)
        clientClosed := make(chan struct{}, 1)
    
        go broker(srvConn, cliConn, clientClosed)
        go broker(cliConn, srvConn, serverClosed)
    
        // wait for one half of the proxy to exit, then trigger a shutdown of the
        // other half by calling CloseRead(). This will break the read loop in the
        // broker and allow us to fully close the connection cleanly without a
        // "use of closed network connection" error.
        var waitFor chan struct{}
        select {
        case <-clientClosed:
            // the client closed first and any more packets from the server aren't
            // useful, so we can optionally SetLinger(0) here to recycle the port
            // faster.
            srvConn.SetLinger(0)
            srvConn.CloseRead()
            waitFor = serverClosed
        case <-serverClosed:
            cliConn.CloseRead()
            waitFor = clientClosed
        }
    
        // Wait for the other connection to close.
        // This "waitFor" pattern isn't required, but gives us a way to track the
        // connection and ensure all copies terminate correctly; we can trigger
        // stats on entry and deferred exit of this function.
        <-waitFor
    }
    
    // This does the actual data transfer.
    // The broker only closes the Read side.
    func broker(dst, src net.Conn, srcClosed chan struct{}) {
        // We can handle errors in a finer-grained manner by inlining io.Copy (it's
        // simple, and we drop the ReaderFrom or WriterTo checks for
        // net.Conn->net.Conn transfers, which aren't needed). This would also let
        // us adjust buffersize.
        _, err := io.Copy(dst, src)
    
        if err != nil {
            log.Printf("Copy error: %s", err)
        }
        if err := src.Close(); err != nil {
            log.Printf("Close error: %s", err)
        }
        srcClosed <- struct{}{}
    }
    

    【讨论】:

    • 我不确定,但它仍然没有解决dst客户端的断开连接问题。基本上,如果dstNet 断开连接然后再次连接,则不再发送数据,也不会出现任何错误。 broker() 只是“复制”0 个字节
    • 我不确定我是否完全理解您在做什么。没有办法“重新启动”或重用 tcp 连接。如果代理的一侧出现问题,除非客户端知道数据可能会丢失(它不是真正的 TCP),否则您需要关闭客户端以确保收到连接断开的通知。如果您真的想尝试并继续传输(您必须确定客户端会接受这一点),请区分错误,以便您知道哪个连接引发了它们,摆脱 bufio,并准确记录一切发生的顺序。
    • 由于某些原因无法正确处理 destNet 客户端连接。基本上在 destNet 客户端重新启动(断开连接并重新连接)后,它不再接收数据。目前,每当我检测到写入的 0 个字节似乎可以解决问题时,我都会关闭侦听器。我已经发布了代码作为答案。
    【解决方案2】:

    事实证明,我不仅要关闭连接,还要重新启动侦听器。如果无法写入(即写入 0 个字节)到 src,我已经修改了代理函数以重置 destNet 侦听器。我仍然不确定这是否是正确的方法(即在多连接情况下关闭侦听器似乎很糟糕,因为我想我重置了在该地址上拨号的所有客户端连接)但到目前为止这是最好的我可以解决它。

     if n == 0 {
            lrNewCh <- 1
        }
    

    这里是所有代码。所有功劳归于@JimB

    // Make a bridge between dstNet which is
    // usually behind NAT and srcNet which is usually a client
    // which wants to route the traffic though the NAT machine.
    package main
    
    import (
        log "github.com/golang/glog"
        "io"
        "net"
    )
    
    // listen on the dstNet so that we can
    // create a connection with the NAT client
    var dstNet *net.TCPAddr = &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 9000}
    
    // listen on srcNet so that we can get traffic
    // to forward to dstNet
    var srcNet *net.TCPAddr = &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: 9001}
    
    var errCh = make(chan error, 1)
    
    // make a channel to send the reverse connections
    var lrCh = make(chan *net.TCPConn, 1)
    var lrNewCh = make(chan int, 1)
    
    func listenDst() {
        // Listen on the dstNet
        lr, err := net.ListenTCP("tcp", dstNet)
        if err != nil {
            log.Error(err)
            errCh <- err
            return
        }
        // accept the connection
        for {
            lrConn, err := lr.AcceptTCP()
            if err != nil {
                    log.Error(err)
                    //errCh <- err
                    //return
            }
            status := <-lrNewCh
                log.Errorf("status request is %v", status)
                if status == 1{
                    log.Errorf("we close and restart the listener and the connection")
                    if err =  lrConn.Close(); err !=nil{
                        log.Error(err)
                    }
                    if err =  lr.Close(); err !=nil{
                        log.Error(err)
                    }
                        lr, err = net.ListenTCP("tcp", dstNet)
                        if err != nil {
                            log.Error(err)
                            errCh <- err
                            return
                        }
                    lrConn, err = lr.AcceptTCP()
                    if err !=nil{
                        log.Error(err)
                        errCh <- err
                    }
                }else{
                    log.Errorf("new connection on its way")
                    lrCh <- lrConn
                }
        //  default:
                // log.Errorf("accepting new connections")
    
    
        }
    
    }
    
    func main() {
    
        go func() {
            for err := range errCh {
                if err != nil {
                    panic(err)
                }
            }
        }()
        // listen for the nat server
        go listenDst()
    
        // listen for clients to connect
        l, err := net.ListenTCP("tcp", srcNet)
        if err != nil {
            log.Error(err)
            panic(err)
        }
        // accept the connection
        for {
            conn, err := l.AcceptTCP()
            if err != nil {
                log.Error(err)
                panic(err)
            }
            // serve the connection
            go func(conn *net.TCPConn) {
                defer conn.Close()
                lrNewCh <- 0
                dst := <-lrCh
                defer dst.Close()
                proxy(dst, conn)
            }(conn)
    
        }
    }
    
    func proxy(srvConn, cliConn *net.TCPConn) {
        // channels to wait on the close event for each connection
        serverClosed := make(chan struct{}, 1)
        clientClosed := make(chan struct{}, 1)
    
        go broker(srvConn, cliConn, clientClosed)
        go broker(cliConn, srvConn, serverClosed)
    
        // wait for one half of the proxy to exit, then trigger a shutdown of the
        // other half by calling CloseRead(). This will break the read loop in the
        // broker and allow us to fully close the connection cleanly without a
        // "use of closed network connection" error.
        var waitFor chan struct{}
        select {
        case <-clientClosed:
            // the client closed first and any more packets from the server aren't
            // useful, so we can optionally SetLinger(0) here to recycle the port
            // faster.
            srvConn.SetLinger(0)
            srvConn.CloseRead()
            waitFor = serverClosed
        case <-serverClosed:
            cliConn.CloseRead()
            waitFor = clientClosed
        }
    
        // Wait for the other connection to close.
        // This "waitFor" pattern isn't required, but gives us a way to track the
        // connection and ensure all copies terminate correctly; we can trigger
        // stats on entry and deferred exit of this function.
        <-waitFor
    }
    
    // This does the actual data transfer.
    // The broker only closes the Read side.
    func broker(dst, src net.Conn, srcClosed chan struct{}) {
        // We can handle errors in a finer-grained manner by inlining io.Copy (it's
        // simple, and we drop the ReaderFrom or WriterTo checks for
        // net.Conn->net.Conn transfers, which aren't needed). This would also let
        // us adjust buffersize.
        n, err := io.Copy(dst, src)
        log.Errorf(" %v bytes copied", n)
        if err != nil {
            log.Errorf("Copy error: %s", err)
            // errCh <- err
        }
        if err := src.Close(); err != nil {
            log.Errorf("Close error: %s", err)
            errCh <- err
        }
        if n == 0 {
            lrNewCh <- 1
        }
        srcClosed <- struct{}{}
    
    }
    

    【讨论】:

    • 你不应该重新启动监听器,监听器所做的就是接受新的连接。关闭并创建一个新连接会留下一个窗口,您将在其中拒绝新连接。
    • @JimB 我知道,但如果我不这样做,dstNet 上当前连接的客户端将不会接收数据(代理一直报告写入 0 字节)。我假设使用了陈旧的连接,或者可能存在其他问题。当前发生的事情(当我关闭侦听器时)是 dstNet 客户端获得重置连接,然后一切正常。当然也有拒绝新连接的窗口,这是不可取的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2023-02-06
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    • 1970-01-01
    相关资源
    最近更新 更多