MPS 高性能的正向反向代理库 - Go

2020-09-22 13:47:31 +08:00
 telan233

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

Feature

范例

examples

🧬 中间件

中间件可以拦截请求和响应,我们内置实现了多个中间件,包括 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))
}
1446 次点击
所在节点    Go 编程语言
2 条回复
heww
2020-09-23 09:58:34 +08:00
相比较标准库的 ReverseProxy 谁的性能更好一些?
fy
2020-10-28 11:29:07 +08:00
迟到的支持 不错

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/709391

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX