AbdulElahGwaith's picture
Upload folder using huggingface_hub
ffb6330 verified
// Package runner ipnet实现
package runner
import (
"encoding/binary"
"github.com/Tencent/AI-Infra-Guard/common/utils"
"net"
"strings"
)
// Targets returns all the targets within a cidr range or the single target
func Targets(target string) chan string {
results := make(chan string)
go func() {
defer close(results)
// A valid target does not contain:
// *
// spaces
if strings.ContainsAny(target, " *") {
return
}
// test if the target is a cidr
if utils.IsCIDR(target) {
cidrIps, err := IPAddresses(target)
if err != nil {
return
}
for _, ip := range cidrIps {
results <- ip
}
} else {
results <- target
}
}()
return results
}
// IPAddresses returns all the IP addresses in a CIDR
func IPAddresses(cidr string) ([]string, error) {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return []string{}, err
}
return IPAddressesIPnet(ipnet), nil
}
// IPAddressesIPnet returns all IP addresses in an IPNet.
func IPAddressesIPnet(ipnet *net.IPNet) (ips []string) {
// convert IPNet struct mask and address to uint32
mask := binary.BigEndian.Uint32(ipnet.Mask)
start := binary.BigEndian.Uint32(ipnet.IP)
// find the final address
finish := (start & mask) | (mask ^ 0xffffffff)
// loop through addresses as uint32
for i := start; i <= finish; i++ {
// convert back to net.IP
ip := make(net.IP, 4)
binary.BigEndian.PutUint32(ip, i)
ips = append(ips, ip.String())
}
return ips
}