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)
## ? 如何使用
一个简单的HTTP代理服务
```go
package main
import (
"github.com/telanflow/mps"
"log"
"net/http"
)
func main() {
proxy := mps.NewHttpProxy()
log.Fatal(http.ListenAndServe(":8080", proxy))
}
```
更多 [范例](https://github.com/telanflow/mps/tree/master/examples)
## ? 中间件
中间件可以拦截请求和响应,我们内置实现了多个中间件,包括 [BasicAuth](https://github.com/telanflow/mps/tree/master/middleware)
```go
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))
}
```
## ♻️ 过滤器
过滤器可以对请求和响应进行筛选,统一进行处理。
它基于中间件实现。
```go
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))
}
```
## ? 开源许可
`MPS`中的源代码在[BSD 3 License](/LICENSE)下可用。
- 授权协议:
- BSD
- 开发语言:
- Go 查看源码»
- 操作系统:
- Windows Mac Linux