原文: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. abp_vue导入导出excel

    后端abp,前端vue导入excel,开始准备用直接用npoi,觉得要写太多的代码,就算从以前的复制粘贴也麻烦,所以偷懒直接用别人的轮子 Magicodes.IE.这样可以节省很多工作,根据实体生成e ...

  2. 谷歌浏览器调试swoole长连接方法

    谷歌浏览器调试swoole长连接方法network标签里面的ws 看frames  

  3. python递归函数(10)

    一个函数在函数体内部调用自己,这样的函数称为递归函数,递归的次数在python是有限制的,默认递归次数是997次,超过997次会报错:RecursionError. 一.递归函数案例 案例一:计算数字 ...

  4. Hystrix学习笔记

     服务降级: DashBoard: 例中的8001为要监控的地址:

  5. TCP/IP协议的工作流程

    1.在源主机上,应用层将一串应用数据流向下传输给传输层 2.传输层将应用数据截流成分组,并加上TCP报头形成TCP段,向下递交给网络层 3.在网络层给TCP段加上包括源主机,目的主机IP地址的IP报头 ...

  6. controller进行数据保存以及作用域

    controller进行数据保存以及作用域 一.request域 1.ModelAndView 在ModelAndView中进行存键值对,也可以进行跳转的地址存储,但是返回类型必须是ModelAndV ...

  7. Django框架之DRF get post put delete 使用简单示例 (利用序列化反序列化)

    路由配置 # 路由 from django.conf.urls import url from django.contrib import admin from app01 import views ...

  8. STL对map排序

    // sort start typedef struct{ ... }Node; // Map的键是字符串,值是结构体.虽然有自动排序特性,但是按字符串的排序并不能符合要求.此时,Map的key可以视 ...

  9. java之hibernate之基于外键的一对一单向关联映射

    这篇讲解基于外键的一对一单向关联映射 1.考察如下信息,人和身份证之间是一个一对一的关系.表的设计 注意:基于外键的一对一关联的表结构和多对一的表结构是一致的,但是,外键是唯一的. 2.类的结构 Pe ...

  10. C# 连接SQLServer数据库自动生成model类代码

    Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading ...