mirror of
https://github.com/alibaba/higress.git
synced 2026-05-29 23:27:28 +08:00
feat: add ip-restriction wasm-go plugin (#759)
This commit is contained in:
37
plugins/wasm-go/extensions/ip-restriction/utils.go
Normal file
37
plugins/wasm-go/extensions/ip-restriction/utils.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/zmap/go-iptree/iptree"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// parseIPNets 解析Ip段配置
|
||||
func parseIPNets(array []gjson.Result) (*iptree.IPTree, error) {
|
||||
if len(array) == 0 {
|
||||
return nil, nil
|
||||
} else {
|
||||
tree := iptree.New()
|
||||
for _, result := range array {
|
||||
err := tree.AddByString(result.String(), 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid IP[%s]", result.String())
|
||||
}
|
||||
}
|
||||
return tree, nil
|
||||
}
|
||||
}
|
||||
|
||||
// parseIP 解析IP
|
||||
func parseIP(source string) string {
|
||||
if strings.Contains(source, ".") {
|
||||
// parse ipv4
|
||||
return strings.Split(source, ":")[0]
|
||||
}
|
||||
//parse ipv6
|
||||
if strings.Contains(source, "]") {
|
||||
return strings.Split(source, "]")[0][1:]
|
||||
}
|
||||
return source
|
||||
}
|
||||
Reference in New Issue
Block a user