« get me outta code hell

TelnetInterfacer.js « util - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/util/TelnetInterfacer.js
blob: dc71157dda95fc1f31d65935c80f9674849dfb36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const ansi = require('./ansi')
const waitForData = require('./waitForData')
const EventEmitter = require('events')

module.exports = class TelnetInterfacer extends EventEmitter {
  constructor(socket) {
    super()

    this.socket = socket

    socket.on('data', buffer => {
      if (buffer[0] === 255) {
        this.handleTelnetData(buffer)
      } else {
        this.emit('inputData', buffer)
      }
    })

    this.initTelnetOptions()
  }

  initTelnetOptions() {
    // Initializes various socket options, using telnet magic.

    // Disables linemode.
    this.socket.write(Buffer.from([
      255, 253, 34,  // IAC DO LINEMODE
      255, 250, 34, 1, 0, 255, 240,  // IAC SB LINEMODE MODE 0 IAC SE
      255, 251, 1    // IAC WILL ECHO
    ]))

    // Will SGA. Helps with putty apparently.
    this.socket.write(Buffer.from([
      255, 251, 3  // IAC WILL SGA
    ]))

    this.socket.write(ansi.hideCursor())
  }

  cleanTelnetOptions() {
    // Resets the telnet options and magic set in initTelnetOptions.

    this.socket.write(ansi.resetAttributes())
    this.socket.write(ansi.showCursor())
  }

  async getScreenSize() {
    this.socket.write(Buffer.from([255, 253, 31])) // IAC DO NAWS

    let didWillNAWS = false
    let didSBNAWS = false
    let sb

    inputLoop: while (true) {
      const data = await waitForData(this.socket)

      for (const command of this.parseTelnetCommands(data)) {
        // WILL NAWS
        if (command[1] === 251 && command[2] === 31) {
          didWillNAWS = true
          continue
        }

        // SB NAWS
        if (didWillNAWS && command[1] === 250 && command[2] === 31) {
          didSBNAWS = true
          sb = command.slice(3)
          continue
        }

        // SE
        if (didSBNAWS && command[1] === 240) { // SE
          break inputLoop
        }
      }
    }

    return this.parseSBNAWS(sb)
  }

  parseTelnetCommands(buffer) {
    if (buffer[0] === 255) {
      // Telnet IAC (Is A Command) - ignore

      // Split the data into multiple IAC commands if more than one IAC was
      // sent.
      const values = Array.from(buffer.values())
      const commands = []
      const curCmd = [255]
      for (const value of values) {
        if (value === 255) { // IAC
          commands.push(Array.from(curCmd))
          curCmd.splice(1, curCmd.length)
          continue
        }
        curCmd.push(value)
      }
      commands.push(curCmd)

      return commands
    } else {
      return []
    }
  }

  write(data) {
    this.socket.write(data)
  }

  handleTelnetData(buffer) {
    let didSBNAWS = false
    let sbNAWS

    for (let command of this.parseTelnetCommands(buffer)) {
      // SB NAWS
      if (command[1] === 250 && command[2] === 31) {
        didSBNAWS = true
        sbNAWS = command.slice(3)
        continue
      }

      // SE
      if (didSBNAWS && command[1] === 240) { // SE
        didSBNAWS = false
        this.emit('screenSizeUpdated', this.parseSBNAWS(sbNAWS))
        this.emit('resize', this.parseSBNAWS(sbNAWS))
        continue
      }
    }
  }

  parseSBNAWS(sb) {
    const cols = (sb[0] << 8) + sb[1]
    const lines = (sb[2] << 8) + sb[3]

    return { cols, lines, width: cols, height: lines }
  }
}