Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use cookie cloud for cookie #16851

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import randUserAgent from '@/utils/rand-user-agent';
import 'dotenv/config';
import { ofetch } from 'ofetch';
import { CookieCloudQuery, cookieCloudQuery, createCookieCloudSyncJob } from '@/utils/cookie-cloud';

let envs = process.env;

Expand Down Expand Up @@ -68,6 +69,12 @@ export type Config = {
};
suffix?: string;
titleLengthLimit: number;
cookieCloud: {
host?: string;
uuid?: string;
password?: string;
updateCron: string;
};
openai: {
apiKey?: string;
model?: string;
Expand Down Expand Up @@ -172,7 +179,7 @@ export type Config = {
password?: string;
};
javdb: {
session?: string;
session: CookieCloudQuery;
};
keylol: {
cookie?: string;
Expand Down Expand Up @@ -445,6 +452,12 @@ const calculateValue = () => {
},
suffix: envs.SUFFIX,
titleLengthLimit: toInt(envs.TITLE_LENGTH_LIMIT, 150),
cookieCloud: {
host: envs.COOKIE_CLOUD_HOST,
uuid: envs.COOKIE_CLOUD_UUID,
password: envs.COOKIE_CLOUD_PASSWORD,
updateCron: envs.COOKIE_CLOUD_UPDATE_CRON || '0 2 * * *',
},
openai: {
apiKey: envs.OPENAI_API_KEY,
model: envs.OPENAI_MODEL || 'gpt-3.5-turbo-16k',
Expand Down Expand Up @@ -552,7 +565,12 @@ const calculateValue = () => {
password: envs.IWARA_PASSWORD,
},
javdb: {
session: envs.JAVDB_SESSION,
session: cookieCloudQuery({
domain: 'javdb.com',
name: '_jdb_session',
path: '/',
default_value: envs.JAVDB_SESSION,
}),
},
keylol: {
cookie: envs.KEYLOL_COOKIE,
Expand Down Expand Up @@ -718,6 +736,8 @@ const calculateValue = () => {
for (const name in _value) {
value[name] = _value[name];
}

createCookieCloudSyncJob(_value.cookieCloud);
};
calculateValue();

Expand Down
5 changes: 3 additions & 2 deletions lib/routes/javdb/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ const ProcessItems = async (ctx, currentUrl, title) => {

const cookieJar = new CookieJar();

if (config.javdb.session) {
const javdbSession = config.javdb.session();
if (javdbSession) {
const cookie = Cookie.fromJSON({
key: '_jdb_session',
value: config.javdb.session,
value: javdbSession,
domain,
path: '/',
});
Expand Down
109 changes: 109 additions & 0 deletions lib/utils/cookie-cloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import CryptoJS from 'crypto-js';
import { CronJob } from 'cron';

interface CookieItem {
domain: string;
name: string;
value: string;
path: string;
expirationDate: number;
hostOnly: boolean;
httpOnly: boolean;
secure: boolean;
sameSite: string;
}

interface CookieData {
[key: string]: CookieItem[];
}

interface DecryptedData {
cookie_data: CookieData;
local_storage_data: Record<string, any>;
}

globalThis.cookieCloudItems = [] as CookieItem[];

const cloudCookie = async (host: string, uuid: string, password: string) => {
let cookies: CookieItem[] = [];
try {
const url = `${host}/get/${uuid}`;
const ret = await fetch(url);
const json = await ret.json();
if (json && json.encrypted) {
const { cookie_data } = cookieDecrypt(uuid, json.encrypted, password);
for (const key in cookie_data) {
if (!cookie_data.hasOwnProperty(key)) {
continue;
}
cookies = cookies.concat(

Check warning

Code scanning / ESLint

Prefer the spread operator over `Array.from(…)`, `Array#concat(…)`, `Array#{slice,toSpliced}()` and `String#split('')`. Warning

Prefer the spread operator over Array#concat(…).
cookie_data[key].map((item) => {
if (item.sameSite === 'unspecified') {
item.sameSite = 'Lax';
}
return item;
})
);
}
}
} catch {

Check warning on line 49 in lib/utils/cookie-cloud.ts

View check run for this annotation

Codecov / codecov/patch

lib/utils/cookie-cloud.ts#L28-L49

Added lines #L28 - L49 were not covered by tests
/* empty */
}
globalThis.cookieCloudItems = cookies;
};

Check warning on line 53 in lib/utils/cookie-cloud.ts

View check run for this annotation

Codecov / codecov/patch

lib/utils/cookie-cloud.ts#L51-L53

Added lines #L51 - L53 were not covered by tests

const cookieDecrypt = (uuid: string, encrypted: string, password: string) => {
const the_key = CryptoJS.MD5(`${uuid}-${password}`).toString().substring(0, 16);

Check failure

Code scanning / CodeQL

Use of password hash with insufficient computational effort High

Password from
an access to COOKIE_CLOUD_PASSWORD
is hashed insecurely.
Password from
an access to password
is hashed insecurely.
Password from
an access to password
is hashed insecurely.
Password from
an access to password
is hashed insecurely.
Password from
an access to cookieCloudPassword
is hashed insecurely.

Check failure

Code scanning / CodeQL

Use of a broken or weak cryptographic algorithm High

A broken or weak cryptographic algorithm
depends on
sensitive data from an access to COOKIE_CLOUD_UUID
.
A broken or weak cryptographic algorithm
depends on
sensitive data from an access to uuid
.
A broken or weak cryptographic algorithm
depends on
sensitive data from an access to uuid
.
A broken or weak cryptographic algorithm
depends on
sensitive data from an access to uuid
.
A broken or weak cryptographic algorithm
depends on
sensitive data from an access to cookieCloudUuid
.
const decrypted = CryptoJS.AES.decrypt(encrypted, the_key).toString(CryptoJS.enc.Utf8);
return JSON.parse(decrypted) as DecryptedData;
};

Check warning on line 59 in lib/utils/cookie-cloud.ts

View check run for this annotation

Codecov / codecov/patch

lib/utils/cookie-cloud.ts#L56-L59

Added lines #L56 - L59 were not covered by tests

let cookieCloudSyncJob: CronJob | null = null;
export const createCookieCloudSyncJob = (config) => {
const cookieCloudHost = config.host;
const cookieCloudUuid = config.uuid;
const cookieCloudPassword = config.password;
cookieCloudSyncJob?.stop();
if (cookieCloudHost !== undefined && cookieCloudUuid !== undefined && cookieCloudPassword !== undefined) {
cookieCloudSyncJob = CronJob.from({
cronTime: config.updateCron,
async onTick() {
await cloudCookie(cookieCloudHost, cookieCloudUuid, cookieCloudPassword);
},
start: true,
runOnInit: true,
});
}

Check warning on line 76 in lib/utils/cookie-cloud.ts

View check run for this annotation

Codecov / codecov/patch

lib/utils/cookie-cloud.ts#L68-L76

Added lines #L68 - L76 were not covered by tests
};

interface CookieCloudQueryParam {
// domain of cookie
domain: string;
// optional cookie key, leave it undefined to get all cookie
name?: string;
// optional cookie path
path?: string;
// optional default value if no cookie matched.
default_value?: string;
}

export type CookieCloudQuery = () => string | undefined;

export const cookieCloudQuery =
(query: CookieCloudQueryParam): CookieCloudQuery =>
() => {
let result: string | undefined;
for (const cookieCloudItem of globalThis.cookieCloudItems || []) {
if (cookieCloudItem.domain === query.domain && query.path !== undefined && cookieCloudItem.path === query.path) {
if (query.name === undefined) {
result = (result || '') + `${cookieCloudItem.name}=${cookieCloudItem.value};`;
continue;
}
if (cookieCloudItem.name === query.name) {
result = cookieCloudItem.value;
break;
}
}
}
return result || query.default_value;
};

Check warning on line 109 in lib/utils/cookie-cloud.ts

View check run for this annotation

Codecov / codecov/patch

lib/utils/cookie-cloud.ts#L95-L109

Added lines #L95 - L109 were not covered by tests
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"cheerio": "1.0.0",
"chrono-node": "2.7.7",
"city-timezones": "1.3.0",
"cron": "^3.1.7",
"cross-env": "7.0.3",
"crypto-js": "4.2.0",
"currency-symbol-map": "5.1.0",
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading