1.4.5.2
This commit is contained in:
568
EdgeNode/internal/firewalls/ddos_protection.go
Normal file
568
EdgeNode/internal/firewalls/ddos_protection.go
Normal file
@@ -0,0 +1,568 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/zero"
|
||||
"github.com/iwind/TeaGo/lists"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var SharedDDoSProtectionManager = NewDDoSProtectionManager()
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
events.On(events.EventReload, func() {
|
||||
if nftablesInstance == nil {
|
||||
return
|
||||
}
|
||||
|
||||
nodeConfig, _ := nodeconfigs.SharedNodeConfig()
|
||||
if nodeConfig != nil {
|
||||
err := SharedDDoSProtectionManager.Apply(nodeConfig.DDoSProtection)
|
||||
if err != nil {
|
||||
remotelogs.Error("FIREWALL", "apply DDoS protection failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
events.On(events.EventNFTablesReady, func() {
|
||||
nodeConfig, _ := nodeconfigs.SharedNodeConfig()
|
||||
if nodeConfig != nil {
|
||||
err := SharedDDoSProtectionManager.Apply(nodeConfig.DDoSProtection)
|
||||
if err != nil {
|
||||
remotelogs.Error("FIREWALL", "apply DDoS protection failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// DDoSProtectionManager DDoS防护
|
||||
type DDoSProtectionManager struct {
|
||||
lastAllowIPList []string
|
||||
lastConfig []byte
|
||||
|
||||
locker sync.Mutex
|
||||
}
|
||||
|
||||
// NewDDoSProtectionManager 获取新对象
|
||||
func NewDDoSProtectionManager() *DDoSProtectionManager {
|
||||
return &DDoSProtectionManager{}
|
||||
}
|
||||
|
||||
// Apply 应用配置
|
||||
func (this *DDoSProtectionManager) Apply(config *ddosconfigs.ProtectionConfig) error {
|
||||
// 加锁防止并发更改
|
||||
if !this.locker.TryLock() {
|
||||
return nil
|
||||
}
|
||||
defer this.locker.Unlock()
|
||||
|
||||
// 同集群节点IP白名单
|
||||
var allowIPListChanged = false
|
||||
nodeConfig, _ := nodeconfigs.SharedNodeConfig()
|
||||
if nodeConfig != nil {
|
||||
var allowIPList = nodeConfig.AllowedIPs
|
||||
if !utils.EqualStrings(allowIPList, this.lastAllowIPList) {
|
||||
allowIPListChanged = true
|
||||
this.lastAllowIPList = allowIPList
|
||||
}
|
||||
}
|
||||
|
||||
// 对比配置
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode config to json failed: %w", err)
|
||||
}
|
||||
if !allowIPListChanged && bytes.Equal(this.lastConfig, configJSON) {
|
||||
return nil
|
||||
}
|
||||
remotelogs.Println("FIREWALL", "change DDoS protection config")
|
||||
|
||||
if len(nftables.NftExePath()) == 0 {
|
||||
return errors.New("can not find nft command")
|
||||
}
|
||||
|
||||
if nftablesInstance == nil {
|
||||
if config == nil || !config.IsOn() {
|
||||
return nil
|
||||
}
|
||||
return errors.New("nftables instance should not be nil")
|
||||
}
|
||||
|
||||
if config == nil {
|
||||
// TCP
|
||||
err := this.removeTCPRules()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO other protocols
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TCP
|
||||
if config.TCP == nil {
|
||||
err := this.removeTCPRules()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// allow ip list
|
||||
var allowIPList = []string{}
|
||||
for _, ipConfig := range config.TCP.AllowIPList {
|
||||
allowIPList = append(allowIPList, ipConfig.IP)
|
||||
}
|
||||
for _, ip := range this.lastAllowIPList {
|
||||
if !lists.ContainsString(allowIPList, ip) {
|
||||
allowIPList = append(allowIPList, ip)
|
||||
}
|
||||
}
|
||||
err = this.updateAllowIPList(allowIPList)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// tcp
|
||||
if config.TCP.IsOn {
|
||||
err := this.addTCPRules(config.TCP)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
err := this.removeTCPRules()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.lastConfig = configJSON
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 添加TCP规则
|
||||
func (this *DDoSProtectionManager) addTCPRules(tcpConfig *ddosconfigs.TCPConfig) error {
|
||||
var nftExe = nftables.NftExePath()
|
||||
if len(nftExe) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查nft版本不能小于0.9
|
||||
if len(nftablesInstance.version) > 0 && stringutil.VersionCompare("0.9", nftablesInstance.version) > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var ports = []int32{}
|
||||
for _, portConfig := range tcpConfig.Ports {
|
||||
if !lists.ContainsInt32(ports, portConfig.Port) {
|
||||
ports = append(ports, portConfig.Port)
|
||||
}
|
||||
}
|
||||
if len(ports) == 0 {
|
||||
ports = []int32{80, 443}
|
||||
}
|
||||
|
||||
for _, filter := range nftablesFilters {
|
||||
chain, oldRules, err := this.getRules(filter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get old rules failed: %w", err)
|
||||
}
|
||||
|
||||
var protocol = filter.protocol()
|
||||
|
||||
// max connections
|
||||
var maxConnections = tcpConfig.MaxConnections
|
||||
if maxConnections <= 0 {
|
||||
maxConnections = nodeconfigs.DefaultTCPMaxConnections
|
||||
if maxConnections <= 0 {
|
||||
maxConnections = 100000
|
||||
}
|
||||
}
|
||||
|
||||
// max connections per ip
|
||||
var maxConnectionsPerIP = tcpConfig.MaxConnectionsPerIP
|
||||
if maxConnectionsPerIP <= 0 {
|
||||
maxConnectionsPerIP = nodeconfigs.DefaultTCPMaxConnectionsPerIP
|
||||
if maxConnectionsPerIP <= 0 {
|
||||
maxConnectionsPerIP = 100000
|
||||
}
|
||||
}
|
||||
|
||||
// new connections rate (minutely)
|
||||
var newConnectionsMinutelyRate = tcpConfig.NewConnectionsMinutelyRate
|
||||
if newConnectionsMinutelyRate <= 0 {
|
||||
newConnectionsMinutelyRate = nodeconfigs.DefaultTCPNewConnectionsMinutelyRate
|
||||
if newConnectionsMinutelyRate <= 0 {
|
||||
newConnectionsMinutelyRate = 100000
|
||||
}
|
||||
}
|
||||
var newConnectionsMinutelyRateBlockTimeout = tcpConfig.NewConnectionsMinutelyRateBlockTimeout
|
||||
if newConnectionsMinutelyRateBlockTimeout < 0 {
|
||||
newConnectionsMinutelyRateBlockTimeout = 0
|
||||
}
|
||||
|
||||
// new connections rate (secondly)
|
||||
var newConnectionsSecondlyRate = tcpConfig.NewConnectionsSecondlyRate
|
||||
if newConnectionsSecondlyRate <= 0 {
|
||||
newConnectionsSecondlyRate = nodeconfigs.DefaultTCPNewConnectionsSecondlyRate
|
||||
if newConnectionsSecondlyRate <= 0 {
|
||||
newConnectionsSecondlyRate = 10000
|
||||
}
|
||||
}
|
||||
var newConnectionsSecondlyRateBlockTimeout = tcpConfig.NewConnectionsSecondlyRateBlockTimeout
|
||||
if newConnectionsSecondlyRateBlockTimeout < 0 {
|
||||
newConnectionsSecondlyRateBlockTimeout = 0
|
||||
}
|
||||
|
||||
// 检查是否有变化
|
||||
var hasChanges = false
|
||||
for _, port := range ports {
|
||||
if !this.existsRule(oldRules, []string{"tcp", types.String(port), "maxConnections", types.String(maxConnections)}) {
|
||||
hasChanges = true
|
||||
break
|
||||
}
|
||||
if !this.existsRule(oldRules, []string{"tcp", types.String(port), "maxConnectionsPerIP", types.String(maxConnectionsPerIP)}) {
|
||||
hasChanges = true
|
||||
break
|
||||
}
|
||||
if !this.existsRule(oldRules, []string{"tcp", types.String(port), "newConnectionsRate", types.String(newConnectionsMinutelyRate), types.String(newConnectionsMinutelyRateBlockTimeout)}) {
|
||||
hasChanges = true
|
||||
break
|
||||
}
|
||||
if !this.existsRule(oldRules, []string{"tcp", types.String(port), "newConnectionsSecondlyRate", types.String(newConnectionsSecondlyRate), types.String(newConnectionsSecondlyRateBlockTimeout)}) {
|
||||
hasChanges = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !hasChanges {
|
||||
// 检查是否有多余的端口
|
||||
var oldPorts = this.getTCPPorts(oldRules)
|
||||
if !this.eqPorts(ports, oldPorts) {
|
||||
hasChanges = true
|
||||
}
|
||||
}
|
||||
|
||||
if !hasChanges {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 先清空所有相关规则
|
||||
err = this.removeOldTCPRules(chain, oldRules)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete old rules failed: %w", err)
|
||||
}
|
||||
|
||||
// 添加新规则
|
||||
for _, port := range ports {
|
||||
if maxConnections > 0 {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, nftExe, "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)}))
|
||||
cmd.WithStderr()
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("add nftables rule '%s' failed: %w (%s)", cmd.String(), err, cmd.Stderr())
|
||||
}
|
||||
}
|
||||
|
||||
// TODO 让用户选择是drop还是reject
|
||||
if maxConnectionsPerIP > 0 {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, nftExe, "add", "rule", protocol, filter.Name, nftablesChainName, "tcp", "dport", types.String(port), "meter", "meter-"+protocol+"-"+types.String(port)+"-max-connections", "{ "+protocol+" saddr ct count over "+types.String(maxConnectionsPerIP)+" }", "counter", "drop", "comment", this.encodeUserData([]string{"tcp", types.String(port), "maxConnectionsPerIP", types.String(maxConnectionsPerIP)}))
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("add nftables rule '%s' failed: %w (%s)", cmd.String(), err, cmd.Stderr())
|
||||
}
|
||||
}
|
||||
|
||||
// 超过一定速率就drop或者加入黑名单(分钟)
|
||||
// TODO 让用户选择是drop还是reject
|
||||
if newConnectionsMinutelyRate > 0 {
|
||||
if newConnectionsMinutelyRateBlockTimeout > 0 {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, nftExe, "add", "rule", protocol, filter.Name, nftablesChainName, "tcp", "dport", types.String(port), "ct", "state", "new", "meter", "meter-"+protocol+"-"+types.String(port)+"-new-connections-rate", "{ "+protocol+" saddr limit rate over "+types.String(newConnectionsMinutelyRate)+"/minute burst "+types.String(newConnectionsMinutelyRate+3)+" packets }", "add", "@deny_set", "{"+protocol+" saddr timeout "+types.String(newConnectionsMinutelyRateBlockTimeout)+"s}", "comment", this.encodeUserData([]string{"tcp", types.String(port), "newConnectionsRate", types.String(newConnectionsMinutelyRate), types.String(newConnectionsMinutelyRateBlockTimeout)}))
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("add nftables rule '%s' failed: %w (%s)", cmd.String(), err, cmd.Stderr())
|
||||
}
|
||||
} else {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, nftExe, "add", "rule", protocol, filter.Name, nftablesChainName, "tcp", "dport", types.String(port), "ct", "state", "new", "meter", "meter-"+protocol+"-"+types.String(port)+"-new-connections-rate", "{ "+protocol+" saddr limit rate over "+types.String(newConnectionsMinutelyRate)+"/minute burst "+types.String(newConnectionsMinutelyRate+3)+" packets }" /**"add", "@deny_set", "{"+protocol+" saddr}",**/, "counter", "drop", "comment", this.encodeUserData([]string{"tcp", types.String(port), "newConnectionsRate", "0"}))
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("add nftables rule '%s' failed: %w (%s)", cmd.String(), err, cmd.Stderr())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 超过一定速率就drop或者加入黑名单(秒)
|
||||
// TODO 让用户选择是drop还是reject
|
||||
if newConnectionsSecondlyRate > 0 {
|
||||
if newConnectionsSecondlyRateBlockTimeout > 0 {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, nftExe, "add", "rule", protocol, filter.Name, nftablesChainName, "tcp", "dport", types.String(port), "ct", "state", "new", "meter", "meter-"+protocol+"-"+types.String(port)+"-new-connections-secondly-rate", "{ "+protocol+" saddr limit rate over "+types.String(newConnectionsSecondlyRate)+"/second burst "+types.String(newConnectionsSecondlyRate+3)+" packets }", "add", "@deny_set", "{"+protocol+" saddr timeout "+types.String(newConnectionsSecondlyRateBlockTimeout)+"s}", "comment", this.encodeUserData([]string{"tcp", types.String(port), "newConnectionsSecondlyRate", types.String(newConnectionsSecondlyRate), types.String(newConnectionsSecondlyRateBlockTimeout)}))
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("add nftables rule '%s' failed: %w (%s)", cmd.String(), err, cmd.Stderr())
|
||||
}
|
||||
} else {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, nftExe, "add", "rule", protocol, filter.Name, nftablesChainName, "tcp", "dport", types.String(port), "ct", "state", "new", "meter", "meter-"+protocol+"-"+types.String(port)+"-new-connections-secondly-rate", "{ "+protocol+" saddr limit rate over "+types.String(newConnectionsSecondlyRate)+"/second burst "+types.String(newConnectionsSecondlyRate+3)+" packets }" /**"add", "@deny_set", "{"+protocol+" saddr}",**/, "counter", "drop", "comment", this.encodeUserData([]string{"tcp", types.String(port), "newConnectionsSecondlyRate", "0"}))
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("add nftables rule '%s' failed: %w (%s)", cmd.String(), err, cmd.Stderr())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 删除TCP规则
|
||||
func (this *DDoSProtectionManager) removeTCPRules() error {
|
||||
for _, filter := range nftablesFilters {
|
||||
chain, rules, err := this.getRules(filter)
|
||||
|
||||
// TCP
|
||||
err = this.removeOldTCPRules(chain, rules)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 组合user data
|
||||
// 数据中不能包含字母、数字、下划线以外的数据
|
||||
func (this *DDoSProtectionManager) encodeUserData(attrs []string) string {
|
||||
if attrs == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return "ZZ" + strings.Join(attrs, "_") + "ZZ"
|
||||
}
|
||||
|
||||
// 解码user data
|
||||
func (this *DDoSProtectionManager) decodeUserData(data []byte) []string {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var dataCopy = make([]byte, len(data))
|
||||
copy(dataCopy, data)
|
||||
|
||||
var separatorLen = 2
|
||||
var index1 = bytes.Index(dataCopy, []byte{'Z', 'Z'})
|
||||
if index1 < 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
dataCopy = dataCopy[index1+separatorLen:]
|
||||
var index2 = bytes.LastIndex(dataCopy, []byte{'Z', 'Z'})
|
||||
if index2 < 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var s = string(dataCopy[:index2])
|
||||
var pieces = strings.Split(s, "_")
|
||||
for index, piece := range pieces {
|
||||
pieces[index] = strings.TrimSpace(piece)
|
||||
}
|
||||
return pieces
|
||||
}
|
||||
|
||||
// 清除规则
|
||||
func (this *DDoSProtectionManager) removeOldTCPRules(chain *nftables.Chain, rules []*nftables.Rule) error {
|
||||
for _, rule := range rules {
|
||||
var pieces = this.decodeUserData(rule.UserData())
|
||||
if len(pieces) < 4 {
|
||||
continue
|
||||
}
|
||||
if pieces[0] != "tcp" {
|
||||
continue
|
||||
}
|
||||
switch pieces[2] {
|
||||
case "maxConnections", "maxConnectionsPerIP", "newConnectionsRate", "newConnectionsSecondlyRate":
|
||||
err := chain.DeleteRule(rule)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 根据参数检查规则是否存在
|
||||
func (this *DDoSProtectionManager) existsRule(rules []*nftables.Rule, attrs []string) (exists bool) {
|
||||
if len(attrs) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, oldRule := range rules {
|
||||
var pieces = this.decodeUserData(oldRule.UserData())
|
||||
if len(attrs) != len(pieces) {
|
||||
continue
|
||||
}
|
||||
var isSame = true
|
||||
for index, piece := range pieces {
|
||||
if strings.TrimSpace(piece) != attrs[index] {
|
||||
isSame = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if isSame {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 获取规则中的端口号
|
||||
func (this *DDoSProtectionManager) getTCPPorts(rules []*nftables.Rule) []int32 {
|
||||
var ports = []int32{}
|
||||
for _, rule := range rules {
|
||||
var pieces = this.decodeUserData(rule.UserData())
|
||||
if len(pieces) != 4 {
|
||||
continue
|
||||
}
|
||||
if pieces[0] != "tcp" {
|
||||
continue
|
||||
}
|
||||
var port = types.Int32(pieces[1])
|
||||
if port > 0 && !lists.ContainsInt32(ports, port) {
|
||||
ports = append(ports, port)
|
||||
}
|
||||
}
|
||||
return ports
|
||||
}
|
||||
|
||||
// 检查端口是否一样
|
||||
func (this *DDoSProtectionManager) eqPorts(ports1 []int32, ports2 []int32) bool {
|
||||
if len(ports1) != len(ports2) {
|
||||
return false
|
||||
}
|
||||
|
||||
var portMap = map[int32]bool{}
|
||||
for _, port := range ports2 {
|
||||
portMap[port] = true
|
||||
}
|
||||
|
||||
for _, port := range ports1 {
|
||||
_, ok := portMap[port]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 查找Table
|
||||
func (this *DDoSProtectionManager) getTable(filter *nftablesTableDefinition) (*nftables.Table, error) {
|
||||
var family nftables.TableFamily
|
||||
if filter.IsIPv4 {
|
||||
family = nftables.TableFamilyIPv4
|
||||
} else if filter.IsIPv6 {
|
||||
family = nftables.TableFamilyIPv6
|
||||
} else {
|
||||
return nil, errors.New("table '" + filter.Name + "' should be IPv4 or IPv6")
|
||||
}
|
||||
return nftablesInstance.conn.GetTable(filter.Name, family)
|
||||
}
|
||||
|
||||
// 查找所有规则
|
||||
func (this *DDoSProtectionManager) getRules(filter *nftablesTableDefinition) (*nftables.Chain, []*nftables.Rule, error) {
|
||||
table, err := this.getTable(filter)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("get table failed: %w", err)
|
||||
}
|
||||
chain, err := table.GetChain(nftablesChainName)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("get chain failed: %w", err)
|
||||
}
|
||||
rules, err := chain.GetRules()
|
||||
return chain, rules, err
|
||||
}
|
||||
|
||||
// 更新白名单
|
||||
func (this *DDoSProtectionManager) updateAllowIPList(allIPList []string) error {
|
||||
if nftablesInstance == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var allMap = map[string]zero.Zero{}
|
||||
for _, ip := range allIPList {
|
||||
allMap[ip] = zero.New()
|
||||
}
|
||||
|
||||
for _, set := range []*nftables.Set{nftablesInstance.allowIPv4Set, nftablesInstance.allowIPv6Set} {
|
||||
var isIPv4 = set == nftablesInstance.allowIPv4Set
|
||||
var isIPv6 = !isIPv4
|
||||
|
||||
// 现有的
|
||||
oldList, err := set.GetIPElements()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var oldMap = map[string]zero.Zero{} // ip=> zero
|
||||
for _, ip := range oldList {
|
||||
oldMap[ip] = zero.New()
|
||||
|
||||
if (utils.IsIPv4(ip) && isIPv4) || (utils.IsIPv6(ip) && isIPv6) {
|
||||
_, ok := allMap[ip]
|
||||
if !ok {
|
||||
// 不存在则删除
|
||||
err = set.DeleteIPElement(ip)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete ip element '%s' failed: %w", ip, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 新增的
|
||||
for _, ip := range allIPList {
|
||||
var ipObj = net.ParseIP(ip)
|
||||
if ipObj == nil {
|
||||
continue
|
||||
}
|
||||
if (utils.IsIPv4(ip) && isIPv4) || (utils.IsIPv6(ip) && isIPv6) {
|
||||
_, ok := oldMap[ip]
|
||||
if !ok {
|
||||
// 不存在则添加
|
||||
err = set.AddIPElement(ip, nil, false)
|
||||
if err != nil {
|
||||
return fmt.Errorf("add ip '%s' failed: %w", ip, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
22
EdgeNode/internal/firewalls/ddos_protection_others.go
Normal file
22
EdgeNode/internal/firewalls/ddos_protection_others.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/ddosconfigs"
|
||||
)
|
||||
|
||||
var SharedDDoSProtectionManager = NewDDoSProtectionManager()
|
||||
|
||||
type DDoSProtectionManager struct {
|
||||
}
|
||||
|
||||
func NewDDoSProtectionManager() *DDoSProtectionManager {
|
||||
return &DDoSProtectionManager{}
|
||||
}
|
||||
|
||||
func (this *DDoSProtectionManager) Apply(config *ddosconfigs.ProtectionConfig) error {
|
||||
return nil
|
||||
}
|
||||
85
EdgeNode/internal/firewalls/firewall.go
Normal file
85
EdgeNode/internal/firewalls/firewall.go
Normal file
@@ -0,0 +1,85 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var currentFirewall FirewallInterface
|
||||
var firewallLocker = &sync.Mutex{}
|
||||
|
||||
// 初始化
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
events.On(events.EventLoaded, func() {
|
||||
var firewall = Firewall()
|
||||
if firewall.Name() != "mock" {
|
||||
remotelogs.Println("FIREWALL", "found local firewall '"+firewall.Name()+"'")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Firewall 查找当前系统中最适合的防火墙
|
||||
func Firewall() FirewallInterface {
|
||||
firewallLocker.Lock()
|
||||
defer firewallLocker.Unlock()
|
||||
if currentFirewall != nil {
|
||||
return currentFirewall
|
||||
}
|
||||
|
||||
// http firewall
|
||||
{
|
||||
endpoint, _ := os.LookupEnv("EDGE_HTTP_FIREWALL_ENDPOINT")
|
||||
if len(endpoint) > 0 {
|
||||
var httpFirewall = NewHTTPFirewall(endpoint)
|
||||
for i := 0; i < 10; i++ {
|
||||
if httpFirewall.IsReady() {
|
||||
currentFirewall = httpFirewall
|
||||
remotelogs.Println("FIREWALL", "using http firewall '"+endpoint+"'")
|
||||
break
|
||||
}
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
return httpFirewall
|
||||
}
|
||||
}
|
||||
|
||||
// nftables
|
||||
if runtime.GOOS == "linux" {
|
||||
nftables, err := NewNFTablesFirewall()
|
||||
if err != nil {
|
||||
remotelogs.Warn("FIREWALL", "'nftables' should be installed on the system to enhance security (init failed: "+err.Error()+")")
|
||||
} else {
|
||||
if nftables.IsReady() {
|
||||
currentFirewall = nftables
|
||||
events.Notify(events.EventNFTablesReady)
|
||||
return nftables
|
||||
} else {
|
||||
remotelogs.Warn("FIREWALL", "'nftables' should be enabled on the system to enhance security")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// firewalld
|
||||
if runtime.GOOS == "linux" {
|
||||
var firewalld = NewFirewalld()
|
||||
if firewalld.IsReady() {
|
||||
currentFirewall = firewalld
|
||||
return currentFirewall
|
||||
}
|
||||
}
|
||||
|
||||
// 至少返回一个
|
||||
currentFirewall = NewMockFirewall()
|
||||
return currentFirewall
|
||||
}
|
||||
47
EdgeNode/internal/firewalls/firewall_base.go
Normal file
47
EdgeNode/internal/firewalls/firewall_base.go
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BaseFirewall struct {
|
||||
locker sync.Mutex
|
||||
latestIPTimes []string // [ip@time, ....]
|
||||
}
|
||||
|
||||
// 检查是否在最近添加过
|
||||
func (this *BaseFirewall) checkLatestIP(ip string) bool {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
|
||||
var expiredIndex = -1
|
||||
for index, ipTime := range this.latestIPTimes {
|
||||
var pieces = strings.Split(ipTime, "@")
|
||||
var oldIP = pieces[0]
|
||||
var oldTimestamp = pieces[1]
|
||||
if types.Int64(oldTimestamp) < time.Now().Unix()-3 /** 3秒外表示过期 **/ {
|
||||
expiredIndex = index
|
||||
continue
|
||||
}
|
||||
if oldIP == ip {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if expiredIndex > -1 {
|
||||
this.latestIPTimes = this.latestIPTimes[expiredIndex+1:]
|
||||
}
|
||||
|
||||
this.latestIPTimes = append(this.latestIPTimes, ip+"@"+types.String(time.Now().Unix()))
|
||||
const maxLen = 128
|
||||
if len(this.latestIPTimes) > maxLen {
|
||||
this.latestIPTimes = this.latestIPTimes[1:]
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
225
EdgeNode/internal/firewalls/firewall_firewalld.go
Normal file
225
EdgeNode/internal/firewalls/firewall_firewalld.go
Normal file
@@ -0,0 +1,225 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/conns"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type firewalldCmd struct {
|
||||
cmd *executils.Cmd
|
||||
denyIP string
|
||||
}
|
||||
|
||||
type Firewalld struct {
|
||||
BaseFirewall
|
||||
|
||||
isReady bool
|
||||
exe string
|
||||
cmdQueue chan *firewalldCmd
|
||||
}
|
||||
|
||||
func NewFirewalld() *Firewalld {
|
||||
var firewalld = &Firewalld{
|
||||
cmdQueue: make(chan *firewalldCmd, 4096),
|
||||
}
|
||||
|
||||
path, err := executils.LookPath("firewall-cmd")
|
||||
if err == nil && len(path) > 0 {
|
||||
var cmd = executils.NewTimeoutCmd(3*time.Second, path, "--state")
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
firewalld.exe = path
|
||||
// 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
|
||||
|
||||
firewalld.isReady = true
|
||||
firewalld.init()
|
||||
}
|
||||
}
|
||||
|
||||
return firewalld
|
||||
}
|
||||
|
||||
func (this *Firewalld) init() {
|
||||
goman.New(func() {
|
||||
for c := range this.cmdQueue {
|
||||
var cmd = c.cmd
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
if strings.HasPrefix(err.Error(), "Warning:") {
|
||||
continue
|
||||
}
|
||||
remotelogs.Warn("FIREWALL", "run command failed '"+cmd.String()+"': "+err.Error())
|
||||
} else {
|
||||
// 关闭连接
|
||||
if len(c.denyIP) > 0 {
|
||||
conns.SharedMap.CloseIPConns(c.denyIP)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Name 名称
|
||||
func (this *Firewalld) Name() string {
|
||||
return "firewalld"
|
||||
}
|
||||
|
||||
func (this *Firewalld) IsReady() bool {
|
||||
return this.isReady
|
||||
}
|
||||
|
||||
// IsMock 是否为模拟
|
||||
func (this *Firewalld) IsMock() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (this *Firewalld) AllowPort(port int, protocol string) error {
|
||||
if !this.isReady {
|
||||
return nil
|
||||
}
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, "--add-port="+types.String(port)+"/"+protocol)
|
||||
this.pushCmd(cmd, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Firewalld) AllowPortRangesPermanently(portRanges [][2]int, protocol string) error {
|
||||
for _, portRange := range portRanges {
|
||||
var port = this.PortRangeString(portRange, protocol)
|
||||
|
||||
{
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, "--add-port="+port, "--permanent")
|
||||
this.pushCmd(cmd, "")
|
||||
}
|
||||
|
||||
{
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, "--add-port="+port)
|
||||
this.pushCmd(cmd, "")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Firewalld) RemovePort(port int, protocol string) error {
|
||||
if !this.isReady {
|
||||
return nil
|
||||
}
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, "--remove-port="+types.String(port)+"/"+protocol)
|
||||
this.pushCmd(cmd, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Firewalld) RemovePortRangePermanently(portRange [2]int, protocol string) error {
|
||||
var port = this.PortRangeString(portRange, protocol)
|
||||
|
||||
{
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, "--remove-port="+port, "--permanent")
|
||||
this.pushCmd(cmd, "")
|
||||
}
|
||||
|
||||
{
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, "--remove-port="+port)
|
||||
this.pushCmd(cmd, "")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Firewalld) PortRangeString(portRange [2]int, protocol string) string {
|
||||
if portRange[0] == portRange[1] {
|
||||
return types.String(portRange[0]) + "/" + protocol
|
||||
} else {
|
||||
return types.String(portRange[0]) + "-" + types.String(portRange[1]) + "/" + protocol
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Firewalld) RejectSourceIP(ip string, timeoutSeconds int) error {
|
||||
if !this.isReady {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 避免短时间内重复添加
|
||||
if this.checkLatestIP(ip) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var family = "ipv4"
|
||||
if strings.Contains(ip, ":") {
|
||||
family = "ipv6"
|
||||
}
|
||||
var args = []string{"--add-rich-rule=rule family='" + family + "' source address='" + ip + "' reject"}
|
||||
if timeoutSeconds > 0 {
|
||||
args = append(args, "--timeout="+types.String(timeoutSeconds)+"s")
|
||||
}
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, args...)
|
||||
this.pushCmd(cmd, ip)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Firewalld) DropSourceIP(ip string, timeoutSeconds int, async bool) error {
|
||||
if !this.isReady {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 避免短时间内重复添加
|
||||
if async && this.checkLatestIP(ip) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var family = "ipv4"
|
||||
if strings.Contains(ip, ":") {
|
||||
family = "ipv6"
|
||||
}
|
||||
var args = []string{"--add-rich-rule=rule family='" + family + "' source address='" + ip + "' drop"}
|
||||
if timeoutSeconds > 0 {
|
||||
args = append(args, "--timeout="+types.String(timeoutSeconds)+"s")
|
||||
}
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, args...)
|
||||
if async {
|
||||
this.pushCmd(cmd, ip)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
defer conns.SharedMap.CloseIPConns(ip)
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return fmt.Errorf("run command failed '%s': %w", cmd.String(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Firewalld) RemoveSourceIP(ip string) error {
|
||||
if !this.isReady {
|
||||
return nil
|
||||
}
|
||||
|
||||
var family = "ipv4"
|
||||
if strings.Contains(ip, ":") {
|
||||
family = "ipv6"
|
||||
}
|
||||
for _, action := range []string{"reject", "drop"} {
|
||||
var args = []string{"--remove-rich-rule=rule family='" + family + "' source address='" + ip + "' " + action}
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, this.exe, args...)
|
||||
this.pushCmd(cmd, "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Firewalld) pushCmd(cmd *executils.Cmd, denyIP string) {
|
||||
select {
|
||||
case this.cmdQueue <- &firewalldCmd{cmd: cmd, denyIP: denyIP}:
|
||||
default:
|
||||
// we discard the command
|
||||
}
|
||||
}
|
||||
150
EdgeNode/internal/firewalls/firewall_http.go
Normal file
150
EdgeNode/internal/firewalls/firewall_http.go
Normal file
@@ -0,0 +1,150 @@
|
||||
// Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type HTTPFirewall struct {
|
||||
client *http.Client
|
||||
endpoint string
|
||||
}
|
||||
|
||||
func NewHTTPFirewall(endpoint string) *HTTPFirewall {
|
||||
return &HTTPFirewall{
|
||||
client: http.DefaultClient,
|
||||
endpoint: endpoint,
|
||||
}
|
||||
}
|
||||
|
||||
// Name 名称
|
||||
func (this *HTTPFirewall) Name() string {
|
||||
result, err := this.get("/name", nil)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return result.GetString("name")
|
||||
}
|
||||
|
||||
// IsReady 是否已准备被调用
|
||||
func (this *HTTPFirewall) IsReady() bool {
|
||||
result, err := this.get("/isReady", nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return result.GetBool("result")
|
||||
}
|
||||
|
||||
// IsMock 是否为模拟
|
||||
func (this *HTTPFirewall) IsMock() bool {
|
||||
result, err := this.get("/isMock", nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return result.GetBool("result")
|
||||
}
|
||||
|
||||
// AllowPort 允许端口
|
||||
func (this *HTTPFirewall) AllowPort(port int, protocol string) error {
|
||||
_, err := this.get("/allowPort", map[string]string{
|
||||
"port": types.String(port),
|
||||
"protocol": protocol,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// RemovePort 删除端口
|
||||
func (this *HTTPFirewall) RemovePort(port int, protocol string) error {
|
||||
_, err := this.get("/removePort", map[string]string{
|
||||
"port": types.String(port),
|
||||
"protocol": protocol,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// RejectSourceIP 拒绝某个源IP连接
|
||||
func (this *HTTPFirewall) RejectSourceIP(ip string, timeoutSeconds int) error {
|
||||
_, err := this.get("/rejectSourceIP", map[string]string{
|
||||
"ip": ip,
|
||||
"timeoutSeconds": types.String(timeoutSeconds),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// DropSourceIP 丢弃某个源IP数据
|
||||
// ip 要封禁的IP
|
||||
// timeoutSeconds 过期时间
|
||||
// async 是否异步
|
||||
func (this *HTTPFirewall) DropSourceIP(ip string, timeoutSeconds int, async bool) error {
|
||||
var asyncString = "false"
|
||||
if async {
|
||||
asyncString = "true"
|
||||
}
|
||||
_, err := this.get("/dropSourceIP", map[string]string{
|
||||
"ip": ip,
|
||||
"timeoutSeconds": types.String(timeoutSeconds),
|
||||
"async": asyncString,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// RemoveSourceIP 删除某个源IP
|
||||
func (this *HTTPFirewall) RemoveSourceIP(ip string) error {
|
||||
_, err := this.get("/removeSourceIP", map[string]string{
|
||||
"ip": ip,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (this *HTTPFirewall) get(path string, args map[string]string) (result maps.Map, err error) {
|
||||
var urlString = this.endpoint + path
|
||||
if len(args) > 0 {
|
||||
var query = &url.Values{}
|
||||
for k, v := range args {
|
||||
query.Add(k, v)
|
||||
}
|
||||
urlString += "?" + query.Encode()
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, urlString, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := this.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors.New("server response code '" + types.String(resp.StatusCode) + "'")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = resp.Body.Close()
|
||||
}()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read response failed: %w", err)
|
||||
}
|
||||
|
||||
if len(data) == 0 {
|
||||
return maps.Map{}, nil
|
||||
}
|
||||
|
||||
result = maps.Map{}
|
||||
err = json.Unmarshal(data, &result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode response failed: %w", err)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
33
EdgeNode/internal/firewalls/firewall_interface.go
Normal file
33
EdgeNode/internal/firewalls/firewall_interface.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package firewalls
|
||||
|
||||
// FirewallInterface 防火墙接口
|
||||
type FirewallInterface interface {
|
||||
// Name 名称
|
||||
Name() string
|
||||
|
||||
// IsReady 是否已准备被调用
|
||||
IsReady() bool
|
||||
|
||||
// IsMock 是否为模拟
|
||||
IsMock() bool
|
||||
|
||||
// AllowPort 允许端口
|
||||
AllowPort(port int, protocol string) error
|
||||
|
||||
// RemovePort 删除端口
|
||||
RemovePort(port int, protocol string) error
|
||||
|
||||
// RejectSourceIP 拒绝某个源IP连接
|
||||
RejectSourceIP(ip string, timeoutSeconds int) error
|
||||
|
||||
// DropSourceIP 丢弃某个源IP数据
|
||||
// ip 要封禁的IP
|
||||
// timeoutSeconds 过期时间
|
||||
// async 是否异步
|
||||
DropSourceIP(ip string, timeoutSeconds int, async bool) error
|
||||
|
||||
// RemoveSourceIP 删除某个源IP
|
||||
RemoveSourceIP(ip string) error
|
||||
}
|
||||
60
EdgeNode/internal/firewalls/firewall_mock.go
Normal file
60
EdgeNode/internal/firewalls/firewall_mock.go
Normal file
@@ -0,0 +1,60 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package firewalls
|
||||
|
||||
// MockFirewall 模拟防火墙
|
||||
type MockFirewall struct {
|
||||
}
|
||||
|
||||
func NewMockFirewall() *MockFirewall {
|
||||
return &MockFirewall{}
|
||||
}
|
||||
|
||||
// Name 名称
|
||||
func (this *MockFirewall) Name() string {
|
||||
return "mock"
|
||||
}
|
||||
|
||||
// IsReady 是否已准备被调用
|
||||
func (this *MockFirewall) IsReady() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// IsMock 是否为模拟
|
||||
func (this *MockFirewall) IsMock() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// AllowPort 允许端口
|
||||
func (this *MockFirewall) AllowPort(port int, protocol string) error {
|
||||
_ = port
|
||||
_ = protocol
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePort 删除端口
|
||||
func (this *MockFirewall) RemovePort(port int, protocol string) error {
|
||||
_ = port
|
||||
_ = protocol
|
||||
return nil
|
||||
}
|
||||
|
||||
// RejectSourceIP 拒绝某个源IP连接
|
||||
func (this *MockFirewall) RejectSourceIP(ip string, timeoutSeconds int) error {
|
||||
_ = ip
|
||||
_ = timeoutSeconds
|
||||
return nil
|
||||
}
|
||||
|
||||
// DropSourceIP 丢弃某个源IP数据
|
||||
func (this *MockFirewall) DropSourceIP(ip string, timeoutSeconds int, async bool) error {
|
||||
_ = ip
|
||||
_ = timeoutSeconds
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveSourceIP 删除某个源IP
|
||||
func (this *MockFirewall) RemoveSourceIP(ip string) error {
|
||||
_ = ip
|
||||
return nil
|
||||
}
|
||||
500
EdgeNode/internal/firewalls/firewall_nftables.go
Normal file
500
EdgeNode/internal/firewalls/firewall_nftables.go
Normal file
@@ -0,0 +1,500 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/conns"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
|
||||
"github.com/google/nftables/expr"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"net"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// check nft status, if being enabled we load it automatically
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
if runtime.GOOS == "linux" {
|
||||
var ticker = time.NewTicker(3 * time.Minute)
|
||||
goman.New(func() {
|
||||
for range ticker.C {
|
||||
// if already ready, we break
|
||||
if nftablesIsReady {
|
||||
ticker.Stop()
|
||||
break
|
||||
}
|
||||
var nftExe = nftables.NftExePath()
|
||||
if len(nftExe) > 0 {
|
||||
nftablesFirewall, err := NewNFTablesFirewall()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
currentFirewall = nftablesFirewall
|
||||
remotelogs.Println("FIREWALL", "nftables is ready")
|
||||
|
||||
// fire event
|
||||
if nftablesFirewall.IsReady() {
|
||||
events.Notify(events.EventNFTablesReady)
|
||||
}
|
||||
|
||||
ticker.Stop()
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var nftablesInstance *NFTablesFirewall
|
||||
var nftablesIsReady = false
|
||||
var nftablesFilters = []*nftablesTableDefinition{
|
||||
// we shorten the name for table name length restriction
|
||||
{Name: "edge_dft_v4", IsIPv4: true},
|
||||
{Name: "edge_dft_v6", IsIPv6: true},
|
||||
}
|
||||
var nftablesChainName = "input"
|
||||
|
||||
type nftablesTableDefinition struct {
|
||||
Name string
|
||||
IsIPv4 bool
|
||||
IsIPv6 bool
|
||||
}
|
||||
|
||||
func (this *nftablesTableDefinition) protocol() string {
|
||||
if this.IsIPv6 {
|
||||
return "ip6"
|
||||
}
|
||||
return "ip"
|
||||
}
|
||||
|
||||
type blockIPItem struct {
|
||||
action string
|
||||
ip string
|
||||
timeoutSeconds int
|
||||
}
|
||||
|
||||
func NewNFTablesFirewall() (*NFTablesFirewall, error) {
|
||||
conn, err := nftables.NewConn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var firewall = &NFTablesFirewall{
|
||||
conn: conn,
|
||||
dropIPQueue: make(chan *blockIPItem, 4096),
|
||||
}
|
||||
err = firewall.init()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return firewall, nil
|
||||
}
|
||||
|
||||
type NFTablesFirewall struct {
|
||||
BaseFirewall
|
||||
|
||||
conn *nftables.Conn
|
||||
isReady bool
|
||||
version string
|
||||
|
||||
allowIPv4Set *nftables.Set
|
||||
allowIPv6Set *nftables.Set
|
||||
|
||||
denyIPv4Sets []*nftables.Set
|
||||
denyIPv6Sets []*nftables.Set
|
||||
|
||||
firewalld *Firewalld
|
||||
|
||||
dropIPQueue chan *blockIPItem
|
||||
}
|
||||
|
||||
func (this *NFTablesFirewall) init() error {
|
||||
// check nft
|
||||
var nftPath = nftables.NftExePath()
|
||||
if len(nftPath) == 0 {
|
||||
return errors.New("'nft' not found")
|
||||
}
|
||||
this.version = this.readVersion(nftPath)
|
||||
|
||||
// table
|
||||
for _, tableDef := range nftablesFilters {
|
||||
var family nftables.TableFamily
|
||||
if tableDef.IsIPv4 {
|
||||
family = nftables.TableFamilyIPv4
|
||||
} else if tableDef.IsIPv6 {
|
||||
family = nftables.TableFamilyIPv6
|
||||
} else {
|
||||
return errors.New("invalid table family: " + types.String(tableDef))
|
||||
}
|
||||
table, err := this.conn.GetTable(tableDef.Name, family)
|
||||
if err != nil {
|
||||
if nftables.IsNotFound(err) {
|
||||
if tableDef.IsIPv4 {
|
||||
table, err = this.conn.AddIPv4Table(tableDef.Name)
|
||||
} else if tableDef.IsIPv6 {
|
||||
table, err = this.conn.AddIPv6Table(tableDef.Name)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("create table '%s' failed: %w", tableDef.Name, err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("get table '%s' failed: %w", tableDef.Name, err)
|
||||
}
|
||||
}
|
||||
if table == nil {
|
||||
return errors.New("can not create table '" + tableDef.Name + "'")
|
||||
}
|
||||
|
||||
// chain
|
||||
var chainName = nftablesChainName
|
||||
chain, err := table.GetChain(chainName)
|
||||
if err != nil {
|
||||
if nftables.IsNotFound(err) {
|
||||
chain, err = table.AddAcceptChain(chainName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("create chain '%s' failed: %w", chainName, err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("get chain '%s' failed: %w", chainName, err)
|
||||
}
|
||||
}
|
||||
if chain == nil {
|
||||
return errors.New("can not create chain '" + chainName + "'")
|
||||
}
|
||||
|
||||
// allow lo
|
||||
var loRuleName = []byte("lo")
|
||||
_, err = chain.GetRuleWithUserData(loRuleName)
|
||||
if err != nil {
|
||||
if nftables.IsNotFound(err) {
|
||||
_, err = chain.AddAcceptInterfaceRule("lo", loRuleName)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("add 'lo' rule failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// allow set
|
||||
// "allow" should be always first
|
||||
for _, setAction := range []string{"allow", "deny", "deny1", "deny2", "deny3", "deny4"} {
|
||||
var setName = setAction + "_set"
|
||||
|
||||
set, err := table.GetSet(setName)
|
||||
if err != nil {
|
||||
if nftables.IsNotFound(err) {
|
||||
var keyType nftables.SetDataType
|
||||
if tableDef.IsIPv4 {
|
||||
keyType = nftables.TypeIPAddr
|
||||
} else if tableDef.IsIPv6 {
|
||||
keyType = nftables.TypeIP6Addr
|
||||
}
|
||||
set, err = table.AddSet(setName, &nftables.SetOptions{
|
||||
KeyType: keyType,
|
||||
HasTimeout: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("create set '%s' failed: %w", setName, err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("get set '%s' failed: %w", setName, err)
|
||||
}
|
||||
}
|
||||
if set == nil {
|
||||
return errors.New("can not create set '" + setName + "'")
|
||||
}
|
||||
if tableDef.IsIPv4 {
|
||||
if setAction == "allow" {
|
||||
this.allowIPv4Set = set
|
||||
} else {
|
||||
this.denyIPv4Sets = append(this.denyIPv4Sets, set)
|
||||
}
|
||||
} else if tableDef.IsIPv6 {
|
||||
if setAction == "allow" {
|
||||
this.allowIPv6Set = set
|
||||
} else {
|
||||
this.denyIPv6Sets = append(this.denyIPv6Sets, set)
|
||||
}
|
||||
}
|
||||
|
||||
// rule
|
||||
var ruleName = []byte(setAction)
|
||||
rule, err := chain.GetRuleWithUserData(ruleName)
|
||||
|
||||
// 将以前的drop规则删掉,替换成后面的reject
|
||||
if err == nil && setAction != "allow" && rule != nil && rule.VerDict() == expr.VerdictDrop {
|
||||
deleteErr := chain.DeleteRule(rule)
|
||||
if deleteErr == nil {
|
||||
err = nftables.ErrRuleNotFound
|
||||
rule = nil
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if nftables.IsNotFound(err) {
|
||||
if tableDef.IsIPv4 {
|
||||
if setAction == "allow" {
|
||||
rule, err = chain.AddAcceptIPv4SetRule(setName, ruleName)
|
||||
} else {
|
||||
rule, err = chain.AddRejectIPv4SetRule(setName, ruleName)
|
||||
}
|
||||
} else if tableDef.IsIPv6 {
|
||||
if setAction == "allow" {
|
||||
rule, err = chain.AddAcceptIPv6SetRule(setName, ruleName)
|
||||
} else {
|
||||
rule, err = chain.AddRejectIPv6SetRule(setName, ruleName)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("add rule failed: %w", err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("get rule failed: %w", err)
|
||||
}
|
||||
}
|
||||
if rule == nil {
|
||||
return errors.New("can not create rule '" + string(ruleName) + "'")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.isReady = true
|
||||
nftablesIsReady = true
|
||||
nftablesInstance = this
|
||||
|
||||
goman.New(func() {
|
||||
for ipItem := range this.dropIPQueue {
|
||||
switch ipItem.action {
|
||||
case "drop":
|
||||
err := this.DropSourceIP(ipItem.ip, ipItem.timeoutSeconds, false)
|
||||
if err != nil {
|
||||
remotelogs.Warn("NFTABLES", "drop ip '"+ipItem.ip+"' failed: "+err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// load firewalld
|
||||
var firewalld = NewFirewalld()
|
||||
if firewalld.IsReady() {
|
||||
this.firewalld = firewalld
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name 名称
|
||||
func (this *NFTablesFirewall) Name() string {
|
||||
return "nftables"
|
||||
}
|
||||
|
||||
// IsReady 是否已准备被调用
|
||||
func (this *NFTablesFirewall) IsReady() bool {
|
||||
return this.isReady
|
||||
}
|
||||
|
||||
// IsMock 是否为模拟
|
||||
func (this *NFTablesFirewall) IsMock() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// AllowPort 允许端口
|
||||
func (this *NFTablesFirewall) AllowPort(port int, protocol string) error {
|
||||
if this.firewalld != nil {
|
||||
return this.firewalld.AllowPort(port, protocol)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePort 删除端口
|
||||
func (this *NFTablesFirewall) RemovePort(port int, protocol string) error {
|
||||
if this.firewalld != nil {
|
||||
return this.firewalld.RemovePort(port, protocol)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AllowSourceIP Allow把IP加入白名单
|
||||
func (this *NFTablesFirewall) AllowSourceIP(ip string) error {
|
||||
var data = net.ParseIP(ip)
|
||||
if data == nil {
|
||||
return errors.New("invalid ip '" + ip + "'")
|
||||
}
|
||||
|
||||
if strings.Contains(ip, ":") { // ipv6
|
||||
if this.allowIPv6Set == nil {
|
||||
return errors.New("ipv6 ip set is nil")
|
||||
}
|
||||
return this.allowIPv6Set.AddElement(data.To16(), nil, false)
|
||||
}
|
||||
|
||||
// ipv4
|
||||
if this.allowIPv4Set == nil {
|
||||
return errors.New("ipv4 ip set is nil")
|
||||
}
|
||||
return this.allowIPv4Set.AddElement(data.To4(), nil, false)
|
||||
}
|
||||
|
||||
// RejectSourceIP 拒绝某个源IP连接
|
||||
// we did not create set for drop ip, so we reuse DropSourceIP() method here
|
||||
func (this *NFTablesFirewall) RejectSourceIP(ip string, timeoutSeconds int) error {
|
||||
return this.DropSourceIP(ip, timeoutSeconds, true)
|
||||
}
|
||||
|
||||
// DropSourceIP 丢弃某个源IP数据
|
||||
func (this *NFTablesFirewall) DropSourceIP(ip string, timeoutSeconds int, async bool) error {
|
||||
var data = net.ParseIP(ip)
|
||||
if data == nil {
|
||||
return errors.New("invalid ip '" + ip + "'")
|
||||
}
|
||||
|
||||
// 尝试关闭连接
|
||||
conns.SharedMap.CloseIPConns(ip)
|
||||
|
||||
// 避免短时间内重复添加
|
||||
if async && this.checkLatestIP(ip) {
|
||||
return nil
|
||||
}
|
||||
|
||||
if async {
|
||||
select {
|
||||
case this.dropIPQueue <- &blockIPItem{
|
||||
action: "drop",
|
||||
ip: ip,
|
||||
timeoutSeconds: timeoutSeconds,
|
||||
}:
|
||||
default:
|
||||
return errors.New("drop ip queue is full")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 再次尝试关闭连接
|
||||
defer conns.SharedMap.CloseIPConns(ip)
|
||||
|
||||
if strings.Contains(ip, ":") { // ipv6
|
||||
if len(this.denyIPv6Sets) == 0 {
|
||||
return errors.New("ipv6 ip set not found")
|
||||
}
|
||||
var setIndex = iputils.ParseIP(ip).Mod(len(this.denyIPv6Sets))
|
||||
return this.denyIPv6Sets[setIndex].AddElement(data.To16(), &nftables.ElementOptions{
|
||||
Timeout: time.Duration(timeoutSeconds) * time.Second,
|
||||
}, false)
|
||||
}
|
||||
|
||||
// ipv4
|
||||
if len(this.denyIPv4Sets) == 0 {
|
||||
return errors.New("ipv4 ip set not found")
|
||||
}
|
||||
var setIndex = iputils.ParseIP(ip).Mod(len(this.denyIPv4Sets))
|
||||
return this.denyIPv4Sets[setIndex].AddElement(data.To4(), &nftables.ElementOptions{
|
||||
Timeout: time.Duration(timeoutSeconds) * time.Second,
|
||||
}, false)
|
||||
}
|
||||
|
||||
// RemoveSourceIP 删除某个源IP
|
||||
func (this *NFTablesFirewall) RemoveSourceIP(ip string) error {
|
||||
var data = net.ParseIP(ip)
|
||||
if data == nil {
|
||||
return errors.New("invalid ip '" + ip + "'")
|
||||
}
|
||||
|
||||
if strings.Contains(ip, ":") { // ipv6
|
||||
var setIndex = iputils.ParseIP(ip).Mod(len(this.denyIPv6Sets))
|
||||
if len(this.denyIPv6Sets) > 0 {
|
||||
err := this.denyIPv6Sets[setIndex].DeleteElement(data.To16())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if this.allowIPv6Set != nil {
|
||||
err := this.allowIPv6Set.DeleteElement(data.To16())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ipv4
|
||||
if len(this.denyIPv4Sets) > 0 {
|
||||
var setIndex = iputils.ParseIP(ip).Mod(len(this.denyIPv4Sets))
|
||||
err := this.denyIPv4Sets[setIndex].DeleteElement(data.To4())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if this.allowIPv4Set != nil {
|
||||
err := this.allowIPv4Set.DeleteElement(data.To4())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 读取版本号
|
||||
func (this *NFTablesFirewall) readVersion(nftPath string) string {
|
||||
var cmd = executils.NewTimeoutCmd(10*time.Second, nftPath, "--version")
|
||||
cmd.WithStdout()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var outputString = cmd.Stdout()
|
||||
var versionMatches = regexp.MustCompile(`nftables v([\d.]+)`).FindStringSubmatch(outputString)
|
||||
if len(versionMatches) <= 1 {
|
||||
return ""
|
||||
}
|
||||
return versionMatches[1]
|
||||
}
|
||||
|
||||
// 检查是否在最近添加过
|
||||
func (this *NFTablesFirewall) existLatestIP(ip string) bool {
|
||||
this.locker.Lock()
|
||||
defer this.locker.Unlock()
|
||||
|
||||
var expiredIndex = -1
|
||||
for index, ipTime := range this.latestIPTimes {
|
||||
var pieces = strings.Split(ipTime, "@")
|
||||
var oldIP = pieces[0]
|
||||
var oldTimestamp = pieces[1]
|
||||
if types.Int64(oldTimestamp) < time.Now().Unix()-3 /** 3秒外表示过期 **/ {
|
||||
expiredIndex = index
|
||||
continue
|
||||
}
|
||||
if oldIP == ip {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if expiredIndex > -1 {
|
||||
this.latestIPTimes = this.latestIPTimes[expiredIndex+1:]
|
||||
}
|
||||
|
||||
this.latestIPTimes = append(this.latestIPTimes, ip+"@"+types.String(time.Now().Unix()))
|
||||
const maxLen = 128
|
||||
if len(this.latestIPTimes) > maxLen {
|
||||
this.latestIPTimes = this.latestIPTimes[1:]
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
61
EdgeNode/internal/firewalls/firewall_nftables_others.go
Normal file
61
EdgeNode/internal/firewalls/firewall_nftables_others.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
func NewNFTablesFirewall() (*NFTablesFirewall, error) {
|
||||
return nil, errors.New("not implemented")
|
||||
}
|
||||
|
||||
type NFTablesFirewall struct {
|
||||
}
|
||||
|
||||
// Name 名称
|
||||
func (this *NFTablesFirewall) Name() string {
|
||||
return "nftables"
|
||||
}
|
||||
|
||||
// IsReady 是否已准备被调用
|
||||
func (this *NFTablesFirewall) IsReady() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsMock 是否为模拟
|
||||
func (this *NFTablesFirewall) IsMock() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// AllowPort 允许端口
|
||||
func (this *NFTablesFirewall) AllowPort(port int, protocol string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePort 删除端口
|
||||
func (this *NFTablesFirewall) RemovePort(port int, protocol string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AllowSourceIP Allow把IP加入白名单
|
||||
func (this *NFTablesFirewall) AllowSourceIP(ip string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RejectSourceIP 拒绝某个源IP连接
|
||||
func (this *NFTablesFirewall) RejectSourceIP(ip string, timeoutSeconds int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// DropSourceIP 丢弃某个源IP数据
|
||||
func (this *NFTablesFirewall) DropSourceIP(ip string, timeoutSeconds int, async bool) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveSourceIP 删除某个源IP
|
||||
func (this *NFTablesFirewall) RemoveSourceIP(ip string) error {
|
||||
return nil
|
||||
}
|
||||
3
EdgeNode/internal/firewalls/nftables/build_remote.sh
Normal file
3
EdgeNode/internal/firewalls/nftables/build_remote.sh
Normal file
@@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
scp -r ../nftables/ root@192.168.2.60:/root/src/EdgeNode/internal/firewalls/
|
||||
369
EdgeNode/internal/firewalls/nftables/chain.go
Normal file
369
EdgeNode/internal/firewalls/nftables/chain.go
Normal file
@@ -0,0 +1,369 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
nft "github.com/google/nftables"
|
||||
"github.com/google/nftables/expr"
|
||||
)
|
||||
|
||||
const MaxChainNameLength = 31
|
||||
|
||||
type RuleOptions struct {
|
||||
Exprs []expr.Any
|
||||
UserData []byte
|
||||
}
|
||||
|
||||
// Chain chain object in table
|
||||
type Chain struct {
|
||||
conn *Conn
|
||||
rawTable *nft.Table
|
||||
rawChain *nft.Chain
|
||||
}
|
||||
|
||||
func NewChain(conn *Conn, rawTable *nft.Table, rawChain *nft.Chain) *Chain {
|
||||
return &Chain{
|
||||
conn: conn,
|
||||
rawTable: rawTable,
|
||||
rawChain: rawChain,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Chain) Raw() *nft.Chain {
|
||||
return this.rawChain
|
||||
}
|
||||
|
||||
func (this *Chain) Name() string {
|
||||
return this.rawChain.Name
|
||||
}
|
||||
|
||||
func (this *Chain) AddRule(options *RuleOptions) (*Rule, error) {
|
||||
var rawRule = this.conn.Raw().AddRule(&nft.Rule{
|
||||
Table: this.rawTable,
|
||||
Chain: this.rawChain,
|
||||
Exprs: options.Exprs,
|
||||
UserData: options.UserData,
|
||||
})
|
||||
err := this.conn.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewRule(rawRule), nil
|
||||
}
|
||||
|
||||
func (this *Chain) AddAcceptIPv4Rule(ip []byte, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 12,
|
||||
Len: 4,
|
||||
},
|
||||
&expr.Cmp{
|
||||
Op: expr.CmpOpEq,
|
||||
Register: 1,
|
||||
Data: ip,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictAccept,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddAcceptIPv6Rule(ip []byte, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 8,
|
||||
Len: 16,
|
||||
},
|
||||
&expr.Cmp{
|
||||
Op: expr.CmpOpEq,
|
||||
Register: 1,
|
||||
Data: ip,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictAccept,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddDropIPv4Rule(ip []byte, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 12,
|
||||
Len: 4,
|
||||
},
|
||||
&expr.Cmp{
|
||||
Op: expr.CmpOpEq,
|
||||
Register: 1,
|
||||
Data: ip,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictDrop,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddDropIPv6Rule(ip []byte, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 8,
|
||||
Len: 16,
|
||||
},
|
||||
&expr.Cmp{
|
||||
Op: expr.CmpOpEq,
|
||||
Register: 1,
|
||||
Data: ip,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictDrop,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddRejectIPv4Rule(ip []byte, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 12,
|
||||
Len: 4,
|
||||
},
|
||||
&expr.Cmp{
|
||||
Op: expr.CmpOpEq,
|
||||
Register: 1,
|
||||
Data: ip,
|
||||
},
|
||||
&expr.Reject{},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddRejectIPv6Rule(ip []byte, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 8,
|
||||
Len: 16,
|
||||
},
|
||||
&expr.Cmp{
|
||||
Op: expr.CmpOpEq,
|
||||
Register: 1,
|
||||
Data: ip,
|
||||
},
|
||||
&expr.Reject{},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddAcceptIPv4SetRule(setName string, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 12,
|
||||
Len: 4,
|
||||
},
|
||||
&expr.Lookup{
|
||||
SourceRegister: 1,
|
||||
SetName: setName,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictAccept,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddAcceptIPv6SetRule(setName string, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 8,
|
||||
Len: 16,
|
||||
},
|
||||
&expr.Lookup{
|
||||
SourceRegister: 1,
|
||||
SetName: setName,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictAccept,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddDropIPv4SetRule(setName string, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 12,
|
||||
Len: 4,
|
||||
},
|
||||
&expr.Lookup{
|
||||
SourceRegister: 1,
|
||||
SetName: setName,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictDrop,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddDropIPv6SetRule(setName string, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 8,
|
||||
Len: 16,
|
||||
},
|
||||
&expr.Lookup{
|
||||
SourceRegister: 1,
|
||||
SetName: setName,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictDrop,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddRejectIPv4SetRule(setName string, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 12,
|
||||
Len: 4,
|
||||
},
|
||||
&expr.Lookup{
|
||||
SourceRegister: 1,
|
||||
SetName: setName,
|
||||
},
|
||||
&expr.Reject{},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddRejectIPv6SetRule(setName string, userData []byte) (*Rule, error) {
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Payload{
|
||||
DestRegister: 1,
|
||||
Base: expr.PayloadBaseNetworkHeader,
|
||||
Offset: 8,
|
||||
Len: 16,
|
||||
},
|
||||
&expr.Lookup{
|
||||
SourceRegister: 1,
|
||||
SetName: setName,
|
||||
},
|
||||
&expr.Reject{},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) AddAcceptInterfaceRule(interfaceName string, userData []byte) (*Rule, error) {
|
||||
if len(interfaceName) >= 16 {
|
||||
return nil, errors.New("invalid interface name '" + interfaceName + "'")
|
||||
}
|
||||
var ifname = make([]byte, 16)
|
||||
copy(ifname, interfaceName+"\x00")
|
||||
|
||||
return this.AddRule(&RuleOptions{
|
||||
Exprs: []expr.Any{
|
||||
&expr.Meta{
|
||||
Key: expr.MetaKeyIIFNAME,
|
||||
Register: 1,
|
||||
},
|
||||
&expr.Cmp{
|
||||
Op: expr.CmpOpEq,
|
||||
Register: 1,
|
||||
Data: ifname,
|
||||
},
|
||||
&expr.Verdict{
|
||||
Kind: expr.VerdictAccept,
|
||||
},
|
||||
},
|
||||
UserData: userData,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *Chain) GetRuleWithUserData(userData []byte) (*Rule, error) {
|
||||
rawRules, err := this.conn.Raw().GetRule(this.rawTable, this.rawChain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, rawRule := range rawRules {
|
||||
if bytes.Compare(rawRule.UserData, userData) == 0 {
|
||||
return NewRule(rawRule), nil
|
||||
}
|
||||
}
|
||||
return nil, ErrRuleNotFound
|
||||
}
|
||||
|
||||
func (this *Chain) GetRules() ([]*Rule, error) {
|
||||
rawRules, err := this.conn.Raw().GetRule(this.rawTable, this.rawChain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var result = []*Rule{}
|
||||
for _, rawRule := range rawRules {
|
||||
result = append(result, NewRule(rawRule))
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (this *Chain) DeleteRule(rule *Rule) error {
|
||||
err := this.conn.Raw().DelRule(rule.Raw())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return this.conn.Commit()
|
||||
}
|
||||
|
||||
func (this *Chain) Flush() error {
|
||||
this.conn.Raw().FlushChain(this.rawChain)
|
||||
return this.conn.Commit()
|
||||
}
|
||||
14
EdgeNode/internal/firewalls/nftables/chain_policy.go
Normal file
14
EdgeNode/internal/firewalls/nftables/chain_policy.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import nft "github.com/google/nftables"
|
||||
|
||||
type ChainPolicy = nft.ChainPolicy
|
||||
|
||||
// Possible ChainPolicy values.
|
||||
const (
|
||||
ChainPolicyDrop = nft.ChainPolicyDrop
|
||||
ChainPolicyAccept = nft.ChainPolicyAccept
|
||||
)
|
||||
132
EdgeNode/internal/firewalls/nftables/chain_test.go
Normal file
132
EdgeNode/internal/firewalls/nftables/chain_test.go
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func getIPv4Chain(t *testing.T) *nftables.Chain {
|
||||
conn, err := nftables.NewConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
table, err := conn.GetTable("test_ipv4", nftables.TableFamilyIPv4)
|
||||
if err != nil {
|
||||
if err == nftables.ErrTableNotFound {
|
||||
table, err = conn.AddIPv4Table("test_ipv4")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
chain, err := table.GetChain("test_chain")
|
||||
if err != nil {
|
||||
if err == nftables.ErrChainNotFound {
|
||||
chain, err = table.AddAcceptChain("test_chain")
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return chain
|
||||
}
|
||||
|
||||
func TestChain_AddAcceptIPRule(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
_, err := chain.AddAcceptIPv4Rule(net.ParseIP("192.168.2.40").To4(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChain_AddDropIPRule(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
_, err := chain.AddDropIPv4Rule(net.ParseIP("192.168.2.31").To4(), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChain_AddAcceptSetRule(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
_, err := chain.AddAcceptIPv4SetRule("ipv4_black_set", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChain_AddDropSetRule(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
_, err := chain.AddDropIPv4SetRule("ipv4_black_set", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChain_AddRejectSetRule(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
_, err := chain.AddRejectIPv4SetRule("ipv4_black_set", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChain_GetRuleWithUserData(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
rule, err := chain.GetRuleWithUserData([]byte("test"))
|
||||
if err != nil {
|
||||
if err == nftables.ErrRuleNotFound {
|
||||
t.Log("rule not found")
|
||||
return
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
t.Log("rule:", rule)
|
||||
}
|
||||
|
||||
func TestChain_GetRules(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
rules, err := chain.GetRules()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, rule := range rules {
|
||||
t.Log("handle:", rule.Handle(), "set name:", rule.LookupSetName(),
|
||||
"verdict:", rule.VerDict(), "user data:", string(rule.UserData()))
|
||||
}
|
||||
}
|
||||
|
||||
func TestChain_DeleteRule(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
rule, err := chain.GetRuleWithUserData([]byte("test"))
|
||||
if err != nil {
|
||||
if err == nftables.ErrRuleNotFound {
|
||||
t.Log("rule not found")
|
||||
return
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = chain.DeleteRule(rule)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChain_Flush(t *testing.T) {
|
||||
var chain = getIPv4Chain(t)
|
||||
err := chain.Flush()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
87
EdgeNode/internal/firewalls/nftables/conn.go
Normal file
87
EdgeNode/internal/firewalls/nftables/conn.go
Normal file
@@ -0,0 +1,87 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
nft "github.com/google/nftables"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
const MaxTableNameLength = 27
|
||||
|
||||
type Conn struct {
|
||||
rawConn *nft.Conn
|
||||
}
|
||||
|
||||
func NewConn() (*Conn, error) {
|
||||
conn, err := nft.New()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Conn{
|
||||
rawConn: conn,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (this *Conn) Raw() *nft.Conn {
|
||||
return this.rawConn
|
||||
}
|
||||
|
||||
func (this *Conn) GetTable(name string, family TableFamily) (*Table, error) {
|
||||
rawTables, err := this.rawConn.ListTables()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, rawTable := range rawTables {
|
||||
if rawTable.Name == name && rawTable.Family == family {
|
||||
return NewTable(this, rawTable), nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ErrTableNotFound
|
||||
}
|
||||
|
||||
func (this *Conn) AddTable(name string, family TableFamily) (*Table, error) {
|
||||
if len(name) > MaxTableNameLength {
|
||||
return nil, errors.New("table name too long (max " + types.String(MaxTableNameLength) + ")")
|
||||
}
|
||||
|
||||
var rawTable = this.rawConn.AddTable(&nft.Table{
|
||||
Family: family,
|
||||
Name: name,
|
||||
})
|
||||
|
||||
err := this.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewTable(this, rawTable), nil
|
||||
}
|
||||
|
||||
func (this *Conn) AddIPv4Table(name string) (*Table, error) {
|
||||
return this.AddTable(name, TableFamilyIPv4)
|
||||
}
|
||||
|
||||
func (this *Conn) AddIPv6Table(name string) (*Table, error) {
|
||||
return this.AddTable(name, TableFamilyIPv6)
|
||||
}
|
||||
|
||||
func (this *Conn) DeleteTable(name string, family TableFamily) error {
|
||||
table, err := this.GetTable(name, family)
|
||||
if err != nil {
|
||||
if err == ErrTableNotFound {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
this.rawConn.DelTable(table.Raw())
|
||||
return this.Commit()
|
||||
}
|
||||
|
||||
func (this *Conn) Commit() error {
|
||||
return this.rawConn.Flush()
|
||||
}
|
||||
91
EdgeNode/internal/firewalls/nftables/conn_test.go
Normal file
91
EdgeNode/internal/firewalls/nftables/conn_test.go
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package nftables_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConn_Test(t *testing.T) {
|
||||
_, err := executils.LookPath("nft")
|
||||
if err == nil {
|
||||
t.Log("ok")
|
||||
return
|
||||
}
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
func TestConn_GetTable_NotFound(t *testing.T) {
|
||||
conn, err := nftables.NewConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
table, err := conn.GetTable("a", nftables.TableFamilyIPv4)
|
||||
if err != nil {
|
||||
if err == nftables.ErrTableNotFound {
|
||||
t.Log("table not found")
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Log("table:", table)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConn_GetTable(t *testing.T) {
|
||||
conn, err := nftables.NewConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
table, err := conn.GetTable("myFilter", nftables.TableFamilyIPv4)
|
||||
if err != nil {
|
||||
if err == nftables.ErrTableNotFound {
|
||||
t.Log("table not found")
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Log("table:", table)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConn_AddTable(t *testing.T) {
|
||||
conn, err := nftables.NewConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
{
|
||||
table, err := conn.AddIPv4Table("test_ipv4")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(table.Name())
|
||||
}
|
||||
{
|
||||
table, err := conn.AddIPv6Table("test_ipv6")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(table.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConn_DeleteTable(t *testing.T) {
|
||||
conn, err := nftables.NewConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = conn.DeleteTable("test_ipv4", nftables.TableFamilyIPv4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
8
EdgeNode/internal/firewalls/nftables/element.go
Normal file
8
EdgeNode/internal/firewalls/nftables/element.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package nftables
|
||||
|
||||
type Element struct {
|
||||
}
|
||||
22
EdgeNode/internal/firewalls/nftables/errors.go
Normal file
22
EdgeNode/internal/firewalls/nftables/errors.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrTableNotFound = errors.New("table not found")
|
||||
var ErrChainNotFound = errors.New("chain not found")
|
||||
var ErrSetNotFound = errors.New("set not found")
|
||||
var ErrRuleNotFound = errors.New("rule not found")
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
return err == ErrTableNotFound || err == ErrChainNotFound || err == ErrSetNotFound || err == ErrRuleNotFound || strings.Contains(err.Error(), "no such file or directory")
|
||||
}
|
||||
65
EdgeNode/internal/firewalls/nftables/expration.go
Normal file
65
EdgeNode/internal/firewalls/nftables/expration.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Expiration struct {
|
||||
m map[string]time.Time // key => expires time
|
||||
|
||||
lastGCAt int64
|
||||
|
||||
locker sync.RWMutex
|
||||
}
|
||||
|
||||
func NewExpiration() *Expiration {
|
||||
return &Expiration{
|
||||
m: map[string]time.Time{},
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Expiration) AddUnsafe(key []byte, expires time.Time) {
|
||||
this.m[string(key)] = expires
|
||||
}
|
||||
|
||||
func (this *Expiration) Add(key []byte, expires time.Time) {
|
||||
this.locker.Lock()
|
||||
this.m[string(key)] = expires
|
||||
this.gc()
|
||||
this.locker.Unlock()
|
||||
}
|
||||
|
||||
func (this *Expiration) Remove(key []byte) {
|
||||
this.locker.Lock()
|
||||
delete(this.m, string(key))
|
||||
this.locker.Unlock()
|
||||
}
|
||||
|
||||
func (this *Expiration) Contains(key []byte) bool {
|
||||
this.locker.RLock()
|
||||
expires, ok := this.m[string(key)]
|
||||
if ok && expires.Year() > 2000 && time.Now().After(expires) {
|
||||
ok = false
|
||||
}
|
||||
this.locker.RUnlock()
|
||||
return ok
|
||||
}
|
||||
|
||||
func (this *Expiration) gc() {
|
||||
// we won't gc too frequently
|
||||
var currentTime = time.Now().Unix()
|
||||
if this.lastGCAt >= currentTime {
|
||||
return
|
||||
}
|
||||
this.lastGCAt = currentTime
|
||||
|
||||
var now = time.Now().Add(-10 * time.Second) // gc elements expired before 10 seconds ago
|
||||
for key, expires := range this.m {
|
||||
if expires.Year() > 2000 && now.After(expires) {
|
||||
delete(this.m, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
59
EdgeNode/internal/firewalls/nftables/expration_test.go
Normal file
59
EdgeNode/internal/firewalls/nftables/expration_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package nftables_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestExpiration_Add(t *testing.T) {
|
||||
var expiration = nftables.NewExpiration()
|
||||
{
|
||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Now())
|
||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||
}
|
||||
{
|
||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(1*time.Second))
|
||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||
}
|
||||
{
|
||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Time{})
|
||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||
}
|
||||
{
|
||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(-1*time.Second))
|
||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||
}
|
||||
{
|
||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(-10*time.Second))
|
||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||
}
|
||||
{
|
||||
expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(1*time.Second))
|
||||
expiration.Remove([]byte{'a', 'b', 'c'})
|
||||
t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
|
||||
}
|
||||
{
|
||||
expiration.Add(net.ParseIP("10.254.0.75").To4(), time.Now())
|
||||
t.Log(expiration.Contains(net.ParseIP("10.254.0.75").To4()))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkNewExpiration(b *testing.B) {
|
||||
var expiration = nftables.NewExpiration()
|
||||
for i := 0; i < 10_000; i++ {
|
||||
expiration.Add([]byte(types.String(types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255)))), time.Now().Add(3600*time.Second))
|
||||
}
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
expiration.Add([]byte(types.String(types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255)))), time.Now().Add(3600*time.Second))
|
||||
}
|
||||
})
|
||||
}
|
||||
19
EdgeNode/internal/firewalls/nftables/family.go
Normal file
19
EdgeNode/internal/firewalls/nftables/family.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
nft "github.com/google/nftables"
|
||||
)
|
||||
|
||||
type TableFamily = nft.TableFamily
|
||||
|
||||
const (
|
||||
TableFamilyINet TableFamily = nft.TableFamilyINet
|
||||
TableFamilyIPv4 TableFamily = nft.TableFamilyIPv4
|
||||
TableFamilyIPv6 TableFamily = nft.TableFamilyIPv6
|
||||
TableFamilyARP TableFamily = nft.TableFamilyARP
|
||||
TableFamilyNetdev TableFamily = nft.TableFamilyNetdev
|
||||
TableFamilyBridge TableFamily = nft.TableFamilyBridge
|
||||
)
|
||||
148
EdgeNode/internal/firewalls/nftables/installer.go
Normal file
148
EdgeNode/internal/firewalls/nftables/installer.go
Normal file
@@ -0,0 +1,148 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/events"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
executils "github.com/TeaOSLab/EdgeNode/internal/utils/exec"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/goman"
|
||||
"github.com/iwind/TeaGo/logs"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if !teaconst.IsMain {
|
||||
return
|
||||
}
|
||||
|
||||
events.On(events.EventReload, func() {
|
||||
// linux only
|
||||
if runtime.GOOS != "linux" {
|
||||
return
|
||||
}
|
||||
|
||||
nodeConfig, err := nodeconfigs.SharedNodeConfig()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if nodeConfig == nil || !nodeConfig.AutoInstallNftables {
|
||||
return
|
||||
}
|
||||
|
||||
if os.Getgid() == 0 { // root user only
|
||||
if len(NftExePath()) > 0 {
|
||||
return
|
||||
}
|
||||
goman.New(func() {
|
||||
err := NewInstaller().Install()
|
||||
if err != nil {
|
||||
// 不需要传到API节点
|
||||
logs.Println("[NFTABLES]install nftables failed: " + err.Error())
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// NftExePath 查找nftables可执行文件路径
|
||||
func NftExePath() string {
|
||||
path, _ := executils.LookPath("nft")
|
||||
if len(path) > 0 {
|
||||
return path
|
||||
}
|
||||
|
||||
for _, possiblePath := range []string{
|
||||
"/usr/sbin/nft",
|
||||
} {
|
||||
_, err := os.Stat(possiblePath)
|
||||
if err == nil {
|
||||
return possiblePath
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
type Installer struct {
|
||||
}
|
||||
|
||||
func NewInstaller() *Installer {
|
||||
return &Installer{}
|
||||
}
|
||||
|
||||
func (this *Installer) Install() error {
|
||||
// linux only
|
||||
if runtime.GOOS != "linux" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查是否已经存在
|
||||
if len(NftExePath()) > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var cmd *executils.Cmd
|
||||
var aptCmd *executils.Cmd
|
||||
|
||||
// check dnf
|
||||
{
|
||||
dnfExe, err := executils.LookPath("dnf")
|
||||
if err == nil {
|
||||
cmd = executils.NewCmd(dnfExe, "-y", "install", "nftables")
|
||||
}
|
||||
}
|
||||
|
||||
// check apt
|
||||
if cmd == nil {
|
||||
aptGetExe, err := executils.LookPath("apt-get")
|
||||
if err == nil {
|
||||
cmd = executils.NewCmd(aptGetExe, "install", "nftables")
|
||||
}
|
||||
|
||||
aptExe, aptErr := executils.LookPath("apt")
|
||||
if aptErr == nil {
|
||||
aptCmd = executils.NewCmd(aptExe, "install", "nftables")
|
||||
}
|
||||
}
|
||||
|
||||
// check yum
|
||||
if cmd == nil {
|
||||
yumExe, err := executils.LookPath("yum")
|
||||
if err == nil {
|
||||
cmd = executils.NewCmd(yumExe, "-y", "install", "nftables")
|
||||
}
|
||||
}
|
||||
|
||||
if cmd == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
cmd.WithTimeout(10 * time.Minute)
|
||||
cmd.WithStderr()
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
// try 'apt-get' instead of 'apt'
|
||||
if aptCmd != nil {
|
||||
cmd = aptCmd
|
||||
cmd.WithTimeout(10 * time.Minute)
|
||||
cmd.WithStderr()
|
||||
err = cmd.Run()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %s", err, cmd.Stderr())
|
||||
}
|
||||
}
|
||||
|
||||
remotelogs.Println("NFTABLES", "installed nftables with command '"+cmd.String()+"' successfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
52
EdgeNode/internal/firewalls/nftables/rule.go
Normal file
52
EdgeNode/internal/firewalls/nftables/rule.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
nft "github.com/google/nftables"
|
||||
"github.com/google/nftables/expr"
|
||||
)
|
||||
|
||||
type Rule struct {
|
||||
rawRule *nft.Rule
|
||||
}
|
||||
|
||||
func NewRule(rawRule *nft.Rule) *Rule {
|
||||
return &Rule{
|
||||
rawRule: rawRule,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Rule) Raw() *nft.Rule {
|
||||
return this.rawRule
|
||||
}
|
||||
|
||||
func (this *Rule) LookupSetName() string {
|
||||
for _, e := range this.rawRule.Exprs {
|
||||
exp, ok := e.(*expr.Lookup)
|
||||
if ok {
|
||||
return exp.SetName
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (this *Rule) VerDict() expr.VerdictKind {
|
||||
for _, e := range this.rawRule.Exprs {
|
||||
exp, ok := e.(*expr.Verdict)
|
||||
if ok {
|
||||
return exp.Kind
|
||||
}
|
||||
}
|
||||
|
||||
return -100
|
||||
}
|
||||
|
||||
func (this *Rule) Handle() uint64 {
|
||||
return this.rawRule.Handle
|
||||
}
|
||||
|
||||
func (this *Rule) UserData() []byte {
|
||||
return this.rawRule.UserData
|
||||
}
|
||||
206
EdgeNode/internal/firewalls/nftables/set.go
Normal file
206
EdgeNode/internal/firewalls/nftables/set.go
Normal file
@@ -0,0 +1,206 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
nft "github.com/google/nftables"
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const MaxSetNameLength = 15
|
||||
|
||||
type SetOptions struct {
|
||||
Id uint32
|
||||
HasTimeout bool
|
||||
Timeout time.Duration
|
||||
KeyType SetDataType
|
||||
DataType SetDataType
|
||||
Constant bool
|
||||
Interval bool
|
||||
Anonymous bool
|
||||
IsMap bool
|
||||
}
|
||||
|
||||
type ElementOptions struct {
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
type Set struct {
|
||||
conn *Conn
|
||||
rawSet *nft.Set
|
||||
batch *SetBatch
|
||||
|
||||
expiration *Expiration
|
||||
}
|
||||
|
||||
func NewSet(conn *Conn, rawSet *nft.Set) *Set {
|
||||
var set = &Set{
|
||||
conn: conn,
|
||||
rawSet: rawSet,
|
||||
expiration: nil,
|
||||
batch: &SetBatch{
|
||||
conn: conn,
|
||||
rawSet: rawSet,
|
||||
},
|
||||
}
|
||||
|
||||
// retrieve set elements to improve "delete" speed
|
||||
set.initElements()
|
||||
|
||||
return set
|
||||
}
|
||||
|
||||
func (this *Set) Raw() *nft.Set {
|
||||
return this.rawSet
|
||||
}
|
||||
|
||||
func (this *Set) Name() string {
|
||||
return this.rawSet.Name
|
||||
}
|
||||
|
||||
func (this *Set) AddElement(key []byte, options *ElementOptions, overwrite bool) error {
|
||||
// check if already exists
|
||||
if this.expiration != nil && !overwrite && this.expiration.Contains(key) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var expiresTime = time.Time{}
|
||||
var rawElement = nft.SetElement{
|
||||
Key: key,
|
||||
}
|
||||
if options != nil {
|
||||
rawElement.Timeout = options.Timeout
|
||||
|
||||
if options.Timeout > 0 {
|
||||
expiresTime = time.UnixMilli(time.Now().UnixMilli() + options.Timeout.Milliseconds())
|
||||
}
|
||||
}
|
||||
err := this.conn.Raw().SetAddElements(this.rawSet, []nft.SetElement{
|
||||
rawElement,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = this.conn.Commit()
|
||||
if err == nil {
|
||||
if this.expiration != nil {
|
||||
this.expiration.Add(key, expiresTime)
|
||||
}
|
||||
} else {
|
||||
var isFileExistsErr = strings.Contains(err.Error(), "file exists")
|
||||
if !overwrite && isFileExistsErr {
|
||||
// ignore file exists error
|
||||
return nil
|
||||
}
|
||||
|
||||
// retry if exists
|
||||
if overwrite && isFileExistsErr {
|
||||
deleteErr := this.conn.Raw().SetDeleteElements(this.rawSet, []nft.SetElement{
|
||||
{
|
||||
Key: key,
|
||||
},
|
||||
})
|
||||
if deleteErr == nil {
|
||||
err = this.conn.Raw().SetAddElements(this.rawSet, []nft.SetElement{
|
||||
rawElement,
|
||||
})
|
||||
if err == nil {
|
||||
err = this.conn.Commit()
|
||||
if err == nil {
|
||||
if this.expiration != nil {
|
||||
this.expiration.Add(key, expiresTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (this *Set) AddIPElement(ip string, options *ElementOptions, overwrite bool) error {
|
||||
var ipObj = net.ParseIP(ip)
|
||||
if ipObj == nil {
|
||||
return errors.New("invalid ip '" + ip + "'")
|
||||
}
|
||||
|
||||
if utils.IsIPv4(ip) {
|
||||
return this.AddElement(ipObj.To4(), options, overwrite)
|
||||
} else {
|
||||
return this.AddElement(ipObj.To16(), options, overwrite)
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Set) DeleteElement(key []byte) error {
|
||||
// if set element does not exist, we return immediately
|
||||
if this.expiration != nil && !this.expiration.Contains(key) {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := this.conn.Raw().SetDeleteElements(this.rawSet, []nft.SetElement{
|
||||
{
|
||||
Key: key,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = this.conn.Commit()
|
||||
if err == nil {
|
||||
if this.expiration != nil {
|
||||
this.expiration.Remove(key)
|
||||
}
|
||||
} else {
|
||||
if strings.Contains(err.Error(), "no such file or directory") {
|
||||
err = nil
|
||||
|
||||
if this.expiration != nil {
|
||||
this.expiration.Remove(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (this *Set) DeleteIPElement(ip string) error {
|
||||
var ipObj = net.ParseIP(ip)
|
||||
if ipObj == nil {
|
||||
return errors.New("invalid ip '" + ip + "'")
|
||||
}
|
||||
|
||||
if utils.IsIPv4(ip) {
|
||||
return this.DeleteElement(ipObj.To4())
|
||||
} else {
|
||||
return this.DeleteElement(ipObj.To16())
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Set) Batch() *SetBatch {
|
||||
return this.batch
|
||||
}
|
||||
|
||||
func (this *Set) GetIPElements() ([]string, error) {
|
||||
elements, err := this.conn.Raw().GetSetElements(this.rawSet)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result = []string{}
|
||||
for _, element := range elements {
|
||||
result = append(result, net.IP(element.Key).String())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// not work current time
|
||||
/**func (this *Set) Flush() error {
|
||||
this.conn.Raw().FlushSet(this.rawSet)
|
||||
return this.conn.Commit()
|
||||
}**/
|
||||
37
EdgeNode/internal/firewalls/nftables/set_batch.go
Normal file
37
EdgeNode/internal/firewalls/nftables/set_batch.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
nft "github.com/google/nftables"
|
||||
)
|
||||
|
||||
type SetBatch struct {
|
||||
conn *Conn
|
||||
rawSet *nft.Set
|
||||
}
|
||||
|
||||
func (this *SetBatch) AddElement(key []byte, options *ElementOptions) error {
|
||||
var rawElement = nft.SetElement{
|
||||
Key: key,
|
||||
}
|
||||
if options != nil {
|
||||
rawElement.Timeout = options.Timeout
|
||||
}
|
||||
return this.conn.Raw().SetAddElements(this.rawSet, []nft.SetElement{
|
||||
rawElement,
|
||||
})
|
||||
}
|
||||
|
||||
func (this *SetBatch) DeleteElement(key []byte) error {
|
||||
return this.conn.Raw().SetDeleteElements(this.rawSet, []nft.SetElement{
|
||||
{
|
||||
Key: key,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (this *SetBatch) Commit() error {
|
||||
return this.conn.Commit()
|
||||
}
|
||||
58
EdgeNode/internal/firewalls/nftables/set_data_type.go
Normal file
58
EdgeNode/internal/firewalls/nftables/set_data_type.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import nft "github.com/google/nftables"
|
||||
|
||||
type SetDataType = nft.SetDatatype
|
||||
|
||||
var (
|
||||
TypeInvalid = nft.TypeInvalid
|
||||
TypeVerdict = nft.TypeVerdict
|
||||
TypeNFProto = nft.TypeNFProto
|
||||
TypeBitmask = nft.TypeBitmask
|
||||
TypeInteger = nft.TypeInteger
|
||||
TypeString = nft.TypeString
|
||||
TypeLLAddr = nft.TypeLLAddr
|
||||
TypeIPAddr = nft.TypeIPAddr
|
||||
TypeIP6Addr = nft.TypeIP6Addr
|
||||
TypeEtherAddr = nft.TypeEtherAddr
|
||||
TypeEtherType = nft.TypeEtherType
|
||||
TypeARPOp = nft.TypeARPOp
|
||||
TypeInetProto = nft.TypeInetProto
|
||||
TypeInetService = nft.TypeInetService
|
||||
TypeICMPType = nft.TypeICMPType
|
||||
TypeTCPFlag = nft.TypeTCPFlag
|
||||
TypeDCCPPktType = nft.TypeDCCPPktType
|
||||
TypeMHType = nft.TypeMHType
|
||||
TypeTime = nft.TypeTime
|
||||
TypeMark = nft.TypeMark
|
||||
TypeIFIndex = nft.TypeIFIndex
|
||||
TypeARPHRD = nft.TypeARPHRD
|
||||
TypeRealm = nft.TypeRealm
|
||||
TypeClassID = nft.TypeClassID
|
||||
TypeUID = nft.TypeUID
|
||||
TypeGID = nft.TypeGID
|
||||
TypeCTState = nft.TypeCTState
|
||||
TypeCTDir = nft.TypeCTDir
|
||||
TypeCTStatus = nft.TypeCTStatus
|
||||
TypeICMP6Type = nft.TypeICMP6Type
|
||||
TypeCTLabel = nft.TypeCTLabel
|
||||
TypePktType = nft.TypePktType
|
||||
TypeICMPCode = nft.TypeICMPCode
|
||||
TypeICMPV6Code = nft.TypeICMPV6Code
|
||||
TypeICMPXCode = nft.TypeICMPXCode
|
||||
TypeDevGroup = nft.TypeDevGroup
|
||||
TypeDSCP = nft.TypeDSCP
|
||||
TypeECN = nft.TypeECN
|
||||
TypeFIBAddr = nft.TypeFIBAddr
|
||||
TypeBoolean = nft.TypeBoolean
|
||||
TypeCTEventBit = nft.TypeCTEventBit
|
||||
TypeIFName = nft.TypeIFName
|
||||
TypeIGMPType = nft.TypeIGMPType
|
||||
TypeTimeDate = nft.TypeTimeDate
|
||||
TypeTimeHour = nft.TypeTimeHour
|
||||
TypeTimeDay = nft.TypeTimeDay
|
||||
TypeCGroupV2 = nft.TypeCGroupV2
|
||||
)
|
||||
8
EdgeNode/internal/firewalls/nftables/set_ext.go
Normal file
8
EdgeNode/internal/firewalls/nftables/set_ext.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build linux && !plus
|
||||
|
||||
package nftables
|
||||
|
||||
func (this *Set) initElements() {
|
||||
// NOT IMPLEMENTED
|
||||
}
|
||||
23
EdgeNode/internal/firewalls/nftables/set_ext_plus.go
Normal file
23
EdgeNode/internal/firewalls/nftables/set_ext_plus.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build linux && plus
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (this *Set) initElements() {
|
||||
elements, err := this.conn.Raw().GetSetElements(this.rawSet)
|
||||
var unixMilli = time.Now().UnixMilli()
|
||||
if err == nil {
|
||||
this.expiration = NewExpiration()
|
||||
for _, element := range elements {
|
||||
if element.Expires == 0 {
|
||||
this.expiration.AddUnsafe(element.Key, time.Time{})
|
||||
} else {
|
||||
this.expiration.AddUnsafe(element.Key, time.UnixMilli(unixMilli+element.Expires.Milliseconds()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
111
EdgeNode/internal/firewalls/nftables/set_test.go
Normal file
111
EdgeNode/internal/firewalls/nftables/set_test.go
Normal file
@@ -0,0 +1,111 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"github.com/mdlayher/netlink"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getIPv4Set(t *testing.T) *nftables.Set {
|
||||
var table = getIPv4Table(t)
|
||||
set, err := table.GetSet("test_ipv4_set")
|
||||
if err != nil {
|
||||
if err == nftables.ErrSetNotFound {
|
||||
set, err = table.AddSet("test_ipv4_set", &nftables.SetOptions{
|
||||
KeyType: nftables.TypeIPAddr,
|
||||
HasTimeout: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
func TestSet_AddElement(t *testing.T) {
|
||||
var set = getIPv4Set(t)
|
||||
err := set.AddElement(net.ParseIP("192.168.2.31").To4(), &nftables.ElementOptions{Timeout: 86400 * time.Second}, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestSet_DeleteElement(t *testing.T) {
|
||||
var set = getIPv4Set(t)
|
||||
err := set.DeleteElement(net.ParseIP("192.168.2.31").To4())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestSet_Batch(t *testing.T) {
|
||||
var batch = getIPv4Set(t).Batch()
|
||||
|
||||
for _, ip := range []string{"192.168.2.30", "192.168.2.31", "192.168.2.32", "192.168.2.33", "192.168.2.34"} {
|
||||
var ipData = net.ParseIP(ip).To4()
|
||||
//err := batch.DeleteElement(ipData)
|
||||
//if err != nil {
|
||||
// t.Fatal(err)
|
||||
//}
|
||||
err := batch.AddElement(ipData, &nftables.ElementOptions{Timeout: 10 * time.Second})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
err := batch.Commit()
|
||||
if err != nil {
|
||||
t.Logf("%#v", errors.Unwrap(err).(*netlink.OpError))
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestSet_Add_Many(t *testing.T) {
|
||||
var set = getIPv4Set(t)
|
||||
|
||||
for i := 0; i < 255; i++ {
|
||||
t.Log(i)
|
||||
for j := 0; j < 255; j++ {
|
||||
var ip = "192.167." + types.String(i) + "." + types.String(j)
|
||||
var ipData = net.ParseIP(ip).To4()
|
||||
err := set.Batch().AddElement(ipData, &nftables.ElementOptions{Timeout: 3600 * time.Second})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if j%10 == 0 {
|
||||
err = set.Batch().Commit()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
err := set.Batch().Commit()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
/**func TestSet_Flush(t *testing.T) {
|
||||
var set = getIPv4Set(t)
|
||||
err := set.Flush()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}**/
|
||||
156
EdgeNode/internal/firewalls/nftables/table.go
Normal file
156
EdgeNode/internal/firewalls/nftables/table.go
Normal file
@@ -0,0 +1,156 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
nft "github.com/google/nftables"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Table struct {
|
||||
conn *Conn
|
||||
rawTable *nft.Table
|
||||
}
|
||||
|
||||
func NewTable(conn *Conn, rawTable *nft.Table) *Table {
|
||||
return &Table{
|
||||
conn: conn,
|
||||
rawTable: rawTable,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *Table) Raw() *nft.Table {
|
||||
return this.rawTable
|
||||
}
|
||||
|
||||
func (this *Table) Name() string {
|
||||
return this.rawTable.Name
|
||||
}
|
||||
|
||||
func (this *Table) Family() TableFamily {
|
||||
return this.rawTable.Family
|
||||
}
|
||||
|
||||
func (this *Table) GetChain(name string) (*Chain, error) {
|
||||
rawChains, err := this.conn.Raw().ListChains()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, rawChain := range rawChains {
|
||||
// must compare table name
|
||||
if rawChain.Name == name && rawChain.Table.Name == this.rawTable.Name {
|
||||
return NewChain(this.conn, this.rawTable, rawChain), nil
|
||||
}
|
||||
}
|
||||
return nil, ErrChainNotFound
|
||||
}
|
||||
|
||||
func (this *Table) AddChain(name string, chainPolicy *ChainPolicy) (*Chain, error) {
|
||||
if len(name) > MaxChainNameLength {
|
||||
return nil, errors.New("chain name too long (max " + types.String(MaxChainNameLength) + ")")
|
||||
}
|
||||
|
||||
var rawChain = this.conn.Raw().AddChain(&nft.Chain{
|
||||
Name: name,
|
||||
Table: this.rawTable,
|
||||
Hooknum: nft.ChainHookInput,
|
||||
Priority: nft.ChainPriorityFilter,
|
||||
Type: nft.ChainTypeFilter,
|
||||
Policy: chainPolicy,
|
||||
})
|
||||
|
||||
err := this.conn.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewChain(this.conn, this.rawTable, rawChain), nil
|
||||
}
|
||||
|
||||
func (this *Table) AddAcceptChain(name string) (*Chain, error) {
|
||||
var policy = ChainPolicyAccept
|
||||
return this.AddChain(name, &policy)
|
||||
}
|
||||
|
||||
func (this *Table) AddDropChain(name string) (*Chain, error) {
|
||||
var policy = ChainPolicyDrop
|
||||
return this.AddChain(name, &policy)
|
||||
}
|
||||
|
||||
func (this *Table) DeleteChain(name string) error {
|
||||
chain, err := this.GetChain(name)
|
||||
if err != nil {
|
||||
if err == ErrChainNotFound {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
this.conn.Raw().DelChain(chain.Raw())
|
||||
return this.conn.Commit()
|
||||
}
|
||||
|
||||
func (this *Table) GetSet(name string) (*Set, error) {
|
||||
rawSet, err := this.conn.Raw().GetSetByName(this.rawTable, name)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "no such file or directory") {
|
||||
return nil, ErrSetNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewSet(this.conn, rawSet), nil
|
||||
}
|
||||
|
||||
func (this *Table) AddSet(name string, options *SetOptions) (*Set, error) {
|
||||
if len(name) > MaxSetNameLength {
|
||||
return nil, errors.New("set name too long (max " + types.String(MaxSetNameLength) + ")")
|
||||
}
|
||||
|
||||
if options == nil {
|
||||
options = &SetOptions{}
|
||||
}
|
||||
var rawSet = &nft.Set{
|
||||
Table: this.rawTable,
|
||||
ID: options.Id,
|
||||
Name: name,
|
||||
Anonymous: options.Anonymous,
|
||||
Constant: options.Constant,
|
||||
Interval: options.Interval,
|
||||
IsMap: options.IsMap,
|
||||
HasTimeout: options.HasTimeout,
|
||||
Timeout: options.Timeout,
|
||||
KeyType: options.KeyType,
|
||||
DataType: options.DataType,
|
||||
}
|
||||
err := this.conn.Raw().AddSet(rawSet, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = this.conn.Commit()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NewSet(this.conn, rawSet), nil
|
||||
}
|
||||
|
||||
func (this *Table) DeleteSet(name string) error {
|
||||
set, err := this.GetSet(name)
|
||||
if err != nil {
|
||||
if err == ErrSetNotFound {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
this.conn.Raw().DelSet(set.Raw())
|
||||
return this.conn.Commit()
|
||||
}
|
||||
|
||||
func (this *Table) Flush() error {
|
||||
this.conn.Raw().FlushTable(this.rawTable)
|
||||
return this.conn.Commit()
|
||||
}
|
||||
142
EdgeNode/internal/firewalls/nftables/table_test.go
Normal file
142
EdgeNode/internal/firewalls/nftables/table_test.go
Normal file
@@ -0,0 +1,142 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build linux
|
||||
|
||||
package nftables_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func getIPv4Table(t *testing.T) *nftables.Table {
|
||||
conn, err := nftables.NewConn()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
table, err := conn.GetTable("test_ipv4", nftables.TableFamilyIPv4)
|
||||
if err != nil {
|
||||
if err == nftables.ErrTableNotFound {
|
||||
table, err = conn.AddIPv4Table("test_ipv4")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return table
|
||||
}
|
||||
|
||||
func TestTable_AddChain(t *testing.T) {
|
||||
var table = getIPv4Table(t)
|
||||
|
||||
{
|
||||
chain, err := table.AddChain("test_default_chain", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("created:", chain.Name())
|
||||
}
|
||||
|
||||
{
|
||||
chain, err := table.AddAcceptChain("test_accept_chain")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("created:", chain.Name())
|
||||
}
|
||||
|
||||
// Do not test drop chain before adding accept rule, you will drop yourself!!!!!!!
|
||||
/**{
|
||||
chain, err := table.AddDropChain("test_drop_chain")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("created:", chain.Name())
|
||||
}**/
|
||||
}
|
||||
|
||||
func TestTable_GetChain(t *testing.T) {
|
||||
var table = getIPv4Table(t)
|
||||
for _, chainName := range []string{"not_found_chain", "test_default_chain"} {
|
||||
chain, err := table.GetChain(chainName)
|
||||
if err != nil {
|
||||
if err == nftables.ErrChainNotFound {
|
||||
t.Log(chainName, ":", "not found")
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Log(chainName, ":", chain)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTable_DeleteChain(t *testing.T) {
|
||||
var table = getIPv4Table(t)
|
||||
err := table.DeleteChain("test_default_chain")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestTable_AddSet(t *testing.T) {
|
||||
var table = getIPv4Table(t)
|
||||
{
|
||||
set, err := table.AddSet("ipv4_black_set", &nftables.SetOptions{
|
||||
HasTimeout: false,
|
||||
KeyType: nftables.TypeIPAddr,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(set.Name())
|
||||
}
|
||||
|
||||
{
|
||||
set, err := table.AddSet("ipv6_black_set", &nftables.SetOptions{
|
||||
HasTimeout: true,
|
||||
//Timeout: 3600 * time.Second,
|
||||
KeyType: nftables.TypeIP6Addr,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(set.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTable_GetSet(t *testing.T) {
|
||||
var table = getIPv4Table(t)
|
||||
for _, setName := range []string{"not_found_set", "ipv4_black_set"} {
|
||||
set, err := table.GetSet(setName)
|
||||
if err != nil {
|
||||
if err == nftables.ErrSetNotFound {
|
||||
t.Log(setName, ": not found")
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
t.Log(setName, ":", set)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTable_DeleteSet(t *testing.T) {
|
||||
var table = getIPv4Table(t)
|
||||
err := table.DeleteSet("ipv4_black_set")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
|
||||
func TestTable_Flush(t *testing.T) {
|
||||
var table = getIPv4Table(t)
|
||||
err := table.Flush()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log("ok")
|
||||
}
|
||||
30
EdgeNode/internal/firewalls/utils.go
Normal file
30
EdgeNode/internal/firewalls/utils.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package firewalls
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// DropTemporaryTo 使用本地防火墙临时拦截IP数据包
|
||||
func DropTemporaryTo(ip string, expiresAt int64) {
|
||||
// 如果为0,则表示是长期有效
|
||||
if expiresAt <= 0 {
|
||||
expiresAt = time.Now().Unix() + 3600
|
||||
}
|
||||
|
||||
var timeout = expiresAt - time.Now().Unix()
|
||||
if timeout < 1 {
|
||||
return
|
||||
}
|
||||
if timeout > 3600 {
|
||||
timeout = 3600
|
||||
}
|
||||
|
||||
// 使用本地防火墙延长封禁
|
||||
var fw = Firewall()
|
||||
if fw != nil && !fw.IsMock() {
|
||||
// 这里 int(int64) 转换的前提是限制了 timeout <= 3600,否则将有整型溢出的风险
|
||||
_ = fw.DropSourceIP(ip, int(timeout), true)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user