forked from x-dr/telegraph-Image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_58duihuan.js
135 lines (104 loc) · 3.98 KB
/
cf_58duihuan.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const handleRequest = async (request) => {
// 解析请求 URL
const url = new URL(request.url);
const ip = request.headers.get("x-forwarded-for") || request.headers.get("clientIP")
const asOrganization = request.cf.asOrganization || ""
if (url.pathname == "/") {
const html = await fetch("https://raw.githubusercontent.com/x-dr/telegraph-Image/main/img.html")
const page = await html.text()
return new Response(page, {
headers: {
"content-type": "text/html;charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*",
"ip": `Access cloudflare's ip:${ip}`
},
})
} else if (url.pathname == "/upload") {
const imgData = await getImgData(request);
return Response.json({
"url": imgData,
}, {
headers: { 'Content-Type': 'application/json' }
});
} else if (url.pathname == "/ip") {
return Response.json({
"ip": ip,
"asOrganization": asOrganization
}, {
headers: {
"content-type": "application/json;charset=UTF-8",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Headers": "*",
"Access-Control-Allow-Methods": "*",
"ip": ip
},
});
}
}
// 生成指定长度的随机字符串
function generateRandomString(length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
return Array.from({ length }, () => characters[Math.floor(Math.random() * characters.length)]).join('');
}
// 计算 MD5 哈希
async function calculateMD5Hash(nonce, timestamp) {
const secret = "fuck-your-mother-three-thousand-times-apes-not-kill-apes";
const data = `nonce=${nonce}×tamp=${timestamp}${secret}`;
// 将字符串编码为 ArrayBuffer
const encoder = new TextEncoder();
const dataBuffer = encoder.encode(data);
// 使用 Web Crypto API 计算 MD5 哈希
const hashBuffer = await crypto.subtle.digest('MD5', dataBuffer);
// 将 ArrayBuffer 转换为十六进制字符串
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashHex;
}
async function getImgData(request) {
// 生成随机字符串和时间戳
const nonce = generateRandomString(8);
const timestamp = String(Math.floor(new Date().getTime()));
// 计算 MD5 哈希
const acceptLocale = await calculateMD5Hash(nonce, timestamp);
// 构建请求头
const imgheaders = {
"Accept": "application/json, text/plain, */*",
"Accept-Language": "zh-CN,zh;q=0.9",
"Accept-Locale": acceptLocale,
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "cross-site",
};
// 解析请求体
const formData1 = await request.formData()
console.log(formData1.get('file'));
const formData = new FormData();
formData.append('nonce', nonce);
formData.append('timestamp', timestamp);
formData.append('file', formData1.get('file'));
const res_img = await fetch('https://api.weixinyanxuan.com/mall/api/img/upload', {
method: request.method,
headers: imgheaders,
body: formData,
});
const responseData = await res_img.json();
try {
if (responseData.code === 200) {
const _URL = responseData.data;
return _URL
} else {
return responseData.message
}
} catch (e) {
return "error"
}
}