前言

写爬虫的时候迁移到群辉中发现各种不兼容,各种环境都会出问题,所以就有个本文。

搭建两个环境selenium/standalone-chromev2fly/v2fly-core

selenium

docker run -d -p 4444:4444 -p 7900:7900 selenium/standalone-chrome:latest

selenium的4444端口是API使用的,而7900是noNVC访问的,密码的话是secret,如果要看每个调用结果和堵塞情况,可以直接访问http://127.0.0.1:4444/ui/#查看API调用信息

v2ray

搭建v2ray的官方docker是v2fly/v2fly-core,这边注意的需要先配置一个config.json文件到本地的/text/Downloads文件目录中

{
"log": {
"access": "",
"loglevel": "info",
"error": ""
},
"inbounds": [
{
"protocol": "socks",
"settings": {
"udp": true,
"auth": "noauth"
},
"listen": "0.0.0.0",# 如果不开局域网访问就填127.0.0.1
"port": "1080"
},
{
"listen": "0.0.0.0",# 如果不开局域网访问就填127.0.0.1
"port": "1087",
"protocol": "http",
"settings": {
"timeout": 360
}
}
],
"outbounds": [
{
"settings": {
"vnext": [
{
"address": "11.00.11.99", #服务器地址
"port": 987, #服务器端口
"users": [
{
"security": "auto",
"level": 0,
"id": "xxxxxxxx-xxxxxx-4xxxxx-xxxx8-xxxxxx4532", #用户ID
"alterId": 0
}
]
}
]
},
"tag": "proxy",
"protocol": "vmess",
"streamSettings": {
"security": "none",
"network": "quic", #使用协议
"quicSettings": {
"key": "",
"security": "none",
"header": {
"type": "wechat-video" #伪装协议
}
}
},
"mux": {
"enabled": false,
"concurrency": 8
}
},
{
"protocol": "freedom",
"settings": {
"userLevel": 0,
"domainStrategy": "UseIP"
},
"tag": "direct"
},
{
"settings": {
"response": {
"type": "none"
}
},
"protocol": "blackhole",
"tag": "block"
}
],
"dns": {},
"routing": {
"rules": [],
"balancers": [],
"domainStrategy": "AsIs"
}
}

然后使用命令启动容器,我这边是socks用1080端口,而http使用1087端口

docker run -d --name v2ray -e TZ=Asia/Shanghai -v /text/Downloads:/etc/v2ray -p 1087:1087 -p 1080:1080 --restart always v2fly/v2fly-core run -c /etc/v2ray/config.json

接着启动容器中的v2ray

# 启动v2
docker container start v2ray
# 停止
docker container stop v2ray
# 重启
docker container restart v2ray
# 查看日志
docker container logs v2ray

如果出现问题或者更改了config.json文件的话,需要删除容器,然后重新启动

docker container stop v2ray
docker container rm v2ray

容器互通

如果要让selenium访问v2ray需要把两个环境放到一个docker的网络中

$ docker network ls
NETWORK ID NAME DRIVER SCOPE
672ada83978a bridge bridge local
42558ceae89b host host local
74f4c9550edf none null local

正常来说如果没有特殊设置他们会在同一个网络中,也就是bridge中。

使用docker exec -it 容器ID bash来进入两个容器中查看IP是否在一个网段中,进入容器后ip addr命令就可以查看。如果想要保险点,可以进入selenium的docker中使用下面命令来安装ping工具

apt-get update
apt-get install iputils-ping

最终使用python代码来验证是否访问成功

#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 配置日志
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s: %(message)s')


def InitializationRemoteChrome():
# Docker 容器中的 Selenium Hub 地址
selenium_hub_url = "http://127.0.0.1:4444/wd/hub"

# 设置 Chrome 浏览器选项
chrome_options = Options()
# Selenium 4.x 推荐的隐藏 WebDriver 痕迹方法
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
chrome_options.add_argument('--proxy-server=socks://172.17.0.3:1080') #v2ray中docker的IP
# 不加载图片
chrome_options.add_argument("--blink-settings=imagesEnabled=false")

# 其他常用参数
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--headless') # 无头模式

try:
# Selenium 4.x 远程连接方式
driver = webdriver.Remote(
command_executor=selenium_hub_url,
options=chrome_options
)

# 额外的浏览器设置
driver.implicitly_wait(10) # 隐式等待

logging.info("远程浏览器初始化成功")
return driver

except Exception as e:
logging.error(f"初始化远程浏览器失败:{e}")
return None


def main():
driver = None
try:
driver = InitializationRemoteChrome()

if driver:
# 设置窗口大小
driver.set_window_size(1920, 1080)

# 访问测试网站
driver.get('https://httpbin.org/ip')

# 打印页面源码和当前URL
logging.info(f"Page Source: {driver.page_source}")
logging.info(f"Current URL: {driver.current_url}")

# 获取IP信息
ip_element = driver.find_element("xpath", "//body")
logging.info(f"IP Info: {ip_element.text}")

except Exception as e:
logging.error(f"浏览器操作异常:{e}")

finally:
# 确保关闭浏览器
if driver:
driver.quit()


if __name__ == "__main__":
main()

最终返回结果就是你V2ray服务器的IP地址

qbittorrent

16881是里面的随机端口

docker run -d --name=qbittorrent -e PUID=1000 -e PGID=1000 -e TZ=Asia/Shanghai -p 8080:8080 -p 16881:16881 -p 16881:16881/udp -v C:\Users\ascotbe\Downloads\config:/config -v C:\Users\ascotbe\Downloads\downloads:/downloads linuxserver/qbittorrent

需要注意的是:

外网不能直接访问,会提示unauthorized。

修改qBittorrent.conf文件,增加一行WebUI\HostHeaderValidation=false关闭主机头验证就可以看到登陆界面