準(zhǔn)備web服務(wù)
使用Flask啟動(dòng)一個(gè)簡(jiǎn)單的web服務(wù)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(port=5000)
瀏覽器訪問:http://127.0.0.1:5000/
通過pf實(shí)現(xiàn)端口轉(zhuǎn)發(fā)
將所有端口8080
的請(qǐng)求,都轉(zhuǎn)發(fā)到127.0.0.1:5000
,類似Nginx的反向代理
1、開啟IPv4 的轉(zhuǎn)發(fā)
# 開啟 IPv4 的轉(zhuǎn)發(fā)
$ sudo sysctl -w net.inet.ip.forwarding=1
# 查看當(dāng)前端口轉(zhuǎn)發(fā)功能狀態(tài)
sudo sysctl -a | grep forward
net.inet.ip.forwarding: 1
net.inet6.ip6.forwarding: 0
2、添加轉(zhuǎn)發(fā)配置文件
sudo vim /etc/pf.anchors/http
rdr pass on lo0 inet proto tcp from any to any port 8080 -> 127.0.0.1 port 5000
rdr pass on en0 inet proto tcp from any to any port 8080 -> 127.0.0.1 port 5000
查看到的網(wǎng)絡(luò)名稱
$ ifconfig -a
# lo0 是本機(jī)ipv4地址 eg: 127.0.0.1
# en0 是局域網(wǎng)ipv4地址 eg: 192.168.0.100
檢查正確性
$ sudo pfctl -vnf /etc/pf.anchors/http
3、修改pf配置文件
sudo vim /etc/pf.conf
增加如下配置
# 在 rdr-anchor "com.apple/*" 下添加
rdr-anchor "http-forwarding"
# 在 load anchor "com.apple" from "/etc/pf.anchors/com.apple" 下添加
load anchor "http-forwarding" from "/etc/pf.anchors/http"
重啟pf服務(wù)文章來源:http://www.zghlxwxcb.cn/news/detail-739504.html
sudo pfctl -ef /etc/pf.conf
瀏覽器訪問:http://127.0.0.1:8080/文章來源地址http://www.zghlxwxcb.cn/news/detail-739504.html
其他命令
# 啟用pf服務(wù)
$ sudo pfctl -e
# 強(qiáng)制重啟PF服務(wù)
$ sudo pfctl -E
# 關(guān)閉PF
$ sudo pfctl -d
參考文章
- https://man.openbsd.org/pfctl
- https://man.openbsd.org/pf.conf
- Mac下使用PF進(jìn)行端口轉(zhuǎn)發(fā)和防火墻配置(類似Linux的iptables)
到了這里,關(guān)于MacOS使用PF實(shí)現(xiàn)iptables的端口轉(zhuǎn)發(fā)功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!