Skip to content

Commit

Permalink
Merge pull request #3 from WumboSpasm/main
Browse files Browse the repository at this point in the history
Miscellaneous improvements
  • Loading branch information
colin969 authored Nov 27, 2023
2 parents a0d4196 + 4814944 commit 0672b3c
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 85 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ fpproxy.csr
Flashpoint Proxy.exe
Flashpoint Game Server.exe
fpProxy.exe
FlashpointGameServer
testdata/htdocs
testdata/cgi-bin
testdata/cgi-bin
167 changes: 88 additions & 79 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
Expand All @@ -17,7 +16,6 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -26,29 +24,30 @@ import (
)

type ServerSettings struct {
AllowCrossDomain bool `json:"allowCrossDomain"`
VerboseLogging bool `json:"verboseLogging"`
ProxyPort string `json:"proxyPort"`
ServerHTTPPort string `json:"serverHTTPPort"`
GameRootPath string `json:"gameRootPath"`
ApiPrefix string `json:"apiPrefix"`
ExternalFilePaths []string `json:"externalFilePaths"`
ExtScriptTypes []string `json:"extScriptTypes"`
ExtIndexTypes []string `json:"extIndexTypes"`
ExtMimeTypes map[string]string `json:"extMimeTypes"`
ExtGzippeddTypes []string `json:"extGzippedTypes"`
HandleLegacyRequests bool `json:"handleLegacyRequests"`
ExternalLegacyPort string `json:"externalLegacyPort"`
LegacyHTDOCSPath string `json:"legacyHTDOCSPath"`
RootPath string `json:"rootPath"`
GameDataPath string `json:"gameDataPath"`
LegacyPHPPath string `json:"legacyPHPPath"`
LegacyCGIBINPath string `json:"legacyCGIBINPath"`
LegacyHTDOCSPath string `json:"legacyHTDOCSPath"`
PhpCgiPath string `json:"phpCgiPath"`
OverridePaths []string `json:"overridePaths"`
LegacyOverridePaths []string `json:"legacyOverridePaths"`
UseInfinityServer bool `json:"useInfinityServer"`
InfinityServerURL string `json:"infinityServerURL"`
HandleLegacyRequests bool `json:"handleLegacyRequests"`
ExternalLegacyPort string `json:"externalLegacyPort"`
ProxyPort string `json:"proxyPort"`
ServerHTTPPort string `json:"serverHTTPPort"`
UseMad4FP bool `json:"useMad4FP"`
EnableHttpsProxy bool `json:"enableHttpsProxy"`
AllowCrossDomain bool `json:"allowCrossDomain"`
VerboseLogging bool `json:"verboseLogging"`
ApiPrefix string `json:"apiPrefix"`
OverridePaths []string `json:"overridePaths"`
LegacyOverridePaths []string `json:"legacyOverridePaths"`
ExternalFilePaths []string `json:"externalFilePaths"`
ExtScriptTypes []string `json:"extScriptTypes"`
ExtIndexTypes []string `json:"extIndexTypes"`
ExtGzippeddTypes []string `json:"extGzippedTypes"`
ExtMimeTypes map[string]string `json:"extMimeTypes"`
}

// ExtApplicationTypes is a map that holds the content types of different file extensions
Expand All @@ -57,97 +56,104 @@ var proxy *goproxy.ProxyHttpServer
var cwd string

