-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
290 lines (252 loc) · 9.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/elazarl/goproxy"
"github.com/krum110487/zipfs"
)
type ProxySettings struct {
AllowCrossDomain bool `json:"allowCrossDomain"`
VerboseLogging bool `json:"verboseLogging"`
ProxyPort string `json:"proxyPort"`
ServerHTTPPort string `json:"serverHTTPPort"`
ServerHTTPSPort string `json:"serverHTTPSPort"`
GameRootPath string `json:"gameRootPath"`
ApiPrefix string `json:"apiPrefix"`
ExternalFilePaths []string `json:"externalFilePaths"`
ExtScriptTypes []string `json:"extScriptTypes"`
ExtIndexTypes []string `json:"extIndexTypes"`
ExtMimeTypesZipFS map[string]string `json:"extMimeTypesZipFS"`
ExtMimeTypesProxyOverride map[string]string `json:"extMimeTypesProxyOverride"`
UseMad4FP bool `json:"useMad4FP"`
LegacyGoPort string `json:"legacyGoPort"`
LegacyPHPPort string `json:"legacyPHPPort"`
LegacyPHPPath string `json:"legacyPHPPath"`
LegacyUsePHPServer bool `json:"legacyUsePHPServer"`
LegacyHTDOCSPath string `json:"legacyHTDOCSPath"`
LegacyCGIBINPath string `json:"legacyCGIBINPath"`
}
// ExtApplicationTypes is a map that holds the content types of different file extensions
var proxySettings ProxySettings
var proxy *goproxy.ProxyHttpServer
var cwd string
func init() {
// Load the content types from the JSON file
data, err := os.ReadFile("proxySettings.json")
if err != nil {
panic(err)
}
// Unmarshal the JSON data into a Config struct
err = json.Unmarshal(data, &proxySettings)
if err != nil {
panic(err)
}
//Get the CWD of this application
exe, err := os.Executable()
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")
serverHTTPSPort := flag.Int("serverHttpsPort", 22502, "zip server https listen port")
gameRootPath := flag.String("gameRootPath", "D:\\Flashpoint 11 Infinity\\Data\\Games", "This is the path where to find the zips")
apiPrefix := flag.String("apiPrefix", proxySettings.ApiPrefix, "apiPrefix is used to prefix any API call.")
useMad4FP := flag.Bool("UseMad4FP", false, "flag to turn on/off Mad4FP.")
legacyGoPort := flag.Int("legacyGoPort", 22601, "port that the legacy GO server listens on")
legacyPHPPort := flag.Int("legacyPHPPort", 22600, "port that the legacy PHP server listens on")
legacyPHPPath := flag.String("legacyPHPPath", "D:\\Flashpoint 11 Infinity\\Legacy", "This is the path for HTDOCS")
legacyUsePHPServer := flag.Bool("legacyUsePHPServer", true, "This will run the original PHP script in parallel")
legacyHTDOCSPath := flag.String("legacyHTDOCSPath", "D:\\Flashpoint 11 Infinity\\Legacy\\htdocs", "This is the path for HTDOCS")
flag.Parse()
//Apply all of the flags to the settings
proxySettings.VerboseLogging = *verboseLogging
proxySettings.ProxyPort = strconv.Itoa(*proxyPort)
proxySettings.ServerHTTPPort = strconv.Itoa(*serverHTTPPort)
proxySettings.ServerHTTPSPort = strconv.Itoa(*serverHTTPSPort)
proxySettings.GameRootPath = *gameRootPath
proxySettings.ApiPrefix = *apiPrefix
proxySettings.UseMad4FP = *useMad4FP
proxySettings.LegacyGoPort = strconv.Itoa(*legacyGoPort)
proxySettings.LegacyPHPPort = strconv.Itoa(*legacyPHPPort)
proxySettings.LegacyPHPPath = *legacyPHPPath
proxySettings.LegacyUsePHPServer = *legacyUsePHPServer
proxySettings.LegacyHTDOCSPath = *legacyHTDOCSPath
//Setup the proxy.
proxy = goproxy.NewProxyHttpServer()
proxy.Verbose = proxySettings.VerboseLogging
gamePath, _ := normalizePath("", proxySettings.GameRootPath, false)
fmt.Printf("Proxy Server Started on port %s\n", proxySettings.ProxyPort)
fmt.Printf("Zip Server Started\n\tHTTP Port: %s\n\tHTTPS Port: %s\n\tGame Root: %s\n",
proxySettings.ServerHTTPPort,
proxySettings.ServerHTTPSPort,
gamePath)
}
func setContentType(r *http.Request, resp *http.Response) {
if r == nil || resp == nil {
return
}
//Get the ZipFS and Request Extensions if they have it.
rext := filepath.Ext(strings.ToLower(resp.Header.Get("ZIPSVR_FILENAME")))
ext := filepath.Ext(strings.ToLower(r.URL.Path))
//If the request already has an extension, just use it, only when setup in the ProxyOverride
if ext != "" {
resp.Header.Set("Content-Type", proxySettings.ExtMimeTypesProxyOverride[ext[1:]])
return
}
//If the response has an extension, use that.
if rext != "" {
resp.Header.Set("Content-Type", proxySettings.ExtMimeTypesProxyOverride[rext[1:]])
return
}
//Finally, just use the default type IF it exists AND it is not already set.
existingMime := resp.Header.Get("Content-Type")
defExt, ok := proxySettings.ExtMimeTypesProxyOverride["default"]
if ok && existingMime == "" {
resp.Header.Set("Content-Type", defExt)
}
}
func main() {
proxy.NonproxyHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
//TODO: Needs Actual implementation of described below
//This will be used to handle the response from zipfs instead of the launcher sending it
//directly to the zipfs, it would be forwarded through this handler.
//Handle the endpoints here and forward it to zipfs, intercept the response
//If the response for adding a zip is < 400 then we will intercept it and grab the body
//which contains a list of Paths for the redirect and other configs for the proxy.
//Then we hit the zipfs server to pull the files contents and parse it
//Once it is parsed and ready THEN return the response to the front-end.
//This will make the front-end wait until we are ready to continue.
http.Error(w, "The endpoint is invalid.", 500)
})
//Handle the re-routing to local files or what not.
proxy.OnRequest().DoFunc(func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
fmt.Printf("Proxy Request: %s\n", r.URL.Host+r.URL.Path)
newURL := *r.URL
newPath, _ := url.PathUnescape(r.URL.Path)
newURL.Path = "content/" + strings.TrimSpace(r.URL.Host+newPath)
if r.TLS == nil {
//HTTP request
newURL.Host = "127.0.0.1:" + proxySettings.ServerHTTPPort
} else {
//HTTPS request, currently goes to the same server
newURL.Host = "127.0.0.1:" + proxySettings.ServerHTTPSPort
}
//Make the request to the zip server.
client := &http.Client{}
proxyReq, err := http.NewRequest(r.Method, newURL.String(), r.Body)
proxyReq.Header = r.Header
proxyResp, err := client.Do(proxyReq)
if proxyResp.StatusCode < 400 {
fmt.Printf("\tServing from Zip...\n")
}
//Check Legacy
if proxyResp.StatusCode >= 400 {
fmt.Printf("\tServing from Legacy...\n")
//Decide on the port to use
port := proxySettings.LegacyGoPort
if proxySettings.LegacyUsePHPServer {
port = proxySettings.LegacyPHPPort
}
//Set the Proxy URL and apply it to the Transpor layer so that the request respects the proxy.
proxyURL, _ := url.Parse("http://127.0.0.1:" + port)
proxy := http.ProxyURL(proxyURL)
transport := &http.Transport{Proxy: proxy}
//A custom Dialer is required for the "localflash" urls, instead of using the DNS, we use this.
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
//Set Dialer timeout and keepalive to 30 seconds and force the address to localhost.
dialer := &net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}
addr = "127.0.0.1:" + port
return dialer.DialContext(ctx, network, addr)
}
//TODO: Investigate if I need to blank this out... I don't think this is required.
r.RequestURI = ""
//Make the request with the custom transport.
client := &http.Client{Transport: transport, Timeout: 300 * time.Second}
proxyResp, err = client.Do(r)
}
//An error occured, log it for debug purposes
if err != nil {
fmt.Printf("UNHANDLED ERROR: %s\n", err)
}
//Update the content type based upon ext for now.
setContentType(r, proxyResp)
return r, proxyResp
})
//Start ZIP server
go func() {
//TODO: Update these to be modifiable in the properties json.
//TODO: Also update the "fpProxy/api/" to be in the properties json.
log.Fatal(http.ListenAndServe(":"+proxySettings.ServerHTTPPort,
zipfs.EmptyFileServer(
proxySettings.ApiPrefix,
"",
proxySettings.VerboseLogging,
proxySettings.ExtIndexTypes,
proxySettings.ExtMimeTypesZipFS,
),
))
}()
//Start Legacy server
go func() {
if proxySettings.LegacyUsePHPServer {
runLegacyPHP()
} else {
log.Fatal(http.ListenAndServe(":"+proxySettings.LegacyGoPort, getLegacyProxy()))
}
}()
//Start PROXY server
log.Fatal(http.ListenAndServe(":"+proxySettings.ProxyPort, http.AllowQuerySemicolons(proxy)))
}
func runLegacyPHP() {
phpPath := filepath.Join(proxySettings.LegacyPHPPath, "php")
cmd := exec.Command(phpPath, "-S", "127.0.0.1:"+proxySettings.LegacyPHPPort, "router.php")
cmd.Dir = proxySettings.LegacyPHPPath
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
cmd.Start()
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, os.Kill)
go func() {
<-c
// cleanup
cmd.Process.Kill()
os.Exit(1)
}()
var wg sync.WaitGroup
wg.Add(1)
go func() {
s := bufio.NewScanner(stdout)
for s.Scan() {
fmt.Println(s.Text())
}
wg.Done()
}()
wg.Add(1)
go func() {
s := bufio.NewScanner(stderr)
for s.Scan() {
fmt.Println(s.Text())
}
wg.Done()
}()
wg.Wait()
}