2018-05-08 05:16:01 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-01-07 00:41:10 +01:00
|
|
|
"context"
|
2018-05-08 05:16:01 +02:00
|
|
|
"encoding/json"
|
2019-04-20 03:27:14 +02:00
|
|
|
"net/http"
|
2023-05-25 04:28:38 +02:00
|
|
|
"regexp"
|
2019-04-28 23:49:43 +02:00
|
|
|
"strings"
|
2019-04-20 03:27:14 +02:00
|
|
|
"sync"
|
2021-12-12 01:20:19 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/sirupsen/logrus"
|
2018-05-08 05:16:01 +02:00
|
|
|
)
|
|
|
|
|
2023-05-25 04:28:38 +02:00
|
|
|
var tcpShieldPattern = regexp.MustCompile("///.*")
|
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
func init() {
|
|
|
|
apiRoutes.Path("/routes").Methods("GET").
|
|
|
|
Headers("Accept", "application/json").
|
|
|
|
HandlerFunc(routesListHandler)
|
|
|
|
apiRoutes.Path("/routes").Methods("POST").
|
|
|
|
Headers("Content-Type", "application/json").
|
|
|
|
HandlerFunc(routesCreateHandler)
|
2019-04-20 03:27:14 +02:00
|
|
|
apiRoutes.Path("/defaultRoute").Methods("POST").
|
|
|
|
Headers("Content-Type", "application/json").
|
|
|
|
HandlerFunc(routesSetDefault)
|
2018-05-08 05:16:01 +02:00
|
|
|
apiRoutes.Path("/routes/{serverAddress}").Methods("DELETE").HandlerFunc(routesDeleteHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
func routesListHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
mappings := Routes.GetMappings()
|
|
|
|
bytes, err := json.Marshal(mappings)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Failed to marshal mappings")
|
|
|
|
writer.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writer.Write(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func routesDeleteHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
serverAddress := mux.Vars(request)["serverAddress"]
|
2022-12-05 14:58:13 +01:00
|
|
|
RoutesConfig.DeleteMapping(serverAddress)
|
2018-05-08 05:16:01 +02:00
|
|
|
if serverAddress != "" {
|
|
|
|
if Routes.DeleteMapping(serverAddress) {
|
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
} else {
|
|
|
|
writer.WriteHeader(http.StatusNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func routesCreateHandler(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
var definition = struct {
|
|
|
|
ServerAddress string
|
2019-04-20 03:27:14 +02:00
|
|
|
Backend string
|
2018-05-08 05:16:01 +02:00
|
|
|
}{}
|
|
|
|
|
|
|
|
defer request.Body.Close()
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(request.Body)
|
|
|
|
err := decoder.Decode(&definition)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Unable to get request body")
|
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:41:10 +01:00
|
|
|
Routes.CreateMapping(definition.ServerAddress, definition.Backend, func(ctx context.Context) error { return nil })
|
2022-12-05 14:58:13 +01:00
|
|
|
RoutesConfig.AddMapping(definition.ServerAddress, definition.Backend)
|
2018-05-08 05:16:01 +02:00
|
|
|
writer.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
2019-04-20 03:27:14 +02:00
|
|
|
func routesSetDefault(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
var body = struct {
|
|
|
|
Backend string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
defer request.Body.Close()
|
|
|
|
|
|
|
|
decoder := json.NewDecoder(request.Body)
|
|
|
|
err := decoder.Decode(&body)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).Error("Unable to parse request")
|
|
|
|
writer.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Routes.SetDefaultRoute(body.Backend)
|
2022-12-05 14:58:13 +01:00
|
|
|
RoutesConfig.SetDefaultRoute(body.Backend)
|
2019-04-20 03:27:14 +02:00
|
|
|
writer.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
type IRoutes interface {
|
2022-07-05 16:53:23 +02:00
|
|
|
Reset()
|
2018-05-08 05:16:01 +02:00
|
|
|
RegisterAll(mappings map[string]string)
|
|
|
|
// FindBackendForServerAddress returns the host:port for the external server address, if registered.
|
2022-01-07 00:41:10 +01:00
|
|
|
// Otherwise, an empty string is returned. Also returns the normalized version of the given serverAddress.
|
|
|
|
// The 3rd value returned is an (optional) "waker" function which a caller must invoke to wake up serverAddress.
|
|
|
|
FindBackendForServerAddress(ctx context.Context, serverAddress string) (string, string, func(ctx context.Context) error)
|
2018-05-08 05:16:01 +02:00
|
|
|
GetMappings() map[string]string
|
|
|
|
DeleteMapping(serverAddress string) bool
|
2022-01-07 00:41:10 +01:00
|
|
|
CreateMapping(serverAddress string, backend string, waker func(ctx context.Context) error)
|
2019-04-20 03:27:14 +02:00
|
|
|
SetDefaultRoute(backend string)
|
2022-06-26 16:37:21 +02:00
|
|
|
SimplifySRV(srvEnabled bool)
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:53:23 +02:00
|
|
|
var Routes = NewRoutes()
|
2018-05-08 05:16:01 +02:00
|
|
|
|
2019-04-28 23:49:43 +02:00
|
|
|
func NewRoutes() IRoutes {
|
|
|
|
r := &routesImpl{
|
2022-01-07 00:41:10 +01:00
|
|
|
mappings: make(map[string]mapping),
|
2019-04-28 23:49:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
func (r *routesImpl) RegisterAll(mappings map[string]string) {
|
2022-01-07 00:41:10 +01:00
|
|
|
for k, v := range mappings {
|
2022-07-05 16:53:23 +02:00
|
|
|
r.CreateMapping(k, v, func(ctx context.Context) error { return nil })
|
2022-01-07 00:41:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mapping struct {
|
|
|
|
backend string
|
|
|
|
waker func(ctx context.Context) error
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type routesImpl struct {
|
|
|
|
sync.RWMutex
|
2022-01-07 00:41:10 +01:00
|
|
|
mappings map[string]mapping
|
2019-04-20 03:27:14 +02:00
|
|
|
defaultRoute string
|
2022-06-26 16:37:21 +02:00
|
|
|
simplifySRV bool
|
2019-04-20 03:27:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:53:23 +02:00
|
|
|
func (r *routesImpl) Reset() {
|
|
|
|
r.mappings = make(map[string]mapping)
|
|
|
|
}
|
|
|
|
|
2019-04-20 03:27:14 +02:00
|
|
|
func (r *routesImpl) SetDefaultRoute(backend string) {
|
|
|
|
r.defaultRoute = backend
|
|
|
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"backend": backend,
|
|
|
|
}).Info("Using default route")
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
|
2022-06-26 16:37:21 +02:00
|
|
|
func (r *routesImpl) SimplifySRV(srvEnabled bool) {
|
|
|
|
r.simplifySRV = srvEnabled
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:41:10 +01:00
|
|
|
func (r *routesImpl) FindBackendForServerAddress(ctx context.Context, serverAddress string) (string, string, func(ctx context.Context) error) {
|
2018-05-08 05:16:01 +02:00
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
2022-07-05 16:53:23 +02:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"serverAddress": serverAddress,
|
|
|
|
}).Debug("Finding backend for server address")
|
|
|
|
|
2022-06-26 16:37:21 +02:00
|
|
|
if r.simplifySRV {
|
|
|
|
serverAddress = strings.TrimSuffix(serverAddress, ".")
|
|
|
|
parts := strings.Split(serverAddress, ".")
|
|
|
|
tcpIndex := -1
|
|
|
|
for i, part := range parts {
|
|
|
|
if part == "_tcp" {
|
|
|
|
tcpIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tcpIndex != -1 {
|
|
|
|
parts = parts[tcpIndex+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
serverAddress = strings.Join(parts, ".")
|
|
|
|
}
|
|
|
|
|
2019-04-29 00:02:21 +02:00
|
|
|
addressParts := strings.Split(serverAddress, "\x00")
|
2019-04-28 23:49:43 +02:00
|
|
|
|
2022-07-12 06:16:15 +02:00
|
|
|
address := strings.ToLower(
|
|
|
|
// trim the root zone indicator, see https://en.wikipedia.org/wiki/Fully_qualified_domain_name
|
|
|
|
strings.TrimSuffix(addressParts[0], "."))
|
2019-06-25 20:31:49 +02:00
|
|
|
|
2023-05-25 04:28:38 +02:00
|
|
|
// Strip suffix of TCP Shield
|
|
|
|
address = tcpShieldPattern.ReplaceAllString(address, "")
|
|
|
|
|
2021-12-12 01:20:19 +01:00
|
|
|
if r.mappings != nil {
|
2022-01-07 00:41:10 +01:00
|
|
|
if mapping, exists := r.mappings[address]; exists {
|
|
|
|
return mapping.backend, address, mapping.waker
|
2019-04-20 03:27:14 +02:00
|
|
|
}
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
2022-01-07 00:41:10 +01:00
|
|
|
return r.defaultRoute, address, nil
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routesImpl) GetMappings() map[string]string {
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
|
|
|
|
result := make(map[string]string, len(r.mappings))
|
|
|
|
for k, v := range r.mappings {
|
2022-01-07 00:41:10 +01:00
|
|
|
result[k] = v.backend
|
2018-05-08 05:16:01 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *routesImpl) DeleteMapping(serverAddress string) bool {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
logrus.WithField("serverAddress", serverAddress).Info("Deleting route")
|
|
|
|
|
|
|
|
if _, ok := r.mappings[serverAddress]; ok {
|
|
|
|
delete(r.mappings, serverAddress)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 00:41:10 +01:00
|
|
|
func (r *routesImpl) CreateMapping(serverAddress string, backend string, waker func(ctx context.Context) error) {
|
2018-05-08 05:16:01 +02:00
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
2019-06-25 21:15:59 +02:00
|
|
|
serverAddress = strings.ToLower(serverAddress)
|
2019-06-25 20:31:49 +02:00
|
|
|
|
2018-05-08 05:16:01 +02:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"serverAddress": serverAddress,
|
2019-04-20 03:27:14 +02:00
|
|
|
"backend": backend,
|
2022-07-05 16:53:23 +02:00
|
|
|
}).Info("Created route mapping")
|
2022-01-07 00:41:10 +01:00
|
|
|
r.mappings[serverAddress] = mapping{backend: backend, waker: waker}
|
2019-04-20 03:27:14 +02:00
|
|
|
}
|