|
| 1 | +import { getRandomItems } from "./../utils/index"; |
| 2 | +import { CfIpResponse } from "../components/TestPage"; |
| 3 | +import axios from "axios"; |
| 4 | +import { from, mergeMap } from "rxjs"; |
| 5 | +import { CfIpListV4 } from "../constants/CfIpListV4"; |
| 6 | + |
| 7 | +const getCfResponseTestFile = (ip: string) => { |
| 8 | + return axios.request({ |
| 9 | + url: `http://${ip}`, |
| 10 | + headers: { |
| 11 | + "User-Agent": |
| 12 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", |
| 13 | + Host: "v2ray.xianshenglu.xyz", |
| 14 | + }, |
| 15 | + params: { |
| 16 | + v: Math.random(), |
| 17 | + }, |
| 18 | + timeout: 5 * 1000, |
| 19 | + }); |
| 20 | +}; |
| 21 | + |
| 22 | +const getCfDownloadTestFile = (ip: string) => { |
| 23 | + return axios.request<File>({ |
| 24 | + url: `http://${ip}/media/to-do-mvc.b2da547c.mp4`, |
| 25 | + headers: { |
| 26 | + "User-Agent": |
| 27 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36", |
| 28 | + Host: "v2ray.xianshenglu.xyz", |
| 29 | + }, |
| 30 | + params: { |
| 31 | + v: Math.random(), |
| 32 | + }, |
| 33 | + timeout: 30 * 1000, |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +const getCfNodeResponseTestTime = async (ip: string) => { |
| 38 | + const startTime = Date.now(); |
| 39 | + const result: CfIpResponse = { |
| 40 | + ip, |
| 41 | + meanRespond: 0, |
| 42 | + hasError: false, |
| 43 | + responseTestStatus: "PENDING", |
| 44 | + }; |
| 45 | + try { |
| 46 | + const response = await getCfResponseTestFile(ip); |
| 47 | + console.log(response.status, "response success"); |
| 48 | + |
| 49 | + result.responseTestStatus = "SUCCESS"; |
| 50 | + result.meanRespond = Date.now() - startTime; |
| 51 | + } catch (error) { |
| 52 | + result.hasError = true; |
| 53 | + result.responseTestStatus = "ERROR"; |
| 54 | + } |
| 55 | + return result; |
| 56 | +}; |
| 57 | +const getCfNodeDownloadTestTime = async (ip: string) => { |
| 58 | + const startTime = Date.now(); |
| 59 | + const result: CfIpResponse = { |
| 60 | + ip, |
| 61 | + meanDownloadSpeed: 0, |
| 62 | + hasError: false, |
| 63 | + speedTestStatus: "PENDING", |
| 64 | + }; |
| 65 | + try { |
| 66 | + await getCfResponseTestFile(ip); |
| 67 | + console.log("start download", ip); |
| 68 | + const { data: file } = await getCfDownloadTestFile(ip); |
| 69 | + console.log(file, "data file"); |
| 70 | + result.speedTestStatus = "SUCCESS"; |
| 71 | + const time = (Date.now() - startTime) / 1000; |
| 72 | + result.meanDownloadSpeed = Math.round(file.size / time); |
| 73 | + } catch (error) { |
| 74 | + console.log("download fail", error); |
| 75 | + |
| 76 | + result.hasError = true; |
| 77 | + result.speedTestStatus = "ERROR"; |
| 78 | + } |
| 79 | + return result; |
| 80 | +}; |
| 81 | +export const getCfNodesResponseTestTime = ( |
| 82 | + coCurrentNum: number, |
| 83 | + totalCount: number |
| 84 | +) => { |
| 85 | + const ipList = getRandomItems(CfIpListV4, totalCount); |
| 86 | + return from(ipList).pipe( |
| 87 | + mergeMap((ip) => { |
| 88 | + return getCfNodeResponseTestTime(ip); |
| 89 | + }, coCurrentNum) |
| 90 | + ); |
| 91 | +}; |
| 92 | +export const getCfNodesDownloadTestTime = ( |
| 93 | + coCurrentNum: number, |
| 94 | + totalCount: number |
| 95 | +) => { |
| 96 | + const ipList = getRandomItems(CfIpListV4, totalCount); |
| 97 | + return from(ipList).pipe( |
| 98 | + mergeMap((ip) => { |
| 99 | + return getCfNodeDownloadTestTime(ip); |
| 100 | + }, coCurrentNum) |
| 101 | + ); |
| 102 | +}; |
0 commit comments