forked from moon-source/moonsource-fanqie
每个书源一个独立插件目录,含微信读书公众号订阅、书架导入等完整功能。 Co-authored-by: Copilot <[email protected]>
275 lines
10 KiB
Lua
275 lines
10 KiB
Lua
--[[--
|
||
番茄阅读设备身份:本机生成画像,经 device_register 换取 device_id/install_id。
|
||
禁止复用别人的 did;每台 KOReader 安装各自持久化一份。
|
||
|
||
@module koplugin.book.moonsource_fanqie.reading.device
|
||
--]]
|
||
|
||
local JSON = require("json")
|
||
local Request = require("http.request")
|
||
local Aes = require("crypto.aes")
|
||
local logger = require("utils.log")
|
||
local bit = require("bit")
|
||
local _ = require("l10n").get("moonsource_fanqie")
|
||
|
||
local Device = {}
|
||
|
||
local REGISTER_HOST = "log.snssdk.com"
|
||
local REGISTER_PATH = "/service/2/device_register/"
|
||
local TIMEOUT_RATIO = 2 / 3 -- × 「网络超时」,缺省 20 秒
|
||
|
||
-- 画像模板(不含服务端签发的 id)。
|
||
local PROFILE_TEMPLATE = {
|
||
channel = "43536163a",
|
||
version_code = "73733",
|
||
version_name = "7.3.7.33",
|
||
device_type = "P30",
|
||
device_brand = "HUAWEI",
|
||
language = "zh",
|
||
os_api = "31",
|
||
os_version = "12",
|
||
resolution = "1280*720",
|
||
dpi = "240",
|
||
host_abi = "arm64-v8a",
|
||
rom_version = "S643.217451.03386707",
|
||
}
|
||
|
||
local urandom = require("moonsource_fanqie.reading.crypto").urandom
|
||
|
||
local function uuid4()
|
||
local raw = urandom(16)
|
||
local b = { raw:byte(1, 16) }
|
||
b[7] = bit.bor(bit.band(b[7], 0x0f), 0x40)
|
||
b[9] = bit.bor(bit.band(b[9], 0x3f), 0x80)
|
||
return string.format(
|
||
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
||
b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8],
|
||
b[9], b[10], b[11], b[12], b[13], b[14], b[15], b[16])
|
||
end
|
||
|
||
---@return table 本地画像(含 cdid/openudid,尚无 device_id)
|
||
function Device.newLocalProfile()
|
||
local out = {}
|
||
for k, v in pairs(PROFILE_TEMPLATE) do
|
||
out[k] = v
|
||
end
|
||
out.cdid = uuid4()
|
||
out.openudid = Aes.to_hex(urandom(8))
|
||
out.clientudid = uuid4()
|
||
return out
|
||
end
|
||
|
||
---@param src table|nil
|
||
---@return table|nil
|
||
function Device.normalize(src)
|
||
if type(src) ~= "table" then return nil end
|
||
local profile = type(src.device_profile) == "table" and src.device_profile or src
|
||
local device_id = tostring(src.device_id or "")
|
||
local install_id = tostring(src.install_id or "")
|
||
if device_id == "" or install_id == "" then
|
||
return nil
|
||
end
|
||
local out = Device.newLocalProfile()
|
||
for k in pairs(PROFILE_TEMPLATE) do
|
||
if profile[k] ~= nil and tostring(profile[k]) ~= "" then
|
||
out[k] = tostring(profile[k])
|
||
end
|
||
end
|
||
if profile.cdid and profile.cdid ~= "" then out.cdid = tostring(profile.cdid) end
|
||
if profile.openudid and profile.openudid ~= "" then out.openudid = tostring(profile.openudid) end
|
||
if profile.clientudid and profile.clientudid ~= "" then
|
||
out.clientudid = tostring(profile.clientudid)
|
||
elseif src.clientudid and src.clientudid ~= "" then
|
||
out.clientudid = tostring(src.clientudid)
|
||
end
|
||
out.device_id = device_id
|
||
out.install_id = install_id
|
||
out.version_code = tostring(out.version_code or PROFILE_TEMPLATE.version_code)
|
||
out.version_name = tostring(out.version_name or PROFILE_TEMPLATE.version_name)
|
||
return out
|
||
end
|
||
|
||
---@param settings FanqieSettings
|
||
---@param device table
|
||
local function persist(settings, device)
|
||
settings:set("reading_device", {
|
||
install_id = device.install_id,
|
||
device_id = device.device_id,
|
||
clientudid = device.clientudid,
|
||
device_profile = {
|
||
channel = device.channel,
|
||
version_code = device.version_code,
|
||
version_name = device.version_name,
|
||
device_type = device.device_type,
|
||
device_brand = device.device_brand,
|
||
language = device.language,
|
||
os_api = device.os_api,
|
||
os_version = device.os_version,
|
||
resolution = device.resolution,
|
||
dpi = device.dpi,
|
||
host_abi = device.host_abi,
|
||
cdid = device.cdid,
|
||
openudid = device.openudid,
|
||
rom_version = device.rom_version,
|
||
},
|
||
})
|
||
settings:flush()
|
||
end
|
||
|
||
---@param profile table
|
||
---@return string url, string body, table headers
|
||
local function buildRegisterRequest(profile)
|
||
local now = os.time() * 1000 + math.random(0, 999)
|
||
local openudid = profile.openudid
|
||
local cdid = profile.cdid
|
||
local qs = table.concat({
|
||
"ac=wifi",
|
||
"channel=" .. tostring(profile.channel),
|
||
"aid=1967",
|
||
"app_name=novelapp",
|
||
"version_code=" .. tostring(profile.version_code),
|
||
"version_name=" .. tostring(profile.version_name),
|
||
"device_platform=android",
|
||
"ssmix=a",
|
||
"device_type=" .. tostring(profile.device_type),
|
||
"device_brand=" .. tostring(profile.device_brand),
|
||
"language=" .. tostring(profile.language),
|
||
"os_api=" .. tostring(profile.os_api),
|
||
"os_version=" .. tostring(profile.os_version),
|
||
"openudid=" .. tostring(openudid),
|
||
"manifest_version_code=" .. tostring(profile.version_code),
|
||
"resolution=" .. tostring(profile.resolution),
|
||
"dpi=" .. tostring(profile.dpi),
|
||
"update_version_code=" .. tostring(profile.version_code),
|
||
"_rticket=" .. tostring(now),
|
||
"cdid=" .. tostring(cdid),
|
||
}, "&")
|
||
local body = {
|
||
magic_tag = "ss_app_log",
|
||
header = {
|
||
display_name = "番茄小说",
|
||
update_version_code = tonumber(profile.version_code) or 73733,
|
||
manifest_version_code = tonumber(profile.version_code) or 73733,
|
||
aid = 1967,
|
||
channel = profile.channel,
|
||
package = "com.dragon.read",
|
||
app_version = profile.version_name,
|
||
version_code = tonumber(profile.version_code) or 73733,
|
||
sdk_version = "3.7.2.cn",
|
||
os = "Android",
|
||
os_version = profile.os_version,
|
||
os_api = tonumber(profile.os_api) or 31,
|
||
device_model = profile.device_type,
|
||
device_brand = profile.device_brand,
|
||
device_manufacturer = profile.device_brand,
|
||
cpu_abi = profile.host_abi,
|
||
density_dpi = tonumber(profile.dpi) or 240,
|
||
resolution = (tostring(profile.resolution or "")):gsub("%*", "x"),
|
||
language = profile.language,
|
||
timezone = 8,
|
||
access = "wifi",
|
||
not_request_sender = 0,
|
||
rom_version = profile.rom_version,
|
||
cdid = cdid,
|
||
openudid = openudid,
|
||
clientudid = profile.clientudid or uuid4(),
|
||
region = "CN",
|
||
tz_name = "Asia/Shanghai",
|
||
tz_offset = 28800,
|
||
sim_region = "cn",
|
||
req_id = uuid4(),
|
||
apk_first_install_time = now - 86400000,
|
||
is_system_app = 0,
|
||
sdk_flavor = "china",
|
||
},
|
||
_gen_time = now,
|
||
}
|
||
local ua = string.format(
|
||
"com.dragon.read/%s (Linux; U; Android %s; zh_CN; %s; Build/%s;tt-ok/3.12.13.4-tiktok)",
|
||
profile.version_code, profile.os_version, profile.device_type,
|
||
tostring(profile.rom_version):match("^[^%+]+") or "S643")
|
||
return "https://" .. REGISTER_HOST .. REGISTER_PATH .. "?" .. qs,
|
||
JSON.encode(body),
|
||
{
|
||
["Content-Type"] = "application/json; charset=utf-8",
|
||
["User-Agent"] = ua,
|
||
["Accept-Encoding"] = "identity",
|
||
}
|
||
end
|
||
|
||
--- 向 log.snssdk.com 注册,拿到本机专属 device_id/install_id。
|
||
---@param settings FanqieSettings
|
||
---@param cb fun(device: table|nil, err: string|nil)
|
||
---@return CancelHandle
|
||
function Device.registerAsync(settings, cb)
|
||
local profile = Device.newLocalProfile()
|
||
local url, body, headers = buildRegisterRequest(profile)
|
||
return Request.request({
|
||
url = url,
|
||
method = "POST",
|
||
headers = headers,
|
||
body = body,
|
||
timeout = require("http.timeout").net(TIMEOUT_RATIO),
|
||
allow_redirects = true,
|
||
}, function(res, err)
|
||
-- turbo 超时仍返回 res 表,错误在 err;只看 res 会变成「HTTP nil」。
|
||
if err or not res then
|
||
local text = tostring(err or "")
|
||
if text:find("refused", 1, true) or text:find("Could not connect", 1, true) then
|
||
cb(nil, _("番茄正文需要注册设备,当前网络拦了 log.snssdk.com"))
|
||
return
|
||
end
|
||
cb(nil, err or _("番茄设备注册失败,请放行 log.snssdk.com"))
|
||
return
|
||
end
|
||
local code = tonumber(res.code)
|
||
if not Request.ok(code) then
|
||
cb(nil, "device_register HTTP " .. tostring(code))
|
||
return
|
||
end
|
||
local text = res.body or ""
|
||
if text == "" then
|
||
cb(nil, "device_register 空响应")
|
||
return
|
||
end
|
||
local ok, data = pcall(JSON.decode, text)
|
||
if not ok or type(data) ~= "table" then
|
||
cb(nil, "device_register 响应无效")
|
||
return
|
||
end
|
||
local device_id = tostring(data.device_id_str or data.device_id or "")
|
||
local install_id = tostring(data.install_id_str or data.install_id or "")
|
||
if device_id == "" or install_id == "" then
|
||
cb(nil, "device_register 未返回 device_id")
|
||
return
|
||
end
|
||
profile.device_id = device_id
|
||
profile.install_id = install_id
|
||
persist(settings, profile)
|
||
logger.info("fanqie device_register ok", "did=" .. device_id, "iid=" .. install_id)
|
||
cb(profile)
|
||
end)
|
||
end
|
||
|
||
--- 已有身份下一帧返回;否则异步注册。
|
||
--- 命中也必须异步:调用方在回调里换上后续请求的句柄,同步回调后再返回的句柄会把它覆盖掉。
|
||
---@param settings FanqieSettings
|
||
---@param cb fun(device: table|nil, err: string|nil)
|
||
---@return CancelHandle
|
||
function Device.ensureAsync(settings, cb)
|
||
local cached = Device.get(settings)
|
||
if cached then
|
||
return require("source.base").defer(cb, cached)
|
||
end
|
||
return Device.registerAsync(settings, cb)
|
||
end
|
||
|
||
--- 同步取已落盘身份;没有则返回 nil(调用方应走 ensureAsync)。
|
||
---@param settings FanqieSettings
|
||
---@return table|nil
|
||
function Device.get(settings)
|
||
return Device.normalize(settings:get("reading_device", nil))
|
||
end
|
||
|
||
return Device
|