V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
telan233
V2EX  ›  Go 编程语言

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

  •  
  •   telan233 · 2020-09-22 13:47:31 +08:00 · 1442 次点击
    这是一个创建于 1310 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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

    • 支持 HTTP(s)代理、正向代理、反向代理、中间人代理、Websocket 代理等
    • TCP 连接池、证书池
    • 中间件( Middleware )
    • 过滤器( Filter)

    范例

    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))
    }
    
    2 条回复    2020-10-28 11:29:07 +08:00
    heww
        1
    heww  
       2020-09-23 09:58:34 +08:00
    相比较标准库的 ReverseProxy 谁的性能更好一些?
    fy
        2
    fy  
       2020-10-28 11:29:07 +08:00
    迟到的支持 不错
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1010 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:58 · PVG 05:58 · LAX 14:58 · JFK 17:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.