golang端口转发_golang tcp多路复用

golang端口转发_golang tcp多路复用Golangtcp转发第一版本accept获取的Conn里的localAddr做为源地址,remoteAddr来做为目的地址//tcpForwardpackagemainimport(“fmt””net””os”)fu

golang端口转发_golang

Golang tcp 转发

第一版本

accept获取的Conn里的localAddr做为源地址,remoteAddr来做为目的地址

// tcpForward
package main

import (
    "fmt"
    "net"
    "os"
)

func TcpForward(port int) {
    lisPort := fmt.Sprint(":", port)
    listen, err := net.Listen("tcp", lisPort)
    if err != nil {
        fmt.Println("fault to listen,err: %s", err.Error())
        os.Exit(1)
    }
    defer listen.Close()
    fmt.Println("listenning now!")
    for {
        fromConn, err := listen.Accept()
        if err != nil {
            fmt.Println("fault,err: %s", err.Error())
            fromConn.Close()
            continue
        }
        go toDial(fromConn)
    }

}

func toDial(fromConn net.Conn) {
    toAddr := fromConn.RemoteAddr()
    toConn, err := net.Dial("tcp", toAddr.String())
    if err != nil {
        fmt.Println("fault,err: %s", err.Error())
        toConn.Close()
    }
    fmt.Println("%s to %s", fromConn.LocalAddr().String(), toConn.RemoteAddr().String())
    go copy(fromConn, toConn, 512)
    go copy(toConn, fromConn, 512)
}

func copy(f, t net.Conn, n int) {
    defer f.Close()
    defer t.Close()

    var buf = make([]byte, n)

    for {
        count, err := f.Read(buf)
        if err != nil {
            fmt.Println("fault,err: %s", err.Error())
            break
        }

        count, err = t.Write(buf[:count])
        if err != nil {
            fmt.Println("fault,err: %s", err.Error())
            break
        }
    }
}

win设置代理,用edge访问网页报错!!!

报错信息:

C:/Go/bin/go.exe build [C:/Users/imcjb/Desktop/egoweb]
成功: 进程退出代码 0.
C:/Users/imcjb/Desktop/egoweb/egoweb.exe  [C:/Users/imcjb/Desktop/egoweb]
Hello World!
C:\Users\imcjb\Desktop\egoweb\egoweb.exe
listenning now!
fault,err: %s dial tcp 127.0.0.1:53391: connectex: No connection could be made because the target machine actively refused it.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x6154cf]

goroutine 20 [running]:
main.toDial(0x6f4a20, 0xc000092038)
    C:/Users/imcjb/Desktop/egoweb/tcpForward.go:36 +0x3bf
created by main.TcpForward
    C:/Users/imcjb/Desktop/egoweb/tcpForward.go:26 +0x2db
fault,err: %s dial tcp 127.0.0.1:53392: connectex: No connection could be made because the target machine actively refused it.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x6154cf]

goroutine 19 [running]:
main.toDial(0x6f4a20, 0xc000092030)
    C:/Users/imcjb/Desktop/egoweb/tcpForward.go:36 +0x3bf
created by main.TcpForward
    C:/Users/imcjb/Desktop/egoweb/tcpForward.go:26 +0x2db
错误: 进程退出代码 2.

错误猜测

这里的remoteaddr返回的其实是转发前的真实ip,而非目的ip

代码部分还有一个小问题println、sprintf使用错误,他们的参数是interface{}

转载于:https://www.cnblogs.com/boxker/p/11041049.html

今天的文章golang端口转发_golang tcp多路复用分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/74146.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注