host-manager.mjs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {readFile,writeFile,rename,mkdir,rm} from 'node:fs/promises'
  2. import {join} from 'node:path'
  3. import {randomUUID} from 'node:crypto'
  4. import {request as httpRequest} from 'node:http'
  5. import {request as httpsRequest} from 'node:https'
  6. import {parseDesktopHostConfiguration} from './host-config.mjs'
  7. const FATAL = Symbol('desktop-host-fatal')
  8. function remoteExchange(config,body){return new Promise((resolve,reject)=>{
  9. const url=new URL(`/api/desktop-host/v1/${config.hostId}/exchange`,config.serverURL)
  10. if(!['http:','https:'].includes(url.protocol))return reject(new Error('夭夭服务地址无效'))
  11. const bytes=Buffer.from(JSON.stringify(body)),send=url.protocol==='https'?httpsRequest:httpRequest
  12. const req=send(url,{method:'POST',headers:{Authorization:`Bearer ${config.token}`,'x-desktop-host-protocol':'1','content-type':'application/json','content-length':bytes.length}},res=>{
  13. let size=0;const chunks=[]
  14. res.on('data',chunk=>{size+=chunk.length;if(size>256*1024)res.destroy(new Error('桌面命令超过限制'));else chunks.push(chunk)})
  15. res.on('error',reject)
  16. res.on('end',()=>{try{
  17. if(res.statusCode===401||res.statusCode===403){const error=new Error('桌面主机授权已失效,请在夭夭服务中重新注册并导入新配置');error[FATAL]=true;throw error}
  18. if(res.statusCode===409){const error=new Error('桌面主机协议不兼容,请更新夭夭后重新导入');error[FATAL]=true;throw error}
  19. if(res.statusCode!==200)throw new Error(`桌面主机服务未连接(HTTP ${res.statusCode})`)
  20. resolve(JSON.parse(Buffer.concat(chunks).toString()))
  21. }catch(error){reject(error)}})
  22. })
  23. req.on('error',reject)
  24. req.setTimeout(10000,()=>req.destroy(new Error('桌面主机连接超时')))
  25. req.end(bytes)
  26. })}
  27. /** Remote desktop host: imports an encrypted pairing config and exchanges
  28. * commands with the paired server. Native-only; no browser IPC. */
  29. export class DesktopHostManager {
  30. constructor(options){
  31. if(typeof options?.createCore!=='function')throw new Error('桌面主机需要命令执行核心')
  32. this.options=options
  33. this.path=join(options.home,'desktop-host.enc')
  34. this.createCore=options.createCore
  35. this.state='未配置桌面主机'
  36. this.core=undefined
  37. this.config=undefined
  38. this.timer=undefined
  39. this.backoff=400
  40. this.chain=Promise.resolve()
  41. this.generation=0
  42. }
  43. publish(message){this.state=message;this.options.onState?.(message)}
  44. serial(work){const result=this.chain.catch(()=>{}).then(work);this.chain=result;return result}
  45. async read(){
  46. let encrypted
  47. try{encrypted=await readFile(this.path)}catch(error){if(error.code==='ENOENT')return;throw error}
  48. try{return parseDesktopHostConfiguration(JSON.parse(await this.options.decrypt(encrypted)))}
  49. catch{throw new Error('无法解锁桌面主机配置,请检查钥匙串或重新导入')}
  50. }
  51. importFile(path){
  52. const generation=++this.generation
  53. return this.serial(async()=>{
  54. let config
  55. try{config=parseDesktopHostConfiguration(JSON.parse(await readFile(path,'utf8')))}
  56. catch(error){throw error instanceof Error?error:new Error('桌面主机配置无效,请从夭夭重新下载配置')}
  57. // Encrypt before stopping a working host: failed Keychain access preserves it.
  58. if(generation!==this.generation)return
  59. const bytes=await this.options.encrypt(JSON.stringify(config))
  60. if(generation!==this.generation)return
  61. await this.stopLoop()
  62. if(generation!==this.generation)return
  63. await mkdir(this.options.home,{recursive:true,mode:0o700})
  64. const temporary=`${this.path}.${randomUUID()}`
  65. try{await writeFile(temporary,bytes,{flag:'wx',mode:0o600});await rename(temporary,this.path)}
  66. finally{await rm(temporary,{force:true})}
  67. if(generation!==this.generation)return
  68. await this.startLoop(config)
  69. })
  70. }
  71. start(){const generation=++this.generation;return this.serial(async()=>{const config=await this.read();if(generation!==this.generation)return;if(config)await this.startLoop(config)})}
  72. async startLoop(config){
  73. await this.stopLoop()
  74. this.config=config
  75. this.core=this.createCore({root:this.options.root,dataRoot:this.options.dataRoot})
  76. this.backoff=400
  77. this.publish(`桌面主机正在连接 ${new URL(config.serverURL).host}`)
  78. this.timer=setTimeout(()=>void this.cycle(),400)
  79. this.timer.unref?.()
  80. }
  81. async cycle(){
  82. const config=this.config,core=this.core
  83. if(!config||!core||this.core!==core)return
  84. try{
  85. const value=await remoteExchange(config,{host:core.info(),results:core.takeResults()})
  86. this.backoff=400
  87. if(this.config!==config||this.core!==core)return
  88. this.publish(`桌面主机已连接 ${new URL(config.serverURL).host}`)
  89. await core.handle(value.commands??[])
  90. }catch(error){
  91. if(this.config!==config||this.core!==core)return
  92. if(error&&error[FATAL]){this.publish(error.message);await this.stopLoop();return}
  93. this.backoff=Math.min(this.backoff*2,5000)
  94. this.publish(`桌面主机连接中断,正在重试:${String(error.message).slice(0,120)}`)
  95. }finally{
  96. if(this.core===core){this.timer=setTimeout(()=>void this.cycle(),this.backoff);this.timer.unref?.()}
  97. }
  98. }
  99. async stopLoop(){
  100. clearTimeout(this.timer);this.timer=undefined
  101. const core=this.core
  102. this.core=undefined;this.config=undefined
  103. if(core)await core.close().catch(()=>{})
  104. }
  105. stop(){this.generation++;return this.serial(()=>this.stopLoop())}
  106. forget(){this.generation++;return this.serial(async()=>{await this.stopLoop();await rm(this.path,{force:true});this.publish('未配置桌面主机')})}
  107. revoke(){return this.serial(async()=>{await this.core?.revoke()})}
  108. }