Wednesday, 31 May 2017

Can I use Node.js Express to intercept download requests from apps running the cordova-plugin-file-transfer plugin?

I am creating a proxy server which intercepts all requests from an app. Each request is checked to see if the app user which sent the request is authorized. This is to prevent a user being logged in on multiple devices.

I know how to use express to intercept regular GET requests like this:

app.get('/download', function(req, res) { handleDownloadRequest(req) });

So if I surf to myserver.com/download my request will be handled.

Now the app uses code-push (https://microsoft.github.io/code-push/) to update. The code-push plugin uses other plugins such as cordova-plugin-file-transfer (I would post link here but I can only post 1 in this question) for the actual download.

Now the problem is that I can't seem to find a way to intercept the requests comming from the file transfer plugin.

The next piece of code is from FileTransfer.java in cordova-plugin-file-transfer/src/android/

connection = resourceApi.createHttpConnection(sourceUri);
if (useHttps && trustEveryone) {
   // Setup the HTTPS connection class to trust everyone
   HttpsURLConnection https = (HttpsURLConnection)connection;
   oldSocketFactory = trustAllHosts(https);
   // Save the current hostnameVerifier
   oldHostnameVerifier = https.getHostnameVerifier();
   // Setup the connection not to verify hostnames
   https.setHostnameVerifier(DO_NOT_VERIFY);
}

connection.setRequestMethod("GET");

// This must be explicitly set for gzip progress tracking to work.
connection.setRequestProperty("Accept-Encoding", "gzip");

// Handle the other headers
if (headers != null) {
    addHeadersToRequest(connection, headers);
}
connection.connect();

So how I see this is that firstly the connection gets created with a URI (the same URI as above being http://myserver.com/download. Secondly the requestmethod gets set to GET, and lastly connection.connect() is called to establish the connection.

Is it simply not possible to intercept these kinds of requests with node.js express?



via Jonas Semeelen

No comments:

Post a Comment