青龙API请求失败,请检查网络

青龙执行任务,如果执行任务时间过长就会黑ip,应对解决ip黑名单问题,利用代理来实现切换出网ip

本想着青龙面板得任务主要是nodejs环境居多,便想着搞一个nodejs代理,但是发现我自己还是喜欢python和go语言写得脚本较多, 然后是变成了修改linux代理,因为青龙是用docker容器跑得服务,有演变成修改docker代理,docker修改代理又有局部或者全局变量 最后演变成设置linux代理来解决其他得http或者https得请求,甭管您是nodejs还是python又或者go,总是要经过系统网络,那就修改 docker容器里面得linux得网络即可

选择clash得原因是支持api

clash代理

mkdir /opt/clash && cd $_
wget '订阅地址' -O config.yaml
docker run -d --name=clash -v "$PWD/config.yaml:/root/.config/clash/config.yaml" -p "7890:7890" -p "9090:9090" --restart=unless-stopped dreamacro/clash

通过api接口打开clash,下面是手机得两个网址(电脑是ubuntu火狐可以连接chrome连接不上安全问题,建议用火狐浏览器)ISSUE

建议clash配置修改成全局代理(主要是rules)

vi config.yaml
mixed-port: 7890
allow-lan: true
mode: global
log-level: info
external-controller: '0.0.0.0:9090'
# 把您的节点拷贝到下面并删除rules下面的全删掉
proxies:
  # Trojan
  - name: "trojan"
    type: trojan
    server: dev.dra-m.com
    port: 29895
    password: hV3Fwjyvig
    udp: true
    sni: dev.dra-m.com # 填写伪装域名
    #alpn:
    #   - h2
    #   - http/1.1
    skip-cert-verify: true

可以把订阅节点放在示例配置文件proxies里面

clash api调用(这里用curl演示,可以用go或者pyhon自动获取节点然后测速,并每小时切换节点地址)

设置代理(青龙)

打开青龙配置管理把下面拷贝到config.sh 把${CLASH_HOST}修改成自己的服务器ip

## diy_linux_proxy
CLASH_HOST="192.168.0.4"
CLASH_PORT="9090"
# 获取节点个数
NODE_LINE=$(curl --silent --location --header "Content-Type: application/json" --request GET "http://${CLASH_HOST}:${CLASH_PORT}/proxies" | jq -r ".proxies.GLOBAL.all[]" |wc -l)

## 随机获取一个节点
CHAINS=$(curl --silent --location --header "Content-Type: application/json" --request GET "http://${CLASH_HOST}:${CLASH_PORT}/proxies"|jq -r ".proxies.GLOBAL.all[]" |sed -n $[$RANDOM%$NODE_LINE]p)
echo -e "选择的节点是: ${CHAINS}"
curl --silent --location --request PUT "http://${CLASH_HOST}:${CLASH_PORT}/proxies/GLOBAL" \
--header 'Content-Type: application/json' \
--data-raw '{
	"name": "'"${CHAINS}"'"
}'

# 设置代理
export ALL_PROXY="socks5://${CLASH_HOST}:7890"
PROXY_CLAH=$(curl --silent --location --request GET 'https://cip.cc/')
echo -e "当前代理: $ALL_PROXY"
echo -e "开始查询公网IP: $PROXY_CLAH"

青龙测试日志如下

下载此文件

python测试代码

#!/usr/bin/env python3
# -*- coding:utf-8 _*-
"""
@Author:Myki
@Blog(个人博客地址): https://www.1nth.com
@WeChat Official Account(微信公众号):MykiFan
@Github:www.github.com  https://github.com/21ki
cron: 30 9 * * *
new Env('get_ip_public');
"""

import socket
import requests
import re
from sendNotify import send

title = ''
content = ''

class IP:
    @staticmethod
    def get_ip_public():
        """
        获取本机公网IP
        :return:
        """
        try:
            text = requests.get("http://txt.go.sohu.com/ip/soip").text
            ip_public = re.findall(r'\d+.\d+.\d+.\d+', text)[0]
            return ip_public
        except:
            return '0.0.0.0'

    @staticmethod
    def get_ip_local():
        """
        查询本机内网IP
        :return:
        """
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(('8.8.8.8', 80))
            ip = s.getsockname()[0]
        finally:
            s.close()

        return ip


if __name__ == '__main__':
    print("内网IP:{}".format(IP.get_ip_local()))
    print("外网IP:{}".format(IP.get_ip_public()))
    title= "get_ip_public"
    content = IP.get_ip_public()
    send(title, content) 

nojs测试代码

var request = require('request');
var options = {
    'method': 'GET',
    'url': 'https://api.ip.sb/ip',
    'headers': {
    }
};
request(options, function (error, response) {
    if (error) throw new Error(error);
    console.log(response.body);
});

如果喜欢用python自动切换节点如下,脚本临时写得没有写自动过滤延迟高得节点,谁写好可以推过来哈,没时间折腾,只是为了测试功能是否实现而已

#!/usr/bin/env python3
# -*- coding:utf-8 _*-
"""
@Author:Myki
@Blog(个人博客地址): https://www.1nth.com
@WeChat Official Account(微信公众号):MykiFan
@Github:www.github.com  https://github.com/21ki
cron: 50 23 * * *
new Env('jd_sync_ck')
"""

import os
import requests
import json
import random

payload = {}
headers = {}

# clash所在的服务器ip
clash_host = "192.168.1.61"
# clash ui对外暴露的端口
clash_port = "9090"



def get_proxies():
    url = "http://%s:%s/proxies" % (clash_host, clash_port)
    r = requests.request("GET", url, headers=headers, data=payload)
    print(r.text)
    item = json.loads(r.text)['proxies']['PROXY']['all']
    return item


def chains_delay():
    url = "http://%s:%s/proxies/trojan/delay?timeout=10000&url=http://www.gstatic.com/generate_204" % (
        clash_host, clash_port)
    r = requests.request("GET", url, headers=headers, data=payload)
    item = json.loads(r.text)['delay']
    print(item)


def selector_chains(chains):
    url = "http://%s:%s/proxies/PROXY" % (clash_host, clash_port)
    payload = json.dumps({
        "name": chains
    })
    headers = {
        'Content-Type': 'application/json'
    }
    r = requests.request("PUT", url, headers=headers, data=payload)
    print(r.text)


def main():
    if "clash_host" in os.environ and os.environ["clash_host"]:
        BARK = os.environ["clash_host"]
    if "clash_port" in os.environ and os.environ["clash_port"]:
        BARK = os.environ["clash_port"]

    chains_list = get_proxies()
    chains = random.choice(chains_list)
    print("本次选择的节点是 %s" % chains)
    # chains_delay()
    selector_chains(chains)


if __name__ == "__main__":
    main()

后记

临时测试了下,可以完美解决"青龙API请求失败,请检查网络"得问题,脚本等完善之后再放出来给大家用把.思路给大伙说了