1 Star 0 Fork 0

carlmax_my/console-core-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
service_cmd.go 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
carlmax_my 提交于 2024-12-02 23:02 . fix iptables cross platform issue
package iptables
import (
"fmt"
"os/exec"
)
type IptablesCmdService struct{}
func NewCmdService() IIptablesService {
return &IptablesCmdService{}
}
// # 增加iptables规则(如果需要从远程访问本地子网)
// iptables -A FORWARD -i wg0 -j ACCEPT
// iptables -A FORWARD -o wg0 -j ACCEPT
// iptables -A -t nat POSTROUTING -o eth0 -j MASQUERADE
// ip6tables -A FORWARD -i wg0 -j ACCEPT
// ip6tables -A FORWARD -o wg0 -j ACCEPT
// ip6tables -A -t nat POSTROUTING -o eth0 -j MASQUERADE
// # 对应的删除命令
// iptables -D FORWARD -i wg0 -j ACCEPT
// iptables -D FORWARD -o wg0 -j ACCEPT
// iptables -D -t nat POSTROUTING -o eth0 -j MASQUERADE
// ip6tables -D FORWARD -i wg0 -j ACCEPT
// ip6tables -D FORWARD -o wg0 -j ACCEPT
// ip6tables -D -t nat POSTROUTING -o eth0 -j MASQUERADE
func fmt_cmd_add_IptablesErr(err error, output []byte) error {
return fmt.Errorf("iptables.add err: %s; %s", err.Error(), output)
}
func fmt_cmd_del_IptablesErr(err error, output []byte) error {
return fmt.Errorf("iptables.del err: %s; %s", err.Error(), output)
}
func (s *IptablesCmdService) Add(wgIface string, eth string) error {
if output, err := exec.Command("iptables", "-A", "FORWARD", "-i", wgIface, "-j", "ACCEPT").CombinedOutput(); err != nil {
return fmt_cmd_add_IptablesErr(err, output)
}
if output, err := exec.Command("iptables", "-A", "FORWARD", "-o", wgIface, "-j", "ACCEPT").CombinedOutput(); err != nil {
return fmt_cmd_add_IptablesErr(err, output)
}
if output, err := exec.Command("iptables", "-A", "-t", "nat", "POSTROUTING", "-o", eth, "-j", "MASQUERADE").CombinedOutput(); err != nil {
return fmt_cmd_add_IptablesErr(err, output)
}
return nil
}
func (s *IptablesCmdService) Del(wgIface string, eth string) error {
if output, err := exec.Command("iptables", "-D", "FORWARD", "-i", wgIface, "-j", "ACCEPT").CombinedOutput(); err != nil {
return fmt_cmd_del_IptablesErr(err, output)
}
if output, err := exec.Command("iptables", "-D", "FORWARD", "-o", wgIface, "-j", "ACCEPT").CombinedOutput(); err != nil {
return fmt_cmd_del_IptablesErr(err, output)
}
if output, err := exec.Command("iptables", "-D", "-t", "nat", "POSTROUTING", "-o", eth, "-j", "MASQUERADE").CombinedOutput(); err != nil {
return fmt_cmd_del_IptablesErr(err, output)
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/carlmax_my/console-core-go.git
git@gitee.com:carlmax_my/console-core-go.git
carlmax_my
console-core-go
console-core-go
v0.0.24

搜索帮助

0d507c66 1850385 C8b1a773 1850385