func initServer() {
// Load the content types from the JSON file
data, err := os.ReadFile("proxySettings.json")
// Get the CWD of this application
exe, err := os.Executable()
if err != nil {
panic(err)
}
cwd = filepath.Dir(exe)

// Unmarshal the JSON data into a Config struct
err = json.Unmarshal(data, &serverSettings)
// Load the content types from the JSON file
data, err := os.ReadFile(filepath.Join(cwd, "proxySettings.json"))
if err != nil {
panic(err)
}

//Get the CWD of this application
exe, err := os.Executable()
// Unmarshal the JSON data into a Config struct
err = json.Unmarshal(data, &serverSettings)
if err != nil {
panic(err)
}
cwd = strings.ReplaceAll(filepath.Dir(exe), "\\", "/")

//TODO: Update proxySettings.LegacyHTDOCSPath AND proxySettings.LegacyPHPPath for the default values!

//Get all of the paramaters passed in.
verboseLogging := flag.Bool("v", false, "should every proxy request be logged to stdout")
proxyPort := flag.Int("proxyPort", 22500, "proxy listen port")
serverHTTPPort := flag.Int("serverHttpPort", 22501, "zip server http listen port")
gameRootPath := flag.String("gameRootPath", serverSettings.GameRootPath, "This is the path where to find the zips")
rootPath := flag.String("rootPath", "D:\\Flashpoint", "The path that other relative paths use as a base")
apiPrefix := flag.String("apiPrefix", "/fpProxy/api", "apiPrefix is used to prefix any API call.")
useMad4FP := flag.Bool("UseMad4FP", false, "flag to turn on/off Mad4FP.")
externalLegacyPort := flag.String("externalLegacyPort", "22600", "The port that the external legacy server is running on (if handling legacy is disabled).")

// Get all of the parameters passed in
// TODO: Figure out a way to (partially?) automate everything that's going on below
// TODO: Improve descriptions
rootPath := flag.String("rootPath", serverSettings.RootPath, "The path that other relative paths use as a base")
gameDataPath := flag.String("gameRootPath", serverSettings.GameDataPath, "This is the path where to find the zips")
legacyPHPPath := flag.String("legacyPHPPath", serverSettings.LegacyPHPPath, "This is the path for PHP")
legacyCGIBINPath := flag.String("legacyCGIBINPath", serverSettings.LegacyCGIBINPath, "This is the path for CGI-BIN")
legacyHTDOCSPath := flag.String("legacyHTDOCSPath", serverSettings.LegacyHTDOCSPath, "This is the path for HTDOCS")
phpCgiPath := flag.String("phpCgiPath", serverSettings.PhpCgiPath, "Path to PHP CGI executable")
useInfinityServer := flag.Bool("useInfinityServer", false, "Whether to use the infinity server or not")
useInfinityServer := flag.Bool("useInfinityServer", serverSettings.UseInfinityServer, "Whether to use the infinity server or not")
infinityServerURL := flag.String("infinityServerURL", serverSettings.InfinityServerURL, "The URL of the infinity server")
legacyCGIBINPath := flag.String("legacyCGIBINPath", serverSettings.LegacyCGIBINPath, "This is the path for CGI-BIN")
handleLegacyRequests := flag.Bool("handleLegacyRequests", false, "Whether to handle legacy requests internally (true) or externally (false)")
enableHttpsProxy := flag.Bool("enableHttpsProxy", false, "Whether to enable HTTPS proxying or not")
handleLegacyRequests := flag.Bool("handleLegacyRequests", serverSettings.HandleLegacyRequests, "Whether to handle legacy requests internally (true) or externally (false)")
externalLegacyPort := flag.String("externalLegacyPort", serverSettings.ExternalLegacyPort, "The port that the external legacy server is running on (if handling legacy is disabled).")
proxyPort := flag.String("proxyPort", serverSettings.ProxyPort, "proxy listen port")
serverHttpPort := flag.String("serverHttpPort", serverSettings.ServerHTTPPort, "zip server http listen port")
useMad4FP := flag.Bool("UseMad4FP", serverSettings.UseMad4FP, "flag to turn on/off Mad4FP.")
enableHttpsProxy := flag.Bool("enableHttpsProxy", serverSettings.EnableHttpsProxy, "Whether to enable HTTPS proxying or not")
allowCrossDomain := flag.Bool("allowCrossDomain", serverSettings.AllowCrossDomain, "Whether to allow cross-domain requests")
verboseLogging := flag.Bool("verboseLogging", serverSettings.VerboseLogging, "should every proxy request be logged to stdout")
apiPrefix := flag.String("apiPrefix", serverSettings.ApiPrefix, "apiPrefix is used to prefix any API call.")

flag.Parse()

//Apply all of the flags to the settings
serverSettings.VerboseLogging = *verboseLogging
serverSettings.RootPath, err = filepath.Abs(strings.Trim(*rootPath, "\""))
// Apply all of the flags to the settings
serverSettings.RootPath, err = filepath.Abs(strings.Trim(*rootPath, string(os.PathSeparator)))
if err != nil {
fmt.Printf("Failed to get absolute game root path")
return
fmt.Println("Failed to get absolute root path")
panic(err)
}
serverSettings.EnableHttpsProxy = *enableHttpsProxy
serverSettings.ProxyPort = strconv.Itoa(*proxyPort)
serverSettings.ServerHTTPPort = strconv.Itoa(*serverHTTPPort)
serverSettings.ApiPrefix = *apiPrefix
serverSettings.UseMad4FP = *useMad4FP
serverSettings.ExternalLegacyPort = *externalLegacyPort
serverSettings.LegacyCGIBINPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyCGIBINPath, "\"")))
serverSettings.GameDataPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*gameDataPath, string(os.PathSeparator))))
if err != nil {
fmt.Printf("Failed to get absolute cgi-bin path")
return
fmt.Println("Failed to get absolute game data path")
panic(err)
}
serverSettings.LegacyHTDOCSPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyHTDOCSPath, "\"")))
serverSettings.LegacyPHPPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyPHPPath, string(os.PathSeparator))))
if err != nil {
fmt.Printf("Failed to get absolute htdocs path")
return
fmt.Println("Failed to get absolute PHP path")
panic(err)
}
serverSettings.GameRootPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*gameRootPath, "\"")))
serverSettings.LegacyCGIBINPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyCGIBINPath, string(os.PathSeparator))))
if err != nil {
fmt.Printf("Failed to get absolute game root path")
return
fmt.Println("Failed to get absolute cgi-bin path")
panic(err)
}
serverSettings.PhpCgiPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*phpCgiPath, "\"")))
serverSettings.LegacyHTDOCSPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyHTDOCSPath, string(os.PathSeparator))))
if err != nil {
fmt.Printf("Failed to get absolute php cgi path")
return
fmt.Println("Failed to get absolute htdocs path")
panic(err)
}
serverSettings.PhpCgiPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*phpCgiPath, string(os.PathSeparator))))
if err != nil {
fmt.Println("Failed to get absolute PHP-CGI path")
panic(err)
}
serverSettings.UseInfinityServer = *useInfinityServer
serverSettings.InfinityServerURL = *infinityServerURL
serverSettings.HandleLegacyRequests = *handleLegacyRequests
serverSettings.ExternalLegacyPort = *externalLegacyPort
serverSettings.ProxyPort = *proxyPort
serverSettings.ServerHTTPPort = *serverHttpPort
serverSettings.UseMad4FP = *useMad4FP
serverSettings.EnableHttpsProxy = *enableHttpsProxy
serverSettings.AllowCrossDomain = *allowCrossDomain
serverSettings.VerboseLogging = *verboseLogging
serverSettings.ApiPrefix = *apiPrefix

