2021-01-13 17:07:04 +00:00
|
|
|
package proxy
|
2020-04-08 22:59:22 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-02 09:56:48 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2020-04-12 01:51:41 +00:00
|
|
|
"fmt"
|
2020-12-26 13:11:49 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/api"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/config"
|
|
|
|
"gitlab.com/inetmock/inetmock/pkg/logging"
|
2020-04-08 22:59:22 +00:00
|
|
|
"go.uber.org/zap"
|
2020-04-12 01:51:41 +00:00
|
|
|
"gopkg.in/elazarl/goproxy.v1"
|
2020-04-08 22:59:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
name = "http_proxy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type httpProxy struct {
|
2020-04-13 22:14:56 +00:00
|
|
|
logger logging.Logger
|
2020-04-12 01:51:41 +00:00
|
|
|
proxy *goproxy.ProxyHttpServer
|
|
|
|
server *http.Server
|
2020-04-08 22:59:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func (h *httpProxy) Start(ctx api.PluginContext, cfg config.HandlerConfig) (err error) {
|
2020-10-02 09:56:48 +00:00
|
|
|
var opts httpProxyOptions
|
2020-12-26 13:11:49 +00:00
|
|
|
if err = cfg.Options.Unmarshal(&opts); err != nil {
|
2020-10-02 09:56:48 +00:00
|
|
|
return
|
|
|
|
}
|
2020-12-26 13:11:49 +00:00
|
|
|
listenAddr := cfg.ListenAddr()
|
2020-10-02 09:56:48 +00:00
|
|
|
h.server = &http.Server{Addr: listenAddr, Handler: h.proxy}
|
2020-04-12 01:51:41 +00:00
|
|
|
h.logger = h.logger.With(
|
2020-12-26 13:11:49 +00:00
|
|
|
zap.String("handler_name", cfg.HandlerName),
|
2020-10-02 09:56:48 +00:00
|
|
|
zap.String("address", listenAddr),
|
2020-04-12 01:51:41 +00:00
|
|
|
)
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
tlsConfig := ctx.CertStore().TLSConfig()
|
2020-04-25 22:22:45 +00:00
|
|
|
|
2020-04-12 01:51:41 +00:00
|
|
|
proxyHandler := &proxyHttpHandler{
|
2020-12-26 13:11:49 +00:00
|
|
|
handlerName: cfg.HandlerName,
|
2020-10-02 09:56:48 +00:00
|
|
|
options: opts,
|
|
|
|
logger: h.logger,
|
2020-04-12 01:51:41 +00:00
|
|
|
}
|
2020-04-25 22:22:45 +00:00
|
|
|
|
|
|
|
proxyHttpsHandler := &proxyHttpsHandler{
|
2020-12-26 13:11:49 +00:00
|
|
|
handlerName: cfg.HandlerName,
|
2020-10-02 09:56:48 +00:00
|
|
|
tlsConfig: tlsConfig,
|
|
|
|
logger: h.logger,
|
2020-04-25 22:22:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 01:51:41 +00:00
|
|
|
h.proxy.OnRequest().Do(proxyHandler)
|
2020-04-25 22:22:45 +00:00
|
|
|
h.proxy.OnRequest().HandleConnect(proxyHttpsHandler)
|
2020-04-12 01:51:41 +00:00
|
|
|
go h.startProxy()
|
2020-04-13 22:14:56 +00:00
|
|
|
return
|
2020-04-12 01:51:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *httpProxy) startProxy() {
|
2020-10-02 09:56:48 +00:00
|
|
|
if err := h.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
2020-04-12 01:51:41 +00:00
|
|
|
h.logger.Error(
|
|
|
|
"failed to start proxy server",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
|
|
|
}
|
2020-04-08 22:59:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 09:56:48 +00:00
|
|
|
func (h *httpProxy) Shutdown(ctx context.Context) (err error) {
|
2020-04-12 01:51:41 +00:00
|
|
|
h.logger.Info("Shutting down HTTP proxy")
|
2020-10-02 09:56:48 +00:00
|
|
|
if err = h.server.Shutdown(ctx); err != nil {
|
2020-04-12 01:51:41 +00:00
|
|
|
h.logger.Error(
|
|
|
|
"failed to shutdown proxy endpoint",
|
|
|
|
zap.Error(err),
|
|
|
|
)
|
2020-04-13 22:14:56 +00:00
|
|
|
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"failed to shutdown proxy endpoint: %w",
|
|
|
|
err,
|
|
|
|
)
|
2020-04-12 01:51:41 +00:00
|
|
|
}
|
2020-04-13 22:14:56 +00:00
|
|
|
return
|
2020-04-08 22:59:22 +00:00
|
|
|
}
|