Skip to content

Commit

Permalink
Made changes at the interceptor to extract and encrypt the URL from H…
Browse files Browse the repository at this point in the history
…ost, so proxy only sees the host, and not the path or query things
  • Loading branch information
huzaifamk committed May 26, 2024
1 parent ba82054 commit 99c134d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,6 @@ dist
.yarn/install-state.gz
.pnp.*

/bin/interceptor.wasm
/bin/interceptor.wasm

.vscode
5 changes: 5 additions & 0 deletions interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,21 @@ func fetch(this js.Value, args []js.Value) interface{} {
// Converting the body to Golag or setting it as null/nil
bodyMap := map[string]interface{}{}
body := options.Get("body")
urlPath := strings.Replace(spURL, host, "", 1)
// fmt.Println("[Interceptor] URL Path: ", urlPath) // For debugging purposes
if body.String() == "<undefined>" {
// body = js.ValueOf(map[string]interface{}{}) <= this will err out as "Uncaught (in promise) Error: invalid character '<' looking for beginning of value"
body = js.ValueOf("{}")
bodyMap["url_path"] = urlPath
} else {
err := json.Unmarshal([]byte(body.String()), &bodyMap)
if err != nil {
reject.Invoke(js.Global().Get("Error").New(err.Error()))
return
}
bodyMap["url_path"] = urlPath
}
// fmt.Println("[Interceptor] BodyMap: ", bodyMap) // For debugging purposes
// encode the body to json
bodyByte, err := json.Marshal(bodyMap)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion internals/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ func (c *Client) do(
}
// create request
client := &http.Client{}
r, err := http.NewRequest("POST", c.proxyURL+parsedURL.Path, bytes.NewBuffer(data))
// !NOTE: GET Requests are also converted to POST requests
// fmt.Println("[Interceptor] c.proxyURL+parsedURL.Path: ", c.proxyURL+parsedURL.Path+parsedURL.RawQuery) // For debugging purposes
r, err := http.NewRequest("POST", c.proxyURL, bytes.NewBuffer(data))
if err != nil {
res := &utils.Response{
Status: 500,
Expand All @@ -168,6 +170,7 @@ func (c *Client) do(
resByte, _ := res.ToJSON()
return resByte
}

// Add headers to the interceptor request.
// Note that at this point, the user's headers are bundled into the encrypted body of the interceptor's request
r.Header.Add("X-Forwarded-Host", parsedURL.Host)
Expand Down

0 comments on commit 99c134d

Please sign in to comment.