目录

Clash-Python环境搭建

最开始的目的是为了通过Docker搭建一个Python开发服务器,可以让Pycharm通过ssh连接实现远程开发, 第一步就踩坑,在win上时,通过镜像centos7构建的容器中,可以正常使用systemctl在后台启动ssh服务, 当在Mac(M)上时,就出现了诸如D-BUS的错误信息。 下一步就想使用clash为它搭建一个专有的代理,该代理还可为你的软路由、Nas等网络接入设备服务。 有了代理就还想为代理部署一个看板,这个看板是独立运行的,可以同时接入多个clash代理的服务。

参考地址:https://parrotsec-cn.org/t/linux-clash-dashboard/5169 此处提供的是将clash与看板部署在一起的方案。

构建镜像

centos7-python310

1
2
# 这一步花费时间会较长
docker build . -t centos7-python310

Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

# 使用基础镜像 CentOS 7
FROM --platform=$TARGETPLATFORM centos:centos7

# 维护者信息
LABEL maintainer="QiMington"

# 将本地的脚本文件复制到容器中
COPY ./init-sys-env.sh /init-sys-env.sh
COPY ./install-Python310.sh /install-Python310.sh
COPY ./install-git2.sh /install-git2.sh
COPY ./entrypoint.sh /entrypoint.sh

# 给脚本文件添加执行权限
RUN chmod +x /init-sys-env.sh
RUN chmod +x /install-Python310.sh
RUN chmod +x /entrypoint.sh
RUN chmod +x /install-git2.sh

# 在容器构建过程中运行脚本
RUN /init-sys-env.sh
RUN /install-Python310.sh
RUN /install-git2.sh

ENTRYPOINT ["/bin/bash", "/entrypoint.sh"]

entrypoint.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash

# 检查是否是第一次运行
if [ ! -e /root/.firstrun ]; then
    # 设置root密码
    # 检查是否存在环境变量ROOT_PASSWD
    if [ -z "${ROOT_PASSWD}" ]; then
        echo "environment 'ROOT_PASSWD' is required"
        exit 1
    fi
    echo "Setting root password..."
    echo "root:${ROOT_PASSWD}" | chpasswd
    # 记录状态以避免再次运行
    touch /root/.firstrun
    echo "Root password set to '${ROOT_PASSWD}'"
else
    echo "Root password already set."
fi

# exec /usr/sbin/init

# 将代理设置到全局
echo -e "export http_proxy=${HTTP_PROXY}\nexport https_proxy=${HTTPS_PROXY}" > "/etc/profile.d/proxy_settings.sh"

# 在默认位置(/etc/ssh)为SSH守护进程生成所有支持的主机密钥对
ssh-keygen -A
# 在终端以调试模式启动ssh, 目的是为了阻塞
/usr/sbin/sshd -D

init-sys.env.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 对系统进行升级
yum update -y
# 添加 End Point 到 CentOS 7 仓库
yum install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm -y
# 安装openssh-server
yum install openssh-server -y
# 安装需要用到的软件vim用于编辑文件、passwd用于设置root或其他用户的密码、openssh-clients用于让此容器可以使用ssh命令、net-tools我暂时只是用来查看ip
yum install vim passwd openssh-clients net-tools -y
## 启动ssh服务
#systemctl start sshd
## 将ssh服务加入到自启项
#systemctl enable sshd

install-Python310.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 安装编译所需依赖
yum -y update
yum -y install openssl-devel libffi-devel bzip2-devel
yum -y install git gcc zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl openssl-devel tk-devel libffi-devel xz-devel tar wget make patch
yum -y groupinstall "Development Tools"
# 安装 Python 3.10 必需的 openssl >= 1.1.1
wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz --no-check-certificate
tar zxf openssl-1.1.1q.tar.gz
cd openssl-1.1.1q
./config --prefix=/usr/local/openssl-1.1.1
make && make install
cd ..
rm -rf openssl-1.1.1q.tar.gz
rm -rf openssl-1.1.1q
# 安装Python 3.10
# 使用国内镜像: https://registry.npmmirror.com/binary.html?path=python
wget https://registry.npmmirror.com/-/binary/python/3.10.8/Python-3.10.8.tgz
# wget https://www.python.org/ftp/python/3.10.8/Python-3.10.8.tgz
tar zxf Python-3.10.8.tgz
cd Python-3.10.8
./configure --prefix=/opt/python/python3108 --enable-optimizations --with-openssl=/usr/local/openssl-1.1.1 --with-openssl-rpath=auto
make altinstall
cd ..
rm -rf Python-3.10.8.tgz
rm -rf Python-3.10.8

install-git2.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
yum -y remove git

yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

wget https://github.com/git/git/archive/refs/tags/v2.34.8.tar.gz

