2022-12-15 00:28:36 +01:00
|
|
|
package scan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-12-16 16:34:52 +01:00
|
|
|
"log"
|
2022-12-15 00:28:36 +01:00
|
|
|
"musique/server/proto"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Network struct {
|
|
|
|
FirstAddress string
|
|
|
|
MaxHostsCount int
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeoutTCPHosts = time.Duration(1) * time.Second
|
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
func nextIP(ip net.IP) (next net.IP) {
|
2022-12-15 00:28:36 +01:00
|
|
|
// FIXME Proper next IP address in network calculation
|
2022-12-16 02:11:43 +01:00
|
|
|
next = make([]byte, 4)
|
|
|
|
bytes := []byte(ip)
|
|
|
|
bytes = bytes[len(bytes)-4:]
|
|
|
|
next[0], next[1], next[2], next[3] = bytes[0], bytes[1], bytes[2], bytes[3]+1
|
|
|
|
return
|
2022-12-15 00:28:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AvailableNetworks returns all IPv4 networks that are available to the host
|
|
|
|
func AvailableNetworks() ([]Network, error) {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("getting interfaces info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
networks := []Network{}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
_, ipNet, err := net.ParseCIDR(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-16 02:11:43 +01:00
|
|
|
if ipNet.IP.IsGlobalUnicast() {
|
|
|
|
if ip := ipNet.IP.To4(); ip != nil {
|
|
|
|
// FIXME We assume mask /24. This is a reasonable assumption performance wise
|
|
|
|
// but may lead to inability to recognize some of the host in network
|
|
|
|
networks = append(networks, Network{nextIP(ipNet.IP).String(), 253})
|
|
|
|
}
|
2022-12-15 00:28:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return networks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
proto.HandshakeResponse
|
|
|
|
Address string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TCPHosts returns all TCP hosts that are in given networks on one of given ports
|
|
|
|
func TCPHosts(networks []Network, ports []uint16) <-chan Response {
|
2022-12-16 16:34:52 +01:00
|
|
|
ips := make(chan Response, 256)
|
2022-12-15 00:28:36 +01:00
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
log.Printf("tcphosts: %+v\n", networks)
|
2022-12-15 00:28:36 +01:00
|
|
|
|
2022-12-16 16:34:52 +01:00
|
|
|
wg := sync.WaitGroup{}
|
2022-12-15 00:28:36 +01:00
|
|
|
for _, network := range networks {
|
|
|
|
ip := net.ParseIP(network.FirstAddress)
|
|
|
|
for i := 0; i < network.MaxHostsCount; i++ {
|
|
|
|
for _, port := range ports {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(ip net.IP, port uint16) {
|
|
|
|
defer wg.Done()
|
|
|
|
target := fmt.Sprintf("%s:%d", ip, port)
|
|
|
|
var hs proto.HandshakeResponse
|
|
|
|
err := proto.CommandTimeout(target, proto.Handshake(), &hs, timeoutTCPHosts)
|
|
|
|
if err == nil {
|
|
|
|
ips <- Response{hs, target}
|
|
|
|
}
|
|
|
|
}(ip, port)
|
|
|
|
}
|
|
|
|
ip = nextIP(ip)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Wait()
|
|
|
|
close(ips)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ips
|
|
|
|
}
|