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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
| addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) })
addEventListener('scheduled', event => { event.waitUntil(handleScheduled(event.scheduledTime)) })
async function handleRequest(request, env) { const url = new URL(request.url)
if (url.pathname === '/login' && request.method === 'POST') { const formData = await request.formData() const password = formData.get('password')
if (password === PASSWORD) { const response = new Response(JSON.stringify({ success: true }), { headers: { 'Content-Type': 'application/json' } }) response.headers.set('Set-Cookie', `auth=${PASSWORD}; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400`) return response } else { return new Response(JSON.stringify({ success: false }), { headers: { 'Content-Type': 'application/json' } }) } } else if (url.pathname === '/run' && request.method === 'POST') { if (!isAuthenticated(request)) { return new Response('Unauthorized', { status: 401 }) }
await handleScheduled(new Date().toISOString()) const results = await CRON_RESULTS.get('lastResults', 'json') return new Response(JSON.stringify(results), { headers: { 'Content-Type': 'application/json' } }) } else if (url.pathname === '/results' && request.method === 'GET') { if (!isAuthenticated(request)) { return new Response(JSON.stringify({ authenticated: false }), { headers: { 'Content-Type': 'application/json' } }) } const results = await CRON_RESULTS.get('lastResults', 'json') return new Response(JSON.stringify({ authenticated: true, results: results || [] }), { headers: { 'Content-Type': 'application/json' } }) } else if (url.pathname === '/check-auth' && request.method === 'GET') { return new Response(JSON.stringify({ authenticated: isAuthenticated(request) }), { headers: { 'Content-Type': 'application/json' } }) } else { // 显示登录页面或结果页面的 HTML return new Response(getHtmlContent(), { headers: { 'Content-Type': 'text/html' }, }) } }
function isAuthenticated(request, env) { const cookies = request.headers.get('Cookie') if (cookies) { const authCookie = cookies.split(';').find(c => c.trim().startsWith('auth=')) if (authCookie) { const authValue = authCookie.split('=')[1] return authValue === PASSWORD } } return false }
function getHtmlContent() { return ` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Serv00 Cron保活</title> <style> div#dashboard { max-height: 100%; /* 可以根据需要调整这个高度 */ max-width: 100%; /* 可以根据需要调整这个宽度 */ width: 100%; /* 宽度为100%,以适应不同屏幕 */ overflow-y: auto; /* 当内容超出容器时添加垂直滚动条 */ overflow-x: auto; /* 当内容超出容器宽度时添加水平滚动条 */ } input#password { max-height: 100%; /* 可以根据需要调整这个高度 */ max-width: 100%; /* 可以根据需要调整这个宽度 */ width: auto; /* 宽度为100%,以适应不同屏幕 */ overflow-y: auto; /* 当内容超出容器时添加垂直滚动条 */ overflow-x: auto; /* 当内容超出容器宽度时添加水平滚动条 */ text-align:center; padding-left: 1%; padding-right: 1%; } body { font-family: Arial, sans-serif; display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0; padding: 0 10px; /* 增加左右间距以适应小屏幕 */ transition: background 0.3s, color 0.3s; flex-direction: column; /* 允许垂直方向上的元素排列 */ } .container { text-align: center; padding: 20px; border-radius: 12px; box-shadow: 0 8px 16px rgba(0,0,0,0.5); max-width: 9000px; width: 90%; /* 宽度为100%,以适应不同屏幕 */ transition: background 0.3s, color 0.3s; } h1 { font-size: 2rem; margin-bottom: 20px; text-shadow: 0 0 10px rgba(20, 255, 236, 0.7); transition: color 0.3s; } input, button { margin: 15px 0; padding: 12px; width: 100%; border-radius: 6px; border: 1px solid; transition: background-color 0.3s, color 0.3s, border-color 0.3s; } button { cursor: pointer; justify-content: center; /* 水平居中 */ transition: all 0.3s; font-size: 1rem; font-weight: bold; box-shadow: 0 4px 8px rgba(0,0,0,0.3); } #status { margin-top: 20px; font-weight: bold; transition: color 0.3s; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { padding: 12px; text-align: left; border: 1px solid; transition: background-color 0.3s, color 0.3s, border-color 0.3s; vertical-align: middle; } th { text-align: center; font-weight: bold; } tbody tr:nth-child(even) { transition: background-color 0.3s; } tbody tr:hover { transition: background-color 0.3s; } .success { color: green; background-color: rgba(0, 128, 0, 0.2); font-weight: bold; transition: color 0.3s; } .failed { color: red; background-color: rgba(255, 0, 0, 0.2); font-weight: bold; transition: color 0.3s; } #themeToggle { position: fixed; /* 位置固定,跟随页面滚动 */ top: 20px; right: 20px; padding: 8px 12px; background-color: #1b1b2f; color: white; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s, color 0.3s; width: auto; min-width: 100px; text-align: center; }
/* 默认深色模式 */ body.dark { background: linear-gradient(135deg, #1a1a2e, #16213e, #0f3460, #1a1a2e); color: #fff; } .container.dark { background: #212b40; color: #fff; } h1.dark { color: #14ffec; } input.dark, button.dark { background-color: #1b1b2f; color: #fff; border-color: #0f3460; } button.dark { background: linear-gradient(145deg, #0d7377, #14ffec); } th.dark { background-color: #2c3e50; color: #fff; border-color: #0f3460; } td.dark { color: #e0e0e0; border-color: #0f3460; } tbody tr:nth-child(even).dark { background-color: #1a1a2e; } tbody tr:hover.dark { background-color: #16213e; }
/* 浅色模式 */ body.light { background: linear-gradient(135deg, #f5f7fa, #c3cfe2); color: #333; } .container.light { background: #ffffff; color: #333; } h1.light { color: #007bff; } input.light, button.light { background-color: #e0e0e0; color: #333; border-color: #ccc; } button.light { background: linear-gradient(145deg, #007bff, #66aaff); color: #fff; } th.light { background-color: #c3cfe2; color: #333; border-color: #ccc; } td.light { color: #333; border-color: #ccc; } tbody tr:nth-child(even).light { background-color: #f5f7fa; } tbody tr:hover.light { background-color: #eaeff7; } .success.light { color: green; background-color: rgba(0, 128, 0, 0.2); } .failed.light { color: red; background-color: rgba(255, 0, 0, 0.2); }
/* 粉色模式 */ body.pink { background: linear-gradient(135deg, #ffe0e9, #ffc3d8); color: #333; } .container.pink { background: #ffe6f0; color: #333; } h1.pink { color: #ff007f; } input.pink, button.pink { background-color: #ffccd5; color: #333; border-color: #ff99b5; } button.pink { background: linear-gradient(145deg, #ff007f, #ff66a3); color: #fff; } th.pink { background-color: #ffc3d8; color: #333; border-color: #ff99b5; } td.pink { color: #333; border-color: #ff99b5; } tbody tr:nth-child(even).pink { background-color: #ffe0e9; } tbody tr:hover.pink { background-color: #ffd6e3; } .success.pink { color: #008000; background-color: rgba(0, 128, 0, 0.2); } .failed.pink { color: #ff0000; background-color: rgba(255, 0, 0, 0.2); }
/* 彩虹模式 */ body.rainbow { background: linear-gradient(135deg, #ff9a9e, #fad0c4, #fbc2eb, #a18cd1, #fbc2eb); color: #333; } .container.rainbow { background: linear-gradient(135deg, rgba(255, 126, 179, 0.8), rgba(255, 101, 163, 0.8), rgba(122, 252, 255, 0.8), rgba(254, 255, 156, 0.8), rgba(162, 255, 153, 0.8)); color: #333; border-radius: 10px; padding: 20px; box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); }
h1.rainbow { color: #ff007f; background: linear-gradient(to right, #ff9a9e, #fad0c4, #fbc2eb); -webkit-background-clip: text; color: transparent; } input.rainbow, button.rainbow { background-color: #ffdde1; color: #333; border-color: #ff9a9e; } button.rainbow { background: linear-gradient(145deg, #fbc2eb, #a18cd1); color: #fff; } th.rainbow { background-color: #fbc2eb; color: #333; border-color: #ff9a9e; } td.rainbow { color: #333; border-color: #ff9a9e; } tbody tr:nth-child(even).rainbow { background-color: #fad0c4; } tbody tr:hover.rainbow { background-color: #fbc2eb; } .success.rainbow { color: green; background-color: rgba(0, 128, 0, 0.2); } .failed.rainbow { color: red; background-color: rgba(255, 0, 0, 0.2); }
/* 移动布局 */ @media (max-width: 768px) { body { padding: 0 5px; /* 更小的左右间距 */ } .container { padding: 15px; /* 减少容器内边距 */ } h1 { font-size: 1.5rem; /* 缩小标题字体大小 */ } input, button { padding: 10px; /* 缩小输入框和按钮的内边距 */ font-size: 0.9rem; /* 缩小字体大小 */ } button { box-shadow: 0 3px 6px rgba(0,0,0,0.3); /* 减少阴影效果 */ } th, td { padding: 8px; /* 缩小表格单元格的内边距 */ font-size: 0.9rem; /* 缩小表格字体大小 */ } #themeToggle { top: 10px; right: 10px; padding: 6px 10px; /* 缩小主题切换按钮的大小 */ font-size: 0.8rem; /* 缩小字体大小 */ } }
@media (max-width: 480px) { h1 { font-size: 1.2rem; /* 进一步缩小标题字体大小 */ } input, button { padding: 8px; /* 进一步缩小输入框和按钮的内边距 */ font-size: 0.8rem; /* 进一步缩小字体大小 */ } th, td { padding: 6px; /* 进一步缩小表格单元格的内边距 */ font-size: 0.8rem; /* 进一步缩小表格字体大小 */ } #themeToggle { top: 5px; right: 5px; padding: 5px 8px; /* 进一步缩小主题切换按钮的大小 */ font-size: 0.7rem; /* 进一步缩小字体大小 */ } } </style> </head> <body class="dark"> <button id="themeToggle" class="dark" onclick="toggleTheme()">🌑 深色模式</button> <div class="container dark"> <h1 class="dark">Serv00 Cron保活</h1> <div id="loginForm"> <input type="password" id="password" placeholder="输入密码" class="dark"> <button onclick="login()" class="dark">登录</button> </div>
<div id="buttoncontainer"> <button onclick="runScript()" class="dark">执行任务</button> </div>
<div id="dashboard"> <div id="status" class="dark"></div> <table id="resultsTable"> <thead> <tr> <th class="dark">Account (账户)</th> <th class="dark">Type (类型)</th> <th class="dark">Status (状态)</th> <th class="dark">Message (信息)</th> <th class="dark" style="width: 160px;">Last Run<br>(最后运行时间)</th> </tr> </thead> <tbody></tbody> </table> </div> </div> <script> let password = '';
function toggleTheme() { const body = document.body; const container = document.querySelector('.container'); const h1 = document.querySelector('h1'); const inputs = document.querySelectorAll('input, button'); const ths = document.querySelectorAll('th'); const tds = document.querySelectorAll('td'); const status = document.getElementById('status'); const themeToggle = document.getElementById('themeToggle');
if (body.classList.contains('dark')) { body.classList.replace('dark', 'light'); container.classList.replace('dark', 'light'); h1.classList.replace('dark', 'light'); status.classList.replace('dark', 'light'); themeToggle.classList.replace('dark', 'light'); themeToggle.textContent = '☀️ 浅色模式'; inputs.forEach(input => input.classList.replace('dark', 'light')); ths.forEach(th => th.classList.replace('dark', 'light')); tds.forEach(td => td.classList.replace('dark', 'light')); } else if (body.classList.contains('light')) { body.classList.replace('light', 'pink'); container.classList.replace('light', 'pink'); h1.classList.replace('light', 'pink'); status.classList.replace('light', 'pink'); themeToggle.classList.replace('light', 'pink'); themeToggle.textContent = '🌸 粉色模式'; inputs.forEach(input => input.classList.replace('light', 'pink')); ths.forEach(th => th.classList.replace('light', 'pink')); tds.forEach(td => td.classList.replace('light', 'pink')); } else if (body.classList.contains('pink')) { body.classList.replace('pink', 'rainbow'); container.classList.replace('pink', 'rainbow'); h1.classList.replace('pink', 'rainbow'); status.classList.replace('pink', 'rainbow'); themeToggle.classList.replace('pink', 'rainbow'); themeToggle.textContent = '🌈 彩虹模式'; inputs.forEach(input => input.classList.replace('pink', 'rainbow')); ths.forEach(th => th.classList.replace('pink', 'rainbow')); tds.forEach(td => td.classList.replace('pink', 'rainbow')); } else if (body.classList.contains('rainbow')) { body.classList.replace('rainbow', 'dark'); container.classList.replace('rainbow', 'dark'); h1.classList.replace('rainbow', 'dark'); status.classList.replace('rainbow', 'dark'); themeToggle.classList.replace('rainbow', 'dark'); themeToggle.textContent = '🌑 深色模式'; inputs.forEach(input => input.classList.replace('rainbow', 'dark')); ths.forEach(th => th.classList.replace('rainbow', 'dark')); tds.forEach(td => td.classList.replace('rainbow', 'dark')); } }
function showLoginForm() { document.getElementById('loginForm').style.display = 'block'; document.getElementById('dashboard').style.display = 'none'; document.getElementById('buttoncontainer').style.display = 'none'; }
function showDashboard() { document.getElementById('loginForm').style.display = 'none'; document.getElementById('dashboard').style.display = 'block'; document.getElementById('buttoncontainer').style.display = 'block'; fetchResults(); }
async function checkAuth() { const response = await fetch('/check-auth'); const data = await response.json(); if (data.authenticated) { showDashboard(); } else { showLoginForm(); } }
async function login() { password = document.getElementById('password').value; const formData = new FormData(); formData.append('password', password); const response = await fetch('/login', { method: 'POST', body: formData }); const result = await response.json(); if (result.success) { showDashboard(); } else { alert('Incorrect password'); } }
async function runScript() { const statusDiv = document.getElementById('status'); statusDiv.textContent = '正在执行脚本...'; try { const response = await fetch('/run', { method: 'POST' }); if (response.ok) { const results = await response.json(); displayResults(results); statusDiv.textContent = '脚本执行成功!'; } else if (response.status === 401) { statusDiv.textContent = '未经授权~ 请重新登录~'; showLoginForm(); } else { statusDiv.textContent = '执行脚本时出错.'; } } catch (error) { statusDiv.textContent = 'Error: ' + error.message; } }
async function fetchResults() { try { const response = await fetch('/results'); if (response.ok) { const data = await response.json(); if (data.authenticated) { displayResults(data.results); } else { showLoginForm(); } } else { console.error('Failed to fetch results'); showLoginForm(); } } catch (error) { console.error('Error fetching results:', error); showLoginForm(); } }
function displayResults(results) { const tbody = document.querySelector('#resultsTable tbody'); tbody.innerHTML = ''; results.forEach(result => { result.cronResults.forEach((cronResult, index) => { const row = tbody.insertRow(); if (index === 0) { row.insertCell(0).textContent = result.username; row.insertCell(1).textContent = result.type; } else { row.insertCell(0).textContent = ''; row.insertCell(1).textContent = ''; } row.insertCell(2).textContent = cronResult.success ? 'Success' : 'Failed'; row.cells[2].className = cronResult.success ? 'success' : 'failed'; row.insertCell(3).textContent = cronResult.message; row.insertCell(4).textContent = new Date(result.lastRun).toLocaleString(); }); }); }
// 页面加载时检查认证状态 document.addEventListener('DOMContentLoaded', checkAuth); </script> </body> </html> `; } async function handleScheduled(scheduledTime) { const accountsData = JSON.parse(ACCOUNTS_JSON); const accounts = accountsData.accounts;
let results = []; for (const account of accounts) { const result = await loginAccount(account); results.push(result); await delay(Math.floor(Math.random() * 8000) + 1000); }
// 保存结果到 KV 存储 await CRON_RESULTS.put('lastResults', JSON.stringify(results)); }
function generateRandomUserAgent() { const browsers = ['Chrome', 'Firefox', 'Safari', 'Edge', 'Opera']; const browser = browsers[Math.floor(Math.random() * browsers.length)]; const version = Math.floor(Math.random() * 100) + 1; const os = ['Windows NT 10.0', 'Macintosh', 'X11']; const selectedOS = os[Math.floor(Math.random() * os.length)]; const osVersion = selectedOS === 'X11' ? 'Linux x86_64' : selectedOS === 'Macintosh' ? 'Intel Mac OS X 10_15_7' : 'Win64; x64';
return `Mozilla/5.0 (${selectedOS}; ${osVersion}) AppleWebKit/537.36 (KHTML, like Gecko) ${browser}/${version}.0.0.0 Safari/537.36`; }
async function loginAccount(account) { const { username, password, panelnum, type, cronCommands } = account let baseUrl = type === 'ct8' ? 'https://panel.ct8.pl' : `https://panel${panelnum}.serv00.com` let loginUrl = `${baseUrl}/login/?next=/cron/`
const userAgent = generateRandomUserAgent();
try { const response = await fetch(loginUrl, { method: 'GET', headers: { 'User-Agent': userAgent, }, })
const pageContent = await response.text() const csrfMatch = pageContent.match(/name="csrfmiddlewaretoken" value="([^"]*)"/) const csrfToken = csrfMatch ? csrfMatch[1] : null
if (!csrfToken) { throw new Error('CSRF token not found') }
const initialCookies = response.headers.get('set-cookie') || ''
const formData = new URLSearchParams({ 'username': username, 'password': password, 'csrfmiddlewaretoken': csrfToken, 'next': '/cron/' })
const loginResponse = await fetch(loginUrl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Referer': loginUrl, 'User-Agent': userAgent, 'Cookie': initialCookies, }, body: formData.toString(), redirect: 'manual' })
if (loginResponse.status === 302 && loginResponse.headers.get('location') === '/cron/') { const loginCookies = loginResponse.headers.get('set-cookie') || '' const allCookies = combineCookies(initialCookies, loginCookies)
// 访问 cron 列表页面 const cronListUrl = `${baseUrl}/cron/` const cronListResponse = await fetch(cronListUrl, { headers: { 'Cookie': allCookies, 'User-Agent': userAgent, } }) const cronListContent = await cronListResponse.text()
console.log(`Cron list URL: ${cronListUrl}`) console.log(`Cron list response status: ${cronListResponse.status}`) console.log(`Cron list content (first 1000 chars): ${cronListContent.substring(0, 1000)}`)
let cronResults = []; for (const cronCommand of cronCommands) { if (!cronListContent.includes(cronCommand)) { // 访问添加 cron 任务页面 const addCronUrl = `${baseUrl}/cron/add` const addCronPageResponse = await fetch(addCronUrl, { headers: { 'Cookie': allCookies, 'User-Agent': userAgent, 'Referer': cronListUrl, } }) const addCronPageContent = await addCronPageResponse.text()
console.log(`Add cron page URL: ${addCronUrl}`) console.log(`Add cron page response status: ${addCronPageResponse.status}`) console.log(`Add cron page content (first 1000 chars): ${addCronPageContent.substring(0, 1000)}`)
const newCsrfMatch = addCronPageContent.match(/name="csrfmiddlewaretoken" value="([^"]*)"/) const newCsrfToken = newCsrfMatch ? newCsrfMatch[1] : null
if (!newCsrfToken) { throw new Error('New CSRF token not found for adding cron task') }
const formData = new URLSearchParams({ 'csrfmiddlewaretoken': newCsrfToken, 'spec': 'manual', 'minute_time_interval': 'on', 'minute': '15', 'hour_time_interval': 'each', 'hour': '*', 'day_time_interval': 'each', 'day': '*', 'month_time_interval': 'each', 'month': '*', 'dow_time_interval': 'each', 'dow': '*', 'command': cronCommand, 'comment': 'Auto added cron job' })
console.log('Form data being sent:', formData.toString())
const { success, response: addCronResponse, content: addCronResponseContent } = await addCronWithRetry(addCronUrl, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Cookie': allCookies, 'User-Agent': userAgent, 'Referer': addCronUrl, 'Origin': baseUrl, 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,en;q=0.5', 'Upgrade-Insecure-Requests': '1' }, body: formData.toString(), })
console.log('Full response content:', addCronResponseContent)
if (success) { if (addCronResponseContent.includes('Cron job has been added') || addCronResponseContent.includes('Zadanie cron zostało dodane')) { const message = `添加了新的 cron 任务:${cronCommand}`; console.log(message); await sendTelegramMessage(`账号 ${username} (${type}) ${message}`); cronResults.push({ success: true, message }); } else { // 如果响应中没有成功信息,再次检查cron列表 const checkCronListResponse = await fetch(cronListUrl, { headers: { 'Cookie': allCookies, 'User-Agent': userAgent, } }); const checkCronListContent = await checkCronListResponse.text();
if (checkCronListContent.includes(cronCommand)) { const message = `确认添加了新的 cron 任务:${cronCommand}`; console.log(message); await sendTelegramMessage(`账号 ${username} (${type}) ${message}`); cronResults.push({ success: true, message }); } else { const message = `尝试添加 cron 任务:${cronCommand},但在列表中未找到。可能添加失败。`; console.error(message); cronResults.push({ success: false, message }); } } } else { const message = `添加 cron 任务失败:${cronCommand}`; console.error(message); cronResults.push({ success: false, message }); } } else { const message = `cron 任务已存在:${cronCommand}`; console.log(message); cronResults.push({ success: true, message }); } } return { username, type, cronResults, lastRun: new Date().toISOString() }; } else { const message = `登录失败,未知原因。请检查账号和密码是否正确。`; console.error(message); return { username, type, cronResults: [{ success: false, message }], lastRun: new Date().toISOString() }; } } catch (error) { const message = `登录或添加 cron 任务时出现错误: ${error.message}`; console.error(message); return { username, type, cronResults: [{ success: false, message }], lastRun: new Date().toISOString() }; } }
async function addCronWithRetry(url, options, maxRetries = 3) { for (let i = 0; i < maxRetries; i++) { try { const response = await fetch(url, options); const responseContent = await response.text(); console.log(`Attempt ${i + 1} response status:`, response.status); console.log(`Attempt ${i + 1} response content (first 1000 chars):`, responseContent.substring(0, 1000));
if (response.status === 200 || response.status === 302 || responseContent.includes('Cron job has been added') || responseContent.includes('Zadanie cron zostało dodane')) { return { success: true, response, content: responseContent }; } } catch (error) { console.error(`Attempt ${i + 1} failed:`, error); } await delay(2000); // Wait 2 seconds before retrying } return { success: false }; }
function combineCookies(cookies1, cookies2) { const cookieMap = new Map()
const parseCookies = (cookieString) => { cookieString.split(',').forEach(cookie => { const [fullCookie] = cookie.trim().split(';') const [name, value] = fullCookie.split('=') if (name && value) { cookieMap.set(name.trim(), value.trim()) } }) }
parseCookies(cookies1) parseCookies(cookies2)
return Array.from(cookieMap.entries()).map(([name, value]) => `${name}=${value}`).join('; ') }
async function sendTelegramMessage(message) { const telegramConfig = JSON.parse(TELEGRAM_JSON) const { telegramBotToken, telegramBotUserId } = telegramConfig const url = `https://api.telegram.org/bot${telegramBotToken}/sendMessage`
try { await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ chat_id: telegramBotUserId, text: message }) }) } catch (error) { console.error('Error sending Telegram message:', error) } }
function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)) }
|