[agent-go] [Bastion] - swap firewall
This commit is contained in:
@@ -1,98 +1,91 @@
|
||||
package bastion_init
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
import "strings"
|
||||
|
||||
type WordC struct {
|
||||
Word string
|
||||
Rank int
|
||||
// 假设我们的预定义字符串列表存储在这个map中,键为字符串索引,值为字符串本身
|
||||
var dictionary = map[string]struct{}{
|
||||
"apple": {},
|
||||
"apply": {},
|
||||
"apron": {},
|
||||
"docker": {},
|
||||
"docker-compose": {},
|
||||
"harbor": {},
|
||||
// ...其他词汇
|
||||
}
|
||||
|
||||
// normalize 将字符串转换为小写并去除特殊符号
|
||||
func normalize(s string) string {
|
||||
// Convert to lowercase and replace hyphens with underscores for consistent comparison.
|
||||
return strings.Map(func(r rune) rune {
|
||||
if 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' {
|
||||
return r
|
||||
}
|
||||
return '_'
|
||||
}, s)
|
||||
return strings.ToLower(strings.ReplaceAll(s, "\\W", ""))
|
||||
}
|
||||
|
||||
func levDpDist(s1, s2 string) int {
|
||||
m, n := len(s1), len(s2)
|
||||
if m == 0 {
|
||||
return n
|
||||
}
|
||||
if n == 0 {
|
||||
return m
|
||||
}
|
||||
// findBestMatch 在字典中找到最合适的单词
|
||||
func findBestMatch(query string) string {
|
||||
normQuery := normalize(query)
|
||||
|
||||
dp := make([][]int, m+1)
|
||||
for i := range dp {
|
||||
dp[i] = make([]int, n+1)
|
||||
}
|
||||
// 将查询字符串排序,以便二分查找
|
||||
sortedQuery := []rune(normQuery)
|
||||
|
||||
for i := 1; i <= m; i++ {
|
||||
for j := 1; j <= n; j++ {
|
||||
cost := 0
|
||||
if s1[i-1] != s2[j-1] {
|
||||
cost = 1
|
||||
}
|
||||
dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + cost
|
||||
}
|
||||
}
|
||||
return dp[m][n]
|
||||
}
|
||||
var bestMatch string
|
||||
minDistance := len(dictionary) + 1 // 最初假设没有匹配项
|
||||
|
||||
func min(a, b, c int) int {
|
||||
minVal := a
|
||||
if b < minVal {
|
||||
minVal = b
|
||||
}
|
||||
if c < minVal {
|
||||
minVal = c
|
||||
}
|
||||
return minVal
|
||||
}
|
||||
// 遍历字典中的所有单词
|
||||
for word, _ := range dictionary {
|
||||
normWord := normalize(word)
|
||||
distance := levenshteinDistance(sortedQuery, []rune(normWord)) // 计算编辑距离
|
||||
|
||||
func FindClosestWordDp(words []string, prefix string) (string, error) {
|
||||
normalizedPrefix := normalize(prefix)
|
||||
normalizedWords := make([]string, len(words))
|
||||
for i, word := range words {
|
||||
normalizedWords[i] = normalize(word)
|
||||
}
|
||||
|
||||
sort.Slice(normalizedWords, func(i, j int) bool {
|
||||
return normalizedWords[i] < normalizedWords[j]
|
||||
})
|
||||
|
||||
minDistance := levDpDist(normalizedPrefix, normalizedWords[0])
|
||||
closestWord := normalizedWords[0]
|
||||
|
||||
for _, word := range normalizedWords {
|
||||
distance := levDpDist(normalizedPrefix, word)
|
||||
if distance < minDistance {
|
||||
// 如果当前单词的编辑距离小于等于最佳匹配的距离,并且它是第一个匹配项(或者距离相同但字典序更低)
|
||||
if distance <= minDistance || (distance == minDistance && strings.ToLower(word) < normQuery) {
|
||||
minDistance = distance
|
||||
closestWord = word
|
||||
bestMatch = word
|
||||
}
|
||||
}
|
||||
|
||||
// Replace underscores with hyphens and convert back to original case.
|
||||
closestWord = strings.ReplaceAll(strings.ReplaceAll(strings.ToUpper(closestWord), "_", "-"), "\"", "")
|
||||
return closestWord, nil
|
||||
return bestMatch
|
||||
}
|
||||
|
||||
// levenshteinDistance 计算两个字符串之间的编辑距离(插入、删除或替换一个字符的次数)
|
||||
func levenshteinDistance(a, b []rune) int {
|
||||
lenA := len(a)
|
||||
lenB := len(b)
|
||||
|
||||
dist := make([][]int, lenA+1)
|
||||
for i := range dist {
|
||||
dist[i] = make([]int, lenB+1)
|
||||
}
|
||||
|
||||
for i := 0; i <= lenA; i++ {
|
||||
dist[i][0] = i
|
||||
}
|
||||
for j := 0; j <= lenB; j++ {
|
||||
dist[0][j] = j
|
||||
}
|
||||
|
||||
for i := 1; i <= lenA; i++ {
|
||||
for j := 1; j <= lenB; j++ {
|
||||
cost := int(a[i-1] - b[j-1])
|
||||
dist[i][j] = minInt(dist[i-1][j]+1, dist[i][j-1]+1, dist[i-1][j-1]+cost)
|
||||
}
|
||||
}
|
||||
|
||||
return dist[lenA][lenB]
|
||||
}
|
||||
|
||||
// minInt 找到三个整数中的最小值
|
||||
func minInt(a, b, c int) int {
|
||||
if a < b {
|
||||
if a < c {
|
||||
return a
|
||||
}
|
||||
return c
|
||||
}
|
||||
if b < c {
|
||||
return b
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func main() {
|
||||
words := []string{"Apple", "Apricot", "Apprentice", "Application"}
|
||||
prefix := "AP"
|
||||
|
||||
closest, err := FindClosestWordDp(words, prefix)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Printf("The closest word to '%s' is '%s'\n", prefix, closest)
|
||||
}
|
||||
query := "app!"
|
||||
bestMatch := findBestMatch(query)
|
||||
println("The best match for", query, "is:", bestMatch)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package bastion_init
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -21,13 +22,37 @@ func TestNewTrie(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDp(t *testing.T) {
|
||||
words := []string{"Apple", "Apricot", "Apprentice", "Application", "Docker-Compose", "Docker-Compose-File", "Docker-Compose-File-V2", "Docker-Compose-File-V3", "Docker-Compose-File-V4", "Docker-Compose-File-V5", "Docker-Compose-File-V6", "Docker-Co", "Install-Docker"}
|
||||
prefix := "Install"
|
||||
// 获取netlink接口(用于网络控制的内核机制)
|
||||
nl := syscall.NewLk(syscall.AT_FDCWD, "netlink")
|
||||
if nl == nil {
|
||||
fmt.Println("Unable to open netlink")
|
||||
return
|
||||
}
|
||||
defer nl.Close()
|
||||
|
||||
closest, err := FindClosestWordDp(words, prefix)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
// 设置对netlink的访问权限
|
||||
if err := syscall.Setpgid(0, syscall.Getpid()); err != nil {
|
||||
fmt.Println("Unable to set pgid", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 设置netlink接口为非阻塞模式
|
||||
if err := nl.Control(syscall.SET_NONBLOCK, 1); err != nil {
|
||||
fmt.Println("Unable to set netlink nonblocking", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取IPv4路由转发设置的值
|
||||
var value syscall.SysctlValInt
|
||||
if err := syscall.Sysctl(nl, "net/ipv4/ip_forward", &value); err != nil {
|
||||
fmt.Println("Unable to get ip_forward value:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 打印IPv4路由转发的状态
|
||||
if value == 1 {
|
||||
fmt.Println("IPv4 routing forwarding is enabled")
|
||||
} else {
|
||||
fmt.Printf("The closest word to '%s' is '%s'\n", prefix, closest)
|
||||
fmt.Println("IPv4 routing forwarding is disabled")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user