Initial commit

This commit is contained in:
Geoff Bourne
2018-05-07 22:16:01 -05:00
commit 17a4bd6515
10 changed files with 658 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
package server
import (
"net/http"
"github.com/sirupsen/logrus"
"github.com/gorilla/mux"
)
var apiRoutes = mux.NewRouter()
func StartApiServer(apiBinding string) {
logrus.WithField("binding", apiBinding).Info("Serving API requests")
go func() {
logrus.WithError(
http.ListenAndServe(apiBinding, apiRoutes)).Error("API server failed")
}()
}
+144
View File
@@ -0,0 +1,144 @@
package server
import (
"net"
"github.com/sirupsen/logrus"
"github.com/itzg/mc-router/mcproto"
"context"
"io"
"bytes"
)
type IConnector interface {
StartAcceptingConnections(ctx context.Context, listenAddress string) error
}
var Connector IConnector = &connectorImpl{}
type connectorImpl struct {
}
func (c *connectorImpl) StartAcceptingConnections(ctx context.Context, listenAddress string) error {
ln, err := net.Listen("tcp", listenAddress)
if err != nil {
logrus.WithError(err).Fatal("Unable to start listening")
return err
}
logrus.WithField("listenAddress", listenAddress).Info("Listening for Minecraft client connections")
go c.acceptConnections(ctx, ln)
return nil
}
func (c *connectorImpl) acceptConnections(ctx context.Context, ln net.Listener) {
defer ln.Close()
for {
select {
case <-ctx.Done():
return
default:
conn, err := ln.Accept()
if err != nil {
logrus.WithError(err).Error("Failed to accept connection")
} else {
go c.HandleConnection(ctx, conn)
}
}
}
}
func (c *connectorImpl) HandleConnection(ctx context.Context, frontendConn net.Conn) {
defer frontendConn.Close()
clientAddr := frontendConn.RemoteAddr()
logrus.WithFields(logrus.Fields{"clientAddr": clientAddr}).Info("Got connection")
inspectionBuffer := new(bytes.Buffer)
inspectionReader := io.TeeReader(frontendConn, inspectionBuffer)
packet, err := mcproto.ReadPacket(inspectionReader)
if err != nil {
logrus.WithError(err).WithField("clientAddr", clientAddr).Error("Failed to read packet")
return
}
logrus.WithFields(logrus.Fields{"length": packet.Length, "packetID": packet.PacketID}).Info("Got packet")
if packet.PacketID == mcproto.PacketIdHandshake {
handshake, err := mcproto.ReadHandshake(packet.Data)
if err != nil {
logrus.WithError(err).WithField("clientAddr", clientAddr).Error("Failed to read handshake")
return
}
logrus.WithFields(logrus.Fields{
"protocolVersion": handshake.ProtocolVersion,
"server": handshake.ServerAddress,
"serverPort": handshake.ServerPort,
"nextState": handshake.NextState,
}).Info("Got handshake")
backendHostPort := Routes.FindBackendForServerAddress(handshake.ServerAddress)
if backendHostPort == "" {
logrus.WithField("serverAddress", handshake.ServerAddress).Warn("Unable to find registered backend")
return
}
logrus.WithField("backendHostPort", backendHostPort).Info("Connecting to backend")
backendConn, err := net.Dial("tcp", backendHostPort)
if err != nil {
logrus.WithError(err).WithFields(logrus.Fields{
"serverAddress": handshake.ServerAddress,
"backend": backendHostPort,
}).Warn("Unable to connect to backend")
return
}
amount, err := io.Copy(backendConn, inspectionBuffer)
if err != nil {
logrus.WithError(err).Error("Failed to write handshake to backend connection")
return
}
logrus.WithField("amount", amount).Debug("Relayed handshake to backend")
pumpConnections(ctx, frontendConn, backendConn)
} else {
logrus.WithField("packetID", packet.PacketID).Error("Unexpected packetID, expected handshake")
}
}
func pumpConnections(ctx context.Context, frontendConn, backendConn net.Conn) {
defer backendConn.Close()
errors := make(chan error, 2)
go pumpFrames(backendConn, frontendConn, errors, "backend", "frontend")
go pumpFrames(frontendConn, backendConn, errors, "frontend", "backend")
for {
select {
case err := <-errors:
if err != io.EOF {
logrus.WithError(err).Error("Error observed on connection relay")
}
return
case <-ctx.Done():
return
}
}
}
func pumpFrames(incoming io.Reader, outgoing io.Writer, errors chan<- error, from, to string) {
amount, err := io.Copy(outgoing, incoming)
if err != nil {
errors <- err
}
logrus.WithField("amount", amount).Infof("Finished relay %s->%s", from, to)
}
+131
View File
@@ -0,0 +1,131 @@
package server
import (
"sync"
"net/http"
"encoding/json"
"github.com/sirupsen/logrus"
"github.com/gorilla/mux"
)
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("/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"]
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
}{}
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
}
Routes.CreateMapping(definition.ServerAddress, definition.Backend)
writer.WriteHeader(http.StatusCreated)
}
type IRoutes interface {
RegisterAll(mappings map[string]string)
// FindBackendForServerAddress returns the host:port for the external server address, if registered.
// Otherwise, an empty string is returned
FindBackendForServerAddress(serverAddress string) string
GetMappings() map[string]string
DeleteMapping(serverAddress string) bool
CreateMapping(serverAddress string, backend string)
}
var Routes IRoutes = &routesImpl{}
func (r *routesImpl) RegisterAll(mappings map[string]string) {
r.Lock()
defer r.Unlock()
r.mappings = mappings
}
type routesImpl struct {
sync.RWMutex
mappings map[string]string
}
func (r *routesImpl) FindBackendForServerAddress(serverAddress string) string {
r.RLock()
defer r.RUnlock()
if r.mappings == nil {
return ""
} else {
return r.mappings[serverAddress]
}
}
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 {
result[k] = v
}
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
}
}
func (r *routesImpl) CreateMapping(serverAddress string, backend string) {
r.Lock()
defer r.Unlock()
logrus.WithFields(logrus.Fields{
"serverAddress": serverAddress,
"backend": backend,
}).Info("Creating route")
r.mappings[serverAddress] = backend
}