MacBook – How to access a computer behind internet sharing

high sierrainternet-sharingmacbook pro

On our router (which has WiFi), we have one ethernet port which is currently being used by my laptop. I need my raspberry pi model B rev. 2 connected to the network as well, but it doesn't use WiFi. (My laptop can, but i prefer ethernet.) I don't want to purchase a network switch, so what I've been doing is sharing our family Macbook Pro's WiFi connection using internet sharing, and connecting the pi to the Macbook over ethernet. The pi now has internet, but here's the catch: I want to run a web server on the pi, and be able to access it from my laptop. Is there a way to port forward over internet sharing?

P.S. If it helps, the web server will be running on port 8080.

To visualize the setup:

 Home Router --+-- WiFi  -- MBP* -- Ether -- Pi
               |
               +-- Ether -- Laptop


 * Internet Sharing 

Best Answer

I got it! I wrote a node.js script, like so, to redirect requests on port 8080 of the macbook to port 8080 of the raspberry pi. the pi's ip is 192.168.2.7.
the script:

var net = require('net');
net.createServer(function(socket) {
  var raspberrypi = net.connect(8080, '192.168.2.7');
  raspberrypi.on('connect', function() {
    raspberrypi.on('data', function(data) {
      socket.write(data);
    });
    socket.on('data', function(data) {
      raspberrypi.write(data);
    });
  });
}).listen(8080);
Related Question