634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published
637 | by the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
662 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 
4 |
5 | # ttydBridge
6 |
7 | English | [简体中文](/README_CN.md)
8 |
9 | Docker containers based on ttyd bypass the isolation mechanism.
10 |
11 | A "bridge" to the host environment, allowing you to easily use the host terminal in your browser.
12 |
13 | [![docker tag][docker-tag-image]][github-url] [![docker pulls][docker-pulls-image]][docker-url] [![docker image size][docker-image-size-image]][docker-url]
14 |
15 | [docker-tag-image]: https://img.shields.io/docker/v/cp0204/ttydbridge
16 | [docker-pulls-image]: https://img.shields.io/docker/pulls/cp0204/ttydbridge
17 | [docker-image-size-image]: https://img.shields.io/docker/image-size/cp0204/ttydbridge
18 | [github-url]: https://github.com/Cp0204/ttydbridge
19 | [docker-url]: https://hub.docker.com/r/cp0204/ttydbridge
20 |
21 |
22 |
23 | ## Usage
24 |
25 | Run the container:
26 |
27 | ```shell
28 | docker run -d \
29 | --name ttydbridge \
30 | -e PORT=2222 \
31 | -v /opt:/opt \
32 | --pid host \
33 | --privileged \
34 | --restart unless-stopped \
35 | cp0204/ttydbridge:latest
36 | ```
37 |
38 | Access the web terminal via `http://yourhost:2222` and login with your system user.
39 |
40 | ## Environment
41 |
42 | | Name | Default | Description |
43 | | ------------------------------- | ------- | --------------------------------------------------------------------------------------------------------- |
44 | | `EXEC_DIR` | `/opt` | Program execution dir, must be consistent with volume mappings, **DO NOT MODIFY if you don't understand** |
45 | | `START_COMMAND` | `login` | ttyd initial command, `login` uses system authentication, `bash` enters directly |
46 | | `PORT` | `2222` | Web port |
47 | | `ALLOW_WRITE` | `true` | Allow terminal input |
48 | | `HTTP_USERNAME` `HTTP_PASSWORD` | | HTTP basic authentication, effective when set at the same time |
49 | | `ENABLE_SSL` | `false` | Enable SSL (https) |
50 | | `SSL_CERT` `SSL_KEY` `SSL_CA` | | Host certificate paths, effective when ENABLE_SSL=true |
51 | | `ENABLE_IPV6` | `false` | Enable IPv6 support |
52 | | `AUTO_ALLOW_PORT` | `false` | Automatically allow web ports |
53 |
54 | ## Donate
55 |
56 | Enjoyed the project? Consider buying me a coffee - it helps me keep going!
57 |
58 |
59 |
60 | 
61 |
62 | ## Thank
63 |
64 | - [ttyd](https://github.com/tsl0922/ttyd) : Share your terminal over the web
65 |
--------------------------------------------------------------------------------
/README_CN.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | 
4 |
5 | # ttydBridge
6 |
7 | [English](/README.md) | 简体中文
8 |
9 | 基于 ttyd 绕过了隔离机制的 Docker 容器
10 |
11 | 如一座“桥”,连通了宿主机环境,让你在浏览器中轻松使用宿主机终端
12 |
13 | [![docker tag][docker-tag-image]][github-url] [![docker pulls][docker-pulls-image]][docker-url] [![docker image size][docker-image-size-image]][docker-url]
14 |
15 | [docker-tag-image]: https://img.shields.io/docker/v/cp0204/ttydbridge
16 | [docker-pulls-image]: https://img.shields.io/docker/pulls/cp0204/ttydbridge
17 | [docker-image-size-image]: https://img.shields.io/docker/image-size/cp0204/ttydbridge
18 | [github-url]: https://github.com/Cp0204/ttydbridge
19 | [docker-url]: https://hub.docker.com/r/cp0204/ttydbridge
20 |
21 |
22 |
23 | ## 使用
24 |
25 | 运行容器:
26 |
27 | ```shell
28 | docker run -d \
29 | --name ttydbridge \
30 | -e PORT=2222 \
31 | -v /opt:/opt \
32 | --pid host \
33 | --privileged \
34 | --restart unless-stopped \
35 | cp0204/ttydbridge:latest
36 | ```
37 |
38 | 通过 `http://yourhost:2222` 访问网页终端,使用系统用户登录。
39 |
40 | ## 环境变量
41 |
42 | | 变量名 | 默认值 | 描述 |
43 | | ------------------------------- | ------- | -------------------------------------------------- |
44 | | `EXEC_DIR` | `/opt` | 程序运行目录,必须配合卷映射三处一致,**不懂勿改** |
45 | | `START_COMMAND` | `login` | ttyd 初始命令,`login`使用系统鉴权,`bash`直接进入 |
46 | | `PORT` | `2222` | 网页端口 |
47 | | `ALLOW_WRITE` | `true` | 允许终端输入 |
48 | | `HTTP_USERNAME` `HTTP_PASSWORD` | | HTTP基础认证,同时设置时生效 |
49 | | `ENABLE_SSL` | `false` | 启用 SSL (https) |
50 | | `SSL_CERT` `SSL_KEY` `SSL_CA` | | 主机证书路径,当 ENABLE_SSL=true 时生效 |
51 | | `ENABLE_IPV6` | `false` | 启用 IPv6 支持 |
52 | | `AUTO_ALLOW_PORT` | `false` | 自动放行网页端口 |
53 |
54 | ## 赞助
55 |
56 | 如果你觉得这个项目对你有帮助,可以给我一点点支持,非常感谢~
57 |
58 | 
59 |
60 | ## 感谢
61 |
62 | - [ttyd](https://github.com/tsl0922/ttyd) : Share your terminal over the web
63 |
--------------------------------------------------------------------------------
/app/run.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | exec_dir=${EXEC_DIR:-"/opt"}
4 | exec_path="$exec_dir/ttyd"
5 | start_command=${START_COMMAND:-"login"}
6 | host_exists_ttyd=0
7 | host_exists_iptables_rule=0
8 |
9 | # ttyd 选项
10 | # https://github.com/tsl0922/ttyd#command-line-options
11 | ttyd_options=()
12 |
13 | # 监听端口
14 | port=${PORT:-2222}
15 | ttyd_options+=(-p "$port")
16 |
17 | # 自动放行端口
18 | auto_allow_port=${AUTO_ALLOW_PORT:-"false"}
19 |
20 | # 允许客户端写入TTY
21 | allow_write=${ALLOW_WRITE:-"true"}
22 | if [[ "$allow_write" != "false" ]]; then
23 | ttyd_options+=("-W")
24 | fi
25 |
26 | # 基本身份验证的凭据
27 | http_username="${HTTP_USERNAME}"
28 | http_password="${HTTP_PASSWORD}"
29 | if [[ -n "$http_username" && -n "$http_password" ]]; then
30 | ttyd_options+=(-c "$http_username:$http_password")
31 | fi
32 |
33 | # 启用SSL
34 | enable_ssl=${ENABLE_SSL:-"false"}
35 | ssl_cert="${SSL_CERT}"
36 | ssl_key="${SSL_KEY}"
37 | ssl_ca="${SSL_CA}"
38 | if [[ "$enable_ssl" != "false" ]]; then
39 | ttyd_options+=(-S)
40 | if [[ -n "$ssl_cert" ]]; then
41 | ttyd_options+=(-C "$ssl_cert")
42 | fi
43 | if [[ -n "$ssl_key" ]]; then
44 | ttyd_options+=(-K "$ssl_key")
45 | fi
46 | if [[ -n "$ssl_ca" ]]; then
47 | ttyd_options+=(-A "$ssl_ca")
48 | fi
49 | fi
50 |
51 | # 启用IPv6支持
52 | enable_ipv6=${ENABLE_IPV6:-"false"}
53 | if [[ "$enable_ipv6" != "false" ]]; then
54 | ttyd_options+=("-6")
55 | fi
56 |
57 | # 其他自定义选项
58 | custom_options="${CUSTOM_OPTIONS}"
59 | if [[ -n "$custom_options" ]]; then
60 | ttyd_options+=("$custom_options")
61 | fi
62 |
63 | host_exec() {
64 | nsenter -m -u -i -n -p -t 1 sh -c "$1"
65 | }
66 |
67 | start() {
68 | echo "Starting..."
69 |
70 | distro=$(host_exec "grep '^PRETTY_NAME' /etc/os-release | awk -F '=' '{print \$2}' | tr -d '\"'")
71 | arch=$(host_exec "uname -m")
72 | echo "HostOS: $distro $arch"
73 |
74 | # Creating directory
75 | if [[ ! -d "$exec_dir" ]]; then
76 | echo "ExecDir: $exec_dir does not exist, creating"
77 | mkdir -p "$exec_dir"
78 | fi
79 | # Create executable
80 | if [[ ! -f "$exec_path" ]]; then
81 | echo "ttyd: Copy to $exec_path"
82 | cp /usr/bin/ttyd $exec_path
83 | else
84 | echo "ttyd: Host already exist $exec_path"
85 | host_exists_ttyd=1
86 | fi
87 | chmod +x $exec_path
88 |
89 | # auto allow port
90 | if [[ "$auto_allow_port" != "false" ]]; then
91 | port_check_error=$(
92 | host_exec "iptables -C INPUT -p tcp --dport $port -j ACCEPT" &>/dev/null
93 | echo $?
94 | )
95 | if [[ "$port_check_error" -eq 0 ]]; then
96 | echo "AutoAllowPort: Iptables rule $port exist"
97 | host_exists_iptables_rule=1
98 | else
99 | echo "AutoAllowPort: Iptables rule $port does not exist, auto allowing"
100 | host_exec "iptables -I INPUT -p tcp --dport $port -j ACCEPT"
101 | fi
102 | fi
103 |
104 | # exec
105 | exec_command="$exec_path ${ttyd_options[*]} $start_command"
106 | echo "ttyd startup command: $exec_command"
107 | host_exec "$exec_command" &
108 |
109 | echo "Keep Running..."
110 | while true; do
111 | sleep 1
112 | done
113 | }
114 |
115 | stop() {
116 | echo "Stopping..."
117 | if [[ -f "$exec_path" && $host_exists_ttyd -eq 0 ]]; then
118 | echo "ttyd: Cleanup $exec_path"
119 | rm "$exec_path"
120 |
121 | fi
122 | if [[ "$auto_allow_port" != "false" && $host_exists_iptables_rule -eq 0 ]]; then
123 | echo "AutoAllowPort: Delete iptables rule $port"
124 | host_exec "iptables -D INPUT -p tcp --dport $port -j ACCEPT"
125 | fi
126 | echo "Goodbye"
127 | echo ""
128 | exit 0
129 | }
130 |
131 | trap 'stop' SIGINT SIGTERM SIGQUIT SIGHUP
132 |
133 | case "$1" in
134 | start)
135 | start
136 | ;;
137 | stop)
138 | stop
139 | ;;
140 | *)
141 | echo "Invalid command. Supported commands: start, stop"
142 | exit 1
143 | ;;
144 | esac
145 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | version: "3.8"
2 | name: ttydbridge
3 | services:
4 | ttydbridge:
5 | # build: .
6 | # image: ttydbridge
7 | image: cp0204/ttydbridge:latest
8 | container_name: ttydbridge
9 | environment:
10 | EXEC_DIR: "/opt"
11 | START_COMMAND: "login"
12 | PORT: "2222"
13 | ALLOW_WRITE: "true"
14 | volumes:
15 | - /opt:/opt
16 | network_mode: bridge
17 | pid: host
18 | privileged: true
19 | restart: unless-stopped
20 |
--------------------------------------------------------------------------------
/img/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Cp0204/ttydBridge/b692b7941bf43675c9cbf6ff6897b7cf4dd1cc52/img/icon.png
--------------------------------------------------------------------------------