How to port forward with firewalld depending on source IP

Solution 1:

You just create a firewalld rule to allow the traffic and then you configure NAT for the traffic. Essentially you are creating an ACL to determine what traffic is allowed in and then are you making a NAT rule to say that the allowed traffic should be translated.

firewall-cmd --permanent --zone=public --add-rich-rule="rule 
family="ipv4" \
source address="1.2.3.4/32" \
port protocol="tcp" port="22" accept"
firewall-cmd --permanent --zone=public --add-forward-port=port=22:proto=tcp:toport=5678:toaddr=*private translated IP address*
firewall-cmd --reload

How to open port for a specific IP address with firewall-cmd on CentOS?

http://www.certdepot.net/rhel7-get-started-firewalld/

Solution 2:

Figured it out:

CUSTOMPORT=$(netstat -tlpn | grep 0.0.0.0.*ssh | cut -d: -f2 | cut -f1 -d\ )
SOURCE_IP=1.2.3.4
firewall-cmd --zone=public --permanent --query-masquerade
firewall-cmd --zone=public --permanent --add-masquerade
firewall-cmd --zone=public --permanent --add-rich-rule="rule family=\"ipv4\" source address=\"${SOURCE_IP}\" forward-port port=\"22\" protocol=\"tcp\" to-port=\"${CUSTOMPORT}\""
firewall-cmd --reload