Go 實(shí)現(xiàn)HTTP中間人代理的操作
goproxy
Go HTTP(S)代理庫, 支持中間人代理解密HTTPS
安裝
go get github.com/ouqiang/goproxy
使用
package main
import (
"net/http"
"time"
"github.com/ouqiang/goproxy"
)
func main() {
proxy := goproxy.New()
server := &http.Server{
Addr: ":8080",
Handler: proxy,
ReadTimeout: 1 * time.Minute,
WriteTimeout: 1 * time.Minute,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
代理測試
curl -x localhost:8080 https://www.baidu.com
中間人代理, 解密HTTPS
系統(tǒng)需導(dǎo)入根證書 mitm-proxy.crt
package main
import (
"crypto/tls"
"net/http"
"sync"
"time"
"github.com/ouqiang/goproxy"
)
// 實(shí)現(xiàn)證書緩存接口
type Cache struct {
m sync.Map
}
func (c *Cache) Set(host string, cert *tls.Certificate) {
c.m.Store(host, cert)
}
func (c *Cache) Get(host string) *tls.Certificate {
v, ok := c.m.Load(host)
if !ok {
return nil
}
return v.(*tls.Certificate)
}
func main() {
proxy := goproxy.New(goproxy.WithDecryptHTTPS(&Cache{}))
server := &http.Server{
Addr: ":8080",
Handler: proxy,
ReadTimeout: 1 * time.Minute,
WriteTimeout: 1 * time.Minute,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
事件處理
實(shí)現(xiàn)Delegate接口
type Delegate interface {
// Connect 收到客戶端連接
Connect(ctx *Context, rw http.ResponseWriter)
// Auth 代理身份認(rèn)證
Auth(ctx *Context, rw http.ResponseWriter)
// BeforeRequest HTTP請求前 設(shè)置X-Forwarded-For, 修改Header、Body
BeforeRequest(ctx *Context)
// BeforeResponse 響應(yīng)發(fā)送到客戶端前, 修改Header、Body、Status Code
BeforeResponse(ctx *Context, resp *http.Response, err error)
// ParentProxy 上級代理
ParentProxy(*http.Request) (*url.URL, error)
// Finish 本次請求結(jié)束
Finish(ctx *Context)
// 記錄錯(cuò)誤信息
ErrorLog(err error)
}
type EventHandler struct{}
func (e *EventHandler) Connect(ctx *goproxy.Context, rw http.ResponseWriter) {
// 保存的數(shù)據(jù)可以在后面的回調(diào)方法中獲取
ctx.Data["req_id"] = "uuid"
// 禁止訪問某個(gè)域名
if strings.Contains(ctx.Req.URL.Host, "example.com") {
rw.WriteHeader(http.StatusForbidden)
ctx.Abort()
return
}
}
func (e *EventHandler) Auth(ctx *goproxy.Context, rw http.ResponseWriter) {
// 身份驗(yàn)證
}
func (e *EventHandler) BeforeRequest(ctx *goproxy.Context) {
// 修改header
ctx.Req.Header.Add("X-Request-Id", ctx.Data["req_id"].(string))
// 設(shè)置X-Forwarded-For
if clientIP, _, err := net.SplitHostPort(ctx.Req.RemoteAddr); err == nil {
if prior, ok := ctx.Req.Header["X-Forwarded-For"]; ok {
clientIP = strings.Join(prior, ", ") + ", " + clientIP
}
ctx.Req.Header.Set("X-Forwarded-For", clientIP)
}
// 讀取Body
body, err := ioutil.ReadAll(ctx.Req.Body)
if err != nil {
// 錯(cuò)誤處理
return
}
// Request.Body只能讀取一次, 讀取后必須再放回去
// Response.Body同理
ctx.Req.Body = ioutil.NopCloser(bytes.NewReader(body))
}
func (e *EventHandler) BeforeResponse(ctx *goproxy.Context, resp *http.Response, err error) {
if err != nil {
return
}
// 修改response
}
// 設(shè)置上級代理
func (e *EventHandler) ParentProxy(req *http.Request) (*url.URL, error) {
return url.Parse("http://localhost:1087")
}
func (e *EventHandler) Finish(ctx *goproxy.Context) {
fmt.Printf("請求結(jié)束 URL:%s\n", ctx.Req.URL)
}
// 記錄錯(cuò)誤日志
func (e *EventHandler) ErrorLog(err error) {
log.Println(err)
}
func main() {
proxy := goproxy.New(goproxy.WithDelegate(&EventHandler{}))
server := &http.Server{
Addr: ":8080",
Handler: proxy,
ReadTimeout: 1 * time.Minute,
WriteTimeout: 1 * time.Minute,
}
err := server.ListenAndServe()
if err != nil {
panic(err)
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
golang 內(nèi)存對齊的實(shí)現(xiàn)
在代碼編譯階段,編譯器會(huì)對數(shù)據(jù)的存儲布局進(jìn)行對齊優(yōu)化,本文主要介紹了golang 內(nèi)存對齊的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08
Golang?Template實(shí)現(xiàn)自定義函數(shù)的操作指南
這篇文章主要為大家詳細(xì)介紹了Golang如何利用Template實(shí)現(xiàn)自定義函數(shù)的操作,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-02-02
Go語言實(shí)現(xiàn)有規(guī)律的數(shù)字版本號的排序工具
這篇文章主要為大家詳細(xì)介紹了如何利用Go語言實(shí)現(xiàn)有規(guī)律的數(shù)字版本號的排序工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01
使用Lumberjack+zap進(jìn)行日志切割歸檔操作
這篇文章主要介紹了使用Lumberjack+zap進(jìn)行日志切割歸檔操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
Go語言基礎(chǔ)切片的創(chuàng)建及初始化示例詳解
這篇文章主要為大家介紹了Go語言基礎(chǔ)切片的創(chuàng)建及初始化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2021-11-11
Golang排列組合算法問題之全排列實(shí)現(xiàn)方法
這篇文章主要介紹了Golang排列組合算法問題之全排列實(shí)現(xiàn)方法,涉及Go語言針對字符串的遍歷及排列組合相關(guān)操作技巧,需要的朋友可以參考下2017-01-01

