Beispiel #1
0
func (v *vncKBPlayback) parseEvent(cmd string) (interface{}, error) {
	var e Event
	var res string
	var err error

	if e, err = vnc.ParseKeyEvent(cmd); err == nil {
		return e, err
	} else if e, err = vnc.ParsePointerEvent(cmd); err == nil {
		return e, err
	} else if res, err = ParseLoadFileEvent(cmd); err == nil {
		return res, err
	} else {
		return nil, errors.New("invalid event specified")
	}
}
Beispiel #2
0
func (v *vncKBPlayback) playFile() {
	scanner := bufio.NewScanner(v.file)

	for scanner.Scan() && v.err == nil {
		s := strings.SplitN(scanner.Text(), ":", 2)

		duration, err := time.ParseDuration(s[0] + "ns")
		if err != nil {
			log.Errorln(err)
			continue
		}

		wait := time.After(duration)
		select {
		case <-wait:
		case <-v.done:
			return
		}

		if res, err := vnc.ParseKeyEvent(s[1]); err == nil {
			v.err = res.Write(v.Conn)
		} else if res, err := vnc.ParsePointerEvent(s[1]); err == nil {
			v.err = res.Write(v.Conn)
		} else if file, err := ParseLoadFileEvent(s[1]); err == nil {
			if !filepath.IsAbs(file) {
				// Our file is in the same directory as the parent
				file = filepath.Join(filepath.Dir(v.file.Name()), file)
			}
			// Save the file we were working from
			oldfile := v.file
			// Load the new file
			v.file, err = os.Open(file)
			if err != nil {
				log.Error("Couldn't load VNC playback file %v: %v", file, err)
				v.err = err
			} else {
				r := &vncKBPlayback{v.vncClient}
				// We will wait until this file has played fully.
				r.playFile()
			}
			v.file = oldfile
		} else {
			log.Error("invalid vnc message: `%s`", s[1])
		}
	}
}
Beispiel #3
0
func (v *vncKBPlayback) Run() {
	err := (&vnc.SetEncodings{
		Encodings: []int32{vnc.CursorPseudoEncoding},
	}).Write(v.Conn)

	if err != nil {
		v.err = fmt.Errorf("unable to set encodings: %v", err)
		return
	}

	scanner := bufio.NewScanner(v.file)

	for scanner.Scan() && v.err == nil {
		s := strings.SplitN(scanner.Text(), ":", 2)

		duration, err := time.ParseDuration(s[0] + "ns")
		if err != nil {
			log.Errorln(err)
			continue
		}

		wait := time.After(duration)
		select {
		case <-wait:
		case <-v.done:
			return
		}

		if res, err := vnc.ParseKeyEvent(s[1]); err == nil {
			v.err = res.Write(v.Conn)
		} else if res, err := vnc.ParsePointerEvent(s[1]); err == nil {
			v.err = res.Write(v.Conn)
		} else {
			log.Error("invalid vnc message: `%s`", s[1])
		}
	}

	// Stop ourselves
	go v.Stop()

	// Block until we receive the done flag if we finished the playback
	<-v.done
	delete(vncKBPlaying, v.Rhost)
}