mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-03 23:20:25 +08:00
优化nftables相关代码
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||||
"github.com/iwind/TeaGo/lists"
|
"github.com/iwind/TeaGo/lists"
|
||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
|
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -153,6 +154,11 @@ func (this *DDoSProtectionManager) Apply(config *ddosconfigs.ProtectionConfig) e
|
|||||||
|
|
||||||
// 添加TCP规则
|
// 添加TCP规则
|
||||||
func (this *DDoSProtectionManager) addTCPRules(tcpConfig *ddosconfigs.TCPConfig) error {
|
func (this *DDoSProtectionManager) addTCPRules(tcpConfig *ddosconfigs.TCPConfig) error {
|
||||||
|
// 检查nft版本不能小于0.9
|
||||||
|
if len(nftablesInstance.version) > 0 && stringutil.VersionCompare("0.9", nftablesInstance.version) > 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
var ports = []int32{}
|
var ports = []int32{}
|
||||||
for _, portConfig := range tcpConfig.Ports {
|
for _, portConfig := range tcpConfig.Ports {
|
||||||
if !lists.ContainsInt32(ports, portConfig.Port) {
|
if !lists.ContainsInt32(ports, portConfig.Port) {
|
||||||
@@ -237,9 +243,11 @@ func (this *DDoSProtectionManager) addTCPRules(tcpConfig *ddosconfigs.TCPConfig)
|
|||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
if maxConnections > 0 {
|
if maxConnections > 0 {
|
||||||
var cmd = exec.Command(this.nftPath, "add", "rule", protocol, filter.Name, nftablesChainName, "tcp", "dport", types.String(port), "ct", "count", "over", types.String(maxConnections), "counter", "drop", "comment", this.encodeUserData([]string{"tcp", types.String(port), "maxConnections", types.String(maxConnections)}))
|
var cmd = exec.Command(this.nftPath, "add", "rule", protocol, filter.Name, nftablesChainName, "tcp", "dport", types.String(port), "ct", "count", "over", types.String(maxConnections), "counter", "drop", "comment", this.encodeUserData([]string{"tcp", types.String(port), "maxConnections", types.String(maxConnections)}))
|
||||||
|
var stderr = &bytes.Buffer{}
|
||||||
|
cmd.Stderr = stderr
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("add nftables rule '" + cmd.String() + "' failed: " + err.Error())
|
return errors.New("add nftables rule '" + cmd.String() + "' failed: " + err.Error() + " (" + stderr.String() + ")")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,12 +23,13 @@ func NewFirewalld() *Firewalld {
|
|||||||
|
|
||||||
path, err := exec.LookPath("firewall-cmd")
|
path, err := exec.LookPath("firewall-cmd")
|
||||||
if err == nil && len(path) > 0 {
|
if err == nil && len(path) > 0 {
|
||||||
var cmd = exec.Command(path, "-V")
|
var cmd = exec.Command(path, "--state")
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
firewalld.exe = path
|
firewalld.exe = path
|
||||||
// TODO check firewalld status with 'firewall-cmd --state' (running or not running),
|
// TODO check firewalld status with 'firewall-cmd --state' (running or not running),
|
||||||
// but we should recover the state when firewalld state changes, maybe check it every minutes
|
// but we should recover the state when firewalld state changes, maybe check it every minutes
|
||||||
|
|
||||||
firewalld.isReady = true
|
firewalld.isReady = true
|
||||||
firewalld.init()
|
firewalld.init()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
package firewalls
|
package firewalls
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||||
@@ -12,6 +13,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -87,6 +89,7 @@ func NewNFTablesFirewall() (*NFTablesFirewall, error) {
|
|||||||
type NFTablesFirewall struct {
|
type NFTablesFirewall struct {
|
||||||
conn *nftables.Conn
|
conn *nftables.Conn
|
||||||
isReady bool
|
isReady bool
|
||||||
|
version string
|
||||||
|
|
||||||
allowIPv4Set *nftables.Set
|
allowIPv4Set *nftables.Set
|
||||||
allowIPv6Set *nftables.Set
|
allowIPv6Set *nftables.Set
|
||||||
@@ -99,10 +102,11 @@ type NFTablesFirewall struct {
|
|||||||
|
|
||||||
func (this *NFTablesFirewall) init() error {
|
func (this *NFTablesFirewall) init() error {
|
||||||
// check nft
|
// check nft
|
||||||
_, err := exec.LookPath("nft")
|
nftPath, err := exec.LookPath("nft")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New("nft not found")
|
return errors.New("nft not found")
|
||||||
}
|
}
|
||||||
|
this.version = this.readVersion(nftPath)
|
||||||
|
|
||||||
// table
|
// table
|
||||||
for _, tableDef := range nftablesFilters {
|
for _, tableDef := range nftablesFilters {
|
||||||
@@ -371,3 +375,21 @@ func (this *NFTablesFirewall) RemoveSourceIP(ip string) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 读取版本号
|
||||||
|
func (this *NFTablesFirewall) readVersion(nftPath string) string {
|
||||||
|
var cmd = exec.Command(nftPath, "--version")
|
||||||
|
var output = &bytes.Buffer{}
|
||||||
|
cmd.Stdout = output
|
||||||
|
err := cmd.Run()
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
var outputString = output.String()
|
||||||
|
var versionMatches = regexp.MustCompile(`nftables v([\d.]+)`).FindStringSubmatch(outputString)
|
||||||
|
if len(versionMatches) <= 1 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return versionMatches[1]
|
||||||
|
}
|
||||||
|
|||||||
@@ -257,6 +257,12 @@ func (this *ListenerManager) addToFirewalld(groupAddrs []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查状态
|
||||||
|
err = exec.Command(firewallCmd, "--state").Run()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
remotelogs.Println("FIREWALLD", "open ports automatically")
|
remotelogs.Println("FIREWALLD", "open ports automatically")
|
||||||
for _, port := range ports {
|
for _, port := range ports {
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user