Golang : Forwarding a local port to a remote server example
原文: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
Golang : Forwarding a local port to a remote server example的更多相关文章
- 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 ...
- 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 ...
- lr11录制时报“Request Connection: Remote Server @ 0.0.0.0:1080 (Service=?) NOT PROXIED! )”解决方法
在录制脚本的时候出现如下现象: 解决方法: LoadRunner录制脚本时出现:Unable to connect to remote server),有事件没有脚本的问题 1.首先要查看IE浏览 ...
- selenium Remote Server 实现原理
selenium作为一个出色的web automation框架,被越来越多的企业采用究其原因,框架设计的比较remarkable, 作为一个开源的框架,能够开辟出一套协议,以至于针对app测试的app ...
- 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 ...
- Call Azure Queue get "The remote server returned an error: (400) Bad Request."
这几天开始研究Windows Azure, 在使用Azure Queue 的时候,CreateInfNotExists 总是抛出异常 "The remote server returned ...
- Request Connection: Remote Server @ 192.229.145.200:80
录制Loadrunner脚本时,提示: Request Connection: Remote Server @ 192.229.145.200:80 NOT INTERCEPTED!(REASON ...
- WebService:The remote server returned an error: (400) Bad Request
开发工具:VS2010.开发组件:WebService.运行环境:Windows 今天一个同事在进行计费接口联调试时,发现了一个非常奇怪的问题:接口在家里环境测试,一切正常,但是部署到现网环境之后,连 ...
- Get-CrmSetting返回Unable to connect to the remote server的解决办法
摘要: 微软动态CRM专家罗勇 ,回复302或者20190125可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 在Dynam ...
随机推荐
- 最新 梆梆安全java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.梆梆安全等10家互联网公司的校招Offer,因为某些自身原因最终选择了梆梆安全.6.7月主要是做系统复习.项目复盘.Leet ...
- JAVA克隆对象报错:The method clone() from the type Object is not visible
将一个对象复制一份,称为对象的克隆技术.在Object类汇总存在一个clone()方法:protected Onject clone() throws CloneNotSupportedExcepti ...
- 为Ubuntu笔记本电脑创建WiFi热点共享上网
from: linux公社 http://www.linuxidc.com/Linux/2014-02/97139.htm 该文由土木坛子转译而来,说是转译,其实看截图就可以方便的设置,没有任何命 ...
- Vue(六)插槽(2.6.0+)
插槽在vue2.6.0开始有了新的更新 具名插槽(数据来自父组件) 子组件(定义插槽)这里版本前后没什么变化 <template> <div> <header> & ...
- FineUI window弹层设置
需在页面先设置 <f:Window ID="Window1" runat="server" Height="600px" Widt ...
- Linux学习-防火墙-Selinux-配置本地YUM源
关闭防火墙并设置开机不启动 systemctl status firewalld.service #查看firewalld状态systemctl stop firewalld #关闭systemctl ...
- JQuery 总结篇
一.基础知识 1.jq的使用 在script块中: 方式1:$(function(){ $("[使用选择器] ...
- JAVA day2 语言基础
一.注释 描述代码的文字 1.// 单行注释 2./* */ 多行注释 (多行注释中不能再嵌套多行注释) 3./** */ 多行注释 配合JavaDoc工具使用(只可以看到注释,看不到代码 ...
- 3.02定义常量之const
[注:本程序验证是使用vs2013版] #include <stdio.h> #include <stdlib.h> #include <string.h> #pr ...
- linux 系统扩容 VMware Centos---VMware ESXi
用到的命令 df fdisk pvcreate pvdisplay vgdisplay vgextend lvdisplay lvextend resize2fs 0 ...