【Linux】mihomo代理

mihomo

阿里云服务器每次访问外网都超时,之前安装工具都是先下载到本地再上传到服务器。前些天复现漏洞下个镜像拖来拖去的,实在是忍无可忍了。网上看了各种文章,尝试了各种方法,最后使用了mihomo代理成功访问外网。

mihomo安装

  1. mihomo下载,github地址:https://github.com/MetaCubeX/mihomo

1
2
3
4
1. 因为没法访问外网,要先下载到本地,再传到服务器。
2. gzip -d mihomo.gz #解压缩
3. mv mihomo /usr/local/bin/mihomo #将 mihomo 移动到 /usr/local/bin/ 目录:
4. sudo chmod +x /usr/local/bin/mihomo #设置可执行权限

经过上述步骤以后,要在为mihomo添加配置文件config.yaml和Country.mmdb。Country.mmdb我是在在github上找的。

2.创建 systemd 配置文件 /etc/systemd/system/mihomo.service,并添加如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=mihomo Daemon, Another Clash Kernel.
After=network.target NetworkManager.service systemd-networkd.service iwd.service

[Service]
Type=simple
LimitNPROC=500
LimitNOFILE=1000000
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_RAW CAP_NET_BIND_SERVICE CAP_SYS_TIME CAP_SYS_PTRACE CAP_DAC_READ_SEARCH CAP_DAC_OVERRIDE
Restart=always
ExecStartPre=/usr/bin/sleep 1s
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
  1. 创建配置文件。mihomo和clash meta用的是相同的内核,因此这个配置文件和我本地Windows系统用的clash meta是一样的。我创建的配置文件在/etc/mihomo/config.yaml。部分细节如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[11:42:20 root@rocky95 ~]# head -n 20 /etc/mihomo/config.yaml 
mixed-port: 7890
allow-lan: true
bind-address: '*'
mode: rule
log-level: info
external-controller: '127.0.0.1:9090'
unified-delay: true
tcp-concurrent: true
dns:
enable: true
ipv6: false
default-nameserver: [223.5.5.5, 119.29.29.29]
enhanced-mode: fake-ip
fake-ip-range: 198.18.0.1/16
use-hosts: true
nameserver: ['https://doh.pub/dns-query', 'https://dns.alidns.com/dns-query']
fallback: ['https://doh.dns.sb/dns-query', 'https://dns.cloudflare.com/dns-query', 'https://dns.twnic.tw/dns-query', 'tls://8.8.4.4:853']
fallback-filter: { geoip: true, ipcidr: [240.0.0.0/4, 0.0.0.0/32] }
proxies:
- { name: '剩余流量:918.16 GB', type: vless, server: pq.aws48.yydjc.top, port: 443, uuid: 6806220d-2952-4fab-8617-ea56feab45b1, udp: true, tls: true, skip-cert-verify: false, flow: xtls-rprx-vision, client-fingerprint: chrome, servername: buylite.tv.apple.com, reality-opts: { public-key: 6T-kYBf65ERaEAhxIyHL1FCfu0QR6P2XQMtcvUgzSjM, short-id: 70ad150d } }
  1. 重启systemd

1
systemctl daemon-reload
  1. 启用 mihomo 服务:

1
systemctl enable mihomo

mihomo使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1. 启用 mihomo 服务:
systemctl enable mihomo
2. 立即启动 mihomo:
systemctl start mihomo
3. 重新加载mihomo
systemctl reload mihomo
4. 检查 mihomo 的运行状况
systemctl status mihomo
5. 检查 mihomo 的运行日志
journalctl -u mihomo -o cat -e

journalctl -u mihomo -o cat -f
6. 关闭 mihomo 服务:
systemctl disable mihomo

为Linux设置代理

临时启用和关闭代理

在使用Linux时,临时启用代理的命令:

1
2
export http_proxy=http://ip:port
export https_proxy=http://ip:port

ip和port为你的代理服务器的ip以及开放的端口。

取消代理:

1
2
unset http_proxy
unset https_proxy

永久全局代理

永久全局代理将临时启用代理的命令添加至系统配置文件中,source刷新shell环境即可。

1
2
3
4
5
6
vim /etc/profile
……
export http_proxy=http://ip:port
export https_proxy=https://ip:port
……
source /etc/profile

设置代理的基本语法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
`环境变量
http_proxy:为http变量设置代理;默认不填开头以http协议传输
# 示例
`以下是常见的基本语法
http_proxy=ip:port
http_proxy=http://ip:port
http_proxy=socks4://ip:port
http_proxy=socks5://ip:port

`如果不想设置白名单,也可以使用用户名和密码进行验证
http_proxy=http://username:password@ip:port
http_proxy=http://username:password@ip:port

https_proxy:为https设置代理
ftp_proxy:为ftp设置代理
all_proxy:全部变量设置代理,设置了这个的时候上面不需要设置
no_proxy:无需代理的主机或域名;可以使用通配符,多个时使用","号分隔
# 示例:
*.aiezu.com,10.*.*.*,192.168.*.*
*.local,localhost,127.0.0.1

部署Web控制面板

部署好了mihomo代理以后,在虚拟机上切换节点、重载配置等较为不便,可以为mihomo部署控制面板,方便管理。但如果服务器暴露在公网中,使用Web面板会很不安全。因此我只在需要调试的时候会把Web面板打开。部署控制面板的操作如下:

  1. 在config.yaml中添加或修改如下配置:

1
2
external-controller: '0.0.0.0:7891'
external-ui: /etc/mihomo/ui
  1. 从github下载

1
sudo git clone https://github.com/metacubex/metacubexd.git -b gh-pages /etc/mihomo/ui
  1. 重启mihomo服务

1
sudo systemctl restart mihomo

本地访问http://ip:port/ui就可以访问Web面板了

注意:虽然查看该面板需要密码,但是密码随机输入即可,并不具备安全性

docker使用网络代理

gitbook上有一篇docker的详解:Docker — 从入门到实践

  • 为 dockerd 创建配置文件夹。

1
sudo mkdir -p /etc/systemd/system/docker.service.d
  • 为 dockerd 创建 HTTP/HTTPS 网络代理的配置文件,文件路径是 /etc/systemd/system/docker.service.d/http-proxy.conf 。并在该文件中添加相关环境变量。

1
2
3
4
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080/"
Environment="HTTPS_PROXY=http://proxy.example.com:8080/"
Environment="NO_PROXY=localhost,127.0.0.1,.example.com"
  • 刷新配置并重启 docker 服务。

1
2
sudo systemctl daemon-reload
sudo systemctl restart docker

客户端使用代理

因为代理是部署在服务器的,因此自己的手机、电脑、平板等也可以使用服务器的代理。但是要修改配置文件allow-lan改为true。

1
allow-lan: true

遇到的问题:

  1. 网上说mihomo的配置文件和clash是一样的,因此要把订阅链接转换成clash的订阅链接,可是我转换后并不能成功使用。经过进一步的了解得知mihomo是clash meta的更新,随后转成meta链接成功(在订阅链接后加&flag=meta)。

  2. 为docker更换源、使用阿里加速器。然而尝试了各种源均没有什么卵用,只有个别镜像拉取成功,并且就算docker配置成功了,我还是没办法直接下载github的资源。

  3. 使用clash代理,然而我花好久终于要整好的时候,clash并不支持我的配置文件中type: hysteria2,也就是不支持hysteria2协议。一时间手足无措,最后在一篇文章里看到mihomo支持,最后配置成功。

本文参考:

Docker — 从入门到实践

-------------本文结束感谢阅读-------------
创作不易,您的支持将鼓励我继续创作!