Skip to content

Commit 0672b3c

Browse files
authored
Merge pull request #3 from WumboSpasm/main
Miscellaneous improvements
2 parents a0d4196 + 4814944 commit 0672b3c

File tree

3 files changed

+104
-85
lines changed

3 files changed

+104
-85
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ fpproxy.csr
44
Flashpoint Proxy.exe
55
Flashpoint Game Server.exe
66
fpProxy.exe
7+
FlashpointGameServer
78
testdata/htdocs
8-
testdata/cgi-bin
9+
testdata/cgi-bin

main.go

Lines changed: 88 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"flag"
99
"fmt"
1010
"io"
11-
"io/ioutil"
1211
"log"
1312
"net"
1413
"net/http"
@@ -17,7 +16,6 @@ import (
1716
"os"
1817
"path"
1918
"path/filepath"
20-
"strconv"
2119
"strings"
2220
"time"
2321

@@ -26,29 +24,30 @@ import (
2624
)
2725

2826
type ServerSettings struct {
29-
AllowCrossDomain bool `json:"allowCrossDomain"`
30-
VerboseLogging bool `json:"verboseLogging"`
31-
ProxyPort string `json:"proxyPort"`
32-
ServerHTTPPort string `json:"serverHTTPPort"`
33-
GameRootPath string `json:"gameRootPath"`
34-
ApiPrefix string `json:"apiPrefix"`
35-
ExternalFilePaths []string `json:"externalFilePaths"`
36-
ExtScriptTypes []string `json:"extScriptTypes"`
37-
ExtIndexTypes []string `json:"extIndexTypes"`
38-
ExtMimeTypes map[string]string `json:"extMimeTypes"`
39-
ExtGzippeddTypes []string `json:"extGzippedTypes"`
40-
HandleLegacyRequests bool `json:"handleLegacyRequests"`
41-
ExternalLegacyPort string `json:"externalLegacyPort"`
42-
LegacyHTDOCSPath string `json:"legacyHTDOCSPath"`
4327
RootPath string `json:"rootPath"`
28+
GameDataPath string `json:"gameDataPath"`
29+
LegacyPHPPath string `json:"legacyPHPPath"`
4430
LegacyCGIBINPath string `json:"legacyCGIBINPath"`
31+
LegacyHTDOCSPath string `json:"legacyHTDOCSPath"`
4532
PhpCgiPath string `json:"phpCgiPath"`
46-
OverridePaths []string `json:"overridePaths"`
47-
LegacyOverridePaths []string `json:"legacyOverridePaths"`
4833
UseInfinityServer bool `json:"useInfinityServer"`
4934
InfinityServerURL string `json:"infinityServerURL"`
35+
HandleLegacyRequests bool `json:"handleLegacyRequests"`
36+
ExternalLegacyPort string `json:"externalLegacyPort"`
37+
ProxyPort string `json:"proxyPort"`
38+
ServerHTTPPort string `json:"serverHTTPPort"`
5039
UseMad4FP bool `json:"useMad4FP"`
5140
EnableHttpsProxy bool `json:"enableHttpsProxy"`
41+
AllowCrossDomain bool `json:"allowCrossDomain"`
42+
VerboseLogging bool `json:"verboseLogging"`
43+
ApiPrefix string `json:"apiPrefix"`
44+
OverridePaths []string `json:"overridePaths"`
45+
LegacyOverridePaths []string `json:"legacyOverridePaths"`
46+
ExternalFilePaths []string `json:"externalFilePaths"`
47+
ExtScriptTypes []string `json:"extScriptTypes"`
48+
ExtIndexTypes []string `json:"extIndexTypes"`
49+
ExtGzippeddTypes []string `json:"extGzippedTypes"`
50+
ExtMimeTypes map[string]string `json:"extMimeTypes"`
5251
}
5352

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

5958
func initServer() {
60-
// Load the content types from the JSON file
61-
data, err := os.ReadFile("proxySettings.json")
59+
// Get the CWD of this application
60+
exe, err := os.Executable()
6261
if err != nil {
6362
panic(err)
6463
}
64+
cwd = filepath.Dir(exe)
6565

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

72-
//Get the CWD of this application
73-
exe, err := os.Executable()
72+
// Unmarshal the JSON data into a Config struct
73+
err = json.Unmarshal(data, &serverSettings)
7474
if err != nil {
7575
panic(err)
7676
}
77-
cwd = strings.ReplaceAll(filepath.Dir(exe), "\\", "/")
78-
79-
//TODO: Update proxySettings.LegacyHTDOCSPath AND proxySettings.LegacyPHPPath for the default values!
80-
81-
//Get all of the paramaters passed in.
82-
verboseLogging := flag.Bool("v", false, "should every proxy request be logged to stdout")
83-
proxyPort := flag.Int("proxyPort", 22500, "proxy listen port")
84-
serverHTTPPort := flag.Int("serverHttpPort", 22501, "zip server http listen port")
85-
gameRootPath := flag.String("gameRootPath", serverSettings.GameRootPath, "This is the path where to find the zips")
86-
rootPath := flag.String("rootPath", "D:\\Flashpoint", "The path that other relative paths use as a base")
87-
apiPrefix := flag.String("apiPrefix", "/fpProxy/api", "apiPrefix is used to prefix any API call.")
88-
useMad4FP := flag.Bool("UseMad4FP", false, "flag to turn on/off Mad4FP.")
89-
externalLegacyPort := flag.String("externalLegacyPort", "22600", "The port that the external legacy server is running on (if handling legacy is disabled).")
77+
78+
// Get all of the parameters passed in
79+
// TODO: Figure out a way to (partially?) automate everything that's going on below
80+
// TODO: Improve descriptions
81+
rootPath := flag.String("rootPath", serverSettings.RootPath, "The path that other relative paths use as a base")
82+
gameDataPath := flag.String("gameRootPath", serverSettings.GameDataPath, "This is the path where to find the zips")
83+
legacyPHPPath := flag.String("legacyPHPPath", serverSettings.LegacyPHPPath, "This is the path for PHP")
84+
legacyCGIBINPath := flag.String("legacyCGIBINPath", serverSettings.LegacyCGIBINPath, "This is the path for CGI-BIN")
9085
legacyHTDOCSPath := flag.String("legacyHTDOCSPath", serverSettings.LegacyHTDOCSPath, "This is the path for HTDOCS")
9186
phpCgiPath := flag.String("phpCgiPath", serverSettings.PhpCgiPath, "Path to PHP CGI executable")
92-
useInfinityServer := flag.Bool("useInfinityServer", false, "Whether to use the infinity server or not")
87+
useInfinityServer := flag.Bool("useInfinityServer", serverSettings.UseInfinityServer, "Whether to use the infinity server or not")
9388
infinityServerURL := flag.String("infinityServerURL", serverSettings.InfinityServerURL, "The URL of the infinity server")
94-
legacyCGIBINPath := flag.String("legacyCGIBINPath", serverSettings.LegacyCGIBINPath, "This is the path for CGI-BIN")
95-
handleLegacyRequests := flag.Bool("handleLegacyRequests", false, "Whether to handle legacy requests internally (true) or externally (false)")
96-
enableHttpsProxy := flag.Bool("enableHttpsProxy", false, "Whether to enable HTTPS proxying or not")
89+
handleLegacyRequests := flag.Bool("handleLegacyRequests", serverSettings.HandleLegacyRequests, "Whether to handle legacy requests internally (true) or externally (false)")
90+
externalLegacyPort := flag.String("externalLegacyPort", serverSettings.ExternalLegacyPort, "The port that the external legacy server is running on (if handling legacy is disabled).")
91+
proxyPort := flag.String("proxyPort", serverSettings.ProxyPort, "proxy listen port")
92+
serverHttpPort := flag.String("serverHttpPort", serverSettings.ServerHTTPPort, "zip server http listen port")
93+
useMad4FP := flag.Bool("UseMad4FP", serverSettings.UseMad4FP, "flag to turn on/off Mad4FP.")
94+
enableHttpsProxy := flag.Bool("enableHttpsProxy", serverSettings.EnableHttpsProxy, "Whether to enable HTTPS proxying or not")
95+
allowCrossDomain := flag.Bool("allowCrossDomain", serverSettings.AllowCrossDomain, "Whether to allow cross-domain requests")
96+
verboseLogging := flag.Bool("verboseLogging", serverSettings.VerboseLogging, "should every proxy request be logged to stdout")
97+
apiPrefix := flag.String("apiPrefix", serverSettings.ApiPrefix, "apiPrefix is used to prefix any API call.")
9798

9899
flag.Parse()
99100

100-
//Apply all of the flags to the settings
101-
serverSettings.VerboseLogging = *verboseLogging
102-
serverSettings.RootPath, err = filepath.Abs(strings.Trim(*rootPath, "\""))
101+
// Apply all of the flags to the settings
102+
serverSettings.RootPath, err = filepath.Abs(strings.Trim(*rootPath, string(os.PathSeparator)))
103103
if err != nil {
104-
fmt.Printf("Failed to get absolute game root path")
105-
return
104+
fmt.Println("Failed to get absolute root path")
105+
panic(err)
106106
}
107-
serverSettings.EnableHttpsProxy = *enableHttpsProxy
108-
serverSettings.ProxyPort = strconv.Itoa(*proxyPort)
109-
serverSettings.ServerHTTPPort = strconv.Itoa(*serverHTTPPort)
110-
serverSettings.ApiPrefix = *apiPrefix
111-
serverSettings.UseMad4FP = *useMad4FP
112-
serverSettings.ExternalLegacyPort = *externalLegacyPort
113-
serverSettings.LegacyCGIBINPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyCGIBINPath, "\"")))
107+
serverSettings.GameDataPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*gameDataPath, string(os.PathSeparator))))
114108
if err != nil {
115-
fmt.Printf("Failed to get absolute cgi-bin path")
116-
return
109+
fmt.Println("Failed to get absolute game data path")
110+
panic(err)
117111
}
118-
serverSettings.LegacyHTDOCSPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyHTDOCSPath, "\"")))
112+
serverSettings.LegacyPHPPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyPHPPath, string(os.PathSeparator))))
119113
if err != nil {
120-
fmt.Printf("Failed to get absolute htdocs path")
121-
return
114+
fmt.Println("Failed to get absolute PHP path")
115+
panic(err)
122116
}
123-
serverSettings.GameRootPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*gameRootPath, "\"")))
117+
serverSettings.LegacyCGIBINPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyCGIBINPath, string(os.PathSeparator))))
124118
if err != nil {
125-
fmt.Printf("Failed to get absolute game root path")
126-
return
119+
fmt.Println("Failed to get absolute cgi-bin path")
120+
panic(err)
127121
}
128-
serverSettings.PhpCgiPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*phpCgiPath, "\"")))
122+
serverSettings.LegacyHTDOCSPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*legacyHTDOCSPath, string(os.PathSeparator))))
129123
if err != nil {
130-
fmt.Printf("Failed to get absolute php cgi path")
131-
return
124+
fmt.Println("Failed to get absolute htdocs path")
125+
panic(err)
126+
}
127+
serverSettings.PhpCgiPath, err = filepath.Abs(path.Join(serverSettings.RootPath, strings.Trim(*phpCgiPath, string(os.PathSeparator))))
128+
if err != nil {
129+
fmt.Println("Failed to get absolute PHP-CGI path")
130+
panic(err)
132131
}
133132
serverSettings.UseInfinityServer = *useInfinityServer
134133
serverSettings.InfinityServerURL = *infinityServerURL
135134
serverSettings.HandleLegacyRequests = *handleLegacyRequests
135+
serverSettings.ExternalLegacyPort = *externalLegacyPort
136+
serverSettings.ProxyPort = *proxyPort
137+
serverSettings.ServerHTTPPort = *serverHttpPort
138+
serverSettings.UseMad4FP = *useMad4FP
139+
serverSettings.EnableHttpsProxy = *enableHttpsProxy
140+
serverSettings.AllowCrossDomain = *allowCrossDomain
141+
serverSettings.VerboseLogging = *verboseLogging
142+
serverSettings.ApiPrefix = *apiPrefix
136143

