This commit is contained in:
crusader 2018-08-31 11:35:02 +09:00
parent be654c1723
commit f1c7315afb
7 changed files with 0 additions and 713 deletions

View File

@ -1,215 +0,0 @@
package mdns
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"sync/atomic"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/model"
"github.com/grandcat/zeroconf"
)
func Scan(discovered model.Discovered) {
serviceEntries, err := browse("_services._dns-sd._udp", "local")
if nil != err {
log.Print("Cannot find service ", err)
}
metaIPTypeEnum := omm.ToMetaIPTypeEnum(discovered.Zone().MetaIPType)
for _, serviceEntry := range serviceEntries {
name := removeDomainName(serviceEntry.Instance, serviceEntry.Domain)
entries, _err := browse(name, serviceEntry.Domain)
if nil != _err {
log.Print("Cannot find entry ", _err)
return
}
LOOP:
for _, entry := range entries {
log.Print("serviceEntry ", entry)
name := entry.Instance // HP\ LaserJet\ P1505n
service := entry.Service // _pdl-datastream._tcp
port := entry.Port // 9100
meta := toMeta(entry.Text)
hostName := removeDomainName(entry.HostName, entry.Domain) // NPIFACA9B
serviceName, portType, cryptoType := parseService(service)
var metaPortType *omm.MetaPortType
switch portType {
case "tcp":
metaPortType = omm.ToMetaPortType(omm.MetaPortTypeEnumTCP)
case "udp":
metaPortType = omm.ToMetaPortType(omm.MetaPortTypeEnumUDP)
}
metaCryptoType := omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumUNKNOWN)
switch cryptoType {
case "NONE":
metaCryptoType = omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumNONE)
case "TLS":
metaCryptoType = omm.ToMetaCryptoType(omm.MetaCryptoTypeEnumTLS)
}
switch metaIPTypeEnum {
case omm.MetaIPTypeEnumV4:
for _, ipv4 := range entry.AddrIPv4 {
h := discovered.AddHost(&omd.Host{
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName,
Address: ipv4.String(),
Meta: meta,
Zone: discovered.Zone(),
DiscoveredDate: omu.NowPtr(),
})
if 1 > port {
continue LOOP
}
p := discovered.AddPort(&omd.Port{
MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)),
Meta: meta,
Host: h,
})
discovered.AddService(&omd.Service{
MetaCryptoType: metaCryptoType,
Key: serviceName,
Name: name,
Port: p,
})
}
case omm.MetaIPTypeEnumV6:
for _, ipv6 := range entry.AddrIPv6 {
h := discovered.AddHost(&omd.Host{
MetaIPType: omm.ToMetaIPType(metaIPTypeEnum),
Name: hostName,
Address: ipv6.String(),
Meta: meta,
Zone: discovered.Zone(),
DiscoveredDate: omu.NowPtr(),
})
if 1 > port {
continue LOOP
}
p := discovered.AddPort(&omd.Port{
MetaPortType: metaPortType,
PortNumber: json.Number(strconv.Itoa(port)),
Meta: meta,
Host: h,
})
discovered.AddService(&omd.Service{
MetaCryptoType: metaCryptoType,
Key: serviceName,
Name: name,
Port: p,
})
}
}
}
}
}
func removeDomainName(instance string, domain string) (service string) {
_instance := strings.TrimRight(instance, ".")
return strings.TrimRight(_instance, fmt.Sprintf(".%s", domain))
}
func toMeta(text []string) map[string]string {
if nil == text || 0 == len(text) {
return nil
}
meta := make(map[string]string)
for _, v := range text {
ss := strings.SplitN(v, "=", 2)
if 2 != len(ss) {
continue
}
if nil != ss {
meta[ss[0]] = ss[1]
}
}
return meta
}
func parseService(service string) (name string, portType string, cryptoType string) {
ss := strings.SplitN(service, ".", 2)
if nil == ss {
return "UNKNOWN", "UNKNOWN", "UNKNOWN"
}
_name := strings.TrimLeft(ss[0], "_")
_portType := strings.TrimLeft(ss[1], "_")
name = _name
portType = _portType
cryptoType = "NONE"
return
}
func browse(service string, domain string) ([]*zeroconf.ServiceEntry, error) {
resolver, err := zeroconf.NewResolver(nil)
if err != nil {
return nil, fmt.Errorf("Failed to initialize resolver %v", err)
}
entryChan := make(chan *zeroconf.ServiceEntry)
timerStopped := make(chan struct{})
serviceEntries := make([]*zeroconf.ServiceEntry, 0)
go func(_entryChan <-chan *zeroconf.ServiceEntry) {
var delay atomic.Value
delay.Store(false)
ticker := time.NewTicker(time.Second * 1)
for {
select {
case entry, ok := <-_entryChan:
if !ok {
return
}
delay.Store(true)
serviceEntries = append(serviceEntries, entry)
case <-ticker.C:
if false == delay.Load().(bool) {
ticker.Stop()
timerStopped <- struct{}{}
return
}
delay.Store(false)
}
}
}(entryChan)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(30))
defer cancel()
err = resolver.Browse(ctx, service, domain, entryChan)
if err != nil {
return nil, fmt.Errorf("Failed to browse %v", err)
}
select {
case <-ctx.Done():
case <-timerStopped:
}
return serviceEntries, nil
}

