NAS: Forward payload requests to external server
Some checks failed
Build CI / build (push) Has been cancelled

This commit is contained in:
Palapeli 2025-06-03 19:46:20 -04:00
parent 262dbdec94
commit 9aba0bbeae
No known key found for this signature in database
GPG Key ID: 1FFE8F556A474925
3 changed files with 58 additions and 9 deletions

View File

@ -11,12 +11,13 @@ type Config struct {
DatabaseAddress string `xml:"databaseAddress"`
DatabaseName string `xml:"databaseName"`
DefaultAddress string `xml:"address"`
GameSpyAddress *string `xml:"gsAddress,omitempty"`
NASAddress *string `xml:"nasAddress,omitempty"`
NASPort string `xml:"nasPort"`
NASAddressHTTPS *string `xml:"nasAddressHttps,omitempty"`
NASPortHTTPS string `xml:"nasPortHttps"`
DefaultAddress string `xml:"address"`
GameSpyAddress *string `xml:"gsAddress,omitempty"`
NASAddress *string `xml:"nasAddress,omitempty"`
NASPort string `xml:"nasPort"`
NASAddressHTTPS *string `xml:"nasAddressHttps,omitempty"`
NASPortHTTPS string `xml:"nasPortHttps"`
PayloadServerAddress string `xml:"payloadServerAddress"`
FrontendAddress string `xml:"frontendAddress"`
FrontendBackendAddress string `xml:"frontendBackendAddress"`

View File

@ -25,6 +25,9 @@
<enableHttpsExploitWii>false</enableHttpsExploitWii>
<enableHttpsExploitDS>false</enableHttpsExploitDS>
<!-- The address the payload server will be located at, leave blank to use legacy integrated payload server -->
<payloadServerAddress>127.0.0.1:29997</payloadServerAddress>
<!-- Path to the certificate and key used for modern web browser requests -->
<certPath>fullchain.pem</certPath>
<keyPath>privkey.pem</keyPath>

View File

@ -3,6 +3,7 @@ package nas
import (
"context"
"errors"
"io"
"net/http"
"regexp"
"strconv"
@ -20,8 +21,9 @@ import (
)
var (
serverName string
server *nhttp.Server
serverName string
server *nhttp.Server
payloadServerAddress string
)
func StartServer(reload bool) {
@ -32,6 +34,8 @@ func StartServer(reload bool) {
address := *config.NASAddress + ":" + config.NASPort
payloadServerAddress = config.PayloadServerAddress
if config.EnableHTTPS {
go startHTTPSProxy(config)
}
@ -122,7 +126,12 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
// Check for /payload
if strings.HasPrefix(r.URL.String(), "/payload") {
logging.Info("NAS", aurora.Yellow(r.Method), aurora.Cyan(r.URL), "via", aurora.Cyan(r.Host), "from", aurora.BrightCyan(r.RemoteAddr))
handlePayloadRequest(moduleName, w, r)
if payloadServerAddress != "" {
// Forward the request to the payload server
forwardPayloadRequest(moduleName, w, r)
} else {
handlePayloadRequest(moduleName, w, r)
}
return
}
@ -207,3 +216,39 @@ func handleNASTest(w http.ResponseWriter) {
w.WriteHeader(200)
w.Write([]byte(response))
}
func forwardPayloadRequest(moduleName string, w http.ResponseWriter, r *http.Request) {
client := &http.Client{
Timeout: 10 * time.Second,
}
r.URL.Scheme = "http"
r.URL.Host = payloadServerAddress
r.RequestURI = ""
r.Host = payloadServerAddress
resp, err := client.Do(r)
if err != nil {
logging.Error(moduleName, "Error forwarding payload request:", err)
replyHTTPError(w, http.StatusBadGateway, "502 Bad Gateway")
return
}
defer resp.Body.Close()
// Copy the response headers and status code
for key, values := range resp.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}
w.WriteHeader(resp.StatusCode)
// Copy the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
logging.Error(moduleName, "Error reading response body:", err)
replyHTTPError(w, http.StatusInternalServerError, "500 Internal Server Error")
return
}
w.Write(body)
}