137144
// Print out all path settings
138-
fmt.Printf("Root Path: %s\n", serverSettings.RootPath)
139-
fmt.Printf("PHP CGI Path: %s\n", serverSettings.PhpCgiPath)
140-
fmt.Printf("Legacy HTDOCS Path: %s\n", serverSettings.LegacyHTDOCSPath)
141-
fmt.Printf("Legacy CGI BIN Path: %s\n", serverSettings.LegacyCGIBINPath)
142-
fmt.Printf("Games Path: %s\n", serverSettings.GameRootPath)
143-
144-
//Setup the proxy.
145+
fmt.Println("Root Path:", serverSettings.RootPath)
146+
fmt.Println("Game Data Path:", serverSettings.GameDataPath)
147+
fmt.Println("Legacy PHP Path:", serverSettings.LegacyPHPPath)
148+
fmt.Println("Legacy CGI-BIN Path:", serverSettings.LegacyCGIBINPath)
149+
fmt.Println("Legacy HTDOCS Path:", serverSettings.LegacyHTDOCSPath)
150+
fmt.Println("PHP-CGI Path:", serverSettings.PhpCgiPath)
151+
152+
// Setup the proxy
145153
proxy = goproxy.NewProxyHttpServer()
146154
proxy.Verbose = serverSettings.VerboseLogging
147-
fmt.Printf("Proxy Server Started on port %s\n", serverSettings.ProxyPort)
148-
fmt.Printf("Zip Server Started\n\tHTTP Port: %s\n\tGame Root: %s\n",
149-
serverSettings.ServerHTTPPort,
150-
serverSettings.GameRootPath)
155+
fmt.Println("Proxy Server started on port", serverSettings.ProxyPort)
156+
fmt.Println("Zip Server started on port", serverSettings.ServerHTTPPort)
151157
}
152158

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

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

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

