func (server *SshServer) handleChannel(user string, newChannel ssh.NewChannel) { if t := newChannel.ChannelType(); t != "session" { newChannel.Reject(ssh.UnknownChannelType, fmt.Sprintf("unknown channel type: %s", t)) return } connection, requests, err := newChannel.Accept() if err != nil { fmt.Printf("Could not accept channel (%s)", err) return } defer connection.Close() logfile, err := os.Create(fmt.Sprintf("%s_%s", user, storyLogFilename(server.story))) if err != nil { panic(err) } defer logfile.Close() logger := log.New(logfile, "", log.LstdFlags) terminal := terminal.NewTerminal(connection, "") zsshterm := &gork.ZSshTerminal{Term: terminal} zm, err := gork.NewZMachine(server.mem, server.header, zsshterm, logger) if err != nil { fmt.Println(err) return } go func() { for req := range requests { switch req.Type { case "shell": if len(req.Payload) == 0 { req.Reply(true, nil) } case "pty-req": termLen := req.Payload[3] w, h := parseDims(req.Payload[termLen+4:]) terminal.SetSize(w, h) case "window-change": w, h := parseDims(req.Payload) terminal.SetSize(w, h) } } }() defer func() { recover() }() zm.InterpretAll() }
func terminalUI(story string, mem *gork.ZMemory, header *gork.ZHeader) { logfile, err := os.Create(storyLogFilename(story)) if err != nil { panic(err) } defer logfile.Close() logger := log.New(logfile, "", log.LstdFlags) zm, err := gork.NewZMachine(mem, header, gork.ZTerminal{}, logger) if err != nil { panic(err) } if err := zm.InterpretAll(); err != nil { panic(err) } }
func (server *WSServer) run(addr string) { var upgrader = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true }, } wsHandler := func(w http.ResponseWriter, r *http.Request) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { fmt.Printf("Failed to upgrade %s\n", err) return } remoteAddr := conn.RemoteAddr().String() logFilename := storyLogFilename(server.story) logfile, err := os.Create(fmt.Sprintf("wsserver_%s_%s", remoteAddr, logFilename)) if err != nil { panic(err) } defer logfile.Close() logger := log.New(logfile, "", log.LstdFlags) wsdev := &gork.ZWSDev{Conn: conn} zm, err := gork.NewZMachine(server.mem, server.header, wsdev, logger) if err != nil { panic(err) } zm.InterpretAll() } http.HandleFunc("/play", wsHandler) err := http.ListenAndServe(addr, nil) if err != nil { panic(err) } }