原文:https://socketloop.com/tutorials/golang-forwarding-a-local-port-to-a-remote-server-example

端口转发, 本地的端口转发到远端服务器的80端口。

------------------------------------------------------------------------------------------------------------------------------------------------

Got a strange request yesterday. A friend who is an IT manager in his company needs to implement some control over his local network. He needs to block all the staffs(his co-workers) access to Facebook during working hours, but at the same time open up a "secret" door access to Facebook for his boss.

It is pretty trivial to configure the local network firewall to block access to certain websites nowadays. However, he prefers not to configure the "secret" door in the local network firewall. The next best solution is to implement a local port forwarding to remote server.

Below is a simple port forwarding solution in Golang that will initiate a bi-directional communication with a remote server(Facebook for example).

Here you go!


 package main

 import (
"io"
"log"
"net"
) var localServerHost = "localhost:8880"
var remoteServerHost = "www.facebook.com:80" func main() { ln, err := net.Listen("tcp", localServerHost)
if err != nil {
log.Fatal(err)
} log.Println("Port forwarding server up and listening on ", localServerHost) for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
} go handleConnection(conn)
}
} func forward(src, dest net.Conn) {
defer src.Close()
defer dest.Close()
io.Copy(src, dest)
} func handleConnection(c net.Conn) { log.Println("Connection from : ", c.RemoteAddr()) remote, err := net.Dial("tcp", remoteServerHost)
if err != nil {
log.Fatal(err)
} log.Println("Connected to ", remoteServerHost) // go routines to initiate bi-directional communication for local server with a
// remote server
go forward(c, remote)
go forward(remote, c)
}

Sample output:

1) Run the program at the background and

2) On the browser, enter localhost:8880 in the address bar.

References:

https://www.socketloop.com/tutorials/golang-simple-client-server-example

https://www.socketloop.com/tutorials/golang-simple-client-server-hmac-authentication-without-ssl-example

Golang : Forwarding a local port to a remote server example的更多相关文章

  1. CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages.

    今天,在用icinga服务器端测试客户端脚本时,报如下错误: [root@mysql-server1 etc]# /usr/local/icinga/libexec/check_nrpe -H 192 ...

  2. WinRM不起作用 Connecting to remote server failed with the following error message : WinRM cannot complete the operation

    当我运行下面的 powershell  脚本时: $FarmAcct = 'domain\user'  $secPassword = ConvertTo-SecureString 'aaa' -AsP ...

  3. lr11录制时报“Request Connection: Remote Server @ 0.0.0.0:1080 (Service=?) NOT PROXIED! )”解决方法

    在录制脚本的时候出现如下现象: 解决方法: LoadRunner录制脚本时出现:Unable to connect to remote server),有事件没有脚本的问题   1.首先要查看IE浏览 ...

  4. selenium Remote Server 实现原理

    selenium作为一个出色的web automation框架,被越来越多的企业采用究其原因,框架设计的比较remarkable, 作为一个开源的框架,能够开辟出一套协议,以至于针对app测试的app ...

  5. System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE

    I am having problems with a bit of code that accesses a restful web service. Running this code, it e ...

  6. Call Azure Queue get "The remote server returned an error: (400) Bad Request."

    这几天开始研究Windows Azure, 在使用Azure Queue 的时候,CreateInfNotExists 总是抛出异常 "The remote server returned ...

  7. Request Connection: Remote Server @ 192.229.145.200:80

    录制Loadrunner脚本时,提示: Request Connection: Remote Server @ 192.229.145.200:80   NOT INTERCEPTED!(REASON ...

  8. WebService:The remote server returned an error: (400) Bad Request

    开发工具:VS2010.开发组件:WebService.运行环境:Windows 今天一个同事在进行计费接口联调试时,发现了一个非常奇怪的问题:接口在家里环境测试,一切正常,但是部署到现网环境之后,连 ...

  9. Get-CrmSetting返回Unable to connect to the remote server的解决办法

    摘要: 微软动态CRM专家罗勇 ,回复302或者20190125可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 在Dynam ...

随机推荐

  1. 论consul正确的关闭姿势

    最近在工作中发现一个有意思的现象,我用 ctrl+c 关闭本地 consul 的时候,报警系统并没有发出告警,说我的 node 异常,自己看了一下代码,发现 consul 的关闭还是有点猫腻的,仔细来 ...

  2. sql数据库查询相关操作,SQL的应用——SQL多表连查、子查询、多行子查询

    ? 1 **SQL多表连查** ? 1 2 3 4 5 6 7 8 --查询员工和部门信息 select * from emp e,dept d where e.deptno=d.deptno --查 ...

  3. OpenJudge 4152 最佳加法表达式

    总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值.例如,在1234中摆放 ...

  4. 12 Spring JdbcTemplate的使用

    1.项目搭建 <1>数据库表account对应的账户实体类 package domain; import java.io.Serializable; /** * 账户实体类 */ publ ...

  5. Flask总结篇

    1 Flask框架的优势? 相信做Python这一块的程序员都有听说这三个框架,就像神一样的存在,每一个框架的介绍我就不写出来了,感兴趣可以自己百度了解了解!下面我就说正事 Django:Python ...

  6. C#的WebApi 与 EasyUi的DataGrid结合生成的可分页界面

    1.从数据库每次取出的数据为当前分页的数据. 2.分页用的是EasyUI 的 Pagination控件,与DataGrid是相对独立的. 3.后台数据获取是通过WebApi去获取. 4.传入参数是:p ...

  7. windows10 iis浏览wcf报404.3错误

    报错:HTTP错误404.3-Not Found 由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加MIME映射. 解决步骤如下: 控制面板->打开 ...

  8. Linux 系统中如何进入退出 vim 编辑器

    在 Linux 中,vim 编辑器是系统自带的文本编辑器,但要修改某个文本文件,可不是像 Windows 那样操作,更有新手,进入 vi 编辑器后,无法退出以致于强制关机,其实,这个vim(vi)也是 ...

  9. open_vPGPv

    加密 // create an instance of the library PGPLib pgp = new PGPLib(); // Import the main company public ...

  10. 修改Excel脚本

    批量修改Excel TODO: 批量修改Excel 功能: 将图片防止在本地,读取excel数据,拆分数据之后根本地照片名称对比,然后上传服务器,创建新得excel. #!/usr/bin/pytho ...