這個項目最初的靈感來源於平時操做無線路由器時想在網頁上執行一些命令,可是目前的開源的路由器系統(openwrt)沒有一個很好的網頁終端模擬器,最多也只是支持執行一個命令而後把結果顯示到網頁上,徹底沒有交互。有些路由器還不限制 top
之類的命令執行,一執行就可能會直接卡住網頁界面。前端
找了一下相似的程序,發現了 Wetty 和 GoTTY,可是 Wetty 須要 node 運行環境, GoTTY 編譯後的二進制文件太大,多數路由器的 flash 都比較小,不適合集成到路由器固件裏。node
一直想本身寫一個,上週末終於有時間開工,如今算是能夠用了,可是目前只實現了基本的 PTY 轉發和網頁上的顯示交互,還不夠完善。linux
https://github.com/tsl0922/ttydgit
mac 用戶能夠直接用 homebrew 安裝:brew install ttyd
,linux 下目前只能手工編譯安裝,具體步驟請查看項目的 README。github
ttyd is a tool for sharing terminal over the web USAGE: ttyd [options] <command> [<arguments...>] VERSION: 1.0.0 OPTIONS: --port, -p Port to listen (default: 7681) --interface, -i Network interface to bind --credential, -c Credential for Basic Authentication (format: username:password) --uid, -u User id to run with --gid, -g Group id to run with --signal, -s Signal to send to the command when exit it (default: SIGHUP) --reconnect, -r Time to reconnect for the client in seconds (default: 10) --ssl, -S Enable ssl --ssl-cert, -C Ssl certificate file path --ssl-key, -K Ssl key file path --ssl-ca, -A Ssl ca file path --debug, -d Set log level (0-9, default: 7) --version, -v Print the version and exit --help, -h Print this text and exit
截圖裏執行的命令是 bash
,若是想要登錄後才能使用能夠執行 login
,這樣網頁上就和 ssh 登錄同樣了。web
除了直接運行 shell 外,ttyd 還能夠有不少有趣的玩法,例如:docker
若是你想給網頁上的每一個人開個 docker 容器玩,能夠用下面的命令啓動 ttyd,這樣每次打開網頁都會進入 docker 容器內的 shell:shell
ttyd docker run -it --rm ubuntu bash
若是你想一打開網頁就能直接 ssh 到另外一個機器上(省去進入 bash 再執行 ssh),能夠這樣啓動 ttyd:ubuntu
ttyd ssh root@192.168.1.1
你甚至還能夠直接執行一個 vim,這樣打開進入網頁後就只有一個 vim 編輯器能夠操做:vim
ttyd vim
ttyd 基本上是 GoTTY 到 C 語言的移植,因此有一些能夠共用的部分是直接使用 GoTTY 裏的代碼的。
ttyd 的開發主要在 Mac 下進行,支持 macOS 和 Linux 系統,還提供了一個能夠給 OpenWrt 編譯出二進制文件的 Makefile,可是我還沒空在真機上測試,有興趣的能夠本身編譯出 ipk 包傳到路由器運行試試。