
前言
写爬虫的时候迁移到群辉中发现各种不兼容,各种环境都会出问题,所以就有个本文。
搭建两个环境selenium/standalone-chrome和v2fly/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
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代码来验证是否访问成功
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(): selenium_hub_url = "http://127.0.0.1:4444/wd/hub"
chrome_options = Options() 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') 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: 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')
logging.info(f"Page Source: {driver.page_source}") logging.info(f"Current URL: {driver.current_url}")
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
关闭主机头验证就可以看到登陆界面