View File

@ -1,139 +0,0 @@
package mdns
import (
"reflect"
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/model"
"github.com/grandcat/zeroconf"
)
func TestScan(t *testing.T) {
type args struct {
discovered model.Discovered
}
tests := []struct {
name string
args args
}{
// TODO: Add test cases.
{
name: "1",
args: args{
discovered: model.New(
&omd.Zone{
Network: "192.168.1.0/24",
Iface: "enp3s0",
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: "192.168.1.101",
Mac: "44:8a:5b:f1:f1:f3",
},
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Scan(tt.args.discovered)
})
}
}
func Test_removeDomainName(t *testing.T) {
type args struct {
instance string
domain string
}
tests := []struct {
name string
args args
wantService string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotService := removeDomainName(tt.args.instance, tt.args.domain); gotService != tt.wantService {
t.Errorf("removeDomainName() = %v, want %v", gotService, tt.wantService)
}
})
}
}
func Test_toMeta(t *testing.T) {
type args struct {
text []string
}
tests := []struct {
name string
args args
want map[string]string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := toMeta(tt.args.text); !reflect.DeepEqual(got, tt.want) {
t.Errorf("toMeta() = %v, want %v", got, tt.want)
}
})
}
}
func Test_parseService(t *testing.T) {
type args struct {
service string
}
tests := []struct {
name string
args args
wantName string
wantPortType string
wantCryptoType string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotName, gotPortType, gotCryptoType := parseService(tt.args.service)
if gotName != tt.wantName {
t.Errorf("parseService() gotName = %v, want %v", gotName, tt.wantName)
}
if gotPortType != tt.wantPortType {
t.Errorf("parseService() gotPortType = %v, want %v", gotPortType, tt.wantPortType)
}
if gotCryptoType != tt.wantCryptoType {
t.Errorf("parseService() gotCryptoType = %v, want %v", gotCryptoType, tt.wantCryptoType)
}
})
}
}
func Test_browse(t *testing.T) {
type args struct {
service string
domain string
}
tests := []struct {
name string
args args
want []*zeroconf.ServiceEntry
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := browse(tt.args.service, tt.args.domain)
if (err != nil) != tt.wantErr {
t.Errorf("browse() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("browse() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,69 +0,0 @@
package snmp
import (
"fmt"
omd "git.loafle.net/overflow/model/discovery"
"github.com/k-sone/snmpgo"
)
var defaultPort = 161
var defaultOIDs = []string{
"1.3.6.1.2.1.1.5.0", //sysName
}
type SNMPResponse struct {
Host *omd.Host
Value map[string]string
Error error
}
func ScanSNMP(host *omd.Host, community string, ch chan *SNMPResponse) {
go func() {
getV2(host, defaultPort, community, defaultOIDs, ch)
}()
}
func getV2(host *omd.Host, port int, community string, _oids []string, ch chan *SNMPResponse) {
address := fmt.Sprintf("%s:%d", host.Address, port)
snmp, err := snmpgo.NewSNMP(snmpgo.SNMPArguments{
Version: snmpgo.V2c,
Address: address,
Retries: 1,
Community: community,
})
if err != nil {
ch <- &SNMPResponse{host, nil, err}
return
}
defer snmp.Close()
oids, err := snmpgo.NewOids(_oids)
if err != nil {
ch <- &SNMPResponse{host, nil, err}
return
}
pdu, err := snmp.GetRequest(oids)
if err != nil {
ch <- &SNMPResponse{host, nil, err}
return
}
if pdu.ErrorStatus() != snmpgo.NoError {
ch <- &SNMPResponse{host, nil, fmt.Errorf("%s", pdu.ErrorStatus().String())}
return
}
if pdu == nil {
ch <- &SNMPResponse{host, nil, fmt.Errorf("%s", "Empty PDU")}
return
}
resMap := make(map[string]string)
for _, val := range pdu.VarBinds() {
resMap[val.Oid.String()] = val.Variable.String()
}
ch <- &SNMPResponse{host, resMap, nil}
return
}

View File

@ -1,23 +0,0 @@
package snmp
import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
)
func TestSNMPScan(t *testing.T) {
host := &omd.Host{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: "192.168.1.229",
}
ch := make(chan *SNMPResponse)
defer close(ch)
ScanSNMP(host, "test1252serc", ch)
msg := <-ch
t.Log(msg)
}

View File

@ -1,49 +0,0 @@
package upnp
import (
"fmt"
omd "git.loafle.net/overflow/model/discovery"
omu "git.loafle.net/overflow/model/util"
"git.loafle.net/overflow_scanner/probe/model"
"github.com/huin/goupnp"
)
const (
TargetRootDevice = "upnp:rootdevice"
TargetSSDPAll = "ssdp:all"
)
func Scan(discovered model.Discovered) {
devs, err := goupnp.DiscoverDevices(TargetRootDevice)
if nil != err {
fmt.Println("DeletePortMapping: ", err)
}
LOOP:
for _, dev := range devs {
rd := dev.Root.Device
if !rd.PresentationURL.Ok {
continue LOOP
}
discovered.AddHost(&omd.Host{
MetaIPType: discovered.Zone().MetaIPType,
Name: rd.FriendlyName,
Address: rd.PresentationURL.URL.Host,
Meta: map[string]string{
"DeviceType": rd.DeviceType,
"Manufacturer": rd.Manufacturer,
"ManufacturerURL": rd.ManufacturerURL.Str,
"ModelName": rd.ModelName,
"ModelDescription": rd.ModelDescription,
"ModelNumber": rd.ModelNumber,
"SerialNumber": rd.SerialNumber,
"UDN": rd.UDN,
"UPC": rd.UPC,
},
Zone: discovered.Zone(),
DiscoveredDate: omu.NowPtr(),
})
}
}

View File

@ -1,40 +0,0 @@
package upnp
import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
"git.loafle.net/overflow_scanner/probe/model"
)
func TestScan(t *testing.T) {
type args struct {
discovered model.Discovered
}
tests := []struct {
name string
args args
}{
// TODO: Add test cases.
{
name: "1",
args: args{
discovered: model.New(
&omd.Zone{
Network: "192.168.1.0/24",
Iface: "enp3s0",
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
Address: "192.168.1.101",
Mac: "44:8a:5b:f1:f1:f3",
},
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Scan(tt.args.discovered)
})
}
}

View File

@ -1,178 +0,0 @@
package model
import (
"encoding/json"
"fmt"
omd "git.loafle.net/overflow/model/discovery"
)
type Discovered interface {
Zone() *omd.Zone
AddHost(host *omd.Host) *omd.Host
AddPort(port *omd.Port) *omd.Port
AddService(service *omd.Service) *omd.Service
}
func New(zone *omd.Zone) Discovered {
return &discovered{
zone: zone,
hosts: make(map[string]*omd.Host),
ports: make(map[*omd.Host]map[json.Number]map[string]*omd.Port),
services: make(map[*omd.Port]map[string]map[string]*omd.Service),
}
}
type discovered struct {
zone *omd.Zone
hosts map[string]*omd.Host
ports map[*omd.Host]map[json.Number]map[string]*omd.Port
services map[*omd.Port]map[string]map[string]*omd.Service
}
func (d *discovered) Zone() *omd.Zone {
return d.zone
}
func (d *discovered) AddHost(host *omd.Host) *omd.Host {
h := d.findHost(host, true)
if "" == h.Mac && "" != host.Mac {
h.Mac = host.Mac
}
h.Meta = d.appendMeta(h.Meta, host.Meta)
return h
}
func (d *discovered) AddPort(port *omd.Port) *omd.Port {
p := d.findPort(port, true)
p.Meta = d.appendMeta(p.Meta, port.Meta)
return p
}
func (d *discovered) AddService(service *omd.Service) *omd.Service {
s := d.findService(service, true)
s.Meta = d.appendMeta(s.Meta, service.Meta)
return s
}
func (d *discovered) findHost(host *omd.Host, add bool) *omd.Host {
h, ok := d.hosts[host.Address]
if !ok {
if !add {
return nil
}
d.hosts[host.Address] = host
h = host
}
return h
}
func (d *discovered) findPort(port *omd.Port, add bool) *omd.Port {
h := d.findHost(port.Host, false)
if nil == h {
return nil
}
hostPorts, ok := d.ports[h]
if !ok {
if !add {
return nil
}
d.ports[h] = make(map[json.Number]map[string]*omd.Port)
hostPorts = d.ports[h]
}
ports, ok := hostPorts[port.PortNumber]
if !ok {
if !add {
return nil
}
hostPorts[port.PortNumber] = make(map[string]*omd.Port)
ports = hostPorts[port.PortNumber]
}
p, ok := ports[port.MetaPortType.Key]
if !ok {
if !add {
return nil
}
ports[port.MetaPortType.Key] = port
p = ports[port.MetaPortType.Key]
}
return p
}
func (d *discovered) findService(service *omd.Service, add bool) *omd.Service {
p := d.findPort(service.Port, false)
if nil == p {
return nil
}
portServices, ok := d.services[p]
if !ok {
if !add {
return nil
}
d.services[p] = make(map[string]map[string]*omd.Service)
portServices = d.services[p]
}
services, ok := portServices[service.Key]
if !ok {
if !add {
return nil
}
portServices[service.Key] = make(map[string]*omd.Service)
services = portServices[service.Key]
}
s, ok := services[service.MetaCryptoType.Key]
if !ok {
if !add {
return nil
}
services[service.MetaCryptoType.Key] = service
s = services[service.MetaCryptoType.Key]
}
return s
}
func (d *discovered) appendMeta(oriMeta map[string]string, newMeta map[string]string) map[string]string {
if nil == newMeta {
return oriMeta
}
if nil == oriMeta {
return newMeta
}
LOOP:
for k, v := range oriMeta {
_v, _ok := oriMeta[k]
if !_ok {
oriMeta[k] = v
continue LOOP
}
if v == _v {
continue LOOP
}
oriMeta[k] = fmt.Sprintf("%s|||%s", _v, v)
}
return oriMeta
}