mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-03-21 17:44:58 -05:00
NAS: Forward payload requests to external server
Some checks failed
Build CI / build (push) Has been cancelled
Some checks failed
Build CI / build (push) Has been cancelled
This commit is contained in:
parent
262dbdec94
commit
9aba0bbeae
|
|
@ -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"`
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
51
nas/main.go
51
nas/main.go
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user