Skip to content

Commit eeeb1c4

Browse files
committed
add support to forward tunneling to remote host.
bump up version number and update readmes
1 parent 8dcf982 commit eeeb1c4

File tree

5 files changed

+250
-105
lines changed

5 files changed

+250
-105
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Variables
22
IMAGE_NAME = oaklight/autossh-tunnel
33
IMAGE_TAG = latest
4-
ADDITIONAL_TAGS = v1.3.0
4+
ADDITIONAL_TAGS = v1.4.0
55
PLATFORMS = linux/amd64,linux/arm64/v8,linux/arm/v7,linux/arm/v6,linux/386,linux/ppc64le,linux/s390x,linux/riscv64
66

77
# Default target

README.md

Lines changed: 105 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,50 @@
22

33
[中文版](README.md) | [English](README_en.md)
44

5-
本项目提供了一个基于 Docker 的解决方案,使用 `autossh` 和 YAML 配置文件来管理 SSH 隧道。该设置允许您轻松地将远程端口映射到本地端口,从而方便地访问位于防火墙后的远程机器上的服务
5+
本项目提供了一个基于 Docker 的解决方案,使用 `autossh` 和 YAML 配置文件来管理 SSH 隧道。该设置允许您轻松地将 **本地服务通过 SSH 隧道暴露到远程服务器**,或将 **远程服务映射到本地端口**,从而方便地访问位于防火墙后的服务
66

77
## 目录
88

