

3min快速掌握Go语言正/反向代理的姿势 - 博客猿马甲哥
source link: https://www.cnblogs.com/JulianHuang/p/16867844.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

3min快速掌握Go语言正/反向代理的姿势
先重温一下什么叫反向代理,正向代理。
所谓正向,反向代理取决于代理的是出站请求,还是入站请求。
正向代理: 代理的出站请求, 客户端能感知到代理程序,架构上距离客户端更近。
反向代理: 代理的是入站请求,客户端认为代理程序就是服务器,客户端感知不到代理逻辑,架构上距离服务端更近。
前几天利用golang实现了反向代理程序,引出了Host请求头在反代中的关键作用。
对于proxy.com的请求,都被透明无感代理到A.com
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
)
func ReverseProxyHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("receive a request from:", r.RemoteAddr, r.Header)
target := "www.baidu.com"
director := func(req *http.Request) {
req.URL.Scheme = "https"
req.URL.Host = target
req.Host = target
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(w, r)
}
func main() {
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", http.HandlerFunc(ReverseProxyHandler)); err != nil {
log.Fatal(err)
}
}
这几天刚好遇到了一个正常代理的case, 简单记录一下。
package main
import (
"fmt"
"log"
"net/http"
"net/http/httputil"
)
func ProxyHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("receive a request from {0} {1}: \n", r.RemoteAddr, r.Header)
if r.Host != "localhost:8080" {
director := func(req *http.Request) {
req.URL.Scheme = "http"
req.URL.Host = r.Host
req.Host = r.Host
}
proxy := &httputil.ReverseProxy{Director: director}
proxy.ServeHTTP(w, r)
} else {
http.NotFound(w, r)
}
}
func main() {
if err := http.ListenAndServe(":8080", http.HandlerFunc(ProxyHandler)); err != nil {
log.Fatal(err)
}
}
其中要注意的就是,正向代理式要规避死循环代理。
使用该服务作为代理程序,将可以出站访问任何地址。
使用时,针对httpclient设置proxy
//adding the proxy settings to the Transport object
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
//adding the Transport object to the http Client
client := &http.Client{
Transport: transport,
}
下面使用curl指令演示(-x 后接代理地址)curl -x 127.0.0.1:8080 www.baidu.com
GO快闪#
本文总结了go语言正反向代理的姿势。
Recommend
-
150
想要让角色显得更加生动,为他配上动作是一个非常重要的要素。相比还是有很多人并不擅长动作方面的作画。 例如: 想要挑战具有跃动感的动作,但是却无法在脑海中完成动作的设想。 
-
93
Nginx 反向代理工作原理简介与配置详解 - 李雄
-
114
Nginx反向代理WebSocket响应403的解决办法 在Nginx反向代理一个带有WebSocket功能的Spring Web程序(
-
105
首先准备三台虚拟机 Client eth0192.168.4.100 Porxy eth0 192.168.4.5 eth1 192.168.2.5 Web1 eth1 192.168.2.100Web1 安装 httpd 关闭防火墙 yum -y i...
-
85
反向代理实现2.4=====================1.准备三台机器:A:客户端B:中间件代理服务器C:后端服务器2.查看B依赖模块:httpd-M|grepproxyproxy_http_module(shared)3.创建Bproxy的配置文件:(建议放在conf.d/目录下)vim/etc/httpd/conf.d/proxy.conf格式:ProxyPass"/&...
-
70
优惠——羊毛党的兴奋剂,虚假促销——电商的清仓法门。价格变化有多异常,你就有多需要查价工具,比如这款让我受到惊吓的无线路由,日常价格是六七百,六月初曾涨到 1899 元。
-
10
coffeeboylinux1周前编译选项和内核编译 首先我们都知道,Linux内核如果用O0编译,是无法编译过的,Linux的内核编译,要么是O2...
-
6
Nginx反向代理(多域名,多ip)防攻击,快速切换 2015年7月2日 由 admin 留言 » 做站最烦人的就是被攻击,...
-
2
1.分支结构的标准公式//单条语句if(测试条件) 条件为真所执行的语句else 条件为假所执行的语句//多条语句if(...
-
7
Nginx是一个开源的高性能HTTP和反向代理服务器,它也可以用作邮件代理服务器和通用的TCP/UDP代理服务器。在本...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK