ttyd: terminal 终端分享工具

发布时间: 更新时间: 总字数:783 阅读时间:2m 作者:IP:上海 网址

ttyd是一个简单的命令行工具,用于通过网络共享终端。

介绍

ttyd是开源的,支持在网页上使用SSH终端的服务。

ttyd 接受到访问事件后,才创建对应的进行,如容器

特点

  • 构建于 Libwebsockets 之上,使用libuv以提高速度

  • 基于 Xterm.js 的全功能终端,支持CJK(中文、日文、韩文)和IME

  • 图形化的ZMODEM集成,支持 lrzsz

  • 基于OpenSSL的SSL支持

  • 运行任何带有选项的自定义命令

  • 支持基本认证(Basic Auth)和许多其他自定义选项

  • 跨平台:MacOS、Linux、FreeBSD/OpenBSD、OpenWrt、Windows

  • 使用案例

安装

二进制安装

# https://github.com/tsl0922/ttyd/releases
wget -O ttyd https://github.com/tsl0922/ttyd/releases/download/1.5.2/ttyd_linux.x86_64
chmod +x ttyd
mv ttyd /usr/local/bin/

# 查看版本
$ ttyd -v
ttyd version 1.5.2

Mac

brew install ttyd

OpenWrt

opkg install ttyd

源码安装

sudo apt-get install build-essential cmake git libjson-c-dev libwebsockets-dev
git clone https://github.com/tsl0922/ttyd.git
cd ttyd && mkdir build && cd build
cmake ..
make && make install

使用

help

ttyd--help ...

示例:运行bash

# 默认启动服务,监听在 7681 端口
$ ttyd bash

# --once 只接受一个客户端并在断开连接时退出
$ ttyd --once bash

# 认证信息
$ ttyd -c user:password bash

# 启动一个容器
$ ttyd --once docker run -it alpine:latest

# 设置超时时间,10后自动杀死进程
$ timeout 10 ttyd --once docker run -it alpine:latest

守护进程创建

  • /etc/systemd/system/ttyd@7681.service
$ cat /etc/systemd/system/ttyd@7681.service
[Unit]
Description=ttyd
After=network.target

[Service]
ExecStart=/usr/local/bin/ttyd -p 7681 bash

[Install]
WantedBy=multi-user.target
  • 启动服务
systemctl daemon-reload
systemctl start ttyd@7681.service
  • 查看日志
journalctl -u ttyd@7681.service

nginx 认证代理

server {
    listen 80;
    server_name _;

    location / {
        auth_basic "Please input password";
        auth_basic_user_file /etc/nginx/passwd;

        proxy_set_header X-WEBAUTH-USER $remote_user;
        proxy_set_header Authorization "";

        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # proxy_pass http://unix:/tmp/ttyd.sock;
        proxy_pass http://127.0.0.1:7681;
    }
}
  • nginx 代理多个端口示例
    location ~ ^/ttyd(.*)$ {
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_pass http://127.0.0.1:$1;
    }
本文总阅读量 次 本站总访问量 次 本站总访客数