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.
- Loading branch information
Showing
11 changed files
with
213 additions
and
20 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
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mutli | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/esonhugh/k8spider/define" | ||
) | ||
|
||
func ScanAll(subnet *net.IPNet) (result <-chan []define.Record) { | ||
subs := NewSubnetScanner() | ||
result = ScanServiceWithChan(subs.ScanSubnet(subnet)) | ||
return result | ||
} |
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,47 @@ | ||
package mutli | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
|
||
"github.com/esonhugh/k8spider/define" | ||
"github.com/esonhugh/k8spider/pkg" | ||
"github.com/esonhugh/k8spider/pkg/scanner" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type SubnetScanner struct { | ||
wg *sync.WaitGroup | ||
} | ||
|
||
func NewSubnetScanner() *SubnetScanner { | ||
return &SubnetScanner{ | ||
wg: new(sync.WaitGroup), | ||
} | ||
} | ||
|
||
func (s *SubnetScanner) ScanSubnet(subnet *net.IPNet) <-chan []define.Record { | ||
if subnet == nil { | ||
log.Tracef("subnet is nil") | ||
return nil | ||
} | ||
out := make(chan []define.Record, 100) | ||
go func() { | ||
if subnets, err := pkg.SubnetShift(subnet, 4); err != nil { | ||
go s.scan(subnet, out) | ||
} else { | ||
for _, sn := range subnets { | ||
go s.scan(sn, out) | ||
} | ||
} | ||
s.wg.Wait() | ||
close(out) | ||
}() | ||
return out | ||
} | ||
|
||
func (s *SubnetScanner) scan(subnet *net.IPNet, to chan []define.Record) { | ||
s.wg.Add(1) | ||
to <- scanner.ScanSubnet(subnet) | ||
s.wg.Done() | ||
} |
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,17 @@ | ||
package mutli | ||
|
||
import ( | ||
"github.com/esonhugh/k8spider/define" | ||
"github.com/esonhugh/k8spider/pkg/scanner" | ||
) | ||
|
||
func ScanServiceWithChan(rev <-chan []define.Record) <-chan []define.Record { | ||
out := make(chan []define.Record, 100) | ||
go func() { | ||
for records := range rev { | ||
out <- scanner.ScanSvcForPorts(records) | ||
} | ||
close(out) | ||
}() | ||
return out | ||
} |
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package pkg | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"net" | ||
) | ||
|
||
// SubnetInto wraps SubnetShift and divides a network into at least count-many, | ||
// equal-sized subnets, which are as large as allowed. | ||
func SubnetInto(network *net.IPNet, count int) ([]*net.IPNet, error) { | ||
maskBits, _ := network.Mask.Size() | ||
hostBits := 32 - maskBits | ||
hostCount := 1 << uint(hostBits) | ||
|
||
// divide hosts among subnets | ||
ideal := float64(hostCount) / float64(count) | ||
// largest power of 2, not exceeding the ideal (float64 to int conversion | ||
// truncates toward zero) | ||
newHostBits := int(math.Log2(ideal)) | ||
shift := hostBits - newHostBits | ||
return SubnetShift(network, shift) | ||
} | ||
|
||
// SubnetShift divides a network into subnets by shifting the given number of bits. | ||
func SubnetShift(network *net.IPNet, bits int) ([]*net.IPNet, error) { | ||
if bits < 0 { | ||
return nil, fmt.Errorf("bit shift may not be negative, got %d", bits) | ||
} | ||
if bits > 31 { | ||
return nil, fmt.Errorf("network subnets cannot be divided %d times", bits) | ||
} | ||
// network divides into 2^bits subnets | ||
subnetCount := 1 << uint(bits) | ||
subnets := make([]*net.IPNet, subnetCount) | ||
|
||
// network info | ||
start := network.IP | ||
maskBits, _ := network.Mask.Size() | ||
hostBits := 32 - maskBits | ||
|
||
if maskBits+bits > 32 { | ||
return nil, fmt.Errorf("network subnet mask greater than /32, /%d is invalid", maskBits+bits) | ||
} | ||
|
||
// divide network into subnets | ||
newMaskBits := maskBits + bits | ||
newHostBits := hostBits - bits | ||
// subnet bitmasks are shifted by 'bits' places | ||
newMask := net.CIDRMask(newMaskBits, 32) | ||
|
||
// hosts per subnet | ||
hostCount := 1 << uint(newHostBits) | ||
|
||
for i := 0; i < subnetCount; i++ { | ||
ip := numeric(start) + uint32(i*hostCount) | ||
subnets[i] = &net.IPNet{ | ||
IP: bytewise(ip), | ||
Mask: newMask, | ||
} | ||
} | ||
|
||
return subnets, nil | ||
} | ||
|
||
// IP <-> integer transforms | ||
|
||
// numeric returns a uint32 numeric representation of a net.IP. | ||
func numeric(bytes net.IP) uint32 { | ||
var ip uint32 | ||
// most significant to least significant | ||
for i, b := range []byte(bytes) { | ||
// bitwise or ("append" in this case) | ||
ip |= uint32(b) << (8 * uint32(3-i)) | ||
} | ||
return ip | ||
} | ||
|
||
// bytewise returns a net.IP byte slice alias representation of an uint32. | ||
// Note that not all uint32 values are valid IP addresses. | ||
func bytewise(numeric uint32) net.IP { | ||
ip := make([]byte, 4) | ||
// least significant to most significant | ||
for i := 3; i >= 0; i-- { | ||
// AND away all but least significant | ||
ip[i] = byte(numeric & 0xFF) | ||
// nuke least significant byte | ||
numeric >>= 8 | ||
} | ||
return net.IP(ip) | ||
} |