forked from FlashpointProject/FlashpointGameServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacyServer.go
322 lines (295 loc) · 10.5 KB
/
legacyServer.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/FlashpointProject/zipfs"
)
// Tries to serve a legacy file if available
func ServeLegacy(w http.ResponseWriter, r *http.Request) {
relPath := filepath.ToSlash(path.Join(r.URL.Host, r.URL.Path))
// @TODO PERFORM REQUEST MODIFICATION HERE
// parseHtaccessPath(serverSettings.LegacyHTDOCSPath, filepath.Dir(relPath), w, r)
relPathWithQuery := filepath.ToSlash(path.Join(r.URL.Host, r.URL.Path+url.PathEscape("?"+r.URL.RawQuery)))
hasQuery := r.URL.RawQuery != ""
// Returns the data from the matching method, finishing the response
successCloser := func(reader io.ReadCloser, fileName string, lastModified string) {
w.Header().Set("Last-Modified", lastModified)
w.Header().Set("ZIPSVR_FILENAME", fileName)
size, err := io.Copy(w, reader)
if err != nil {
fmt.Printf("Error copying file to response: %s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Header().Set("Content-Length", fmt.Sprintf("%d", size))
w.WriteHeader(http.StatusOK)
}
}
/** Overview
* Create groups of paths:
* 1. Exact Files
* 2. Directory Index Files
*
* Peform each check in order, checking each group in order within, to find a match:
* 1. Local File
* 2. Online Server
* 3. Special Behaviour (MAD4FP)
*/
// Building groups of paths
// Local = All Paths
// Online = All non-override paths
// Special = Exact content path only (with index consideration)
// Override paths
exactContentPath := path.Join(serverSettings.LegacyHTDOCSPath, "content", relPath)
exactFilePaths := []string{}
exactOverrideFilePaths := []string{}
indexFilePaths := []string{}
indexOverrideFilePaths := []string{}
// 1. Exact Files
if hasQuery {
for _, override := range serverSettings.LegacyOverridePaths {
exactOverrideFilePaths = append(exactOverrideFilePaths, path.Join(serverSettings.LegacyHTDOCSPath, override, relPathWithQuery))
}
exactFilePaths = append(exactFilePaths, path.Join(serverSettings.LegacyHTDOCSPath, relPathWithQuery))
}
for _, override := range serverSettings.LegacyOverridePaths {
exactOverrideFilePaths = append(exactOverrideFilePaths, path.Join(serverSettings.LegacyHTDOCSPath, override, relPath))
}
exactFilePaths = append(exactFilePaths, path.Join(serverSettings.LegacyHTDOCSPath, relPath))
// CGI bin for scripts
if isScriptUrl(r.URL) {
if hasQuery {
exactFilePaths = append(exactFilePaths, path.Join(serverSettings.LegacyCGIBINPath, relPathWithQuery))
}
exactFilePaths = append(exactFilePaths, path.Join(serverSettings.LegacyCGIBINPath, relPath))
}
// 2. Directory Index Files
for _, ext := range serverSettings.ExtIndexTypes {
for _, override := range serverSettings.LegacyOverridePaths {
indexOverrideFilePaths = append(indexOverrideFilePaths, path.Join(serverSettings.LegacyHTDOCSPath, override, relPath, "index."+ext))
}
indexFilePaths = append(indexFilePaths, path.Join(serverSettings.LegacyHTDOCSPath, relPath, "index."+ext))
}
// Try and find a valid file to respond with
// 1. Local File
for _, filePath := range append(append(append(exactFilePaths, exactOverrideFilePaths...), indexFilePaths...), indexOverrideFilePaths...) {
// Check if file exists
stats, err := os.Stat(filePath)
if err == nil && !stats.IsDir() {
// If it's a PHP file, let CGI handle instead
for _, ext := range serverSettings.ExtScriptTypes {
if filepath.Ext(filePath) == "."+ext {
fmt.Printf("[Legacy] Executing script file: %s\n", filepath.ToSlash(filePath))
zipfs.Cgi(w, r, serverSettings.PhpCgiPath, filePath)
return
}
}
// File exists and is static, serve
f, err := os.Open(filePath)
if err != nil {
// File exists but failed to open, server error
fmt.Printf("[Legacy] Error reading file '%s': %s\n", filePath, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
fmt.Printf("[Legacy] Serving exact file: %s\n", filepath.ToSlash(filePath))
successCloser(io.NopCloser(f), filePath, stats.ModTime().Format(time.RFC1123))
return
}
}
client := &http.Client{
Timeout: time.Second * 10,
}
// 2. Online Server
if serverSettings.UseInfinityServer {
serverUrl := strings.TrimRight(serverSettings.InfinityServerURL, "/")
for _, filePath := range append(exactFilePaths, indexFilePaths...) {
// Do not attempt to run server side scripts
if isScriptFile(filePath) {
continue
}
// Create a new request to the online server
relPath, err := filepath.Rel(serverSettings.LegacyHTDOCSPath, filePath)
if err != nil {
fmt.Printf("[Legacy] Error getting relative path for Infinity request: %s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
url := serverUrl + "/" + strings.ReplaceAll(relPath, string([]rune{'\\'}), "/")
liveReq, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Printf("[Legacy] Error creating Infinity request: %s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
liveReq.Header.Set("User-Agent", "Flashpoint Game Server")
// Perform request
resp, err := DoWebRequest(liveReq, client, 0)
// If 200, serve and save
if err == nil {
serveLiveResponse(w, resp, filePath, successCloser, "Infinity")
return
}
}
}
// 3. Special Behaviour (MAD4FP)
if serverSettings.UseMad4FP {
// Do not attempt to run server side scripts
if !isScriptUrl(r.URL) {
// Clone the entire request, to keep headers intact for better scraping
liveReq := r.Clone(r.Context())
liveReq.Header.Set("User-Agent", "Flashpoint Game Server MAD4FP")
// Perform request
resp, err := DoWebRequest(liveReq, client, 0)
// If 200, serve and save
if err == nil {
serveLiveResponse(w, resp, exactContentPath, successCloser, "MAD4FP")
return
}
}
}
// No response from any method, assume not found
fmt.Printf("[Legacy] 404 Not Found: %s\n", r.URL)
http.NotFound(w, r)
}
// Serves a response from a live server, and saves the response body to the originally requested file
func serveLiveResponse(w http.ResponseWriter, resp *http.Response, filePath string, successCloser func(io.ReadCloser, string, string), sourceName string) {
defer resp.Body.Close()
lastModified := resp.Header.Get("Last-Modified")
// Duplicate response body
contents, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("[Legacy] Error reading %s response body: %s\n", sourceName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Save file
err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
if err != nil {
fmt.Printf("[Legacy] Error saving %s response, cannot make directory: %s\n", sourceName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
file, err := os.Create(filePath)
if err != nil {
fmt.Printf("[Legacy] Error saving %s response, cannot create file: %s\n", sourceName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
_, err = io.Copy(file, bytes.NewReader(contents))
if err != nil {
fmt.Printf("[Legacy] Error saving %s response, cannot write file: %s\n", sourceName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if resp.Header.Get("Last-Modified") != "" {
// Convert header to real time
modifiedTime, err := time.Parse(time.RFC1123, lastModified)
if err == nil {
// Date converted successfuly, set time on file and return with response
err = os.Chtimes(filePath, time.Now(), modifiedTime)
if err != nil {
fmt.Printf("[Legacy] Error saving %s response, cannot set modified time: %s\n", sourceName, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Printf("[Legacy] Serving %s file: %s\n", sourceName, filepath.ToSlash(filePath))
successCloser(io.NopCloser(bytes.NewReader(contents)), filePath, lastModified)
return
}
}
// No last modified found, just use current time
successCloser(io.NopCloser(bytes.NewReader(contents)), filePath, time.Now().Format(time.RFC1123))
}
// Treats non-200 responses as errors, and handles 429 responses with a retry timer
func DoWebRequest(r *http.Request, client *http.Client, depth int) (*http.Response, error) {
resp, err := client.Do(r)
if err != nil {
return nil, err
}
if resp.StatusCode == 429 {
if depth > 3 {
return resp, fmt.Errorf("Too many 429 responses")
}
// Rate limited, wait and try again
timeToSleep := resp.Header.Get("Retry-After")
timeToSleepInt, err := strconv.Atoi(timeToSleep)
if err != nil {
timeToSleepInt = 2
}
time.Sleep(time.Duration(timeToSleepInt) * time.Second)
return DoWebRequest(r, client, depth+1)
}
if resp.StatusCode == 200 {
return resp, nil
}
return resp, fmt.Errorf(resp.Status)
}
func isScriptFile(filePath string) bool {
for _, ext := range serverSettings.ExtScriptTypes {
if filepath.Ext(filePath) == "."+ext {
return true
}
}
return false
}
func isScriptUrl(u *url.URL) bool {
for _, ext := range serverSettings.ExtScriptTypes {
if filepath.Ext(u.Path) == "."+ext {
return true
}
}
return false
}
// func parseHtaccessPath(root string, relPath string, w http.ResponseWriter, r *http.Request) error {
// // Go from top to bottom
// fmt.Printf("[Legacy] Parsing htaccess files for path: %s\n", relPath)
// nextDir := relPath
// for {
// if nextDir == "" {
// return nil
// }
// // Find .htaccess file in next directory
// htaccessPath := path.Join(root, nextDir, ".htaccess")
// fmt.Printf("[Legacy] Looking for htaccess file: %s\n", htaccessPath)
// file, err := os.Open(htaccessPath)
// if err != nil && os.IsNotExist(err) {
// return err
// } else {
// fmt.Printf("[Legacy] Found htaccess file: %s\n", htaccessPath)
// contents, err := io.ReadAll(file)
// if err != nil {
// return err
// }
// ast, err := parser.Parse(string(contents))
// for _, directive := range ast.Dirs {
// switch directive.Name {
// case "RewriteRule":
// // Expect a split of 3
// if len(directive.Params) != 3 {
// return fmt.Errorf("Invalid RewriteRule directive?")
// }
// src := directive.Params[0]
// dest := directive.Params[1]
// flags := directive.Params[2]
// fmt.Printf("[Legacy] RewriteRule: %s -> %s [%s]\n", src.RawString, dest.RawString, flags.RawString)
// src.Capture([]string{path.Join(r.URL.Host, r.URL.Path)})
// fmt.Printf("[Legacy] var: %s\n", src.VariableName)
// fmt.Printf("[Legacy] ass: %s\n", src.AssignValue)
// default:
// }
// }
// }
// nextDir = filepath.Dir(nextDir)
// }
// }