tar -xvf v2.34.8.tar.gz

rm -rf v2.34.8.tar.gz

cd git-2.34.8

make configure

./configure --prefix=/usr

make && make install

cd ..

rm -rf git-2.34.8

clash

1
docker build . -t clash

Dockerfile

1
FROM --platform=$TARGETPLATFORM dreamacro/clash:v1.16.0

这里需要的是通过该镜像运行容器中,位于/clash这个位置的文件,因为没在其他地方找到这位大佬的合适的作品,恰巧在此处发现了。可以自己将其拷贝出来进行保存。该文件的运行方式为

1
2
chmod +x ./clash
./clash -d .

clash-dashboard

1
docker build . -t clash-dashboard

Dockerfile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
FROM --platform=$TARGETPLATFORM nginx:latest

# RUN wget https://github.com/haishanh/yacd/releases/download/v0.3.7/yacd.tar.xz
# RUN tar -zxvf yacd.tar.xz
# RUN move public /dashboard
# dashboard文件夹为编译后的,可以通过脚本下载(如上),也可手动下载到本地(如下)进行一些魔改后再使用
# 也可通过源码自行打包 https://github.com/haishanh/yacd/
COPY ./dashboard /dashboard

COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./htpasswd /etc/nginx/htpasswd

EXPOSE 80

这里还为nginx代理提供了账户密码验证

` htpasswd ` 是一个用于管理基本身份验证(Basic Authentication)密码文件的命令行工具。
通过 ` htpasswd `,你可以创建、修改和删除密码文件中的用户及其密码。
它通常与 Web 服务器一起使用,用于对受密码保护的网页或服务进行访问控制。

default.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
server {
    listen       80;
    server_name  _;
    charset utf-8;

    location / {
        root /dashboard;
        # Basic Authentication
        auth_basic "Restricted Access";
        auth_basic_user_file /etc/nginx/htpasswd/.htpasswd;
    }

    error_page  404              /404.html;
    error_page  500 502 503 504  /50x.html;
}


nginx.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    client_max_body_size 15M;

    include /etc/nginx/conf.d/*.conf;
}

.htpasswd

1
2
qimington:$apr1$cx0nuAaX$6GIY3O/HdSRoKj0zUVVyF.
# 对应的明文账户密码为 qimington 123456

构建容器

1
docker compose up -d -f docker-compose.yaml -p dev-machine up -d

docker-compose.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
version: '3.1'

services:
  dashboard:
    image: qimington/personal:clash-dashboard
    restart: no
    ports:
      - 80:80
#    volumes:
#      - ./volumes/nginx/conf.d/api.conf:/etc/nginx/conf.d/api.conf
  clash:
    image: qimington/personal:clash
    restart: no
    logging:
      options:
        max-size: 1m
    volumes:
      - ./volumes/clash/data/config.yaml:/root/.config/clash/config.yaml
    ports:
      - "9090:9090"
#      - "7890:7890"
#      - "7891:7891"
  centos:
    image: qimington/personal:centos79-python310
    restart: no
    privileged: true
    environment:
      HTTP_PROXY: http://clash:7890
      HTTPS_PROXY: http://clash:7890
      ROOT_PASSWD: 12345
    ports:
      - "3232:22"
    depends_on:
      - clash
    stdin_open: true
    tty: true

clash的config.yaml需要自行获取,其中有一些端口以及服务相关的配置如下, 请确保external-controller: '0.0.0.0:9090'的端口被正确的映射出容器, 这样clash-dashboard才能成功的连接到这个clash服务的后台

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# HTTP 代理端口
port: 7890

# SOCKS5 代理端口
socks-port: 7891

# Linux 和 macOS 的 redir 代理端口
redir-port: 7892

# 允许局域网的连接
allow-lan: true

# 规则模式:Rule(规则) / Global(全局代理)/ Direct(全局直连)
mode: rule

# 设置日志输出级别 (默认级别:silent,即不输出任何内容,以避免因日志内容过大而导致程序内存溢出)。
# 5 个级别:silent / info / warning / error / debug。级别越高日志输出量越大,越倾向于调试,若需要请自行开启。
log-level: info
# Clash 的 RESTful API
external-controller: '0.0.0.0:9090'

# RESTful API 的口令
# secret: '7896'

# 您可以将静态网页资源(如 clash-dashboard)放置在一个目录中,clash 将会服务于 `RESTful API/ui`
# 参数应填写配置目录的相对路径或绝对路径。
# external-ui: folder

成功运行

问题

当连接ssh时,若出现下示信息

1
2
3
4
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!

请将~/.ssh/known_hosts中对应的连接记录删除后重新连接

成功

1
2
[root@9e5c0c043c83 ~]# echo $http_proxy
http://clash:7890