@@ -2,10 +2,8 @@ package neighbor
22
33import (
44 "bufio"
5- "fmt"
65 "net"
76 "os"
8- "strings"
97
108 cmdx "github.com/esonhugh/k8spider/cmd"
119 "github.com/esonhugh/k8spider/define"
@@ -19,34 +17,40 @@ import (
1917var Opts = struct {
2018 NamespaceWordlist string
2119 NamespaceList []string
22- PodCidr string
2320}{}
2421
2522func init () {
23+ NeighborCmd .AddCommand (NeighborSvcCmd , NeighborPodCmd )
2624 cmdx .RootCmd .AddCommand (NeighborCmd )
27- NeighborCmd .Flags ().StringVar (& Opts .NamespaceWordlist , "ns-file" , "" , "namespace wordlist file" )
28- NeighborCmd .Flags ().StringSliceVar (& Opts .NamespaceList , "ns" , []string {}, "namespace list" )
29- NeighborCmd .Flags ().StringVarP (& Opts .PodCidr , "pod-cidr" , "p" , defaultPodCidr (), "pod cidr list, watch out for the network interface name, default is eth0" )
25+ NeighborPodCmd .Flags ().StringVar (& Opts .NamespaceWordlist , "ns-file" , "" , "namespace wordlist file" )
26+ NeighborPodCmd .Flags ().StringSliceVar (& Opts .NamespaceList , "ns" , []string {}, "namespace list" )
3027}
3128
32- func defaultPodCidr () string {
33- interfaces , _ := net .Interfaces ()
34- for _ , i := range interfaces {
35- if i .Name == "eth0" {
36- addrs , _ := i .Addrs ()
37- if addrs != nil || len (addrs ) > 0 {
38- ip := strings .Split (addrs [0 ].String (), "/" )[0 ]
39- return fmt .Sprintf ("%v/16" , ip )
40- }
29+ var NeighborCmd = & cobra.Command {
30+ Use : "neighbor" ,
31+ Short : "neighbor is a tool to discover k8s neighbor pods" ,
32+ Run : func (cmd * cobra.Command , args []string ) {
33+ cmd .Help ()
34+ },
35+ }
36+
37+ var NeighborSvcCmd = & cobra.Command {
38+ Use : "svc" ,
39+ Short : "neighbor is a tool to discover k8s neighbor pod with services" ,
40+ Run : func (cmd * cobra.Command , args []string ) {
41+ ipNets , err := pkg .ParseStringToIPNet (cmdx .Opts .PodCidr )
42+ if err != nil {
43+ log .Warnf ("ParseStringToIPNet failed: %v" , err )
44+ return
4145 }
42- }
43- return "10.0.0.1/16"
46+ r := RunSvc (ipNets , cmdx .Opts .ThreadingNum )
47+ printer .PrintResult (r , cmdx .Opts .OutputFile )
48+ },
4449}
4550
46- var NeighborCmd = & cobra.Command {
47- Use : "neighbor" ,
48- Short : "neighbor is a tool to discover k8s pod and available ip in subnet (require k8s coredns with pod verified config)" ,
49- Aliases : []string {"n" , "nei" },
51+ var NeighborPodCmd = & cobra.Command {
52+ Use : "pod" ,
53+ Short : "neighbor is a tool to discover k8s pod and available ip in subnet (require k8s coredns with pod verified config)" ,
5054 Run : func (cmd * cobra.Command , args []string ) {
5155 if ! pkg .CheckPodVerified () {
5256 log .Fatalf ("k8s coredns with pod verified config could not be set" )
@@ -64,17 +68,17 @@ var NeighborCmd = &cobra.Command{
6468 }
6569 }
6670 log .Tracef ("namespace list: %v" , Opts .NamespaceList )
67- ipNets , err := pkg .ParseStringToIPNet (Opts .PodCidr )
71+ ipNets , err := pkg .ParseStringToIPNet (cmdx . Opts .PodCidr )
6872 if err != nil {
6973 log .Warnf ("ParseStringToIPNet failed: %v" , err )
7074 return
7175 }
72- r := RunMultiThread (Opts .NamespaceList , ipNets , cmdx .Opts .ThreadingNum )
76+ r := RunPod (Opts .NamespaceList , ipNets , cmdx .Opts .ThreadingNum )
7377 printer .PrintResult (r , cmdx .Opts .OutputFile )
7478 },
7579}
7680
77- func RunMultiThread (ns []string , net * net.IPNet , num int ) (finalRecord define.Records ) {
81+ func RunPod (ns []string , net * net.IPNet , num int ) (finalRecord define.Records ) {
7882 scan := mutli .ScanNeighbor (ns , net , num )
7983 for r := range scan {
8084 finalRecord = append (finalRecord , r ... )
@@ -85,3 +89,15 @@ func RunMultiThread(ns []string, net *net.IPNet, num int) (finalRecord define.Re
8589 }
8690 return
8791}
92+
93+ func RunSvc (net * net.IPNet , num int ) (finalRecord define.Records ) {
94+ scan := mutli .ScanNeighborSvc (net , num )
95+ for r := range scan {
96+ finalRecord = append (finalRecord , r ... )
97+ }
98+ if len (finalRecord ) == 0 {
99+ log .Warn ("ScanSubnet Found Nothing" )
100+ return
101+ }
102+ return
103+ }
0 commit comments