1

I'm trying to realise the idea of waldalla to build a proxy server in a Google Chrome extension.

I understand how to code in a Chrome extension the server-facing side of the problem, i.e. sending HTTP requests and receiving responses, but I don't know how to do the client-facing side, i.e. listening on a defined port for HTTP requests from outside the browser.

I have read up on Chrome's Native Messaging API. However, from what I can see it is used for communication via stdin/stdout, not via HTTP.

So, how can I listen to HTTP requests in a Google Chrome extension? And how do I set the permissions for this in the extension's manifest.json file?

Xan
  • 74,770
  • 16
  • 179
  • 206
halloleo
  • 9,216
  • 13
  • 64
  • 122
  • @wOxxOm A local proxy server app is ok, but the HTTP request to the outside world has to be done by Chrome. So the proxy app would need to communicate requests and resposnes with Chrome via Native Messaging, right? – halloleo Nov 21 '18 at 06:11
  • Now that I've actually read the thread you've linked, I can say you need to write a native OS app (e.g. using node.js) that acts as a local proxy server for other apps. This app will route the requests to your extension via nativeMesaging API (which is stdio-based as described in the documentation), the extension will make the request using XMLHttpRequest or fetch in the [background script](https://developer.chrome.com/extensions/background_pages) and send the response back via nativeMessaging API. BTW, add a monotonically increasing numeric request ID to each message to identify its response. – wOxxOm Nov 21 '18 at 06:28
  • @wOxxOm Thanks for the detailed outline of a possible solution. Makes sense to me! – halloleo Nov 21 '18 at 06:40

1 Answers1

2

A Chrome extension has no way* to listen to network connections, at least arbitrary ones.

* Possible exception here being WebRTC, but that's not very useful for proxying standard HTTP.

Native Messaging is, indeed, a way to escape the API limitations by doing whatever you want in a native companion application that talks to the extension via STDIO.

[Extension] <-- STDIO --> [Native host] <-- anything, eg. HTTP --> [Client]

Note that it's not arbitrary STDIO, but a specific JSON-based native host protocol.

Also note that the extension will need to spawn a new copy of the native host application; it can't connect to an already running one.

Xan
  • 74,770
  • 16
  • 179
  • 206