Linux – How to route traffic from ssh server to the local machine

linuxnetworkingroutingssh

I am a student at a college. In my school, we have this strange rule that after 12 midnight, there is an internal LAN Ban. That means I am able to connect to the internet (external traffic) while sitting in my room, but I can't access most of the internal servers (say B) through the LAN, except for one server (say A). I can ssh to that machine (A) and ping other internal servers (B) and even ssh to them.

My College has its own hosted mirrors (B) for various distributions (http://mirror.cse.iitk.ac.in). I have configured package managers to use this mirror and during the day time, it works flawlessly and downloads happen at a whopping 10+ MB/s (its LAN), but during the nights when the LAN Ban is in action, I have to use other Indian Mirrors to download/update packages which is pathetically slow compared to the former.

What I want to achieve is write a script to route the traffic from server A to my local machine since A can access the mirror (B) even during the LAN Ban hours. I googled it up but didn't get what I wanted to do. In short, I want to setup a reverse-proxy like thing that makes my local machine access the mirror(B) by routing the traffic from A.

Is it possible? I am still a newbie and learning things. Any kind of help would be greatly appreciated.

Thanks in advance!

Best Answer

Let's cover all possible bases.

Method 1

Firstly, for the mirror. mirror.cse.iitk.ac.in is an external mirror, and has an external IP. Which means you can access it with an external IP address. At the time of writing, this resolves to 202.3.77.108. Use that in your mirror configuration file, and you should get speeds as good as on LAN (in my experience).

Method 2

Now, coming to the server A (which I'm assuming is webhome.cc.iitk.ac.in). Use an ssh tunnel. In short:

ssh -L8000:mirror.cse.iitk.ac.in:80 <username>@webhome.cc.iitk.ac.in

Keep the above ssh running (you can deamonize the command with a combination of -N and -f). Now, in your configuration file, instead of

http://mirror.cse.iitk.ac.in/

use

http://localhost:8000/

I wrote an article on port forwarding when I learnt about it. So this should be helpful to understand how the above works.

Comments

Next doubt (from my experience) will be about keeping ssh connected in background (reconnect if disconnect). Look into adding KeepAlive, ServerAliveInterval parameters in .ssh/config file ;)

Edit

I noticed that in the comments you said port forwarding is out of the question since you don't have root access on the server. The above command does not require root access on the server because of several reasons.

  1. The port is mapped on YOUR computer (not on the server). i.e. You will finally be listening on localhost:8000.
  2. Listening on ports which are greater than 1024 (8000 in this case) does not anyway require root access.

Notes

  1. If this was https, you'd be forwarding the requests to port 443 of mirror.cse.iitk.ac.in (instead of 80).

  2. The request goes like this:

YOU --> localhost:8000 (your PC) --via-ssh-tunnel-> webhome (forwards it to mirror:80) --> mirror.cse.iitk.ac.in (and then the reverse)

Related Question