Esempio n. 1
0
// GetExternalIPAddress ...
func GetExternalIPAddress() {
	start := time.Now()
	clients, _, totalErr := internetgateway1.NewWANIPConnection1Clients()
	if totalErr != nil {
		log.Printf("Total error retrieving IP clients")
		return
	}
	for _, client := range clients {
		if addr, err := client.GetExternalIPAddress(); err != nil {
			log.Printf("err getting address: %v", err)
		} else {
			log.Printf("IP address: %v", addr)
		}
	}
	// extIPClients := make([]GetExternalIPAddresser, len(clients))
	// for i, client := range clients {
	// 	if addr, err := client.GetExternalIPAddress(); err != nil {
	// 		log.Printf("err getting address: %v", err)
	// 	} else {
	// 		log.Printf("IP address: %v", addr)
	// 	}
	// 	extIPClients[i] = client
	// }
	// DisplayExternalIPResults(extIPClients, errors, err)
	log.Printf("took %v seconds", time.Since(start))
}
Esempio n. 2
0
// 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:
}
Esempio n. 3
0
// Discover scans the local network for routers and returns the first
// UPnP-enabled router it encounters.  It will try up to 3 times to find a
// router, sleeping a random duration between each attempt.  This is to
// mitigate a race condition with many callers attempting to discover
// simultaneously.
//
// TODO: if more than one client is found, only return those on the same
// subnet as the user?
func Discover() (*IGD, error) {
	maxTries := 3
	sleepMs, _ := crypto.RandIntn(5000)
	for try := 0; try < maxTries; try++ {
		time.Sleep(time.Millisecond * time.Duration(sleepMs))
		pppclients, _, _ := internetgateway1.NewWANPPPConnection1Clients()
		if len(pppclients) > 0 {
			return &IGD{pppclients[0]}, nil
		}
		ipclients, _, _ := internetgateway1.NewWANIPConnection1Clients()
		if len(ipclients) > 0 {
			return &IGD{ipclients[0]}, nil
		}
		sleepMs *= 2
	}
	return nil, errors.New("no UPnP-enabled gateway found")
}