3

To emulate the CI integration testing environment for the application that I’m working with, I want to be able to have the following mapping of requests happen:

  • foo:8080 ⟶ localhost:8081
  • bar:8080 ⟶ localhost:8080

Without the port change, I can do this easily enough by editing /etc/hosts¹ to define foo and bar to be 127.0.0.1, but the port forwarding has me stumped. Is this possible with pf and if so, how? I’ve found recipes that would map all requests to port 8080 to a different port, but I only want it remapped if it’s a specific hostname.


  1. I have a vague recollection that Network Utility used to provide this capability but it is now deprecated.

1 Answer 1

4

One method would be to create alias IP addresses on the localhost interface as root

ifconfig lo0 alias 127.0.0.10  0xff000000
ifconfig lo0 alias 127.0.0.100 0xff000000

and then define foo and bar as appropriate in /etc/hosts or a local DNS server

127.0.0.10 foo
127.0.0.100 bar

and then requests involving the one IP address or the other can likely be redirected as need be, or they could be used directly if the software only listens on the specific IP address involved.

(There are several such IP addresses available; 127.0.0.0/8 is pretty big.)

2
  • 1
    I was thinking of something like this. One note though: I'm pretty sure aliases don't survive reboots, so you'd probably have to create something like a LaunchDaemon to recreate the alias(es) after each startup. Jul 26, 2022 at 3:18
  • @GordonDavisson they do not but a LaunchDaemon or an @reboot cronjob or some fire-up-the-test-environment script could handle that.
    – thrig
    Jul 26, 2022 at 14:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .