TelnetInterface.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import EventEmitter from 'node:events'
  2. import * as ansi from '../ansi.js'
  3. import waitForData from '../waitForData.js'
  4. export default class TelnetInterface extends EventEmitter {
  5. constructor(socket) {
  6. super()
  7. this.socket = socket
  8. socket.on('data', buffer => {
  9. if (buffer[0] === 255) {
  10. this.handleTelnetData(buffer)
  11. } else {
  12. this.emit('inputData', buffer)
  13. }
  14. })
  15. this.initTelnetOptions()
  16. }
  17. initTelnetOptions() {
  18. // Initializes various socket options, using telnet magic.
  19. // Disables linemode.
  20. this.socket.write(Buffer.from([
  21. 255, 253, 34, // IAC DO LINEMODE
  22. 255, 250, 34, 1, 0, 255, 240, // IAC SB LINEMODE MODE 0 IAC SE
  23. 255, 251, 1 // IAC WILL ECHO
  24. ]))
  25. // Will SGA. Helps with putty apparently.
  26. this.socket.write(Buffer.from([
  27. 255, 251, 3 // IAC WILL SGA
  28. ]))
  29. this.socket.write(ansi.hideCursor())
  30. }
  31. cleanTelnetOptions() {
  32. // Resets the telnet options and magic set in initTelnetOptions.
  33. this.socket.write(ansi.resetAttributes())
  34. this.socket.write(ansi.showCursor())
  35. }
  36. async getScreenSize() {
  37. this.socket.write(Buffer.from([255, 253, 31])) // IAC DO NAWS
  38. let didWillNAWS = false
  39. let didSBNAWS = false
  40. let sb
  41. inputLoop: while (true) {
  42. const data = await waitForData(this.socket)
  43. for (const command of this.parseTelnetCommands(data)) {
  44. // WILL NAWS
  45. if (command[1] === 251 && command[2] === 31) {
  46. didWillNAWS = true
  47. continue
  48. }
  49. // SB NAWS
  50. if (didWillNAWS && command[1] === 250 && command[2] === 31) {
  51. didSBNAWS = true
  52. sb = command.slice(3)
  53. continue
  54. }
  55. // SE
  56. if (didSBNAWS && command[1] === 240) { // SE
  57. break inputLoop
  58. }
  59. }
  60. }
  61. return this.parseSBNAWS(sb)
  62. }
  63. parseTelnetCommands(buffer) {
  64. if (buffer[0] === 255) {
  65. // Telnet IAC (Is A Command) - ignore
  66. // Split the data into multiple IAC commands if more than one IAC was
  67. // sent.
  68. const values = Array.from(buffer.values())
  69. const commands = []
  70. const curCmd = [255]
  71. for (const value of values) {
  72. if (value === 255) { // IAC
  73. commands.push(Array.from(curCmd))
  74. curCmd.splice(1, curCmd.length)
  75. continue
  76. }
  77. curCmd.push(value)
  78. }
  79. commands.push(curCmd)
  80. return commands
  81. } else {
  82. return []
  83. }
  84. }
  85. write(data) {
  86. this.socket.write(data)
  87. }
  88. handleTelnetData(buffer) {
  89. let didSBNAWS = false
  90. let sbNAWS
  91. for (let command of this.parseTelnetCommands(buffer)) {
  92. // SB NAWS
  93. if (command[1] === 250 && command[2] === 31) {
  94. didSBNAWS = true
  95. sbNAWS = command.slice(3)
  96. continue
  97. }
  98. // SE
  99. if (didSBNAWS && command[1] === 240) { // SE
  100. didSBNAWS = false
  101. this.emit('screenSizeUpdated', this.parseSBNAWS(sbNAWS))
  102. this.emit('resize', this.parseSBNAWS(sbNAWS))
  103. continue
  104. }
  105. }
  106. }
  107. parseSBNAWS(sb) {
  108. const cols = (sb[0] << 8) + sb[1]
  109. const lines = (sb[2] << 8) + sb[3]
  110. return { cols, lines, width: cols, height: lines }
  111. }
  112. }