예제 #1
0
파일: ssh.go 프로젝트: cooper/screenmgr
func deviceLoop(dev *device.Device) {
	// TODO: actually loop on connection drop

	if !dev.Info.SSH.Enabled {
		return
	}

	// create ssh config
	config := &ssh.ClientConfig{
		User: dev.Info.SSH.Username,
		Auth: authMethods(dev),
	}

	// dial
	// TODO: support other ports
	client, err := ssh.Dial("tcp", dev.Info.AddrString+":22", config)
	if err != nil {
		dev.Warn("ssh dial failed: %v", err)
		return
	}

	dev.Log("SSH connection established")

	// call initializer for this OS family
	family := dev.Info.Software["OSFamily"]
	if handler, exists := initializers[family]; exists {
		dev.Debug("initializing via ssh for OS family: %s", family)
		handler(sshClient{dev, client})
	}

}
예제 #2
0
파일: vnc.go 프로젝트: cooper/screenmgr
func handleVNCSnapshotOutput(dev *device.Device, line string) {
	dev.Debug("vncsnapshot: %s", line)
	found := screenshotRegexp.FindString(line)
	if len(found) == 0 {
		return
	}
	dev.LastScreenshot = found
	dev.Debug("updated screenshot: %s", found)
}
예제 #3
0
func osxHandleHardwareMap(dev *device.Device, hw map[string]interface{}) {
	for key, val := range hw {
		str, ok := val.(string)
		if !ok || key == "_name" {
			continue
		}

		// convert system profiler key to screenmgr key
		if newKey, exists := osxSystemProfilerMap[key]; exists {
			dev.Info.Hardware[newKey] = str
			dev.Debug("%s = %s", newKey, str)
		} else {
			dev.Warn("ignoring system profiler key '%s'", key)
		}

	}
}
예제 #4
0
파일: ping.go 프로젝트: cooper/screenmgr
func deviceLoop(dev *device.Device) {
	p := fastping.NewPinger()
	p.MaxRTT = 5 * time.Second
	p.Network("udp")

	// resolve IP
	// TODO: ipv6 support
	ra, err := net.ResolveIPAddr("ip4:icmp", dev.Info.AddrString)
	if err != nil {
		dev.Warn("ping couldn't resolve the IP address")
		return
	}
	p.AddIPAddr(ra)

	// on receive, update last receive time
	lastTime := time.Now()
	p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
		dev.Debug("received ICMP packet: addr=%v, rtt=%v", addr, rtt)

		if !dev.Online {
			dev.Log("now online")
			dev.Online = true
		}
		lastTime = time.Now()
	}

	// on idle, check if it's been a while
	p.OnIdle = func() {
		dev.Debug("ICMP idle")

		// it's been less than 10 seconds; no big deal
		if time.Since(lastTime) < 10*time.Second {
			return
		}

		if dev.Online {
			dev.Log("now offline")
			dev.Online = false
		}
	}

	// do this continuously
	// TODO: if the device is removed or ping is disabled, stop the loop.
	dev.Debug("starting ICMP loop")
	p.RunLoop()

}
예제 #5
0
파일: vnc.go 프로젝트: cooper/screenmgr
func deviceLoop(dev *device.Device) {

	// first check that it's enabled
	if !dev.Info.VNC.Enabled {
		dev.Warn("Attempted to start VNC loop, but VNC is disabled")
		return
	}

	// check that there's a password
	if len(dev.Info.VNC.Password) == 0 {
		dev.Warn("Attempted to start VNC loop, but there's no password")
		return
	}

	// create a passwd file
	passwd := dev.GetFilePath("vncpasswd")
	C.vncEncryptAndStorePasswd(C.CString(dev.Info.VNC.Password), C.CString(passwd))

	tryLater := func(errStr string) {
		dev.Warn(errStr + "; waiting 10 seconds")
		time.Sleep(10 * time.Second)
	}

	// this method will loop so long as the device is
	// configured to run VNC.
	dev.Debug("starting VNC loop")

VNCLoop:
	for dev.Info.VNC.Enabled {

		// not online
		if !dev.Online {
			time.Sleep(10 * time.Second)
			continue VNCLoop
		}

		cmd := exec.Command("vncsnapshot",
			"-passwd", passwd,
			"-fps", "5",
			// "-encodings", "raw",
			"-count", "50",
			dev.Info.AddrString,
			dev.GetFilePath("screenshots/screenshot.jpg"),
		)

		// get STDERR and make a scanner
		stderr, err := cmd.StderrPipe()
		if err != nil {
			tryLater("failed to get vncsnapshot STDERR pipe")
			continue VNCLoop
		}
		scanner := bufio.NewScanner(stderr)

		// start the command
		if err := cmd.Start(); err != nil {
			tryLater("vncsnapshot failed to start")
			continue VNCLoop
		}

		// read from the scanner
		for scanner.Scan() {
			handleVNCSnapshotOutput(dev, scanner.Text())
		}

		// scanner error
		if err := scanner.Err(); err != nil {
			tryLater("Scanner terminated with error: " + err.Error())
			continue VNCLoop
		}

		// vncsnapshot exited with non-zero status
		if err := cmd.Wait(); err != nil {
			tryLater("vncsnapshot exited: " + err.Error())
			continue VNCLoop
		}

	}

}