Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增一个从env查找服务子网的功能 #5

Open
wants to merge 1 commit into
base: Skyworship
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions cmd/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package env

import (
command "github.com/esonhugh/k8spider/cmd"
"github.com/esonhugh/k8spider/pkg/scanner"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

func init() {
command.RootCmd.AddCommand(EnvCmd)
}

var EnvCmd = &cobra.Command{
Use: "env",
Short: "env is a command to detect service subnet from environ",
Run: func(cmd *cobra.Command, args []string) {
env := os.Environ()
serviceSubnetList := scanner.FindServiceSubnet(env)
serviceSubnetList = scanner.UniqueSubnet(serviceSubnetList...)
for _, k := range serviceSubnetList {
log.Infoln("Subnet from env:", k)
}
},
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"github.com/esonhugh/k8spider/cmd"
_ "github.com/esonhugh/k8spider/cmd/all"
_ "github.com/esonhugh/k8spider/cmd/axfr"
_ "github.com/esonhugh/k8spider/cmd/env"
_ "github.com/esonhugh/k8spider/cmd/service"
_ "github.com/esonhugh/k8spider/cmd/subnet"
_ "github.com/esonhugh/k8spider/cmd/wildcard"
)

func main() {
Expand Down
52 changes: 52 additions & 0 deletions pkg/scanner/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package scanner

import (
"fmt"
log "github.com/sirupsen/logrus"
"net"
"regexp"
"strings"
)

func extractValue(env string) (val string) {
val = strings.Split(env, "=")[1]
return strings.TrimSpace(val)
}

// detect service subnet from env
func FindServiceSubnet(env []string) (ServiceIPList []string) {
pattern := regexp.MustCompile(`(.*)_SERVICE_HOST=`)
if len(env) != 0 {
for _, k := range env {
matches := pattern.FindStringSubmatch(k)
if matches == nil {
continue
}
ServiceIPList = append(ServiceIPList, extractValue(k))
}
}
return
}

func UniqueSubnet(serviceIPList ...string) (UniqueSubnetList []string) {
if len(serviceIPList) != 0 {
uniqueSubnets := make(map[string]bool)
for _, ip := range serviceIPList {
_, subnet, err := net.ParseCIDR(ip + "/16")
if err != nil {
log.Fatalf("Invalid IP/CIDR: %v", err)
}

// 获取子网的起始IP和网络地址
subnetIP := subnet.IP.Mask(subnet.Mask)

// 将网络地址作为唯一标识添加到map中
key := subnetIP.String() + "/" + fmt.Sprint(subnet.Mask.Size())
if !uniqueSubnets[key] {
uniqueSubnets[key] = true
UniqueSubnetList = append(UniqueSubnetList, subnet.String())
}
}
}
return
}