54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
// @ts-nocheck
|
||
const { test, expect } = require('@playwright/test');
|
||
const { COOKIE_CLOUD_HOST, COOKIE_CLOUD_UUID, COOKIE_CLOUD_PASSWORD } = require('./config.od');
|
||
const fetch = require('cross-fetch');
|
||
const CryptoJS = require('crypto-js');
|
||
|
||
test('使用CookieCloud访问nexusphp', async ({ page, browser }) => {
|
||
// 读取云端cookie并解密
|
||
const cookies = await cloud_cookie(COOKIE_CLOUD_HOST, COOKIE_CLOUD_UUID, COOKIE_CLOUD_PASSWORD);
|
||
// 添加cookie到浏览器上下文
|
||
console.log(COOKIE_CLOUD_HOST, COOKIE_CLOUD_UUID, COOKIE_CLOUD_PASSWORD);
|
||
const context = await browser.newContext();
|
||
console.log(cookies);
|
||
await context.addCookies(cookies);
|
||
page = await context.newPage();
|
||
// 这之后已经带着Cookie了,按正常流程访问
|
||
await page.goto('https://photos.onedrive.com/photo/D0BBFDDEF22147!scf10ea825fdb4b6e891219f06d3dc692?view=all');
|
||
await expect(page.getByRole('link', { name: 'magik' })).toHaveText("magik");
|
||
await context.close();
|
||
});
|
||
|
||
|
||
async function cloud_cookie( host, uuid, password )
|
||
{
|
||
|
||
const url = host+'/get/'+uuid;
|
||
const ret = await fetch(url);
|
||
const json = await ret.json();
|
||
let cookies = [];
|
||
if( json && json.encrypted )
|
||
{
|
||
const {cookie_data, local_storage_data} = cookie_decrypt(uuid, json.encrypted, password);
|
||
for( const key in cookie_data )
|
||
{
|
||
// merge cookie_data[key] to cookies
|
||
cookies = cookies.concat(cookie_data[key].map( item => {
|
||
if (!['Strict', 'Lax', 'None'].includes(item.sameSite)) {
|
||
item.sameSite = 'Strict';
|
||
}
|
||
return item;
|
||
} ));
|
||
}
|
||
}
|
||
return cookies;
|
||
}
|
||
|
||
function cookie_decrypt( uuid, encrypted, password )
|
||
{
|
||
|
||
const the_key = CryptoJS.MD5(uuid+'-'+password).toString().substring(0,16);
|
||
const decrypted = CryptoJS.AES.decrypt(encrypted, the_key).toString(CryptoJS.enc.Utf8);
|
||
const parsed = JSON.parse(decrypted);
|
||
return parsed;
|
||
} |