254263
// Choose which legacy method we're using
255264
if serverSettings.HandleLegacyRequests {
@@ -364,7 +373,7 @@ XgVWIMrKj4T7p86bcxq4jdWDYUYpRd/2Og==
364373
"",
365374
serverSettings.VerboseLogging,
366375
serverSettings.ExtIndexTypes,
367-
serverSettings.GameRootPath,
376+
serverSettings.GameDataPath,
368377
serverSettings.PhpCgiPath,
369378
serverSettings.ExtMimeTypes,
370379
serverSettings.OverridePaths,

proxySettings.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
{
22
"rootPath": "../",
3-
"legacyHTDOCSPath": "Legacy/htdocs/",
4-
"legacyCGIBINPath": "Legacy/cgi-bin/",
3+
"gameDataPath": "Data/Games/",
54
"legacyPHPPath": "Legacy/",
6-
"gameRootPath": "Data/Games/",
5+
"legacyCGIBINPath": "Legacy/cgi-bin/",
6+
"legacyHTDOCSPath": "Legacy/htdocs/",
77
"phpCgiPath": "Legacy/php-cgi.exe",
8+
"useInfinityServer": false,
89
"infinityServerURL": "https://infinity.unstable.life/Flashpoint/Legacy/htdocs/",
10+
"handleLegacyRequests": false,
11+
"externalLegacyPort": "22600",
12+
"proxyPort": "22500",
13+
"serverHTTPPort": "22501",
14+
"useMad4FP": false,
15+
"enableHttpsProxy": false,
16+
"allowCrossDomain": true,
17+
"verboseLogging": false,
18+
"apiPrefix": "fpProxy/api/",
919
"overridePaths": [
1020
"../Legacy/middleware_overrides/"
1121
],
12-
"apiPrefix": "fpProxy/api/",
13-
"allowCrossDomain": true,
22+
"legacyOverridePaths": [],
1423
"externalFilePaths": [
1524
"http://infinity.unstable.life/Flashpoint/Legacy/htdocs",
1625
"http://archive.org/download/FP90Data/FP90Data.zip/htdocs"

0 commit comments

Comments
 (0)