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 ...
随机推荐
- python:使用Djangorestframework编写post和get接口
1.安装django pip install django 2.新建一个django工程 python manage.py startproject cainiao_monitor_api 3.新建一 ...
- python:校验邮箱格式
# coding:utf-8 import re def validateEmail(email): if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\ ...
- Altera PLL Locked 失锁的原因
Altera PLL 有时可能会出现失锁的情况,查找了官网资料,有总结到有几个情况下会出现失锁. 官网中的网页如下,是英文的: https://www.altera.com.cn/support/su ...
- windows server 守护进程nssm
安装nssm 下载程序 https://nssm.cc/ci/nssm-2.24-101-g897c7ad.zip 安装nssm,以管理员运行cmd.exe,执行如下: C:\Users\prolds ...
- html5获取自己定位的方法
html5获取自己定位的方法直接用高德地图api 不要用百度地图api 不准确 <pre><!doctype html><html><head> < ...
- 12 Cookie、Session和JSP基础
1.会话技术 (1)会话概念:一次会话中包含多次请求和响应.一次会话:浏览器第一次给服务器资源发送请求,会话建立,直到有一方断开为止 (2)会话功能:在一次会话的范围内的多次请求间,共享数据 (3)会 ...
- 【C++札记】指针数组和数组指针
指针数组: 存储指针的数组,数组找那个的每个一元素都是指针 例: int* p1[4],p2[0]是一个指向int类型的指针 char* p2[4],p1[0]是一个指向char类型的指针 数组指针: ...
- SQL——DELETE(删)
一.DELETE语句的基本用法 DELETE语句用于删除表中的记录. DELETE语句语法: DELETE FROM 表名 WHERE 条件语句; 演示student表: 删除student表最后一行 ...
- SPI通讯(Serial Peripheral interface)
1. SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线:SCLK,MISO,MOSI,CS 2. SPI结构简图: 可以看出,SPI主从设备两端都有一个位移寄存器,数据在位 ...
- DFS解决八皇后问题
2019-07-29 16:49:15 #include <bits/stdc++.h> using namespace std; ][]; int tot; int check(int ...