2021-01-13 17:07:04 +00:00
|
|
|
package proxy
|
2020-04-12 01:51:41 +00:00
|
|
|
|
|
|
|
import (
|
2020-04-25 22:22:45 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2020-12-26 13:11:49 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-12-26 13:11:49 +00:00
|
|
|
"gitlab.com/inetmock/inetmock/pkg/logging"
|
2020-04-12 01:51:41 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"gopkg.in/elazarl/goproxy.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type proxyHttpHandler struct {
|
2020-10-02 09:56:48 +00:00
|
|
|
handlerName string
|
|
|
|
options httpProxyOptions
|
|
|
|
logger logging.Logger
|
2020-04-12 01:51:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 22:22:45 +00:00
|
|
|
type proxyHttpsHandler struct {
|
2020-10-02 09:56:48 +00:00
|
|
|
handlerName string
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
logger logging.Logger
|
2020-04-25 22:22:45 +00:00
|
|
|
}
|
2020-04-12 01:51:41 +00:00
|
|
|
|
2020-04-25 22:22:45 +00:00
|
|
|
func (p *proxyHttpsHandler) HandleConnect(req string, _ *goproxy.ProxyCtx) (*goproxy.ConnectAction, string) {
|
2020-10-02 09:56:48 +00:00
|
|
|
totalHttpsRequestCounter.WithLabelValues(p.handlerName).Inc()
|
2020-04-25 22:22:45 +00:00
|
|
|
p.logger.Info(
|
|
|
|
"Intercepting HTTPS proxy request",
|
|
|
|
zap.String("request", req),
|
|
|
|
)
|
2020-04-12 01:51:41 +00:00
|
|
|
|
2020-04-25 22:22:45 +00:00
|
|
|
return &goproxy.ConnectAction{
|
|
|
|
Action: goproxy.ConnectMitm,
|
|
|
|
TLSConfig: func(host string, ctx *goproxy.ProxyCtx) (*tls.Config, error) {
|
|
|
|
return p.tlsConfig, nil
|
|
|
|
},
|
|
|
|
}, ""
|
|
|
|
}
|
2020-04-12 01:51:41 +00:00
|
|
|
|
2020-04-25 22:22:45 +00:00
|
|
|
func (p *proxyHttpHandler) Handle(req *http.Request, ctx *goproxy.ProxyCtx) (retReq *http.Request, resp *http.Response) {
|
2020-10-02 09:56:48 +00:00
|
|
|
timer := prometheus.NewTimer(requestDurationHistogram.WithLabelValues(p.handlerName))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
totalRequestCounter.WithLabelValues(p.handlerName).Inc()
|
|
|
|
|
2020-04-12 01:51:41 +00:00
|
|
|
retReq = req
|
|
|
|
p.logger.Info(
|
|
|
|
"Handling request",
|
|
|
|
zap.String("source", req.RemoteAddr),
|
|
|
|
zap.String("host", req.Host),
|
|
|
|
zap.String("method", req.Method),
|
|
|
|
zap.String("protocol", req.Proto),
|
|
|
|
zap.String("path", req.RequestURI),
|
|
|
|
zap.Reflect("headers", req.Header),
|
|
|
|
)
|
|
|
|
|
2020-04-25 22:22:45 +00:00
|
|
|
var err error
|
|
|
|
if resp, err = ctx.RoundTrip(p.redirectHTTPRequest(req)); err != nil {
|
2020-04-12 01:51:41 +00:00
|
|
|
p.logger.Error(
|
2020-04-25 22:22:45 +00:00
|
|
|
"error while doing roundtrip",
|
2020-04-12 01:51:41 +00:00
|
|
|
zap.Error(err),
|
|
|
|
)
|
2020-04-25 22:22:45 +00:00
|
|
|
return req, nil
|
2020-04-12 01:51:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 22:22:45 +00:00
|
|
|
return
|
2020-04-12 01:51:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 22:22:45 +00:00
|
|
|
func (p proxyHttpHandler) redirectHTTPRequest(originalRequest *http.Request) (redirectReq *http.Request) {
|
|
|
|
redirectReq = &http.Request{
|
|
|
|
Method: originalRequest.Method,
|
|
|
|
URL: &url.URL{
|
2020-10-02 09:56:48 +00:00
|
|
|
Host: p.options.Target.host(),
|
2020-04-25 22:22:45 +00:00
|
|
|
Path: originalRequest.URL.Path,
|
|
|
|
ForceQuery: originalRequest.URL.ForceQuery,
|
|
|
|
Fragment: originalRequest.URL.Fragment,
|
|
|
|
Opaque: originalRequest.URL.Opaque,
|
|
|
|
RawPath: originalRequest.URL.RawPath,
|
|
|
|
RawQuery: originalRequest.URL.RawQuery,
|
|
|
|
User: originalRequest.URL.User,
|
|
|
|
},
|
|
|
|
Proto: originalRequest.Proto,
|
|
|
|
ProtoMajor: originalRequest.ProtoMajor,
|
|
|
|
ProtoMinor: originalRequest.ProtoMinor,
|
|
|
|
Header: originalRequest.Header,
|
|
|
|
Body: originalRequest.Body,
|
|
|
|
GetBody: originalRequest.GetBody,
|
|
|
|
ContentLength: originalRequest.ContentLength,
|
|
|
|
TransferEncoding: originalRequest.TransferEncoding,
|
|
|
|
Close: false,
|
|
|
|
Host: originalRequest.Host,
|
|
|
|
Form: originalRequest.Form,
|
|
|
|
PostForm: originalRequest.PostForm,
|
|
|
|
MultipartForm: originalRequest.MultipartForm,
|
|
|
|
Trailer: originalRequest.Trailer,
|
2020-04-12 01:51:41 +00:00
|
|
|
}
|
2020-04-25 22:22:45 +00:00
|
|
|
redirectReq = redirectReq.WithContext(context.Background())
|
2020-04-12 01:51:41 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|