go - 如何重用监听器/连接?戈兰

标签 go network-programming

我正在尝试通过第 3 方服务器(也称为反向连接)将 NAT 后面的计算机连接到互联网。我正在监听两个端口。在一个端口 (dstNet) 上连接 NAT 后面的计算机,在另一个端口上连接互联网客户端。 问题是我不知道如何处理NAT后面的机器断开连接。即使机器再次连接,流量也不再处理发送/写入...我得到 [DEBUG]ocks: 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
}

最佳答案

发生错误后唯一可以重用连接的情况是如果是临时条件。

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

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

如果你想引用的话,这里有一个更简单的 go 代理模式: 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{}{}
}

关于go - 如何重用监听器/连接?戈兰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27438486/

相关文章:

go - 使用 gocolly 抓取时如何在 html 表格单元格中保留换行符

go - 内部循环定义父链接

io - 同时写入和读取数组的最惯用方式

c - 未接收来自 TCP 连接的文件传输

Perl 数据包制作和注入(inject)模块

xml - Go:将 XML 解码嵌套结构放入接口(interface){}中

c - 如何解决 TCP 服务器地址已在使用错误

c++ - 将空数据写入非阻塞套接字会导致 epoll_wait 挂起

c - free( ) 导致 malloc_error_break

go - 从 vendor 目录使用 google.golang.org/grpc 时出错