Golang tcp转发 remoteAddr错误
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错误的更多相关文章
- Golang TCP转发到指定地址
Golang TCP转发到指定地址 第二个版本,设置指定ip地址 代码 // tcpForward package main import ( "fmt" "net&qu ...
- centos7 编译安装nginx+tcp转发
一.依赖 1. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: yum install gcc-c++ 2. PCRE pc ...
- 手头没证书,如何给https做代理?Nginx TCP转发
线上的一个海外充值接口(https)经常因我朝网络问题中断,想借助hk的机器做个https反向代理又没证书. 一开始 一开始想到的办法是借助Nginx的tcp转发进行代理: 编译NGINX时加入 -- ...
- [转帖]【rinetd】CentOS7.x上轻量级TCP转发工具rinetd的安装配置
[rinetd]CentOS7.x上轻量级TCP转发工具rinetd的安装配置 https://www.jianshu.com/p/2605d247b944 这一个写的更加全面了. 2019.07.0 ...
- nginx编译安装以及配置tcp转发
依赖包安装 yum -y install gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl open ...
- FreeBSD NGINX TCP转发
前几天搞转发,研究了下TCP转发,现在记录下来 首先加载模块 注意:这是FreeBSD的位置.并且需要NGINX支持 load_module /usr/local/libexec/nginx/ngx_ ...
- p2p-tunnel 打洞内网穿透系列(三)TCP转发访问内网web服务
系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...
- p2p-tunnel 打洞内网穿透系列(二)TCP转发访问内网共享文件夹
系列文章 p2p-tunnel 打洞内网穿透系列(一)客户端配置及打洞 p2p-tunnel 打洞内网穿透系列(二)TCP转发访问远程共享文件夹 p2p-tunnel 打洞内网穿透系列(三)TCP转发 ...
- Golang Tcp粘包处理(转)
在用golang开发人工客服系统的时候碰到了粘包问题,那么什么是粘包呢?例如我们和客户端约定数据交互格式是一个json格式的字符串: {"Id":1,"Name" ...
随机推荐
- C# 操作 Excel 文件(.xls 或 .xlsx)
在.net中,常用的操作excel文件的方式,有三种: OLE DB的形式, 第三方框架NPOI, Office组件. 总结: 通过对比,在读取大数据量的excel文件,建议用OLE DB的形式,把e ...
- JS高级学习历程-17
[正则案例] 1 匹配手机号码
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) A
Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always bee ...
- exportExcel()方法注意事项
1.保证数据集里的字段和SQL语句里字段全部一致,包括sql语句里必须有系统字段 2.exportExcel()执行的时候,是先去执行SQL语句,再去到数据集里面进行不对,若有不一致的地方,则报列名无 ...
- Ubuntu 18.04 Python3.6.6导入wx模块报Gtk-Message : 17:06:05.797 :Failed to load module "canberra-gtk-module"
解决办法: root@sishen:~# apt-get install libcanberra-gtk-module
- TDH-常见运维指令
1.查看cpu: cat /proc/cpuinfo | grep processor2.查看磁盘:df -h (查看磁盘使用率) df -i (查看iNode使用) fdisk -l (查看磁盘整体 ...
- Ubuntu常用指令集
Ubuntu Linux 操作系统常用命令详细介绍 ( 1)Udo apt-get install 软件名 安装软件命令 sudo nautilus 打开文件(有 root 权限)su root 切换 ...
- marquee标签(跑马灯)
- ABAP跳转屏幕
1.call transaction语句跳转屏幕 '. CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN. . 2.调用函数 CALL FUNCTION 'M ...
- 字符串在forEach方法里面可以使用include函数
今天在写项目的时候,发现了一个问题,使用forEach函数,arr数组里面的字符串可以调用include方法,我查阅了很多地方,string里面没有这个方法. 但是在forEach函数里面确实可以这样 ...