This commit is contained in:
insanity 2018-08-24 14:41:07 +09:00
parent 14cd4345d7
commit df86e3cf66
5 changed files with 88 additions and 53 deletions

View File

@ -10,5 +10,5 @@
"go.testFlags": [
"-v",
],
"go.testTimeout": "300s"
"go.testTimeout": "100s"
}

View File

@ -1,51 +0,0 @@
package ipv4
import (
"testing"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
omu "git.loafle.net/overflow/model/util"
)
func TestHostScan(t *testing.T) {
z := &omd.Zone{
Network: "192.168.1.0/24",
Iface: "\\Device\\NPF_{1924FA2B-6927-4BA5-AF43-876C3F8853CE}",
Mac: "30:9C:23:15:A3:09",
Address: "192.168.1.201",
DiscoveredDate: omu.NowPtr(),
}
dp := &omd.DiscoverPort{
FirstScanRange: 1,
LastScanRange: 20000,
ExcludePorts: []int{
631,
},
IncludeTCP: true,
IncludeUDP: true,
DiscoverService: nil,
}
dh := &omd.DiscoverHost{
MetaIPType: omm.ToMetaIPType(omm.MetaIPTypeEnumV4),
FirstScanRange: "192.168.1.1",
LastScanRange: "192.168.1.255",
DiscoverPort: dp,
}
resCh := make(chan interface{})
errCh := make(chan error)
stopCh := make(chan struct{})
ScanHost(z, dh, resCh, errCh, stopCh)
for res := range resCh {
t.Log(res)
}
for err := range errCh {
t.Log(err)
}
}

View File

@ -17,7 +17,6 @@ func TestFindIfaces(t *testing.T) {
for _, d := range devices {
t.Log("\nName: ", d.Name)
t.Log("Description: ", d.Description)
t.Log("Devices addresses: ", d.Description)
for _, address := range d.Addresses {
t.Log("- IP address: ", address.IP)

72
ssdp/ssdp.go Normal file
View File

@ -0,0 +1,72 @@
package ssdp
import (
"encoding/xml"
"net/http"
"github.com/koron/go-ssdp" // MIT license
)
type SpecVersion struct {
Major int `xml:"major"`
Minor int `xml:"minor"`
}
type Icon struct {
MIMEType string `xml:"mimetype"`
Width int `xml:"width"`
Height int `xml:"height"`
Depth int `xml:"depth"`
URL string `xml:"url"`
}
type Device struct {
SpecVersion SpecVersion `xml:"specVersion"`
URLBase string `xml:"URLBase"`
DeviceType string `xml:"device>deviceType"`
FriendlyName string `xml:"device>friendlyName"`
Manufacturer string `xml:"device>manufacturer"`
ManufacturerURL string `xml:"device>manufacturerURL"`
ModelDescription string `xml:"device>modelDescription"`
ModelName string `xml:"device>modelName"`
ModelNumber string `xml:"device>modelNumber"`
ModelURL string `xml:"device>modelURL"`
SerialNumber string `xml:"device>serialNumber"`
UDN string `xml:"device>UDN"`
UPC string `xml:"device>UPC"`
PresentationURL string `xml:"device>presentationURL"`
Icons []Icon `xml:"device>iconList>icon"`
}
func SearchRootDevices() ([]Device, error) {
list, err := ssdp.Search(ssdp.RootDevice, 1, "")
if err != nil {
return nil, err
}
devices := make([]Device, 0, len(list))
for _, srv := range list {
device, err := parseResponse(srv.Location)
if err != nil {
continue
}
devices = append(devices, *device)
}
return devices, nil
}
func parseResponse(url string) (*Device, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
decoder := xml.NewDecoder(response.Body)
device := &Device{}
if err := decoder.Decode(device); err != nil {
return nil, err
}
return device, nil
}

15
ssdp/ssdp_test.go Normal file
View File

@ -0,0 +1,15 @@
package ssdp
import "testing"
func TestSSDP(t *testing.T) {
devices, err := SearchRootDevices()
if err != nil {
t.Error(err)
}
for _, device := range devices {
t.Log(device)
}
}
// http://192.168.1.254:44517/etc/linuxigd/gatedesc.xml