Comparing version 2.0.0-alpha.1 to 2.0.0-alpha.2
@@ -1,1 +0,1 @@ | ||
export declare const getCookieFactory: (api: import("../zalo.js").API) => () => string; | ||
export declare const getCookieFactory: (api: import("../zalo.js").API) => () => import("tough-cookie").CookieJar; |
@@ -16,3 +16,3 @@ import EventEmitter from "events"; | ||
this.url = url; | ||
this.cookie = appContext.cookie; | ||
this.cookie = appContext.cookie.getCookieStringSync("https://chat.zalo.me"); | ||
this.userAgent = appContext.userAgent; | ||
@@ -19,0 +19,0 @@ this.selfListen = appContext.options.selfListen; |
@@ -0,1 +1,2 @@ | ||
import { type CookieJar } from "tough-cookie"; | ||
type UploadEventData = { | ||
@@ -25,3 +26,3 @@ fileUrl: string; | ||
imei: string; | ||
cookie: string; | ||
cookie: CookieJar; | ||
userAgent: string; | ||
@@ -43,3 +44,3 @@ language: string; | ||
checkUpdate: boolean; | ||
verbose: boolean; | ||
logging: boolean; | ||
}; | ||
@@ -46,0 +47,0 @@ type ExtraVer = { |
@@ -20,3 +20,3 @@ const _5_MINUTES = 5 * 60 * 1000; | ||
checkUpdate: true, | ||
verbose: false | ||
logging: true | ||
}, | ||
@@ -23,0 +23,0 @@ }; |
import { compare } from "semver"; | ||
import { logger } from "./utils.js"; | ||
import { appContext } from "./context.js"; | ||
const VERSION = "2.0.0-alpha.1"; | ||
const VERSION = "2.0.0-alpha.2"; | ||
const NPM_REGISTRY = "https://registry.npmjs.org/zca-js"; | ||
@@ -6,0 +6,0 @@ export async function checkUpdate() { |
@@ -47,3 +47,3 @@ import cryptojs from "crypto-js"; | ||
export declare function decodeAES(secretKey: string, data: string, t?: number): string | null; | ||
export declare function getDefaultHeaders(): { | ||
export declare function getDefaultHeaders(origin?: string): Promise<{ | ||
Accept: string; | ||
@@ -57,3 +57,3 @@ "Accept-Encoding": string; | ||
"User-Agent": string; | ||
}; | ||
}>; | ||
export declare function request(url: string, options?: RequestInit): Promise<Response>; | ||
@@ -60,0 +60,0 @@ export declare function getImageMetaData(filePath: string): Promise<{ |
@@ -8,2 +8,3 @@ import cryptojs from "crypto-js"; | ||
import SparkMD5 from "spark-md5"; | ||
import toughCookie from "tough-cookie"; | ||
import { appContext, isContextValid } from "./context.js"; | ||
@@ -215,29 +216,5 @@ import { ZaloApiError } from "./Errors/ZaloApiError.js"; | ||
} | ||
function updateCookie(input) { | ||
export async function getDefaultHeaders(origin = "https://chat.zalo.me") { | ||
if (!appContext.cookie) | ||
throw new ZaloApiError("Cookie is not available"); | ||
if (typeof input !== "string" || !Array.isArray(input)) | ||
return null; | ||
const cookieMap = new Map(); | ||
const cookie = appContext.cookie; | ||
cookie.split(";").forEach((cookie) => { | ||
const [key, value] = cookie.split("="); | ||
cookieMap.set(key.trim(), value.trim()); | ||
}); | ||
let newCookie; | ||
if (Array.isArray(input)) | ||
newCookie = input.map((cookie) => cookie.split(";")[0]).join("; "); | ||
else | ||
newCookie = input; | ||
newCookie.split(";").forEach((cookie) => { | ||
const [key, value] = cookie.split("="); | ||
cookieMap.set(key.trim(), value.trim()); | ||
}); | ||
return Array.from(cookieMap.entries()) | ||
.map(([key, value]) => `${key}=${value}`) | ||
.join("; "); | ||
} | ||
export function getDefaultHeaders() { | ||
if (!appContext.cookie) | ||
throw new ZaloApiError("Cookie is not available"); | ||
if (!appContext.userAgent) | ||
@@ -250,3 +227,3 @@ throw new ZaloApiError("User agent is not available"); | ||
"content-type": "application/x-www-form-urlencoded", | ||
Cookie: appContext.cookie, | ||
Cookie: await appContext.cookie.getCookieString(origin), | ||
Origin: "https://chat.zalo.me", | ||
@@ -258,17 +235,32 @@ Referer: "https://chat.zalo.me/", | ||
export async function request(url, options) { | ||
if (options) | ||
options.headers = mergeHeaders(options.headers || {}, getDefaultHeaders()); | ||
if (!appContext.cookie) | ||
appContext.cookie = new toughCookie.CookieJar(); | ||
const origin = new URL(url).origin; | ||
const defaultHeaders = await getDefaultHeaders(origin); | ||
if (options) { | ||
options.headers = Object.assign(defaultHeaders, options.headers || {}); | ||
} | ||
else | ||
options = { headers: getDefaultHeaders() }; | ||
options = { headers: defaultHeaders }; | ||
const response = await fetch(url, options); | ||
if (response.headers.has("set-cookie")) { | ||
const newCookie = updateCookie(response.headers.get("set-cookie")); | ||
if (newCookie) | ||
appContext.cookie = newCookie; | ||
for (const cookie of response.headers.getSetCookie()) { | ||
const parsed = toughCookie.Cookie.parse(cookie); | ||
try { | ||
if (parsed) | ||
await appContext.cookie.setCookie(parsed, origin); | ||
} | ||
catch (_a) { } | ||
} | ||
} | ||
const redirectURL = response.headers.get("location"); | ||
if (redirectURL) { | ||
const redirectOptions = Object.assign({}, options); | ||
redirectOptions.method = "GET"; | ||
// @ts-ignore | ||
redirectOptions.headers["Referer"] = "https://id.zalo.me/"; | ||
return await request(redirectURL, redirectOptions); | ||
} | ||
return response; | ||
} | ||
function mergeHeaders(headers, defaultHeaders) { | ||
return Object.assign(Object.assign({}, defaultHeaders), headers); | ||
} | ||
export async function getImageMetaData(filePath) { | ||
@@ -347,13 +339,16 @@ const fileData = await fs.promises.readFile(filePath); | ||
verbose: (...args) => { | ||
if (appContext.options.verbose) | ||
if (appContext.options.logging) | ||
console.log("\x1b[2m🚀 VERBOSE\x1b[0m", ...args); | ||
}, | ||
info: (...args) => { | ||
console.log("\x1b[34mINFO\x1b[0m", ...args); | ||
if (appContext.options.logging) | ||
console.log("\x1b[34mINFO\x1b[0m", ...args); | ||
}, | ||
warn: (...args) => { | ||
console.log("\x1b[33mWARN\x1b[0m", ...args); | ||
if (appContext.options.logging) | ||
console.log("\x1b[33mWARN\x1b[0m", ...args); | ||
}, | ||
error: (...args) => { | ||
console.log("\x1b[31mERROR\x1b[0m", ...args); | ||
if (appContext.options.logging) | ||
console.log("\x1b[31mERROR\x1b[0m", ...args); | ||
}, | ||
@@ -360,0 +355,0 @@ }; |
@@ -34,21 +34,22 @@ import { Listener } from "./apis/listen.js"; | ||
import { uploadAttachmentFactory } from "./apis/uploadAttachment.js"; | ||
export type J2Cookies = { | ||
url: string; | ||
cookies: { | ||
domain: string; | ||
expirationDate: number; | ||
hostOnly: boolean; | ||
httpOnly: boolean; | ||
name: string; | ||
path: string; | ||
sameSite: string; | ||
secure: boolean; | ||
session: boolean; | ||
storeId: string; | ||
value: string; | ||
}[]; | ||
import { type SerializedCookie } from "tough-cookie"; | ||
export type Cookie = { | ||
domain: string; | ||
expirationDate: number; | ||
hostOnly: boolean; | ||
httpOnly: boolean; | ||
name: string; | ||
path: string; | ||
sameSite: string; | ||
secure: boolean; | ||
session: boolean; | ||
storeId: string; | ||
value: string; | ||
}; | ||
export type Credentials = { | ||
imei: string; | ||
cookie: string | J2Cookies; | ||
cookie: Cookie[] | SerializedCookie[] | { | ||
url: string; | ||
cookies: Cookie[]; | ||
}; | ||
userAgent: string; | ||
@@ -59,6 +60,11 @@ language?: string; | ||
private enableEncryptParam; | ||
constructor(credentials: Credentials, options?: Partial<Options>); | ||
constructor(options?: Partial<Options>); | ||
private parseCookies; | ||
private validateParams; | ||
login(): Promise<API>; | ||
login(credentials: Credentials): Promise<API>; | ||
loginQR(options?: { | ||
userAgent?: string; | ||
language?: string; | ||
qrPath?: string; | ||
}, callback?: (qrPath: string) => void): Promise<API>; | ||
} | ||
@@ -65,0 +71,0 @@ export declare class API { |
@@ -37,10 +37,10 @@ import { Listener } from "./apis/listen.js"; | ||
import { checkUpdate } from "./update.js"; | ||
import toughCookie from "tough-cookie"; | ||
import { loginQR } from "./apis/loginQR.js"; | ||
import { randomUUID } from "node:crypto"; | ||
import { MD5 } from "crypto-js"; | ||
import { ZaloApiError } from "./Errors/ZaloApiError.js"; | ||
export class Zalo { | ||
constructor(credentials, options) { | ||
constructor(options) { | ||
this.enableEncryptParam = true; | ||
this.validateParams(credentials); | ||
appContext.imei = credentials.imei; | ||
appContext.cookie = this.parseCookies(credentials.cookie); | ||
appContext.userAgent = credentials.userAgent; | ||
appContext.language = credentials.language || "vi"; | ||
appContext.secretKey = null; | ||
@@ -51,6 +51,12 @@ if (options) | ||
parseCookies(cookie) { | ||
if (typeof cookie === "string") | ||
return cookie; | ||
const cookieString = cookie.cookies.map((c) => `${c.name}=${c.value}`).join("; "); | ||
return cookieString; | ||
var _a; | ||
const cookieArr = Array.isArray(cookie) ? cookie : cookie.cookies; | ||
const jar = new toughCookie.CookieJar(); | ||
for (const each of cookieArr) { | ||
try { | ||
jar.setCookieSync((_a = toughCookie.Cookie.fromJSON(Object.assign(Object.assign({}, each), { key: each.key || each.name }))) !== null && _a !== void 0 ? _a : "", "https://chat.zalo.me"); | ||
} | ||
catch (_b) { } | ||
} | ||
return jar; | ||
} | ||
@@ -62,8 +68,13 @@ validateParams(credentials) { | ||
} | ||
async login() { | ||
async login(credentials) { | ||
await checkUpdate(); | ||
this.validateParams(credentials); | ||
appContext.imei = credentials.imei; | ||
appContext.cookie = this.parseCookies(credentials.cookie); | ||
appContext.userAgent = credentials.userAgent; | ||
appContext.language = credentials.language || "vi"; | ||
const loginData = await login(this.enableEncryptParam); | ||
const serverInfo = await getServerInfo(this.enableEncryptParam); | ||
if (!loginData || !serverInfo) | ||
throw new Error("Failed to login"); | ||
throw new Error("Đăng nhập thất bại"); | ||
appContext.secretKey = loginData.data.zpw_enk; | ||
@@ -80,2 +91,19 @@ appContext.uid = loginData.data.uid; | ||
} | ||
async loginQR(options, callback) { | ||
if (!options) | ||
options = {}; | ||
if (!options.userAgent) | ||
options.userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0"; | ||
if (!options.language) | ||
options.language = "vi"; | ||
const loginQRResult = await loginQR(options, callback); | ||
if (!loginQRResult) | ||
throw new ZaloApiError("Đăng nhập với QR thất bại"); | ||
return this.login({ | ||
cookie: loginQRResult.cookies, | ||
imei: randomUUID() + "-" + MD5(options.userAgent).toString(), | ||
userAgent: options.userAgent, | ||
language: options.language, | ||
}); | ||
} | ||
} | ||
@@ -82,0 +110,0 @@ export class API { |
{ | ||
"name": "zca-js", | ||
"version": "2.0.0-alpha.1", | ||
"version": "2.0.0-alpha.2", | ||
"description": "Unofficial Zalo API for JavaScript", | ||
@@ -47,2 +47,3 @@ "main": "dist/index.js", | ||
"spark-md5": "^3.0.2", | ||
"tough-cookie": "^5.0.0", | ||
"ws": "^8.18.0" | ||
@@ -49,0 +50,0 @@ }, |
@@ -24,3 +24,5 @@ // rollup.config.js | ||
"fs", | ||
"node:fs/promises", | ||
"tough-cookie" | ||
], | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
302394
146
7582
8
+ Addedtough-cookie@^5.0.0
+ Addedtldts@6.1.64(transitive)
+ Addedtldts-core@6.1.64(transitive)
+ Addedtough-cookie@5.0.0(transitive)