diff --git a/README.md b/README.md index a3252c0..47e9942 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,9 @@ # ttydBridge -基于 ttyd 的 Docker 容器 +基于 ttyd 绕过了隔离机制的 Docker 容器 -如一座“桥”,连通了宿主机环境,让你在浏览器中轻松访问和使用宿主机终端 +如一座“桥”,连通了宿主机环境,让你在浏览器中轻松使用宿主机终端 [![docker tag][docker-tag-image]][docker-url] [![docker pulls][docker-pulls-image]][docker-url] [![docker image size][docker-image-size-image]][docker-url] @@ -37,11 +37,16 @@ docker run -d \ ## 环境变量 -| 变量名 | 默认值 | 描述 | -| --- | -------- | ---- | -| `EXEC_PATH` | `/opt/ttyd` | 运行路径,目录必须配合卷映射,不懂勿改 | -| `START_COMMAND` | `login` | ttyd初始运行命令 | -| `PORT` | `2222` | 网页端口 | +| 变量名 | 默认值 | 描述 | +| ----------------------------- | ------- | ------------------------------------------ | +| `EXEC_DIR` | `/opt` | 运行目录,必须配合卷映射三处一致,不懂勿改 | +| `START_COMMAND` | `login` | ttyd初始运行命令 | +| `PORT` | `2222` | 网页端口 | +| `ALLOW_WRITE` | `true` | 允许写入 | +| `USERNAME` `PASSWORD` | | HTTP基础验证凭据,同时设置时生效 | +| `ENABLE_SSL` | `false` | 启用 SSL | +| `SSL_CERT` `SSL_KEY` `SSL_CA` | | 证书路径,当 ENABLE_SSL=true 时生效 | +| `ENABLE_IPV6` | `false` | 启用 IPv6 支持 | ## 赞助 @@ -51,4 +56,4 @@ docker run -d \ ## 感谢 -- [ttyd](https://github.com/tsl0922/ttyd): Share your terminal over the web +- [ttyd](https://github.com/tsl0922/ttyd) : Share your terminal over the web diff --git a/app/run.sh b/app/run.sh index 1473900..f6266c9 100644 --- a/app/run.sh +++ b/app/run.sh @@ -1,22 +1,80 @@ #!/bin/bash -exec_path=${EXEC_PATH:-"/opt/ttyd"} -port=${PORT:-2222} -start_command=${START_COMMAND:-login} +exec_dir=${EXEC_DIR:-"/opt"} +exec_path="$exec_dir/ttyd" +start_command=${START_COMMAND:-"login"} host_exists_ttyd=0 +# ttyd 选项 +# https://github.com/tsl0922/ttyd#command-line-options +ttyd_options=() + +# 监听端口 +port=${PORT:-2222} +ttyd_options+=(-p "$port") + +# 允许客户端写入TTY +allow_write=${ALLOW_WRITE:-"true"} +if [[ "$allow_write" == "true" ]]; then + ttyd_options+=("-W") +fi + +# 基本身份验证的凭据 +username="${USERNAME}" +password="${PASSWORD}" +if [[ -n "$username" && -n "$password" ]]; then + ttyd_options+=(-c "$username:$password") +fi + +# 启用SSL +enable_ssl=${ENABLE_SSL:-"false"} +ssl_cert="${SSL_CERT}" +ssl_key="${SSL_KEY}" +ssl_ca="${SSL_CA}" +if [[ "$enable_ssl" != "false" ]]; then + ttyd_options+=(-S) + if [[ -n "$ssl_cert" ]]; then + ttyd_options+=(-C "$ssl_cert") + fi + if [[ -n "$ssl_key" ]]; then + ttyd_options+=(-K "$ssl_key") + fi + if [[ -n "$ssl_ca" ]]; then + ttyd_options+=(-A "$ssl_ca") + fi +fi + +# 启用IPv6支持 +enable_ipv6=${ENABLE_IPV6:-"false"} +if [[ "$enable_ipv6" != "false" ]]; then + ttyd_options+=("-6") +fi + +# 其他自定义选项 +custom_options="${CUSTOM_OPTIONS}" +if [[ -n "$custom_options" ]]; then + ttyd_options+=("$custom_options") +fi + start() { echo "Starting..." + if [[ ! -d "$exec_dir" ]]; then + echo "Creating directory ${exec_dir}" + mkdir -p "$exec_dir" + fi if [[ ! -f "$exec_path" ]]; then cp /usr/bin/ttyd $exec_path + chmod +x $exec_path echo "Copy ttyd to $exec_path" else host_exists_ttyd=1 - echo "Host exists $exec_path" + echo "Host already exists $exec_path" fi - nsenter -m -u -i -n -p -t 1 sh -c "$exec_path -p $port -W $start_command" & + exec_command="$exec_path ${ttyd_options[*]} $start_command" + echo "ttyd startup options: $exec_command" + nsenter -m -u -i -n -p -t 1 sh -c "$exec_command" & echo "Keep Running..." while true; do