forked from huin/goupnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
148 lines (136 loc) · 4.97 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package example_test
import (
"fmt"
"net/url"
"os"
"github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/internetgateway1"
"github.com/huin/goupnp/dcps/internetgateway2"
)
// Use discovered WANPPPConnection1 services to find external IP addresses.
func Example_WANPPPConnection1_GetExternalIPAddress() {
clients, errors, err := internetgateway1.NewWANPPPConnection1Clients()
extIPClients := make([]GetExternalIPAddresser, len(clients))
for i, client := range clients {
extIPClients[i] = client
}
DisplayExternalIPResults(extIPClients, errors, err)
// Output:
}
// Use discovered WANIPConnection services to find external IP addresses.
func Example_WANIPConnection_GetExternalIPAddress() {
clients, errors, err := internetgateway1.NewWANIPConnection1Clients()
extIPClients := make([]GetExternalIPAddresser, len(clients))
for i, client := range clients {
extIPClients[i] = client
}
DisplayExternalIPResults(extIPClients, errors, err)
// Output:
}
type GetExternalIPAddresser interface {
GetExternalIPAddress() (NewExternalIPAddress string, err error)
GetServiceClient() *goupnp.ServiceClient
}
func DisplayExternalIPResults(clients []GetExternalIPAddresser, errors []error, err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "Error discovering service with UPnP: ", err)
return
}
if len(errors) > 0 {
fmt.Fprintf(os.Stderr, "Error discovering %d services:\n", len(errors))
for _, err := range errors {
fmt.Println(" ", err)
}
}
fmt.Fprintf(os.Stderr, "Successfully discovered %d services:\n", len(clients))
for _, client := range clients {
device := &client.GetServiceClient().RootDevice.Device
fmt.Fprintln(os.Stderr, " Device:", device.FriendlyName)
if addr, err := client.GetExternalIPAddress(); err != nil {
fmt.Fprintf(os.Stderr, " Failed to get external IP address: %v\n", err)
} else {
fmt.Fprintf(os.Stderr, " External IP address: %v\n", addr)
}
}
}
func Example_ReuseDiscoveredDevice() {
var allMaybeRootDevices []goupnp.MaybeRootDevice
for _, urn := range []string{internetgateway1.URN_WANPPPConnection_1, internetgateway1.URN_WANIPConnection_1} {
maybeRootDevices, err := goupnp.DiscoverDevices(internetgateway1.URN_WANPPPConnection_1)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not discover %s devices: %v\n", urn, err)
}
allMaybeRootDevices = append(allMaybeRootDevices, maybeRootDevices...)
}
locations := make([]*url.URL, 0, len(allMaybeRootDevices))
fmt.Fprintf(os.Stderr, "Found %d devices:\n", len(allMaybeRootDevices))
for _, maybeRootDevice := range allMaybeRootDevices {
if maybeRootDevice.Err != nil {
fmt.Fprintln(os.Stderr, " Failed to probe device at ", maybeRootDevice.Location.String())
} else {
locations = append(locations, maybeRootDevice.Location)
fmt.Fprintln(os.Stderr, " Successfully probed device at ", maybeRootDevice.Location.String())
}
}
fmt.Fprintf(os.Stderr, "Attempt to re-acquire %d devices:\n", len(locations))
for _, location := range locations {
if _, err := goupnp.DeviceByURL(location); err != nil {
fmt.Fprintf(os.Stderr, " Failed to reacquire device at %s: %v\n", location.String(), err)
} else {
fmt.Fprintf(os.Stderr, " Successfully reacquired device at %s\n", location.String())
}
}
// Output:
}
// Use discovered igd1.WANCommonInterfaceConfig1 services to discover byte
// transfer counts.
func Example_WANCommonInterfaceConfig1_GetBytesTransferred() {
clients, errors, err := internetgateway1.NewWANCommonInterfaceConfig1Clients()
if err != nil {
fmt.Fprintln(os.Stderr, "Error discovering service with UPnP:", err)
return
}
fmt.Fprintf(os.Stderr, "Error discovering %d services:\n", len(errors))
for _, err := range errors {
fmt.Println(" ", err)
}
for _, client := range clients {
if recv, err := client.GetTotalBytesReceived(); err != nil {
fmt.Fprintln(os.Stderr, "Error requesting bytes received:", err)
} else {
fmt.Fprintln(os.Stderr, "Bytes received:", recv)
}
if sent, err := client.GetTotalBytesSent(); err != nil {
fmt.Fprintln(os.Stderr, "Error requesting bytes sent:", err)
} else {
fmt.Fprintln(os.Stderr, "Bytes sent:", sent)
}
}
// Output:
}
// Use discovered igd2.WANCommonInterfaceConfig1 services to discover byte
// transfer counts.
func Example_WANCommonInterfaceConfig2_GetBytesTransferred() {
clients, errors, err := internetgateway2.NewWANCommonInterfaceConfig1Clients()
if err != nil {
fmt.Fprintln(os.Stderr, "Error discovering service with UPnP:", err)
return
}
fmt.Fprintf(os.Stderr, "Error discovering %d services:\n", len(errors))
for _, err := range errors {
fmt.Println(" ", err)
}
for _, client := range clients {
if recv, err := client.GetTotalBytesReceived(); err != nil {
fmt.Fprintln(os.Stderr, "Error requesting bytes received:", err)
} else {
fmt.Fprintln(os.Stderr, "Bytes received:", recv)
}
if sent, err := client.GetTotalBytesSent(); err != nil {
fmt.Fprintln(os.Stderr, "Error requesting bytes sent:", err)
} else {
fmt.Fprintln(os.Stderr, "Bytes sent:", sent)
}
}
// Output:
}