Exemplo n.º 1
0
func discoverUPNP_IG2() <-chan NAT {
	res := make(chan NAT, 1)
	go func() {

		// find devices
		devs, err := goupnp.DiscoverDevices(internetgateway2.URN_WANConnectionDevice_2)
		if err != nil {
			return
		}

		for _, dev := range devs {
			if dev.Root == nil {
				continue
			}

			dev.Root.Device.VisitServices(func(srv *goupnp.Service) {
				switch srv.ServiceType {
				case internetgateway2.URN_WANIPConnection_1:
					client := &internetgateway2.WANIPConnection1{ServiceClient: goupnp.ServiceClient{
						SOAPClient: srv.NewSOAPClient(),
						RootDevice: dev.Root,
						Service:    srv,
					}}
					_, isNat, err := client.GetNATRSIPStatus()
					if err == nil && isNat {
						res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-IP1)", dev.Root}
						return
					}

				case internetgateway2.URN_WANIPConnection_2:
					client := &internetgateway2.WANIPConnection2{ServiceClient: goupnp.ServiceClient{
						SOAPClient: srv.NewSOAPClient(),
						RootDevice: dev.Root,
						Service:    srv,
					}}
					_, isNat, err := client.GetNATRSIPStatus()
					if err == nil && isNat {
						res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-IP2)", dev.Root}
						return
					}

				case internetgateway2.URN_WANPPPConnection_1:
					client := &internetgateway2.WANPPPConnection1{ServiceClient: goupnp.ServiceClient{
						SOAPClient: srv.NewSOAPClient(),
						RootDevice: dev.Root,
						Service:    srv,
					}}
					_, isNat, err := client.GetNATRSIPStatus()
					if err == nil && isNat {
						res <- &upnp_NAT{client, make(map[int]int), "UPNP (IG2-PPP1)", dev.Root}
						return
					}

				}
			})
		}

	}()
	return res
}
Exemplo n.º 2
0
func findDevices() map[string]*goupnp.RootDevice {
	var (
		wg   = &sync.WaitGroup{}
		map1 = map[string]*goupnp.RootDevice{}
		map2 = map[string]*goupnp.RootDevice{}
		map3 = map[string]*goupnp.RootDevice{}
	)

	wg.Add(1)
	go func() {
		defer wg.Done()

		devs, err := goupnp.DiscoverDevices(internetgateway1.URN_WANConnectionDevice_1)
		if err != nil {
			return
		}

		for _, dev := range devs {
			if dev.Root != nil {
				map1[dev.Root.Device.SerialNumber] = dev.Root
			}
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()

		devs, err := goupnp.DiscoverDevices(internetgateway2.URN_WANConnectionDevice_1)
		if err != nil {
			log.Fatalf("error: %s", err)
		}

		for _, dev := range devs {
			if dev.Root != nil {
				map2[dev.Root.Device.SerialNumber] = dev.Root
			}
		}
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()

		devs, err := goupnp.DiscoverDevices(internetgateway2.URN_WANConnectionDevice_2)
		if err != nil {
			log.Fatalf("error: %s", err)
		}

		for _, dev := range devs {
			if dev.Root != nil {
				map3[dev.Root.Device.SerialNumber] = dev.Root
			}
		}
	}()

	wg.Wait()

	for id, dev := range map2 {
		if _, f := map1[id]; !f {
			map1[id] = dev
		}
	}

	for id, dev := range map3 {
		if _, f := map1[id]; !f {
			map1[id] = dev
		}
	}

	return map1
}