Пример #1
0
func HandleSocket(w http.ResponseWriter, r *http.Request, ptyFunc func(string) *Pty) {
	websocket.Handler(func(conn *websocket.Conn) {
		var obj termMsg
		dec := json.NewDecoder(conn)
		err := dec.Decode(&obj)
		if err != nil {
			log.Println("hterm:", err)
			return
		}
		if obj.Args == nil || obj.Width == nil || obj.Height == nil {
			log.Println("hterm: no args")
			return
		}
		pty := ptyFunc(*obj.Args)
		pty.Size(*obj.Width, *obj.Height)
		go io.Copy(conn, pty)
		for {
			var obj termMsg
			err := dec.Decode(&obj)
			if err != nil {
				log.Println("hterm:", err)
				if err == io.EOF {
					log.Println("hterm: sending SIGHUP to process")
					pidForKill := fmt.Sprintf("-%d", pty.process.Pid)
					cmd := exec.Command("kill", "-1", pidForKill)
					cmd.Start()
					cmd.Wait()
					pty.Close()

					log.Println("hterm: waiting for exit")
					processState, err := pty.process.Wait()
					log.Println("hterm:", processState, err)
				}
				break
			}

			if obj.Width != nil && obj.Height != nil {
				pty.Size(*obj.Width, *obj.Height)
				continue
			}
			if obj.Data != nil {
				_, err = io.WriteString(pty, *obj.Data)
				if err != nil {
					log.Println("hterm:", err)
					break
				}
			}
		}
	}).ServeHTTP(w, r)
}
Пример #2
0
func HandleSocket(w http.ResponseWriter, r *http.Request, ptyFunc func(string) *Pty) {
	websocket.Handler(func(conn *websocket.Conn) {
		var obj termMsg
		dec := json.NewDecoder(conn)
		err := dec.Decode(&obj)
		if err != nil {
			log.Println("hterm:", err)
			return
		}
		if obj.Args == nil || obj.Width == nil || obj.Height == nil {
			log.Println("hterm: no args")
			return
		}
		pty := ptyFunc(*obj.Args)
		pty.Size(*obj.Width, *obj.Height)
		go io.Copy(conn, pty)
		for {
			var obj termMsg
			err := dec.Decode(&obj)
			if err != nil {
				log.Println("hterm:", err)
				break
			}

			if obj.Width != nil && obj.Height != nil {
				pty.Size(*obj.Width, *obj.Height)
				continue
			}
			if obj.Data != nil {
				_, err = io.WriteString(pty, *obj.Data)
				if err != nil {
					log.Println("hterm:", err)
					break
				}
			}
		}
	}).ServeHTTP(w, r)
}