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 ...
随机推荐
- AOP+Token防止表单重复提交
表单重复提交: 由于用户误操作,多次点击表单提交按钮 由于网速等原因造成页面卡顿,用户重复刷新提交页面 避免表单重复提交的方式: 1.页面上的按钮做防重复点击操作 2.在数据库中可以做唯一约束 3.利 ...
- 【C++札记】函数重载
概述 函数重载指在相同作用域中多个函数名相同,函数的形参不同. 遵循规则: 1.函数名相同 2.函数的参数,类型,个数或顺序不一样都可以构成重载. 3.函数返回值得类型不同不构成重载,编译直接报错,存 ...
- linux初识1
linux 操作系统 概念性的理解 1.Linux内置解释器bash 相当于pyhon解释器 2.Linux的内部大多是使用python去书写 云计算 1.只需要 花钱,买腾讯,阿里云服务器 2.专人 ...
- python学习67-面向对象-封装
封装 1.什么是封装? 根据名字寓意为:把一个东西装起来,然后密封,类似这样的面向对象的编程为封装. 真正的封装是明确的区别内外,只能在内部用,外部无法调用. 2. 举例: class Car: _s ...
- AS3事件类型
Event事件类型: ACTIVATE 激活事件 DEACTIVATE 对象休眠事件 ADDED 可视对象添加事件 ADDED_TO_STAGE 可视对象添加到舞台事件 REMO ...
- IntelliJ IDEA 2019 激活码 | 全产品 | 跨平台 | Goland | PhpStorm | Rider | CentOS | Windows
>>> 下载地址: https://kenkao.pipipan.com/fs/14896800-375468824 >>> 下载地址2: https://pan. ...
- iOS 打包静态库.a文件(真机版 + 虚拟机)
我们以打包IOS开发中封装的高地地图基础功能包 GDMap为例. 1>我们需要准备好需要打包的GDMap 2>接下来我们开始新建一个工程文件取名GDMap 3>删掉无 ...
- 平衡二叉树详解——PHP代码实现
一.什么是平衡二叉树 平衡二叉树(Self-Balancing Binary Search Tree 或者 Height-Balancing Binary Search Tree)译为 自平衡的二叉查 ...
- Vue 使用技巧手记
watch监听 Vue监听属性有很多种写法,也有几个配置选项 使用配置项 new Vue({ watch:{ content:{ handler(old,newVal){ console.log(ol ...
- Bootstrap 结合 PHP ,做简单的登录以及注册界面及功能
登录实现 HTML代码 <div class="container"> <?php if (isset($error_msg)): ?> <div c ...