generated from Esonhugh/go-cli-template-v2
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: pod verified pod ip detection
- Loading branch information
Showing
8 changed files
with
193 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package neighbor | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"net" | ||
"os" | ||
"strings" | ||
|
||
cmdx "github.com/esonhugh/k8spider/cmd" | ||
"github.com/esonhugh/k8spider/define" | ||
"github.com/esonhugh/k8spider/pkg" | ||
"github.com/esonhugh/k8spider/pkg/mutli" | ||
"github.com/esonhugh/k8spider/pkg/printer" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Opts = struct { | ||
NamespaceWordlist string | ||
NamespaceList []string | ||
PodCidr string | ||
}{} | ||
|
||
func init() { | ||
cmdx.RootCmd.AddCommand(NeighborCmd) | ||
NeighborCmd.Flags().StringVar(&Opts.NamespaceWordlist, "ns-file", "", "namespace wordlist file") | ||
NeighborCmd.Flags().StringSliceVar(&Opts.NamespaceList, "ns", []string{}, "namespace list") | ||
NeighborCmd.Flags().StringVarP(&Opts.PodCidr, "pod-cidr", "p", defaultPodCidr(), "pod cidr list, watch out for the network interface name, default is eth0") | ||
} | ||
|
||
func defaultPodCidr() string { | ||
interfaces, _ := net.Interfaces() | ||
for _, i := range interfaces { | ||
if i.Name == "eth0" { | ||
addrs, _ := i.Addrs() | ||
if addrs != nil || len(addrs) > 0 { | ||
ip := strings.Split(addrs[0].String(), "/")[0] | ||
return fmt.Sprintf("%v/16", ip) | ||
} | ||
} | ||
} | ||
return "10.0.0.1/16" | ||
} | ||
|
||
var NeighborCmd = &cobra.Command{ | ||
Use: "neighbor", | ||
Short: "neighbor is a tool to discover k8s pod and available ip in subnet (require k8s coredns with pod verified config)", | ||
Aliases: []string{"n", "nei"}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if !pkg.TestPodVerified() { | ||
log.Fatalf("k8s coredns with pod verified config could not be set") | ||
} | ||
if Opts.NamespaceWordlist != "" { | ||
f, e := os.OpenFile(Opts.NamespaceWordlist, os.O_RDONLY, 0666) | ||
if e != nil { | ||
log.Fatalf("open file %v failed: %v", Opts.NamespaceWordlist, e) | ||
} | ||
defer f.Close() | ||
fileScanner := bufio.NewScanner(f) | ||
fileScanner.Split(bufio.ScanLines) | ||
for fileScanner.Scan() { | ||
Opts.NamespaceList = append(Opts.NamespaceList, fileScanner.Text()) | ||
} | ||
} | ||
log.Tracef("namespace list: %v", Opts.NamespaceList) | ||
ipNets, err := pkg.ParseStringToIPNet(Opts.PodCidr) | ||
if err != nil { | ||
log.Warnf("ParseStringToIPNet failed: %v", err) | ||
return | ||
} | ||
r := RunMultiThread(Opts.NamespaceList, ipNets, cmdx.Opts.ThreadingNum) | ||
printer.PrintResult(r, cmdx.Opts.OutputFile) | ||
}, | ||
} | ||
|
||
func RunMultiThread(ns []string, net *net.IPNet, num int) (finalRecord define.Records) { | ||
scan := mutli.ScanNeighbor(ns, net, num) | ||
for r := range scan { | ||
finalRecord = append(finalRecord, r...) | ||
} | ||
if len(finalRecord) == 0 { | ||
log.Warn("ScanSubnet Found Nothing") | ||
return | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
package scanner | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/esonhugh/k8spider/pkg" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func TestPodVerified(zone string) bool { | ||
iplist := []string{ | ||
"8.8.8.8", | ||
"1.1.1.1", | ||
"114.114.114.114", | ||
func ScanPodExist(ip net.IP, ns string) bool { | ||
targetHostName := pkg.IPtoPodHostName(ip.String(), ns) | ||
ips, err := pkg.ARecord(targetHostName) | ||
if err != nil { | ||
log.Tracef("ScanPodExist %v failed: %v", ip.String(), err) | ||
return false | ||
} | ||
for _, ip := range iplist { | ||
targetHostName := pkg.IPtoPodHostName(ip, zone) | ||
ips, err := pkg.ARecord(targetHostName) | ||
if err != nil { | ||
continue | ||
} | ||
// all of this ip should not return correct ip if verified is set. | ||
for _, i := range ips { | ||
if i.String() == ip { | ||
return false | ||
} | ||
for _, i := range ips { | ||
if i.String() == ip.String() { | ||
return true | ||
} | ||
} | ||
return true | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters