@yusho/server 0.1.17 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/lanxfer.mjs +5 -5
- package/app/launcher.mjs +2715 -2715
- package/app/net.mjs +1 -1
- package/app/p2plink.mjs +142 -132
- package/connector/lib/connector.mjs +3 -3
- package/connector/lib/lanfile.mjs +2 -2
- package/node_modules/@local/yusho-pc/lib/client.js +10 -10
- package/package.json +1 -1
package/app/lanxfer.mjs
CHANGED
|
@@ -11,7 +11,7 @@ const seal = (key, i, plain) => { const c = crypto.createCipheriv('aes-256-gcm',
|
|
|
11
11
|
const open = (key, i, s) => { const d = crypto.createDecipheriv('aes-256-gcm', key, nonceOf(i)); d.setAuthTag(s.subarray(s.length - 16)); return Buffer.concat([d.update(s.subarray(0, s.length - 16)), d.final()]) }
|
|
12
12
|
const fileSha = (p) => new Promise((ok, no) => { const h = crypto.createHash('sha256'); fs.createReadStream(p).on('data', (d) => h.update(d)).on('end', () => ok(h.digest('hex'))).on('error', no) })
|
|
13
13
|
|
|
14
|
-
const RELAY_MAX = 100 * 1024 * 1024 //
|
|
14
|
+
const RELAY_MAX = 100 * 1024 * 1024 // 远程直连没连上、只能经服务器中转时,最多传 100 MB(服务器带宽小)
|
|
15
15
|
// 电脑主动发来(file.push,12-03):acceptPush 只在手机上开;pcOf(pk) 认得是自己的电脑才收;
|
|
16
16
|
// askPush({ name, size, pc, id }) → true 直接收 / false 先挂着等用户点“收下”(比如用流量又超过 100 MB);收完调 onReceived(view)
|
|
17
17
|
export function createLanXfer({ workspace, send, log = () => {}, changed = () => {}, p2p = null, acceptPush = false, pcOf = () => null, askPush = async () => true, onReceived = () => {} }) {
|
|
@@ -35,7 +35,7 @@ export function createLanXfer({ workspace, send, log = () => {}, changed = () =>
|
|
|
35
35
|
if (m.type === 'file.lan') { (waits.get(m.req ?? m.id) ?? waits.get(m.id))?.(m); return }
|
|
36
36
|
if (m.type === 'file.saved' || m.type === 'file.failed') { if (m.req && waits.has(m.req)) return waits.get(m.req)(m); if (waits.has(m.id)) return waits.get(m.id)(m); finals.get(m.id)?.(m) }
|
|
37
37
|
}
|
|
38
|
-
/** 传输通道:同一个局域网走 HTTP
|
|
38
|
+
/** 传输通道:同一个局域网走 HTTP(最快);连不上就远程直连(文件不经过服务器);远程直连也只能走中转时,大文件不传 */
|
|
39
39
|
async function transport(lan, pc, x) {
|
|
40
40
|
try {
|
|
41
41
|
const base = await pickUrl(lan)
|
|
@@ -44,16 +44,16 @@ export function createLanXfer({ workspace, send, log = () => {}, changed = () =>
|
|
|
44
44
|
put: async (i, body) => { const r = await fetch(base + '/' + i + '?t=' + lan.token, { method: 'PUT', body, signal: AbortSignal.timeout(60_000) }); if (!r.ok) throw new Error('HTTP ' + r.status) }, close: () => {} }
|
|
45
45
|
} catch (lanErr) {
|
|
46
46
|
if (!p2p || !lan.id) throw lanErr
|
|
47
|
-
upd(x, { state: '
|
|
47
|
+
upd(x, { state: '不在同一个网络,远程直连…' })
|
|
48
48
|
let link
|
|
49
|
-
try { link = await p2p.open(pc.pk) } catch (e) { throw new Error('
|
|
49
|
+
try { link = await p2p.open(pc.pk) } catch (e) { throw new Error('不在同一个网络,远程直连也没连上(' + e.message + ')。回到同一个 Wi-Fi 再传') }
|
|
50
50
|
if (link.route === 'relay' && (x.size || lan.size || 0) > RELAY_MAX) { link.close(); throw new Error('两边网络都不让直连,只能经服务器中转,文件超过 100 MB 不走中转。回到同一个 Wi-Fi 再传') }
|
|
51
51
|
const call = async (head, body) => { const r = await link.request({ id: lan.id, t: lan.token, ...head }, body ?? null, 90_000); if (r.head.code !== 200) throw new Error(r.head.error || 'HTTP ' + r.head.code); return r }
|
|
52
52
|
return { route: link.route === 'relay' ? 'relay' : 'p2p', status: async () => (await call({ op: 'status' })).head,
|
|
53
53
|
get: async (i) => (await call({ op: 'get', i })).body, put: async (i, body) => { await call({ op: 'put', i }, body) }, close: () => link.close() }
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
-
const ROUTE_ZH = { lan: '局域网直连', p2p: '
|
|
56
|
+
const ROUTE_ZH = { lan: '局域网直连', p2p: '远程直连', relay: '经服务器中转' }
|
|
57
57
|
async function pool(n, items, fn) { let i = 0; await Promise.all(Array.from({ length: Math.min(n, items.length) }, async () => { while (i < items.length) { const k = items[i++]; await fn(k) } })) }
|
|
58
58
|
|
|
59
59
|
/** 把 App 工作目录里的一个文件发给电脑 */
|