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. 解决windows下nginx中文文件名乱码

    我的根目录文件夹放在d盘work文件夹下,一般这样配置 nginx\conf\nginx.conf location / { root D:/work; index index_bak.html; a ...

  2. CC20:高度最小的BST

    题目 对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树. 给定一个有序序列int[] vals,请返回创建的二叉查找树的高度. 解法 这道题感觉如果没有创建树的 ...

  3. safari不支持new Date函数

    最近在做移动Web的时候,在PC上用Chrome调试都成功了,但是在iPhone上真机一测就出现了奇怪的问题.经过一系列调试发现是日期相关的地方出现了问题.起初怀疑是生产环境的问题,但用Mac版的sa ...

  4. Mysql 5.7主主复制配置

    MySQL5.7主主复制配置 主机1IP:192.168.1.2主机2IP:192.168.1.4 一.首先安装MySQL 5.71.卸载两台主机系统中已经有的mysql相关软件包rpm -qa | ...

  5. python进阶11 正则表达式

    python进阶11 正则表达式 一.概念 #正则表达式主要解决什么问题? #1.判断一个字符串是否匹配给定的格式,判断用户提交的又想的格式是否正确 #2.从一个字符串中按指定格式提取信息,抓取页面中 ...

  6. 前端CSS(3)

    前端基础CSS(3)   一.文本属性和字体属性(常用的) 1.文本属性 text-align:left|right|center|justify(两端对齐,只适用于英文);   /*对齐方式*/ c ...

  7. 30道python真实面试题(搜集到的,看看其实都是基础)

    1.一行代码实现1-100之间和 In [1]: sum(range(0,101)) Out[1]: 5050 2.如何在一个函数内部修改全局变量 利用global修改全局变量 In [2]: a = ...

  8. 爬虫(cookie)——renren模拟登陆

    工具:python3 步骤: 1)使用cookiejar.CookieJar()构建一个CookieJar()构建一个对象,用来保存cookie的值 2)使用HTTPCookieProcessor() ...

  9. JS和jquery获取各种屏幕的宽度和高度的代码

    Javascript: 网页可见区域宽: document.body.clientWidth 网页可见区域高: document.body.clientHeight 网页可见区域宽: document ...

  10. Kendo MVVM (一) 概述

    Kendo MVVM (一) 概述 Model View ViewModel (MVVM)  是开发人员经常使用的一种设计模式,以实现数据模型(Model)和视图(View)的分离.MVVM 中的 V ...