✨ 初始提交:月读第三方书源插件集合

每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。

Co-authored-by: Copilot <[email protected]>
This commit is contained in:
GarfieldandCopilot committed 2026-11-16 12:08:03 +08:00
commit 2bc5c24ea7
27 files changed
+3760

No files matched your search

@@ -0,0 +1,354 @@
--[[--
晋江「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 = "ext.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<string, any>
---@param headers table<string, string>
---@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("<br", 1, true) or text:find("[\228-\233]")) ~= nil
end
if encrypt_type ~= "jj" and readable(content) then return content end
local plain = require("crypto.des").decryptBase64(content, DES_KEY, DES_IV)
if encrypt_type == "jj" or (plain and readable(plain)) then return plain end
return nil
end
--- 带 token 取章节正文:已购 VIP 章的响应整包加密、content 又是 DES/Base64 密文,解密放子进程。
--- token 无效(1004)提示重新登录;没买(1025)直接回 unpaid。
--- CDN 偶发给出「头体自洽但整包 DES 解不开」的坏包(模拟器并行压测约千分之一到百分之二),
--- 失败落成「响应无效」/「已购章节解密失败」且不落盘,看起来像隔一章才能看:整次请求重试一次。
---@param novel_id string
---@param chapter_id string
---@param cb fun(text: string|nil, err: string|nil, kind: "unpaid"|nil)
---@return { cancel: fun() }
function Token.contentAsync(novel_id, chapter_id, cb)
---@type boolean, CancelHandle|nil
local cancelled, job = false, nil
local function fetch(token, retry_login, retry_decrypt)
local pending = true
local initial = get("/chapterContent", {
novelId = novel_id, chapterId = chapter_id, versionCode = 381, token = Text.urlEncode(token),
}, CONTENT_HEADERS, function(wire, err)
pending = false
if cancelled then return end
if not wire then
if retry_decrypt and err == _("响应无效") then
require("utils.log").warn("jjwxc chapterContent body decrypt retry", novel_id, chapter_id)
fetch(token, retry_login, false)
return
end
cb(nil, err)
return
end
local data = type(wire.data) == "table" and wire.data.content ~= nil and wire.data or wire
local code = tonumber(wire.code)
if code == LOGIN_CODE or code == UNPAID_CODE or type(data.content) ~= "string" or data.content == "" then
if code == LOGIN_CODE and retry_login then
local fresh = Token.get()
if fresh and fresh ~= token then fetch(fresh, false, retry_decrypt)
else cb(nil, require("source.auth").expire(SOURCE_ID, require("moonsource_jjwxc.setting").expired())) end
return
end
cb(nil, wire.message or _("晋江章节内容为空"), code == UNPAID_CODE and "unpaid" or nil)
return
end
local encrypt_type = data.encryptType
local content_len = #data.content
local decoding = true
local decoder = Job.run(function()
return { text = Token.chapterText(data.content, encrypt_type) }
end, {
name = "jjwxc_token_content",
kind = "light",
timeout = 60,
on_done = function(res)
decoding = false
if cancelled then return end
if res.text then cb(res.text); return end
require("utils.log").warn("jjwxc content DES failed", novel_id, chapter_id,
"encryptType", tostring(encrypt_type), "content_len", content_len)
if retry_decrypt then
fetch(token, false, false)
return
end
cb(nil, _("晋江已购章节解密失败"))
end,
on_failed = function(e) if not cancelled then cb(nil, tostring(e)) end end,
on_cancelled = function() end,
})
if decoding then job = decoder end
end)
if pending then job = initial end
end
fetch(Token.get(), true, true)
return { cancel = function()
cancelled = true
if job then job:cancel() end
end }
end
return Token
@@ -0,0 +1,265 @@
--[[--
晋江「网页」协议:手机网页(m.jjwxc.com)+ 登录 Cookie。
Cookie 来自远程管理里的「浏览器登录」,或在电脑浏览器登录后手动粘贴(手机网页登录要过数美验证,设备上做不了)。
能做:个人收藏(/my/fav?p= 分页,GBK 的 HTML)、取消收藏、VIP 章正文(/vip/<书>/<章>,已购才有正文,反爬字体见 font.lua)、
VIP 章购买状态。Cookie 失效时网页 302 到登录页。
协议接口(protocols/init.lua 按设置选一个协议调用):`available` / `shelfAsync` / `contentAsync`。
@module koplugin.book.moonsource_jjwxc.protocols.web
--]]
local Charset = require("utils.charset")
local Font = require("moonsource_jjwxc.font")
local Job = require("workers.job")
local Mapper = require("moonsource_jjwxc.mapper")
local Request = require("http.request")
local _ = require("l10n").get("moonsource_jjwxc")
local Web = {}
Web.id = "web"
local SOURCE_ID = "ext.jjwxc"
local HOST = "https://m.jjwxc.com"
local FONT_URL = "https://static.jjwxc.net/tmp/fonts/%s.ttf"
local UA = "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) "
.. "Version/16.0 Mobile/15E148 Safari/604.1"
---@return string
function Web.label() return _("网页 Cookie") end
---@return string|nil
function Web.cookie()
local cookie = require("utils.settings").getSource(SOURCE_ID).cookie
return type(cookie) == "string" and cookie ~= "" and cookie or nil
end
---@return boolean
function Web.available() return Web.cookie() ~= nil end
--- 段评接口(Pc/comment)的登录态:VIP 章节要求,网页协议走 Cookie。
---@return string query
---@return table headers
function Web.commentAuth() return "", { Cookie = Web.cookie() } end
--- 带登录态取网页,回调 UTF-8 的 HTML。302 到登录页 = Cookie 失效。
---@param path string
---@param cookie string|nil
---@param cb fun(html: string|nil, err: string|nil, kind: "unpaid"|nil)
---@return HttpJob
local function get(path, cookie, cb)
return Request.get(HOST .. path, {
headers = { ["User-Agent"] = UA },
jar = require("http.cookie_jar").session(SOURCE_ID, cookie),
timeout = require("http.timeout").net(0.5),
cache_ttl = 0,
}, function(raw, err, res)
if not raw then
local location = res and tonumber(res.code) == 302 and Request.header(res, "Location")
if location and not location:find("login", 1, true) then
-- 跳到非登录页(如未购买章节的订阅页)说明 Cookie 有效
cb(nil, _("晋江 VIP 章节未购买"), "unpaid")
elseif location then
-- 校验新 Cookie 时不能污染已保存的登录态。
local msg = require("moonsource_jjwxc.setting").expired()
cb(nil, cookie == Web.cookie() and require("source.auth").expire(SOURCE_ID, msg) or msg)
else
cb(nil, err and tostring(err) or _("请求失败"))
end
return
end
local html, charset = Charset.toUtf8(raw, Request.header(res, "Content-Type"))
if not html then cb(nil, charset); return end
cb(html)
end)
end
--- 拉个人收藏(一页列全)。cookie 显式传入:登录入口要先校验再保存。
---@param cookie string
---@param cb fun(rows: table[]|nil, err: string|nil)
---@return HttpJob
function Web.favAsync(cookie, cb)
return get("/my/fav", cookie, function(html, err)
if not html then cb(nil, err); return end
local rows = Mapper.favRows(html)
if not rows then cb(nil, _("晋江收藏页无法解析")); return end
if cookie == Web.cookie() then require("source.auth").ok(SOURCE_ID) end
cb(rows)
end)
end
--- 收藏按页拉。页面上没有下一页链接,一页 30 本,空页才是尽头。
--- 登录校验仍走 favAsync(只要第一页能解析)。
--- 后面某页失败、或翻到上限这一页还有新书,整次失败:半截列表交给书架对账会把没拉到的书藏掉。
local MAX_FAV_PAGES = 50
---@param cb fun(rows: table[]|nil, err: string|nil)
---@return { cancel: fun() }
function Web.shelfAsync(cb)
local cookie = Web.cookie()
local rows, seen, handle = {}, {}, {}
local function page(n)
local pending = true
local initial = get("/my/fav?p=" .. n, cookie, function(html, err)
pending = false
if handle.cancelled then return end
if cookie ~= Web.cookie() then cb(nil, _("账号已变化,请重新登录")); return end
if not html then cb(nil, err); return end
local batch = Mapper.favRows(html)
if not batch then cb(nil, _("晋江收藏页无法解析")); return end
if n == 1 and cookie == Web.cookie() then require("source.auth").ok(SOURCE_ID) end
local added = 0
for _idx, row in ipairs(batch) do
if not seen[row.id] then
seen[row.id] = true
rows[#rows + 1] = row
added = added + 1
end
end
if #batch == 0 then cb(rows); return end
if added == 0 then cb(nil, _("晋江收藏页无法解析")); return end
if n >= MAX_FAV_PAGES then cb(nil, _("晋江收藏页过多,没有同步完")); return end
page(n + 1)
end)
if pending then handle.inner = initial end
end
page(1)
function handle.cancel()
handle.cancelled = true
if handle.inner then handle.inner:cancel() end
end
return handle
end
--- 取消收藏。网页是「确认页 → /my/delBookSure」两步,这里直接走第二步。
---@param novel_id string
---@param cb fun(ok: boolean|nil, err: string|nil)
---@return { cancel: fun() }
function Web.favRemoveAsync(novel_id, cb)
local cookie = Web.cookie()
if not cookie then return require("source.base").defer(cb, nil, _("未登录晋江")) end
local cancelled, job, pending = false, nil, true
local initial = get("/my/delBookSure?novelid=" .. novel_id, cookie, function(html, err)
pending = false
if cancelled then return end
if not html then cb(nil, err); return end
if cookie ~= Web.cookie() then cb(nil, _("账号已变化,请重新登录")); return end
job = Web.shelfAsync(function(rows, reason)
if cancelled then return end
if not rows then cb(nil, reason); return end
for _idx, row in ipairs(rows) do
if row.id == novel_id then cb(nil, _("晋江没有确认取消收藏")); return end
end
cb(true)
end)
end)
if pending then job = initial end
return { cancel = function()
cancelled = true
if job then job.cancel() end
end }
end
--- 这章 VIP 买了没有:true 已购、false 未购、nil 没问出来(未登录 / 网络 / Cookie 失效)。
---@param novel_id string
---@param chapter_id string
---@param cb fun(paid: boolean|nil)
---@return HttpJob|nil
function Web.paidAsync(novel_id, chapter_id, cb)
local cookie = Web.cookie()
if not cookie then cb(nil); return nil end
return get("/vip/" .. novel_id .. "/" .. chapter_id, cookie, function(html, _err, kind)
if kind == "unpaid" then cb(false)
elseif html and html:find('id="contentvars"', 1, true) then cb(true)
else cb(nil) end
end)
end
--- 反爬字体的私有区字符映射回真字:下字体、解析映射都放子进程。
---@param text string
---@param font string
---@param cancelled fun(): boolean
---@param cb fun(text: string|nil, err: string|nil)
---@return { cancel: fun() }
local function decodeFont(text, font, cancelled, cb)
local job
local pending = true
local initial = Request.get(FONT_URL:format(font), {
timeout = require("http.timeout").net(0.5),
}, function(ttf, font_err)
pending = false
if cancelled() then return end
if not ttf then cb(nil, font_err and tostring(font_err) or _("晋江 VIP 章节字体无法解析")); return end
job = Job.run(function()
local map = Font.mapping(ttf)
return { text = map and Font.decode(text, map) }
end, {
name = "jjwxc_font",
kind = "light",
timeout = 60,
on_done = function(res)
if cancelled() then return end
if res.text then cb(res.text) else cb(nil, _("晋江 VIP 章节字体无法解析")) end
end,
on_failed = function(err) if not cancelled() then cb(nil, tostring(err)) end end,
on_cancelled = function() end,
})
end)
if pending then job = initial end
return { cancel = function() if job then job:cancel() end end }
end
--- VIP 章正文(纯文本,段落以换行分隔):网页只有已购章节才有正文。
---@param novel_id string
---@param chapter_id string
---@param cb fun(text: string|nil, err: string|nil, kind: "unpaid"|nil)
---@return { cancel: fun() }
function Web.contentAsync(novel_id, chapter_id, cb)
local cancelled, job = false, nil
local function isCancelled() return cancelled end
local pending = true
local initial = get("/vip/" .. novel_id .. "/" .. chapter_id, Web.cookie(), function(html, web_err, kind)
pending = false
if cancelled then return end
if not html then
cb(nil, web_err or _("晋江 VIP 章节未购买"), kind and "unpaid" or nil)
return
end
-- AES 解密与字体解析都是纯 Lua 重活,放子进程,免得卡 UI
local decoding = true
local decoder = Job.run(function() local text, why = Mapper.webContent(html); return { text = text, why = why } end, {
name = "jjwxc_vip",
kind = "light",
timeout = 60,
on_done = function(res)
decoding = false
if cancelled then return end
local text = res.text
if not text then
if html:find('id="contentvars"', 1, true) then
require("utils.log").warn("jjwxc vip decrypt failed", novel_id, chapter_id, res.why)
cb(nil, _("晋江 VIP 章节解密失败"))
else
cb(nil, _("晋江 VIP 章节未购买"), "unpaid")
end
return
end
local font = html:match("jjwxcfont_%w+")
if not (font and text:find("[\238\239][\128-\191][\128-\191]")) then cb(text); return end
job = decodeFont(text, font, isCancelled, cb)
end,
on_failed = function(err) if not cancelled then cb(nil, tostring(err)) end end,
on_cancelled = function() end,
})
if decoding then job = decoder end
end)
if pending then job = initial end
return { cancel = function()
cancelled = true
if job then job:cancel() end
end }
end
return Web