mc-router/server/routes.go

235 lines
6.1 KiB
Go
Raw Normal View History

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"
"net/http"
"regexp"
2019-04-28 23:49:43 +02:00
"strings"
"sync"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
2018-05-08 05:16:01 +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)
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, _ *http.Request) {
2018-05-08 05:16:01 +02:00
mappings := Routes.GetMappings()
bytes, err := json.Marshal(mappings)
if err != nil {
logrus.WithError(err).Error("Failed to marshal mappings")
writer.WriteHeader(http.StatusInternalServerError)
return
}
_, err = writer.Write(bytes)
if err != nil {
logrus.WithError(err).Error("Failed to write response")
}
2018-05-08 05:16:01 +02:00
}
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
Backend string
2018-05-08 05:16:01 +02:00
}{}
//goland:noinspection GoUnhandledErrorResult
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)
}
func routesSetDefault(writer http.ResponseWriter, request *http.Request) {
var body = struct {
Backend string
}{}
//goland:noinspection GoUnhandledErrorResult
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)
writer.WriteHeader(http.StatusOK)
}
2018-05-08 05:16:01 +02:00
type IRoutes interface {
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)
SetDefaultRoute(backend string)
SimplifySRV(srvEnabled bool)
2018-05-08 05:16:01 +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 {
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
defaultRoute string
simplifySRV bool
}
func (r *routesImpl) Reset() {
r.mappings = make(map[string]mapping)
}
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
}
func (r *routesImpl) SimplifySRV(srvEnabled bool) {
r.simplifySRV = srvEnabled
}
func (r *routesImpl) FindBackendForServerAddress(_ context.Context, serverAddress string) (string, string, func(ctx context.Context) error) {
2018-05-08 05:16:01 +02:00
r.RLock()
defer r.RUnlock()
// Trim off Forge null-delimited address parts like \x00FML3\x00
serverAddress = strings.Split(serverAddress, "\x00")[0]
serverAddress = strings.ToLower(
// trim the root zone indicator, see https://en.wikipedia.org/wiki/Fully_qualified_domain_name
strings.TrimSuffix(serverAddress, "."))
logrus.WithFields(logrus.Fields{
"serverAddress": serverAddress,
}).Debug("Finding backend for server address")
if r.simplifySRV {
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, ".")
}
// Strip suffix of TCP Shield
serverAddress = tcpShieldPattern.ReplaceAllString(serverAddress, "")
if r.mappings != nil {
if mapping, exists := r.mappings[serverAddress]; exists {
return mapping.backend, serverAddress, mapping.waker
}
2018-05-08 05:16:01 +02:00
}
return r.defaultRoute, serverAddress, 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()
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,
"backend": backend,
}).Info("Created route mapping")
2022-01-07 00:41:10 +01:00
r.mappings[serverAddress] = mapping{backend: backend, waker: waker}
}