MPS 是一个高性能 HTTP(s)中间代理库,它支持正向代理、反向代理、中间人代理、隧道代理、Websocket 代理.
MPS is a high-performance HTTP(S) proxy library that supports forward proxies, reverse proxies, man-in-the-middle proxies, tunnel proxies, Websocket proxies.
项目地址:https://github.com/telanflow/mps
中间件可以拦截请求和响应,我们内置实现了多个中间件,包括 BasicAuth
func main() {
proxy := mps.NewHttpProxy()
proxy.Use(mps.MiddlewareFunc(func(req *http.Request, ctx *mps.Context) (*http.Response, error) {
log.Printf("[INFO] middleware -- %s %s", req.Method, req.URL)
return ctx.Next(req)
}))
proxy.UseFunc(func(req *http.Request, ctx *mps.Context) (*http.Response, error) {
log.Printf("[INFO] middleware -- %s %s", req.Method, req.URL)
resp, err := ctx.Next(req)
if err != nil {
return nil, err
}
log.Printf("[INFO] resp -- %d", resp.StatusCode)
return resp, err
})
log.Fatal( http.ListenAndServe(":8080", proxy))
}
过滤器可以对请求和响应进行筛选,统一进行处理。 它基于中间件实现。
func main() {
proxy := mps.NewHttpProxy()
// request Filter Group
reqGroup := proxy.OnRequest(mps.FilterHostMatches(regexp.MustCompile("^.*$")))
reqGroup.DoFunc(func(req *http.Request, ctx *mps.Context) (*http.Request, *http.Response) {
log.Printf("[INFO] req -- %s %s", req.Method, req.URL)
return req, nil
})
// response Filter Group
respGroup := proxy.OnResponse()
respGroup.DoFunc(func(resp *http.Response, err error, ctx *mps.Context) (*http.Response, error) {
if err != nil {
log.Printf("[ERRO] resp -- %s %v", ctx.Request.Method, err)
return nil, err
}
log.Printf("[INFO] resp -- %d", resp.StatusCode)
return resp, err
})
log.Fatal( http.ListenAndServe(":8080", proxy))
}
1
heww 2020-09-23 09:58:34 +08:00
相比较标准库的 ReverseProxy 谁的性能更好一些?
|
2
fy 2020-10-28 11:29:07 +08:00
迟到的支持 不错
|