The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
telan233

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

  •  
  •   telan233 · Sep 22, 2020 · 1925 views
    This topic created in 2059 days ago, the information mentioned may be changed or developed.

    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 replies    2020-10-28 11:29:07 +08:00
    heww
        1
    heww  
       Sep 23, 2020
    相比较标准库的 ReverseProxy 谁的性能更好一些?
    fy
        2
    fy  
       Oct 28, 2020
    迟到的支持 不错
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1142 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 18:12 · PVG 02:12 · LAX 11:12 · JFK 14:12
    ♥ Do have faith in what you're doing.