2021-01-25 19:16:53 +00:00
|
|
|
package factory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/crypto"
|
2021-09-24 04:56:22 +00:00
|
|
|
"github.com/portainer/portainer/api/http/proxy/factory/agent"
|
|
|
|
"github.com/portainer/portainer/api/internal/endpointutils"
|
2022-08-25 23:55:55 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/url"
|
2022-09-16 16:18:44 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog/log"
|
2021-01-25 19:16:53 +00:00
|
|
|
)
|
|
|
|
|
2021-09-24 04:56:22 +00:00
|
|
|
// ProxyServer provide an extended proxy with a local server to forward requests
|
2021-01-25 19:16:53 +00:00
|
|
|
type ProxyServer struct {
|
|
|
|
server *http.Server
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2021-09-24 04:56:22 +00:00
|
|
|
// NewAgentProxy creates a new instance of ProxyServer that wrap http requests with agent headers
|
|
|
|
func (factory *ProxyFactory) NewAgentProxy(endpoint *portainer.Endpoint) (*ProxyServer, error) {
|
|
|
|
urlString := endpoint.URL
|
2021-09-29 23:58:10 +00:00
|
|
|
|
2022-09-16 16:18:44 +00:00
|
|
|
if endpointutils.IsEdgeEndpoint(endpoint) {
|
2021-09-24 04:56:22 +00:00
|
|
|
tunnel, err := factory.reverseTunnelService.GetActiveTunnel(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed starting tunnel")
|
|
|
|
}
|
2021-01-25 19:16:53 +00:00
|
|
|
|
2021-09-24 04:56:22 +00:00
|
|
|
urlString = fmt.Sprintf("http://127.0.0.1:%d", tunnel.Port)
|
2021-01-25 19:16:53 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 23:55:55 +00:00
|
|
|
endpointURL, err := url.ParseURL(urlString)
|
2021-01-25 19:16:53 +00:00
|
|
|
if err != nil {
|
2021-09-24 04:56:22 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed parsing url %s", endpoint.URL)
|
2021-01-25 19:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endpointURL.Scheme = "http"
|
|
|
|
httpTransport := &http.Transport{}
|
|
|
|
|
|
|
|
if endpoint.TLSConfig.TLS || endpoint.TLSConfig.TLSSkipVerify {
|
|
|
|
config, err := crypto.CreateTLSConfigurationFromDisk(endpoint.TLSConfig.TLSCACertPath, endpoint.TLSConfig.TLSCertPath, endpoint.TLSConfig.TLSKeyPath, endpoint.TLSConfig.TLSSkipVerify)
|
|
|
|
if err != nil {
|
2021-09-24 04:56:22 +00:00
|
|
|
return nil, errors.WithMessage(err, "failed generating tls configuration")
|
2021-01-25 19:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
httpTransport.TLSClientConfig = config
|
|
|
|
endpointURL.Scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy := newSingleHostReverseProxyWithHostHeader(endpointURL)
|
|
|
|
|
2021-09-24 04:56:22 +00:00
|
|
|
proxy.Transport = agent.NewTransport(factory.signatureService, httpTransport)
|
2021-01-25 19:16:53 +00:00
|
|
|
|
|
|
|
proxyServer := &ProxyServer{
|
2021-07-21 01:56:28 +00:00
|
|
|
server: &http.Server{
|
2021-01-25 19:16:53 +00:00
|
|
|
Handler: proxy,
|
|
|
|
},
|
2021-07-21 01:56:28 +00:00
|
|
|
Port: 0,
|
2021-01-25 19:16:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 01:56:28 +00:00
|
|
|
err = proxyServer.start()
|
|
|
|
if err != nil {
|
2021-09-24 04:56:22 +00:00
|
|
|
return nil, errors.Wrap(err, "failed starting proxy server")
|
2021-07-21 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 07:58:26 +00:00
|
|
|
return proxyServer, nil
|
2021-01-25 19:16:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (proxy *ProxyServer) start() error {
|
|
|
|
listener, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy.Port = listener.Addr().(*net.TCPAddr).Port
|
2022-09-16 16:18:44 +00:00
|
|
|
|
2021-01-25 19:16:53 +00:00
|
|
|
go func() {
|
|
|
|
proxyHost := fmt.Sprintf("127.0.0.1:%d", proxy.Port)
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Debug().Str("host", proxyHost).Msg("starting proxy server")
|
2021-01-25 19:16:53 +00:00
|
|
|
|
|
|
|
err := proxy.server.Serve(listener)
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Debug().Str("host", proxyHost).Msg("exiting proxy server")
|
2021-01-25 19:16:53 +00:00
|
|
|
|
2021-07-21 01:56:28 +00:00
|
|
|
if err != nil && err != http.ErrServerClosed {
|
2022-09-16 16:18:44 +00:00
|
|
|
log.Debug().Str("host", proxyHost).Err(err).Msg("proxy server exited with an error")
|
2021-01-25 19:16:53 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close shuts down the server
|
|
|
|
func (proxy *ProxyServer) Close() {
|
|
|
|
if proxy.server != nil {
|
|
|
|
proxy.server.Close()
|
|
|
|
}
|
|
|
|
}
|