// Print out all path settings
fmt.Printf("Root Path: %s\n", serverSettings.RootPath)
fmt.Printf("PHP CGI Path: %s\n", serverSettings.PhpCgiPath)
fmt.Printf("Legacy HTDOCS Path: %s\n", serverSettings.LegacyHTDOCSPath)
fmt.Printf("Legacy CGI BIN Path: %s\n", serverSettings.LegacyCGIBINPath)
fmt.Printf("Games Path: %s\n", serverSettings.GameRootPath)

//Setup the proxy.
fmt.Println("Root Path:", serverSettings.RootPath)
fmt.Println("Game Data Path:", serverSettings.GameDataPath)
fmt.Println("Legacy PHP Path:", serverSettings.LegacyPHPPath)
fmt.Println("Legacy CGI-BIN Path:", serverSettings.LegacyCGIBINPath)
fmt.Println("Legacy HTDOCS Path:", serverSettings.LegacyHTDOCSPath)
fmt.Println("PHP-CGI Path:", serverSettings.PhpCgiPath)

// Setup the proxy
proxy = goproxy.NewProxyHttpServer()
proxy.Verbose = serverSettings.VerboseLogging
fmt.Printf("Proxy Server Started on port %s\n", serverSettings.ProxyPort)
fmt.Printf("Zip Server Started\n\tHTTP Port: %s\n\tGame Root: %s\n",
serverSettings.ServerHTTPPort,
serverSettings.GameRootPath)
fmt.Println("Proxy Server started on port", serverSettings.ProxyPort)
fmt.Println("Zip Server started on port", serverSettings.ServerHTTPPort)
}

