Skip to content

Commit

Permalink
feat: Early file overrides from local paths
Browse files Browse the repository at this point in the history
  • Loading branch information
colin969 committed Oct 13, 2023
1 parent 76f21c9 commit e82f8b8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
28 changes: 15 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type ProxySettings struct {
LegacyHTDOCSPath string `json:"legacyHTDOCSPath"`
LegacyCGIBINPath string `json:"legacyCGIBINPath"`
PhpCgiPath string `json:"phpCgiPath"`
OverridePaths []string `json:"overridePaths"`
}

// ExtApplicationTypes is a map that holds the content types of different file extensions
Expand Down Expand Up @@ -164,7 +165,7 @@ func main() {
newURL.Host = "127.0.0.1:" + proxySettings.ServerHTTPSPort
}

//Make the request to the zip server.
// Make the request to the zip server.
client := &http.Client{}
proxyReq, err := http.NewRequest(r.Method, newURL.String(), r.Body)
proxyReq.Header = r.Header
Expand All @@ -174,43 +175,39 @@ func main() {
fmt.Printf("\tServing from Zip...\n")
}

//Check Legacy
// 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
}
port := proxySettings.LegacyPHPPort

//Set the Proxy URL and apply it to the Transpor layer so that the request respects the proxy.
// 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.
// 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.
// 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.
// 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
// 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.
// Update the content type based upon ext for now.
setContentType(r, proxyResp)
return r, proxyResp
})
Expand All @@ -228,6 +225,7 @@ func main() {
proxySettings.GameRootPath,
proxySettings.PhpCgiPath,
proxySettings.ExtMimeTypes,
proxySettings.OverridePaths,
),
))
}()
Expand Down Expand Up @@ -284,3 +282,7 @@ func runLegacyPHP() {

wg.Wait()
}

func serveOverrideFile(w http.ResponseWriter, r *http.Request) {

}
5 changes: 5 additions & 0 deletions proxySettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"legacyHTDOCSPath": "../Legacy/htdocs/",
"legacyCGIBINPath": "../Legacy/cgi-bin/",
"legacyPHPPath": "../Legacy/",
"overridePaths":
[
"../Legacy/htdocs/content/",
"../Legacy/middleware_overrides/"
],
"apiPrefix": "fpProxy/api/",
"allowCrossDomain": true,
"externalFilePaths": [
Expand Down
2 changes: 1 addition & 1 deletion zipfs
Submodule zipfs updated 1 files
+84 −23 file_server.go

0 comments on commit e82f8b8

Please sign in to comment.