2017-05-23 18:56:10 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2018-12-09 03:49:27 +00:00
|
|
|
"strconv"
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
"github.com/orcaman/concurrent-map"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
2019-11-12 23:41:42 +00:00
|
|
|
"github.com/portainer/portainer/api/docker"
|
|
|
|
"github.com/portainer/portainer/api/http/proxy/factory"
|
2017-05-23 18:56:10 +00:00
|
|
|
)
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// TODO: contain code related to legacy extension management
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
type (
|
2019-11-12 23:41:42 +00:00
|
|
|
// Manager represents a service used to manage proxies to endpoints and extensions.
|
2018-05-06 07:15:57 +00:00
|
|
|
Manager struct {
|
2019-11-12 23:41:42 +00:00
|
|
|
proxyFactory *factory.ProxyFactory
|
|
|
|
endpointProxies cmap.ConcurrentMap
|
2018-12-09 03:49:27 +00:00
|
|
|
extensionProxies cmap.ConcurrentMap
|
|
|
|
legacyExtensionProxies cmap.ConcurrentMap
|
2018-05-06 07:15:57 +00:00
|
|
|
}
|
|
|
|
)
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
// NewManager initializes a new proxy Service
|
2020-05-20 05:23:15 +00:00
|
|
|
func NewManager(dataStore portainer.DataStore, signatureService portainer.DigitalSignatureService, tunnelService portainer.ReverseTunnelService, clientFactory *docker.ClientFactory) *Manager {
|
2017-05-23 18:56:10 +00:00
|
|
|
return &Manager{
|
2019-11-12 23:41:42 +00:00
|
|
|
endpointProxies: cmap.New(),
|
2018-12-09 03:49:27 +00:00
|
|
|
extensionProxies: cmap.New(),
|
|
|
|
legacyExtensionProxies: cmap.New(),
|
2020-05-20 05:23:15 +00:00
|
|
|
proxyFactory: factory.NewProxyFactory(dataStore, signatureService, tunnelService, clientFactory),
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// CreateAndRegisterEndpointProxy creates a new HTTP reverse proxy based on endpoint properties and and adds it to the registered proxies.
|
2018-05-28 14:40:33 +00:00
|
|
|
// It can also be used to create a new HTTP reverse proxy and replace an already registered proxy.
|
2019-11-12 23:41:42 +00:00
|
|
|
func (manager *Manager) CreateAndRegisterEndpointProxy(endpoint *portainer.Endpoint) (http.Handler, error) {
|
|
|
|
proxy, err := manager.proxyFactory.NewEndpointProxy(endpoint)
|
2018-05-28 14:40:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
manager.endpointProxies.Set(string(endpoint.ID), proxy)
|
2017-05-23 18:56:10 +00:00
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// GetEndpointProxy returns the proxy associated to a key
|
|
|
|
func (manager *Manager) GetEndpointProxy(endpoint *portainer.Endpoint) http.Handler {
|
|
|
|
proxy, ok := manager.endpointProxies.Get(string(endpoint.ID))
|
2017-05-23 18:56:10 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-12 23:41:42 +00:00
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
return proxy.(http.Handler)
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// DeleteEndpointProxy deletes the proxy associated to a key
|
|
|
|
func (manager *Manager) DeleteEndpointProxy(endpoint *portainer.Endpoint) {
|
|
|
|
manager.endpointProxies.Remove(string(endpoint.ID))
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// CreateExtensionProxy creates a new HTTP reverse proxy for an extension and
|
|
|
|
// registers it in the extension map associated to the specified extension identifier
|
|
|
|
func (manager *Manager) CreateExtensionProxy(extensionID portainer.ExtensionID) (http.Handler, error) {
|
2019-11-12 23:41:42 +00:00
|
|
|
proxy, err := manager.proxyFactory.NewExtensionProxy(extensionID)
|
2018-02-23 02:10:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
manager.extensionProxies.Set(strconv.Itoa(int(extensionID)), proxy)
|
2018-02-23 02:10:26 +00:00
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// GetExtensionProxy returns an extension proxy associated to an extension identifier
|
|
|
|
func (manager *Manager) GetExtensionProxy(extensionID portainer.ExtensionID) http.Handler {
|
|
|
|
proxy, ok := manager.extensionProxies.Get(strconv.Itoa(int(extensionID)))
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return proxy.(http.Handler)
|
|
|
|
}
|
|
|
|
|
2019-03-10 23:50:10 +00:00
|
|
|
// GetExtensionURL retrieves the URL of an extension running locally based on the extension port table
|
2019-02-18 01:46:34 +00:00
|
|
|
func (manager *Manager) GetExtensionURL(extensionID portainer.ExtensionID) string {
|
2019-11-12 23:41:42 +00:00
|
|
|
return factory.BuildExtensionURL(extensionID)
|
2019-02-18 01:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// DeleteExtensionProxy deletes the extension proxy associated to an extension identifier
|
|
|
|
func (manager *Manager) DeleteExtensionProxy(extensionID portainer.ExtensionID) {
|
|
|
|
manager.extensionProxies.Remove(strconv.Itoa(int(extensionID)))
|
|
|
|
}
|
|
|
|
|
2019-11-13 00:12:55 +00:00
|
|
|
// CreateLegacyExtensionProxy creates a new HTTP reverse proxy for a legacy extension and adds it to the registered proxies
|
2018-12-09 03:49:27 +00:00
|
|
|
func (manager *Manager) CreateLegacyExtensionProxy(key, extensionAPIURL string) (http.Handler, error) {
|
2019-11-12 23:41:42 +00:00
|
|
|
proxy, err := manager.proxyFactory.NewLegacyExtensionProxy(extensionAPIURL)
|
2018-12-09 03:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-05-24 21:53:10 +00:00
|
|
|
manager.legacyExtensionProxies.Set(key, proxy)
|
2018-12-09 03:49:27 +00:00
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// GetLegacyExtensionProxy returns a legacy extension proxy associated to a key
|
|
|
|
func (manager *Manager) GetLegacyExtensionProxy(key string) http.Handler {
|
|
|
|
proxy, ok := manager.legacyExtensionProxies.Get(key)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|
2019-11-12 23:41:42 +00:00
|
|
|
return proxy.(http.Handler)
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|
2019-11-12 03:28:31 +00:00
|
|
|
|
2019-11-13 00:12:55 +00:00
|
|
|
// CreateGitlabProxy creates a new HTTP reverse proxy that can be used to send requests to the Gitlab API
|
2019-11-12 03:28:31 +00:00
|
|
|
func (manager *Manager) CreateGitlabProxy(url string) (http.Handler, error) {
|
2019-11-13 00:12:55 +00:00
|
|
|
return manager.proxyFactory.NewGitlabProxy(url)
|
2019-11-12 03:28:31 +00:00
|
|
|
}
|