There are following options to extract remote IP address in gRPC middleware.
-
Running your custom plugin in the custom post (request) middleware. Anything in pre or auth/post auth isn't possible without native Golang.
-
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 in that retrieves that 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.
-
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 with gRPC Java.
Js 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 Golang to do it instead. Of course this may affect the api design of your other APIs. 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() {}
Comments
0 comments
Please sign in to leave a comment.