9-
* [功能](#功能)
10-
* [先决条件](#先决条件)
11-
* [发布版本](#发布版本)
12-
* [设置](#设置)
13-
+ [1. 克隆仓库](#1-克隆仓库)
14-
+ [2. 配置 SSH 密钥](#2-配置-ssh-密钥)
15-
+ [3. 配置 YAML 文件](#3-配置-yaml-文件)
16-
+ [4. 构建并运行 Docker 容器](#4-构建并运行-docker-容器)
17-
+ [5. 访问服务](#5-访问服务)
18-
* [自定义](#自定义)
19-
+ [添加更多隧道](#添加更多隧道)
20-
+ [修改 Dockerfile](#修改-dockerfile)
21-
+ [修改入口点脚本](#修改入口点脚本)
22-
* [动态 UID/GID 支持](#动态-uidgid-支持)
23-
* [故障排除](#故障排除)
24-
+ [SSH 密钥权限](#ssh-密钥权限)
25-
+ [Docker 权限](#docker-权限)
26-
+ [日志](#日志)
27-
* [许可证](#许可证)
28-
* [致谢](#致谢)
9+
- [功能](#功能)
10+
- [先决条件](#先决条件)
11+
- [发布版本](#发布版本)
12+
- [设置](#设置)
13+
- [1. 克隆仓库](#1-克隆仓库)
14+
- [2. 配置 SSH 密钥](#2-配置-ssh-密钥)
15+
- [3. 配置 YAML 文件](#3-配置-yaml-文件)
16+
- [4. 构建并运行 Docker 容器](#4-构建并运行-docker-容器)
17+
- [5. 访问服务](#5-访问服务)
18+
- [自定义](#自定义)
19+
- [添加更多隧道](#添加更多隧道)
20+
- [修改 Dockerfile](#修改-dockerfile)
21+
- [修改入口点脚本](#修改入口点脚本)
22+
- [动态 UID/GID 支持](#动态-uidgid-支持)
23+
- [安全注意事项](#安全注意事项)
24+
- [故障排除](#故障排除)
25+
- [SSH 密钥权限](#ssh-密钥权限)
26+
- [Docker 权限](#docker-权限)
27+
- [日志](#日志)
28+
- [许可证](#许可证)
29+
- [致谢](#致谢)
2930

3031
## 功能
3132

32-
* **Docker 化**:使用 Docker 封装环境,使其易于部署和管理。
33-
* **非 root 用户**:以非 root 用户运行容器,增强安全性。
34-
* **YAML 配置**:使用 `config.yaml` 文件定义多个 SSH 隧道映射。
35-
* **Autossh**:自动维护 SSH 连接,确保隧道保持活动状态。
36-
* **动态 UID/GID 支持**:通过 `PUID``PGID` 环境变量动态设置容器用户的 UID 和 GID,以匹配主机用户的权限。
37-
* **多架构支持**:现已支持所有 Alpine 的底层架构,包括 `linux/amd64``linux/arm64/v8``linux/arm/v7``linux/arm/v6``linux/386``linux/ppc64le``linux/s390x``linux/riscv64`
33+
- **Docker 化**:使用 Docker 封装环境,使其易于部署和管理。
34+
- **非 root 用户**:以非 root 用户运行容器,增强安全性。
35+
- **YAML 配置**:使用 `config.yaml` 文件定义多个 SSH 隧道映射。
36+
- **Autossh**:自动维护 SSH 连接,确保隧道保持活动状态。
37+
- **动态 UID/GID 支持**:通过 `PUID``PGID` 环境变量动态设置容器用户的 UID 和 GID,以匹配主机用户的权限。
38+
- **多架构支持**:现已支持所有 Alpine 的底层架构,包括 `linux/amd64``linux/arm64/v8``linux/arm/v7``linux/arm/v6``linux/386``linux/ppc64le``linux/s390x``linux/riscv64`
39+
- **灵活的方向配置**:支持将本地服务暴露到远程服务器(`local_to_remote`)或将远程服务映射到本地端口(`remote_to_local`)。
3840

3941
## 先决条件
4042

41-
* 本地机器上已安装 Docker 和 Docker Compose。
42-
* 已设置用于访问远程主机的 SSH 密钥。
43+
- 本地机器上已安装 Docker 和 Docker Compose。
44+
- 已设置用于访问远程主机的 SSH 密钥。
4345

4446
## 发布版本
4547

46-
我已将第一个版本发布到 Docker Hub。您可以通过以下链接访问该版本:
48+
我已将打包好的 docker image 发布到 Docker Hub。您可以通过以下链接访问该版本:
4749

4850
[Docker Hub 链接](https://hub.docker.com/r/oaklight/autossh-tunnel)
4951

@@ -56,8 +58,8 @@
5658
将此仓库克隆到您的本地机器:
5759

5860
```sh
59-
git clone https://github.com/yourusername/ssh-tunnel-manager.git
60-
cd ssh-tunnel-manager
61+
git clone https://github.com/Oaklight/autossh-tunnel-dockerized.git
62+
cd autossh-tunnel-dockerized
6163
```
6264

6365
### 2. 配置 SSH 密钥
@@ -66,21 +68,65 @@ cd ssh-tunnel-manager
6668

6769
### 3. 配置 YAML 文件
6870

69-
编辑 `config.yaml` 文件以定义您的 SSH 隧道映射。每个条目应指定远程主机、远程端口和本地端口
71+
编辑 `config.yaml` 文件以定义您的 SSH 隧道映射。每个条目应指定远程主机、远程端口、本地端口和方向(`local_to_remote``remote_to_local`
7072

7173
示例 `config.yaml.sample` (复制到 `config.yaml` 并进行必要的更改):
7274

7375
```yaml
7476
tunnels:
77+
# 将本地服务暴露到远程服务器
7578
- remote_host: "user@remote-host1"
79+
remote_port: 22323
80+
local_port: 18120
81+
direction: local_to_remote
82+
# 将远程服务映射到本地端口
83+
- remote_host: "user@remote-host2"
7684
remote_port: 8000
7785
local_port: 8001
78-
- remote_host: "user@remote-host2"
79-
remote_port: 9000
80-
local_port: 9001
86+
direction: remote_to_local
8187
# 根据需要添加更多隧道
8288
```
8389

90+
#### 高级配置:指定绑定地址
91+
92+
如果您希望将 **远程端口****本地服务** 绑定到特定 IP 地址,可以使用 `ip:port` 格式。
93+
94+
##### 1. **指定远程绑定地址**
95+
96+
将远程端口绑定到特定 IP 地址(例如 `192.168.45.130`):
97+
98+
```yaml
99+
tunnels:
100+
- remote_host: "user@remote-host1"
101+
remote_port: "192.168.45.130:22323" # 远程绑定到 192.168.45.130
102+
local_port: 18120 # 本地服务端口
103+
direction: local_to_remote
104+
```
105+
106+
##### 2. **指定本地绑定地址**
107+
108+
将本地服务绑定到特定 IP 地址(例如 `192.168.1.100`):
109+
110+
```yaml
111+
tunnels:
112+
- remote_host: "user@remote-host1"
113+
remote_port: 22323 # 远程端口
114+
local_port: "192.168.1.100:18120" # 本地绑定到 192.168.1.100
115+
direction: local_to_remote
116+
```
117+
118+
##### 3. **同时指定远程和本地绑定地址**
119+
120+
```yaml
121+
tunnels:
122+
- remote_host: "user@remote-host1"
123+
remote_port: "192.168.45.130:22323" # 远程绑定到 192.168.45.130
124+
local_port: "192.168.1.100:18120" # 本地绑定到 192.168.1.100
125+
direction: local_to_remote
126+
```
127+
128+
通过这种方式,您可以灵活地控制隧道绑定的 IP 地址,从而满足不同的网络环境和安全需求。
129+
84130
### 4. 构建并运行 Docker 容器
85131

86132
#### 使用 Dockerhub 发布版本
@@ -100,7 +146,7 @@ docker compose -f compose.dev.yaml up -d
100146

101147
### 5. 访问服务
102148

103-
容器运行后,您可以使用指定的本地端口访问本地机器上的服务。例如,如果您将 `remote-host1:8000` 映射到 `localhost:8001` ,则可以通过 `http://localhost:8001` 访问服务
149+
容器运行后,您可以通过远程服务器的指定端口(例如 `remote-host1:22323`)访问本地服务,或通过本地端口(例如 `localhost:8001`)访问远程服务
104150

105151
## 自定义
106152

@@ -112,6 +158,7 @@ docker compose -f compose.dev.yaml up -d
112158
- remote_host: "user@remote-host"
113159
remote_port: <remote_port>
114160
local_port: <local_port>
161+
direction: <local_to_remote 或 remote_to_local> (如不填写则默认: remote_to_local)
115162
```
116163

117164
### 修改 Dockerfile
@@ -149,6 +196,24 @@ docker run --net host -v ~/.ssh:/home/myuser/.ssh:ro -v ./config:/etc/autossh/co
149196

150197
您可以根据主机用户的 UID 和 GID 调整 `PUID` 和 `PGID` 的值,以确保容器能够正确访问主机的 `.ssh` 目录。
151198

199+
## 安全注意事项
200+
201+
在启用 `-R` 参数时,远程端口默认绑定到 `localhost`。如果希望通过远程服务器的其他 IP 地址访问隧道,需在远程服务器的 `sshd_config` 中启用 `GatewayPorts` 选项:
202+
203+
```bash
204+
# 编辑 /etc/ssh/sshd_config
205+
GatewayPorts clientspecified # 允许客户端指定绑定地址
206+
GatewayPorts yes # 或绑定到所有网络接口
207+
```
208+
209+
重启 SSH 服务:
210+
211+
```bash
212+
sudo systemctl restart sshd
213+
```
214+
215+
启用 `GatewayPorts` 可能会暴露服务到公网,请确保采取适当的安全措施,例如配置防火墙或启用访问控制。
216+
152217
## 故障排除
153218

154219
### SSH 密钥权限
@@ -182,9 +247,9 @@ docker compose logs -f
182247

183248
## 致谢
184249

185-
* [autossh](http://www.harding.motd.ca/autossh/) 用于维护 SSH 连接。
186-
* [yq](https://github.com/mikefarah/yq) 用于解析 YAML 配置文件。
187-
* [Docker](https://www.docker.com/) 用于容器化。
250+
- [autossh](http://www.harding.motd.ca/autossh/) 用于维护 SSH 连接。
251+
- [yq](https://github.com/mikefarah/yq) 用于解析 YAML 配置文件。
252+
- [Docker](https://www.docker.com/) 用于容器化。
188253

189254
---
190255

0 commit comments

Comments
 (0)