There are various ways to extract or obtain the remote IP address. The following options are:
Internally
-
Running your custom plugin in the custom post request or endpoint middleware. Anything in pre or auth/post auth isn't possible without native Golang.
Using global headers and context variable to retrieve the valueJs plugin snippet below.var ipListHeaderKey = "X-Ip-List";
var xIpList = getHeaderOrEmptyString(request, ipListHeaderKey);
if (xIpList != "") {
request = returnOverride(request, 200, "Got it! " + xIpList[0]);
request = deleteHeader(request, ipListHeaderKey);
}
else {
request = returnOverride(request, 500, "Missed it!");
}
log("Ip list: " + JSON.stringi -
Use another API definition as the proxy and retrieve the value of the IP in the post middleware. Then URL rewrite to your desired API definition with looping. The header value should be available in the pre request within any plugin.
-
Use Golang to do it instead. The nice thing is that the IP can be retrieved from the pre-request middleware scope in Golang.
Golang snippet
// AddFooBarHeader adds custom "Foo: Bar" header to the request
func AddFooBarHeader(rw http.ResponseWriter, r *http.Request) {
logger.Info("Processing HTTP request in Golang plugin!!")
// get origin ip address of request
requestOrigin := r.RemoteAddr
// Add origin ip to custom header
r.Header.Add("X-Ip-Address", requestOrigin)
}
func main() {}
Externally
Find a way to retrieve the IP before it's sent to Tyk. You could pass it directly in a header or within some proxy that retrieves the value before it's sent to the gateway. I believe nginx is a good example here as discussed in our article on creating an IP-based rate-limiter with Tyk and JavaScript middleware.
Comments
0 comments
Please sign in to leave a comment.