Giter VIP home page Giter VIP logo

telanflow / mps Goto Github PK

View Code? Open in Web Editor NEW
95.0 4.0 17.0 192 KB

MPS is a high-performance HTTP(S) proxy library that supports forward proxies, reverse proxies, man-in-the-middle proxies, tunnel proxies, Websocket proxies. MPS 是一个高性能HTTP(s)中间代理库,它支持正向代理、反向代理、中间人代理、隧道代理、Websocket代理

License: BSD 3-Clause "New" or "Revised" License

Go 99.65% Shell 0.35%
proxy http-proxy https-proxy forward-proxy reverse-proxy weksocket-proxy middleware tunnel-proxy agent proxy-server mps mitmproxy socks5-proxy reverse forward proxies mitm man-in-the-middle goproxy go-proxy

mps's Introduction


MPS

English | 🇨🇳中文

📖 Introduction

MPS stars GitHub release (latest SemVer) GitHub go.mod Go version license

MPS (middle-proxy-server) is an high-performance middle proxy library. support HTTP, HTTPS, Websocket, ForwardProxy, ReverseProxy, TunnelProxy, MitmProxy.

🚀 Features

  • Http Proxy
  • Https Proxy
  • Forward Proxy
  • Reverse Proxy
  • Tunnel Proxy
  • Mitm Proxy (Man-in-the-middle)
  • WekSocket Proxy

🧰 Install

go get -u github.com/telanflow/mps

🛠 How to use

A simple proxy service

package main

import (
    "github.com/telanflow/mps"
    "log"
    "net/http"
)

func main() {
    proxy := mps.NewHttpProxy()
    log.Fatal(http.ListenAndServe(":8080", proxy))
}

More examples

🧬 Middleware

Middleware can intercept requests and responses. we have several middleware implementations built in, including 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))
}

♻️ Filters

Filters can filter requests and responses for unified processing. It is based on middleware implementation.

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))
}

📄 License

Source code in MPS is available under the BSD 3 License.

mps's People

Contributors

telanflow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

mps's Issues

Migrate request context on mitmHandler

Hi, so I have a TLS listener with my mps proxy.
During handshake, I generate a variable and need to pass this to my .OnRequest(...) function.
However, anything I set on req Context with the following function is not propagated?

func getInsertor(proxy *mps.HttpProxy) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req * http.Request) {
		logger := log.WithField("ip", req.RemoteAddr).WithField("host", req.Host)

		testID, err := pkg.ExtractTestID(req)
		if testID <= 0 || err != nil {
			logger.WithError(err).Error("could not extract testid")
			_, _ = rw.Write([]byte("invalid credentials provided"))
			rw.WriteHeader(http.StatusForbidden)
			return
		}
		proxy.ServeHTTP(rw, req.WithContext(context.WithValue(req.Context(), "testid", testID)))
	})
}

	proxyInstance := mps.NewHttpProxy()
	mitmHandler := mps.NewMitmHandler()

	mitmHandler.OnRequest().DoFunc(func(req *http.Request, ctx *mps.Context) (*http.Request, *http.Response){
		testID, ok := req.Context().Value("testid").(uint64) // This will always be nil/zero

Performance during MiTM

Hi, just to let you know that when using mitmHandler, a lot of CPU time is spent in mitm_handler.transmit.

image

Not sure if the client-side connections are pooled?

增加wss支持

如题,现在mitm proxy只支持https的,能否增加https+websokcet(wss)呢?

请问如何作为库在GIN中使用?

请问如何作为库在GIN中使用,我这样写不能正常工作

r.Any("/*path", func(c *gin.Context) {
	// 检查是否为WebSocket协议
	if c.GetHeader("Upgrade") == "websocket" {
		WSProxy(c.Request.URL).ServeHTTP(c.Writer, c.Request)
	} else {
		// 如果不是WebSocket协议,则使用HTTP协议进行反向代理
		HTTPProxy("https://www.baidu.com").ServeHTTP(c.Writer, c.Request)
	}
})

func WSProxy(endPoint *url.URL) *mps.WebsocketHandler {
	// 启动websocket代理
	ws := mps.NewWebsocketHandler()
	ws.Transport().Proxy = func(r *http.Request) (*url.URL, error) {
		endPoint.Host = endPointAddr
		log.Info("%v", endPoint)
		return endPoint, nil
	}
	return ws
}

HTTP Proxy TunnelHandler 使用 BasicAuth 中间件无效

  1. 在几个代理的 ServeHTTP 中运行中间件后,如果 err == nil ,不管 response 返回什么状态码都不会终止连接目标服务器
    参考:
    if err != nil && err != MethodNotSupportErr {
  2. 在 BasicAuth 中间件中,如果验证不通过,会返回 statusCode = 407 但是由于 1 ,这个响应会被忽略,继续连接目标服务器
    参考:
    return BasicUnauthorized(req, realm), nil

是我用的方法不对吗?还是说这里逻辑有问题??

Firefox: SSL_ERROR_RX_RECORD_TOO_LONG

Hi, trying out your fork with below code, returns either Secure Connection Failed or SSL_ERROR_RX_RECORD_TOO_LONG. Anything I am doing wrong? No logs from mps.

	proxyInstance := mps.NewHttpProxy()
	mitmHandler, err := mps.NewMitmHandlerWithCert(
		proxyInstance.Ctx,
		crtBytes,
		keyBytes,
	)
	if err != nil {
		log.Panic(err)
	}
	proxyInstance.HandleConnect = mitmHandler
        log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", config.ListenPort), proxyInstance))

MiTM passthrough

Hi, is it possible to perform TLS passthrough based on a set of conditions (domain, ...) ?
So if those conditions match, no TLS MiTM should be done.

Ignored crt certificate

Hi
I generated cert but have a problem

Software is preventing Firefox from safely connecting to this site

www.youtube.com is probably a secure site, but a secure connection cannot be established. This problem is caused by mps.github.io, software on your computer or on your network.

ca.crt i installed as root cert in my windows system

握手

请老师增加握手阶段检测wss,一个http代理自动识别wss,tcp,HTTPS,类似mitmproxy 8.1,兄弟正打算用fyne为这个库写一个抓包ui界面

socket

socket4/5代理可以支持下吗?有些ip是socket4协议的,另外不知道用kcp实现一个内网通会不会快一点,通过扫描局域网网段

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.