原文: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. Qt deletelater函数分析(1)

               生活的全部意义在于无穷地探索尚未知道的东西,在于不断地增加更多的知识.--左拉 该函数是QObject类的函数:                             ---- ...

  2. 数组,const,#define

    #include<stdio.h> #define COUNT 6 int main(){// const类型的常量不能作为数组的个数,大部分编译器不支持//    const int C ...

  3. ARM中断深入分析几点

    ARM中断深入分析几点 1.程序发生中断后,是如何跳转到中断程序里面的? 2.执行完中断后,如何返回到原来被打断的地方接着执行呢? 3.ARM处理器的流水线结构对中断返回地址的计算有什么影响? 4.A ...

  4. mysql 免费的图形管理工具

    在学习go语言开发时,使用了mysql 使用了两天mysql命令行,感觉实在是无法忍受, 找到了一个免费好用的 图形数据库管理工具SQLyog Professional 版本: 注册名:luoye25 ...

  5. D03-R语言基础学习

    R语言基础学习——D03 20190423内容纲要: 1.导入数据 (1)从键盘输入 (2)从文本文件导入 (3)从excel文件导入 2.用户自定义函数   3.R访问MySQL数据库 (1)安装R ...

  6. jenkins配合dockerfile部署项目

    前言 本节需要对jenkinsfile有点了解,对dockerfile有点了解,对shell有点了解,对docker有点了解 执行流程 jenkins拉取代码仓库中的代码 jenkins执行jenki ...

  7. Pycharm下直接升级库所遇到的'main'问题

    Pycharm下直接升级库所遇到的pip模块中无'main'问题 Author : Benjamin142857 Date : 8/19/2018 对于Pycharm中直接升级库,只需在 \(Sett ...

  8. grafana部署安装

    部署grafana 在prometheus& grafana server节点部署grafana服务. 1. 下载&安装 # 下载 [root@prometheus ~]# cd /u ...

  9. 「LibreOJ NOI Round #2」不等关系

    「LibreOJ NOI Round #2」不等关系 解题思路 令 \(F(k)\) 为恰好有 \(k\) 个大于号不满足的答案,\(G(k)\) 表示钦点了 \(k\) 个大于号不满足,剩下随便填的 ...

  10. 解决springboot 新版本 2.1.6 spring-boot-starter-actuator 访问报404

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...