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. java基础笔记(三)——main方法

    1.解析public static void main(String[] args)方法 JVM在运行程序时,会首先查找main()方法作为入口,main是JVM识别的特殊方法名. public是权限 ...

  2. PHP 扩展篇 _ 持续更新

    记住这个网站:http://pecl.php.net/ PHP-Redis扩展更新时间:2019/05/06 PHP安装Redis 1:下载目前最新版的redis插件 wget http://pecl ...

  3. css各种水平垂直居中

    css各种水平垂直居中,网上查了一下,总结以下几种 line-height垂直居中 缺点,仅限于单行文字 .item{ text-align: center; height: 100px; line- ...

  4. ERROR 1366 (HY000): Incorrect string value: '\xB3\xA4\xC9\xB3' for column

    在用以下方法之前,请先执行下面命令查看. show variables like 'character%';  ——查看所有编码方式 show create table table_name;   — ...

  5. Javascript中常用方法简介

    Array数组常用方法       先创建一个数组var abc = [1,2,3,4,5,6,7,8,9]; (1)pop(); 这个方法会删除数组的最后一项并返回删除掉的值. 比如:console ...

  6. [Android基础]Android四大组件之BroadCast

    BroadCast的定义: 广播是一种订阅--通知 事件,广播接收者向Android系统 register (订阅广播),广播发送者向Adnroid系统 sendBroadCast(发送广播),然后A ...

  7. UML类图学习总结

    1.首先来认识下类图?以及类图的作用 类图(Class diagram)由许多(静态)说明性的模型元素(例如类.包和它们之间的关系,这些元素和它们的内容互相连接)组成.类图可以组织在(并且属于)包中, ...

  8. 开源项目android-uitableview介绍

    在iOS应用中,UITableView应该是使用率最高的视图之一了.iPod.时钟.日历.备忘录.Mail.天气.照片.电话.短信. Safari.App Store.iTunes.Game Cent ...

  9. @ConfigurationProperties

    功能 将属性文件与一个Java类绑定,属性文件中的变量与Java类中的成员变量一一对应,无需完全一致. 如需将 @ConfigurationProperties 注解的目标类添加到Spring IOC ...

  10. springboot集成shiro实现权限认证

    github:https://github.com/peterowang/shiro 基于上一篇:springboot集成shiro实现身份认证 1.加入UserController package ...