Class: Chocomint::Console::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/chocomint/console/server.rb

Overview

xterm.js のバックエンドとなる WebSocket サーバー。

WEBrick は WebSocket 非対応なので、コンソール用途だけ独立した TCP サーバーを 別スレッド・別ポートで動かす (既存の /edit・/logs HTTP は WEBrick のまま)。 接続ごとに ConPTY ワーカー (新規コンソール付き) を spawn し、ブラウザの キー入力を tty へ、tty 出力をブラウザへ双方向に橋渡しする。

セキュリティ: 生の対話 tty は監督外で任意コマンド実行が可能。bind は 127.0.0.1 固定。token を要求し (URL クエリ ?token=)、一致しなければ拒否する。

Defined Under Namespace

Classes: Connection

Instance Method Summary collapse

Constructor Details

#initialize(host: "127.0.0.1", port: 8081, command: ConPTY::DEFAULT_SHELL, cwd: nil, token: nil, logger: $stderr) ⇒ Server

command: 起動するシェル。cwd: 作業ディレクトリ。token: 接続に要求する共有秘密。



23
24
25
26
27
28
29
30
31
# File 'lib/chocomint/console/server.rb', line 23

def initialize(host: "127.0.0.1", port: 8081, command: ConPTY::DEFAULT_SHELL,
               cwd: nil, token: nil, logger: $stderr)
  @host = host
  @port = port
  @command = command
  @cwd = cwd
  @token = token
  @logger = logger
end

Instance Method Details

#startObject

ブロッキングで待ち受ける。別スレッドで呼ぶ想定 (#start_in_thread)。



34
35
36
37
38
39
40
41
42
43
# File 'lib/chocomint/console/server.rb', line 34

def start
  server = TCPServer.new(@host, @port)
  @logger.puts("chocomint console (ws) listening on ws://#{@host}:#{@port}/")
  loop do
    socket = server.accept
    Thread.new(socket) { |s| handle_connection(s) }
  end
rescue IOError, Errno::EBADF
  # サーバーソケットが閉じられた (shutdown)。
end

#start_in_threadObject



45
46
47
# File 'lib/chocomint/console/server.rb', line 45

def start_in_thread
  Thread.new { start }
end