--[[-- 晋江「App」协议:账号密码登录拿 token(android API)。 只保存账号与 token,不保存密码;token 失效时交给 Auth 提示重新登录。和网页 Cookie 协议(web.lua)互不相干; token 的请求头各接口不同,响应既可能是明文 JSON,也可能整包加密(密钥由响应头 accesskey / keystring 派生,见 decryptBody)。 协议接口(protocols/init.lua 按设置选一个协议调用):`available` / `shelfAsync` / `contentAsync`。 @module koplugin.book.moonsource_jjwxc.protocols.app --]] local Job = require("workers.job") local JSON = require("json") local Mapper = require("moonsource_jjwxc.mapper") local Request = require("http.request") local Text = require("utils.text") local _ = require("l10n").get("moonsource_jjwxc") local Token = {} Token.id = "app" local SOURCE_ID = "jjwxc" local APP = "https://app-cdn.jjwxc.net/androidapi" -- 接口回包 code:1004 = 要登录(token 无效),1025 = 登录态有效但这章没买。 local LOGIN_CODE = 1004 local UNPAID_CODE = 1025 -- 登录接口的版本号与 App 内置的 DES 密钥(密钥写在 App 里,不是用户机密)。 local LOGIN_VERSION = 402 local DES_KEY, DES_IV = "KW8Dvm2N", "1ae2c94b" local ACCOUNT_HEADERS = { ["User-Agent"] = "Mobile", ["Accept-Encoding"] = "identity", ["Referer"] = "http://android.jjwxc.net?v=" .. LOGIN_VERSION, } -- 带 token 取正文必须带 App 的 UA,UA 是 "Mobile" 时服务端回 2016「浏览器标识异常」。 local CONTENT_HEADERS = { ["User-Agent"] = "JINJIANG-Android/381", ["Referer"] = "http://android.jjwxc.net?v=381", ["Accept-Encoding"] = "identity", } -- 要求设备验证(手机 / 邮箱验证码)的 code。 local DEVICE_VERIFY_CODE = 221003 ---@return string function Token.label() return _("App 账号") end ---@return string|nil function Token.get() local Settings = require("utils.settings") local cfg = Settings.getSource(SOURCE_ID) if cfg.password ~= nil then cfg.password = nil Settings.saveSource(SOURCE_ID) end local token = cfg.token return type(token) == "string" and token ~= "" and token or nil end ---@return boolean function Token.available() return Token.get() ~= nil end --- 段评接口(Pc/comment)的登录态:VIP 章节要求,App 协议把 token 放查询参数(放请求头 / Cookie 都被拒)。 ---@return string query ---@return table headers function Token.commentAuth() local token = Token.get() return token and "&token=" .. Text.urlEncode(token) or "", {} end --- 登录成功后保存账号与 token。 ---@param account string ---@param _password string 仅为旧调用签名保留,不落盘 ---@param token string function Token.save(account, _password, token) local Settings = require("utils.settings") local cfg = Settings.getSource(SOURCE_ID) cfg.account, cfg.password, cfg.token = account, nil, token Settings.saveSource(SOURCE_ID) end --- 整包加密响应的解密:accesskey / keystring 两个响应头派生 DES 密钥,12 字符的盐在正文头或尾(看 accesskey 末字符奇偶)。 ---@param body string ---@param accesskey string ---@param keystring string ---@return string|nil function Token.decryptBody(body, accesskey, keystring) local sum = 0 for i = 1, #accesskey do sum = sum + accesskey:byte(i) end local start, count = sum % #keystring, math.floor(sum / 65) local part = start + count > #keystring and keystring:sub(start + 1) or keystring:sub(start + 1, start + count) local salt, cipher if accesskey:byte(-1) % 2 == 1 then salt, cipher = body:sub(-12), body:sub(1, -13) else salt, cipher = body:sub(1, 12), body:sub(13) end local md5, Des = require("ffi/sha2").md5, require("crypto.des") local key, iv = md5(part .. salt):sub(1, 8), md5(salt):sub(1, 8) if cipher:find("^%x+$") then local raw = require("crypto.hex").from_hex(cipher) return raw and Des.decrypt(raw, key, iv) end return Des.decryptBase64(cipher, key, iv) end ---@param path string ---@param params table ---@param headers table ---@param cb fun(wire: table|nil, err: string|nil) ---@return HttpJob local function get(path, params, headers, cb) local query = {} for key, value in pairs(params) do query[#query + 1] = key .. "=" .. tostring(value) end table.sort(query) local cancelled, job, pending = false, nil, true local initial = Request.get(APP .. path .. "?" .. table.concat(query, "&"), { accept = "application/json", headers = headers, timeout = require("http.timeout").net(1), cache_ttl = 0, }, function(raw, err, res) pending = false if cancelled then return end if not raw then cb(nil, err and tostring(err) or _("请求失败")); return end local ok, wire = pcall(JSON.decode, raw) if ok and type(wire) == "table" then cb(wire); return end local accesskey, keystring = Request.header(res, "accesskey"), Request.header(res, "keystring") if not (accesskey and keystring and keystring ~= "") then require("utils.log").warn("jjwxc invalid response", path, #raw) cb(nil, _("响应无效")) return end -- DES 是纯 Lua 重活,放子进程 job = Job.run(function() return { plain = Token.decryptBody(raw, accesskey, keystring) } end, { name = "jjwxc_body", kind = "light", timeout = 60, on_done = function(r) if cancelled then return end local ok2, wire2 = pcall(JSON.decode, r.plain or "") if ok2 and type(wire2) == "table" then cb(wire2); return end require("utils.log").warn("jjwxc decrypt body failed", path, #raw) cb(nil, _("响应无效")) end, on_failed = function(e) if not cancelled then cb(nil, tostring(e)) end end, on_cancelled = function() end, }) end) if pending then job = initial end return { cancel = function() cancelled = true if job then job:cancel() end end } end ---@param value any ---@return string|nil local function nonEmpty(value) return type(value) == "string" and value ~= "" and value or nil end ---@param n integer ---@param alphabet string ---@return string local function randomString(n, alphabet) local out = {} for i = 1, n do local k = math.random(#alphabet) out[i] = alphabet:sub(k, k) end return table.concat(out) end --- 账号密码登录,拿 App token。 --- 回调 (token) / (nil, err) / (nil, message, "verify"):后者表示要先做设备验证再带 checktype + code 重试。 ---@param account string ---@param password string ---@param verify { checktype: "phone"|"email", code: string }|nil ---@param cb fun(token: string|nil, err: string|nil, kind: "verify"|nil) ---@return HttpJob function Token.loginAsync(account, password, verify, cb) local Des = require("crypto.des") local identifiers = randomString(20, "0123456789") .. ":" .. randomString(16, "0123456789abcdef") .. "d4:" local params = { loginName = Text.urlEncode(account), encode = 1, loginPassword = Text.urlEncode(Des.encryptBase64(password, DES_KEY, DES_IV)), sign = Text.urlEncode(Des.encryptBase64(os.time() * 1000 .. "_" .. identifiers .. "_", DES_KEY, DES_IV)), identifiers = Text.urlEncode(identifiers), autologin = 1, versionCode = LOGIN_VERSION, } if verify then params.checktype = verify.checktype params.checkdevicecode = Text.urlEncode(verify.code) end return get("/login", params, ACCOUNT_HEADERS, function(wire, err) if not wire then cb(nil, err); return end if type(wire.token) == "string" and wire.token ~= "" then cb(wire.token); return end if tonumber(wire.code) == DEVICE_VERIFY_CODE and not verify then cb(nil, nonEmpty(wire.message) or _("晋江要求设备验证"), "verify") return end cb(nil, nonEmpty(wire.message) or _("晋江登录失败")) end) end --- 让晋江给账号绑定的手机 / 邮箱发设备验证码。 ---@param account string ---@param checktype "phone"|"email" ---@param cb fun(message: string|nil, err: string|nil) ---@return HttpJob function Token.sendCodeAsync(account, checktype, cb) local headers = { ["User-Agent"] = "Mobile", ["Accept-Encoding"] = "identity" } return Request.post("https://app.jjwxc.org/appDevicesecurityAndroid/getDeviceSecurityCode", Text.formEncode({ versionCode = 401, username = account, checktype = checktype }), { accept = "application/json", headers = headers, content_type = "application/x-www-form-urlencoded", timeout = require("http.timeout").net(1), }, function(raw, err) if not raw then cb(nil, err and tostring(err) or _("请求失败")); return end local ok, wire = pcall(JSON.decode, raw) if not ok or type(wire) ~= "table" then cb(nil, _("响应无效")); return end -- 发送成功时提示语在 data.message;失败(如 2021 缺少前置步骤、1004)只有顶层 message local sent = type(wire.data) == "table" and wire.data.message if type(sent) == "string" and sent ~= "" and tonumber(wire.code) ~= 1004 then cb(sent); return end cb(nil, nonEmpty(wire.message) or _("晋江没有确认验证码已发送")) end) end --- 带 token 的书架;旧请求不能让新登录态过期。 ---@param cb fun(rows: table[]|nil, err: string|nil) ---@return { cancel: fun() } function Token.shelfAsync(cb) ---@type boolean, CancelHandle|nil local cancelled, job = false, nil local function fetch(token, retry) local pending = true local initial = get("/incrementFavorite", { versionCode = 185, token = Text.urlEncode(token), backupTime = 1041350400, order = 1 }, ACCOUNT_HEADERS, function(wire, err) pending = false if cancelled then return end if not wire then cb(nil, err); return end if tonumber(wire.code) == LOGIN_CODE and retry then local fresh = Token.get() if fresh and fresh ~= token then fetch(fresh, false) else cb(nil, require("source.auth").expire(SOURCE_ID, require("moonsource_jjwxc.setting").expired())) end return end local list = wire.addData if type(list) ~= "table" then cb(nil, wire.message or _("晋江书架无法解析")); return end require("source.auth").ok(SOURCE_ID) cb(Mapper.tokenFavRows(list)) end) if pending then job = initial end end fetch(Token.get(), true) return { cancel = function() cancelled = true if job then job.cancel() end end } end --- 已购正文。encryptType 为 jj 时一定是 DES;漏标时不像章节的内容才解,解开后像章节才采用。 ---@param content string ---@param encrypt_type string|nil ---@return string|nil function Token.chapterText(content, encrypt_type) local function readable(text) return type(text) == "string" and (text:find("