func setContentType(r *http.Request, resp *http.Response) {
Expand Down Expand Up @@ -205,7 +211,7 @@ func handleRequest(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http
// Remove port from host if exists (old apps don't clean it before sending requests?)
r.URL.Host = strings.Split(r.URL.Host, ":")[0]
// Clone the body into both requests by reading and making 2 new readers
contents, _ := ioutil.ReadAll(r.Body)
contents, _ := io.ReadAll(r.Body)

// Copy the original request
gamezipRequest := &http.Request{
Expand All @@ -228,8 +234,11 @@ func handleRequest(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http
fmt.Printf("UNHANDLED GAMEZIP ERROR: %s\n", err)
}
proxyReq.Header = gamezipRequest.Header
proxyResp, err := client.Do(proxyReq)

proxyResp, err := client.Do(proxyReq)
if err != nil {
fmt.Printf("UNHANDLED GAMEZIP SERVER ERROR: %s\n", err)
}
if proxyResp.StatusCode >= 500 {
fmt.Println("Gamezip Server Error: ", proxyResp.StatusCode)
}
Expand All @@ -249,7 +258,7 @@ func handleRequest(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http
Body: nil,
}
// Copy in a new body reader
legacyRequest.Body = ioutil.NopCloser(bytes.NewReader(contents))
legacyRequest.Body = io.NopCloser(bytes.NewReader(contents))

// Choose which legacy method we're using
if serverSettings.HandleLegacyRequests {
Expand Down Expand Up @@ -364,7 +373,7 @@ XgVWIMrKj4T7p86bcxq4jdWDYUYpRd/2Og==
"",
serverSettings.VerboseLogging,
serverSettings.ExtIndexTypes,
serverSettings.GameRootPath,
serverSettings.GameDataPath,
serverSettings.PhpCgiPath,
serverSettings.ExtMimeTypes,
serverSettings.OverridePaths,
Expand Down
19 changes: 14 additions & 5 deletions proxySettings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
{
"rootPath": "../",
"legacyHTDOCSPath": "Legacy/htdocs/",
"legacyCGIBINPath": "Legacy/cgi-bin/",
"gameDataPath": "Data/Games/",
"legacyPHPPath": "Legacy/",
"gameRootPath": "Data/Games/",
"legacyCGIBINPath": "Legacy/cgi-bin/",
"legacyHTDOCSPath": "Legacy/htdocs/",
"phpCgiPath": "Legacy/php-cgi.exe",
"useInfinityServer": false,
"infinityServerURL": "https://infinity.unstable.life/Flashpoint/Legacy/htdocs/",
"handleLegacyRequests": false,
"externalLegacyPort": "22600",
"proxyPort": "22500",
"serverHTTPPort": "22501",
"useMad4FP": false,
"enableHttpsProxy": false,
"allowCrossDomain": true,
"verboseLogging": false,
"apiPrefix": "fpProxy/api/",
"overridePaths": [
"../Legacy/middleware_overrides/"
],
"apiPrefix": "fpProxy/api/",
"allowCrossDomain": true,
"legacyOverridePaths": [],
"externalFilePaths": [
"http://infinity.unstable.life/Flashpoint/Legacy/htdocs",
"http://archive.org/download/FP90Data/FP90Data.zip/htdocs"
Expand Down

0 comments on commit 0672b3c

Please sign in to comment.