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{}

Golang tcp转发 remoteAddr错误的更多相关文章

  1. Golang TCP转发到指定地址

    Golang TCP转发到指定地址 第二个版本,设置指定ip地址 代码 // tcpForward package main import ( "fmt" "net&qu ...

  2. centos7 编译安装nginx+tcp转发

    一.依赖 1. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: yum install gcc-c++ 2. PCRE pc ...

  3. 手头没证书,如何给https做代理?Nginx TCP转发

    线上的一个海外充值接口(https)经常因我朝网络问题中断,想借助hk的机器做个https反向代理又没证书. 一开始 一开始想到的办法是借助Nginx的tcp转发进行代理: 编译NGINX时加入 -- ...

  4. [转帖]【rinetd】CentOS7.x上轻量级TCP转发工具rinetd的安装配置

    [rinetd]CentOS7.x上轻量级TCP转发工具rinetd的安装配置 https://www.jianshu.com/p/2605d247b944 这一个写的更加全面了. 2019.07.0 ...

  5. nginx编译安装以及配置tcp转发

    依赖包安装 yum -y install gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl open ...

  6. FreeBSD NGINX TCP转发

    前几天搞转发,研究了下TCP转发,现在记录下来 首先加载模块 注意:这是FreeBSD的位置.并且需要NGINX支持 load_module /usr/local/libexec/nginx/ngx_ ...

  7. p2p-tunnel 打洞内网穿透系列(三)TCP转发访问内网web服务

    系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...

  8. p2p-tunnel 打洞内网穿透系列(二)TCP转发访问内网共享文件夹

    系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...

  9. Golang Tcp粘包处理(转)

    在用golang开发人工客服系统的时候碰到了粘包问题,那么什么是粘包呢?例如我们和客户端约定数据交互格式是一个json格式的字符串: {"Id":1,"Name" ...

随机推荐

  1. layui实现下拉分类多级

    Layui tree 下拉菜单树   1.效果: 2.html  代码: <!DOCTYPE html> <html> <head> <meta charse ...

  2. JS高级学习历程-8

    2 构造函数和普通函数的区别 两者本身没有实质区别,具体看使用 new  函数();   -------->构造函数 函数();        ---------> 普通函数 <!D ...

  3. K.河北美食

    链接:https://ac.nowcoder.com/acm/contest/903/K 题意: icebound最喜欢吃河北菜,于是他想要大厨做一桌河北菜宴请宾客.icebound购买了一些食材,并 ...

  4. 关于MySQL索引的一点小见解

    索引: 优缺点: 1.用的合理可以提高查询效率 2.建立过多索引会占用物理和数据空间,同时也会降低插入和更新效率 需不需要建立索引: 1.一般表的数据低于2000条就不用建立索引了,超过2000条酌情 ...

  5. 如何减小SQL 的物理读,。

    1.dev time:1226 1个跑批 db_file_multiblock_read_count =128 60.05 (mins) 26-Dec-17 16:00:20 ~ 26-Dec-17 ...

  6. 关于Spring配置文件xml文档的schema约束

    最开始使用spring框架的时候,对于其配置文件xml,只是网上得知其使用方法,而不明其意.最近想着寻根问底的探究一下.以下是本文主要内容: 1.配置文件示例. <?xml version=&q ...

  7. SpringBoot---Web开发---WebSocket

    [广播式] 1. <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...

  8. 牛客网Java刷题知识点之什么是匿名内部类、匿名内部类的使用原则、匿名内部类初始化、匿名内部类使用的形参为何要为final 和 案例

    不多说,直接上干货! 什么是匿名内部类 匿名内部类就是没有名字的内部类. 不使用关键字class . extends .implements 没有构造函数 必须继承其他类或实现其他接口 正因为没有名字 ...

  9. RabbitMQ使用教程(二)RabbitMQ用户管理,角色管理及权限设置

    上一篇博客 RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例 中,我们成功的安装好了RabbitMQ环境,并通过一个Java客户端示例了解了用生产者来发布消息,用 ...

  10. Enable-Migrations 迁移错误,提示找不到连接字符串

    把迁移项目设为启动项目即可,若是MVC Web项目可能就没有这个问题.