httransform is the library/framework to build your own HTTP proxies. It relies on high-performant and memory-efficient fasthttp library as HTTP base layer and can give you the ability to build a proxy where you can control every aspect.
Main features of this framework:
- Support of HTTP (plain HTTP) proxy protocol.
- Support of HTTPS (with CONNECT method) protocol. This library does MITM and provides the possibility to generate TLS certificates for the hosts on-the-fly.
- Keeps and maintains the order of headers and their case (no normalization).
- Supports the concept of layers or middlewares which process HTTP requests and responses.
- Supports custom executors: a function which converts HTTP requests to HTTP responses. Allowing your proxy to fetch the data from other services, which are not necessarily HTTP. The executor simply converts HTTP request structure to HTTP response.
Please check the full documentation for more details.
Just a small example to give you the feeling of how it all looks like:
package main
import (
"net"
"github.com/9seconds/httransform"
)
// These are generates examples of self-signed certificates
// to simplify the example.
var caCert = []byte(`-----BEGIN CERTIFICATE-----
MIICWzCCAcSgAwIBAgIJAJ34yk7oiKv5MA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTgxMjAyMTQyNTAyWhcNMjgxMTI5MTQyNTAyWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB
gQDL7Hzfmx7xfFWTRm26t/lLsCZwOri6VIzp2dYM5Hp0dV4XUZ+q60nEbHwN3Usr
GKAK/Rsr9Caam3A18Upn2ly69Tyr29kVK+PlsOgSSCUnAYcqT166/j205n3CGNLL
OPtQKfAT/iH3dPBObd8N4FR9FlXiYIiAp1opCbyu2mlHiwIDAQABo1MwUTAdBgNV
HQ4EFgQUOJ+uGtIhHxXHPNESBNI4YbwAl+wwHwYDVR0jBBgwFoAUOJ+uGtIhHxXH
PNESBNI4YbwAl+wwDwYDVR0TAQH/BAUwAwEB/zANBgkqhk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oWx0DJIQRZifeCvw
4J45NsSQjuuAAWs=
-----END PRIVATE KEY-----`)
func main() {
ln, err := net.Listen("tcp", "127.0.0.1:3128")
if err != nil {
panic(err)
}
opts := ServerOpts{
CertCA: caCert,
CertKey: caPrivateKey,
Layers: []Layer{
&ProxyAuthorizationBasicLayer{
User: []byte("user"),
Password: []byte("password"),
Realm: "test",
},
&AddRemoveHeaderLayer{
AbsentRequestHeaders: []string{"proxy-authorization"},
},
},
}
srv, err := NewServer(opts)
if err != nil {
panic(err)
}
if err := srv.Serve(ln); err != nil {
panic(err)
}
}
This will create an HTTP proxy on 127.0.0.1:3128
. It will also require
authentication (user
and password
) and will remove the Proxy-Authorization
header before sending the request further.