2 Star 1 Fork 0

李玮/trireme-lib

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ipsetprovider.go 2.67 KB
一键复制 编辑 原始数据 按行查看 历史
李玮 提交于 2020-01-29 13:23 +08:00 . v1
package provider
import (
"fmt"
"os/exec"
"strings"
"github.com/zhaolanbao/go-ipset/ipset"
"go.uber.org/zap"
)
// IpsetProvider returns a fabric for Ipset.
type IpsetProvider interface {
NewIpset(name string, ipsetType string, p *ipset.Params) (Ipset, error)
GetIpset(name string) Ipset
DestroyAll(prefix string) error
ListIPSets() ([]string, error)
}
// Ipset is an abstraction of all the methods an implementation of userspace
// ipsets need to provide.
type Ipset interface {
Add(entry string, timeout int) error
AddOption(entry string, option string, timeout int) error
Del(entry string) error
Destroy() error
Flush() error
Test(entry string) (bool, error)
}
type goIpsetProvider struct{}
func ipsetCreateBitmapPort(setname string) error {
//Bitmap type is not supported by the ipset library
path, _ := exec.LookPath("ipset")
out, err := exec.Command(path, "create", setname, "bitmap:port", "range", "0-65535", "timeout", "0").CombinedOutput()
if err != nil {
if strings.Contains(string(out), "set with the same name already exists") {
zap.L().Warn("Set already exists - cleaning up", zap.String("set name", setname))
// Clean up the existing set
if _, cerr := exec.Command(path, "-F", setname).CombinedOutput(); cerr != nil {
return fmt.Errorf("Failed to clean up existing ipset: %s", err)
}
return nil
}
zap.L().Error("Unable to create set", zap.String("set name", setname), zap.String("ipset-output", string(out)))
}
return err
}
// NewIpset returns an IpsetProvider interface based on the go-ipset
// external package.
func (i *goIpsetProvider) NewIpset(name string, ipsetType string, p *ipset.Params) (Ipset, error) {
// Check if hashtype is a type of hash
if strings.HasPrefix(ipsetType, "hash:") {
return ipset.New(name, ipsetType, p)
}
if err := ipsetCreateBitmapPort(name); err != nil {
return nil, err
}
return &ipset.IPSet{Name: name}, nil
}
// GetIpset gets the ipset object from the name.
func (i *goIpsetProvider) GetIpset(name string) Ipset {
return &ipset.IPSet{
Name: name,
}
}
// DestroyAll destroys all the ipsets - it will fail if there are existing references
func (i *goIpsetProvider) DestroyAll(prefix string) error {
return ipset.DestroyAll()
}
func (i *goIpsetProvider) ListIPSets() ([]string, error) {
path, err := exec.LookPath("ipset")
if err != nil {
return nil, fmt.Errorf("ipset command not found: %s", err)
}
out, err := exec.Command(path, "-L", "-name").CombinedOutput()
if err != nil {
return nil, fmt.Errorf("unable to list ipsets:%s", err)
}
return strings.Split(string(out), "\n"), nil
}
// NewGoIPsetProvider Return a Go IPSet Provider
func NewGoIPsetProvider() IpsetProvider {
return &goIpsetProvider{}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/emmoblin/trireme-lib.git
git@gitee.com:emmoblin/trireme-lib.git
emmoblin
trireme-lib
trireme-lib
7726874a2b9a

搜索帮助