Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HTTP requests for Squirrel scripts #344

Merged
merged 40 commits into from
Dec 13, 2022

Conversation

Erlite
Copy link
Contributor

@Erlite Erlite commented Nov 25, 2022

Requires R2Northstar/NorthstarMods#531.

This PR aims to allow Squirrel scripts to make HTTP requests. This would allow them to do things like query APIs, send data to a server to save and more.

All HTTP request methods are supported. However, the following limitations apply:

  • You cannot make a request to a private network address.
  • You may only make a request to a hostname that resolves to an IPv4 address.

The above limitations are for security reasons. If for some reason, you need access to private network addresses or IPv6 hosts, you may start the game with -allowlocalhttp to bypass those checks.

You can also opt-out of letting mods use HTTP requests by starting your game with -disablehttprequests.

Checklist

  • Native HttpRequestHandler
  • Squirrel functions to make an HTTP request.
  • Prevent localhost HTTP requests unless explicitly enabled using -allowlocalhttp.
    • This is to prevent any potential exploit. This can be disabled for certain... usecases.
  • Add support for "arrays" in query parameters.

Example

I will be simplifying the functions a little bit, but for now, this is how it looks:

void function OnSuccess(int code, string body, string headers)
{
    printt(code, body, headers)
}

void function OnFailure(int error, string message)
{
    printt(error, message)
}

void function NSTestRequest()
{
    HttpRequest request = { ... }
    request.method = HttpRequestMethod.GET
    request.baseUrl = "https://northstar.tf/accounts/get_username"
    request.headers["hello"] <- "world"
    request.queryParameters["uid"] <- GetPlayerArray()[0].GetUID()
    request.contentType = "application/json"

    NSMakeHTTPRequest(request, OnSuccess, OnFailure)
}

Screenshots.

image

# Conflicts:
#	NorthstarDLL/NorthstarDLL.vcxproj
#	NorthstarDLL/NorthstarDLL.vcxproj.filters
# Conflicts:
#	NorthstarDLL/NorthstarDLL.vcxproj
#	NorthstarDLL/NorthstarDLL.vcxproj.filters
@Erlite
Copy link
Contributor Author

Erlite commented Nov 25, 2022

Rebasing murdered the history a little, oh well.

@pg9182 pg9182 self-requested a review November 25, 2022 23:58
@pg9182 pg9182 changed the title Implement HTTP requests for Squirrel scripts. Implement HTTP requests for Squirrel scripts Nov 25, 2022
@Erlite
Copy link
Contributor Author

Erlite commented Nov 26, 2022

Feature complete, fixing format errors, and I'll also write the Squirrel side before we can consider reviewing and merging.

@Erlite Erlite marked this pull request as ready for review November 26, 2022 12:18
@Erlite
Copy link
Contributor Author

Erlite commented Nov 26, 2022

Requires the Squirrel side first, making a PR later, don't merge this yet!

@GeckoEidechse GeckoEidechse marked this pull request as draft November 26, 2022 12:39
@GeckoEidechse
Copy link
Member

Set to draft to prevent accidental merge

@Erlite Erlite marked this pull request as ready for review November 27, 2022 20:54
@Erlite
Copy link
Contributor Author

Erlite commented Nov 27, 2022

Ready for review.

@Erlite
Copy link
Contributor Author

Erlite commented Nov 28, 2022

Add -disablehttpssl to ignore SSL verifications

This is especially useful when the host's ssl cert is invalid, or when testing in development for things like APIs running on localhost and the API refuses non-HTTPs requests.

Copy link
Member

@emma-miler emma-miler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review looks good, just a few small things. Will test and re-review once these issues are resolved

NorthstarDLL/httprequesthandler.cpp Outdated Show resolved Hide resolved
NorthstarDLL/httprequesthandler.cpp Outdated Show resolved Hide resolved
NorthstarDLL/httprequesthandler.cpp Show resolved Hide resolved
NorthstarDLL/httprequesthandler.cpp Show resolved Hide resolved
NorthstarDLL/httprequesthandler.cpp Show resolved Hide resolved
@emma-miler emma-miler self-requested a review December 13, 2022 16:51
@emma-miler emma-miler merged commit f028561 into R2Northstar:main Dec 13, 2022
@Erlite Erlite deleted the squirrel-http-main branch December 13, 2022 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants