【Network】端口转发(Port Forward)

Posted by 西维蜀黍 on 2024-05-06, Last Modified on 2024-05-07

Forward A Local Port to A Local/External Port

ssh

Refer to https://swsmile.info/post/linux-ssh-usage/

socat

Refer to https://swsmile.info/post/linux-socat/

iptables

Refer to https://swsmile.info/post/linux-iptables/

pfctl

Refer to https://swsmile.info/post/freebsd-pfctl/

Forward An External Port to An External Port

My scenarios is:

  1. The IP of 1.1.1.1 is xx.rwlb.singapore.rds.aliyuncs.com
  2. My local development env is not able to directly access 1.1.1.1 due to security concerns
  3. My DBA sets a jump server (2.2.2.2:3306) as a proxy to allow my local development env to indirectly access 1.1.1.1
  4. However, all middleware addresses are hard coded in my code repo, i.e., 1.1.1.1
  5. I wanna not modify the code and am still able to connect to all middleware

Idea: map 1.1.1.1:32111 to 2.2.2.2:3306 on kernel level, so that connecting 1.1.1.1:32111 actually is forwarded to 2.2.2.2:3306. As a result, my code doesn’t need any changes.

iptables

Refer to https://swsmile.info/post/linux-iptables/

pfctl

// TODO didn’t figure out how exactly

Reference