UNPKG

@honor-minigame/cli

Version:

honor minigame pack cli

197 lines (180 loc) 5.57 kB
/** * Copyright (C) 2017, hapjs.org. All rights reserved. */ import os from 'os' import fs from 'fs' import path from 'path' import ipaddr from 'ipaddr.js' import { projectPath } from '../utils/index.js' import { error } from '../utils/logger.js' import { homedir } from 'os' const home = homedir() export const CLIENT_PORT = 39517 export const clientRecordPath = path.join(home, 'quickgame-toolkit-client-records.json') export const LINK_MODE = { NULL: 0, WIFI: 1, ADB: 2 } /** * 读取记录文件 * @param {String} clientRecordPath - 记录文件的地址 * @returns {Object} - 记录的全部设备信息 */ export function getRecords (clientRecordPath) { let recordData = { records: {} } try { recordData = JSON.parse(fs.readFileSync(clientRecordPath).toString()) } catch (err) { console.error(`读取client.json文件出错: ${err.message}`) } return recordData } /** * 将设备记录写入记录文件 * @param {String} clientRecordPath - 记录文件的地址 * @param {Object} recordData - 要写入的全部设备记录 */ function writeRecords (clientRecordPath, recordData) { try { fs.writeFileSync(clientRecordPath, JSON.stringify(recordData, null, 2)) } catch (err) { error(`### App Server ### writeRecords出错: ${err.message}`) } } /** * 读取一个项目的设备记录 * @param {Object} recordData - 全部设备信息 * @returns {Array} - 设备记录 */ export function getProjectClients (recordData) { let records = recordData.records records = records instanceof Object ? records : {} const clients = records[projectPath] if (clients && clients instanceof Array) { return clients } return [] } /** * 保存设备记录 * @param {String} clientRecordPath - 记录文件的地址 * @param {String} newClient - 新的设备记录 * @param {callback} logHook - log信息的钩子,参数为要打印的信息 */ export function recordClient (clientRecordPath, newClient, logHook) { try { const pathParentDir = path.dirname(clientRecordPath) let recordData mkdirsSync(pathParentDir) if (fs.existsSync(clientRecordPath)) { recordData = getRecords(clientRecordPath) let clients = getProjectClients(recordData) logHook && logHook(`writeClientLogFile(): before: ${JSON.stringify(recordData.records)}`) clients = clients.filter(client => { return client.ip !== newClient.ip || client.port !== newClient.port }) // 保留最后的4条记录,最多记录5条 while (clients.length > 4) { clients.shift() } recordData.records[projectPath] = clients } else { recordData = { records: {} } recordData.records[projectPath] = [] } // 写入文件 recordData.records[projectPath].push(newClient) writeRecords(clientRecordPath, recordData) logHook && logHook(`writeClientLogFile(): after: ${JSON.stringify(recordData.records)}`) } catch (err) { error(`### App Server ### recordClient出错: ${err.message}`) } } /** * 根据设备sn和ip获取设备的记录 * @param {String} clientRecordPath - 记录文件地址 * @param {String} sn - 设备序列号 * @param {String} ip * @returns {Object} - 匹配的设备记录 */ export function getRecordClient (clientRecordPath, sn, ip) { try { if (fs.existsSync(clientRecordPath)) { const recordData = getRecords(clientRecordPath) const clients = getProjectClients(recordData) return clients.find(client => { return client.sn === sn && client.ip === ip && client.port }) } } catch (err) { error(`### App Server ### getRecordClient出错: ${err.message}`) } } /** * 创建任意深度的路径的文件夹 * @param dirname * @returns {boolean} */ export function mkdirsSync (dirname) { if (fs.existsSync(dirname)) { return true } else { if (mkdirsSync(path.dirname(dirname))) { fs.mkdirSync(dirname) return true } } } /** * 获取服务器端的IP */ export function getIPv4IPAddress () { const ifaces = os.networkInterfaces() let result for (const prop in ifaces) { if (Object.prototype.hasOwnProperty.call(ifaces, prop)) { const iface = ifaces[prop] iface.every(eachAlias => { if ( eachAlias.family === 'IPv4' && !eachAlias.internal && eachAlias.address !== '127.0.0.1' ) { result = eachAlias return false } return true }) if (result !== void 0) { break } } } return result && result.address } // ipv6地址转ipv4 export function ipConvert (str) { if (ipaddr.IPv6.isValid(str)) { var addr = ipaddr.IPv6.parse(str) if (addr.isIPv4MappedAddress()) { return addr.toIPv4Address().toString() } } return str } /** * 冗余方法,获取客户端的请求信息 * @param request * @returns {{clientIp: (any|*|string), sn, linkMode}} */ export function getClientFromRequest (request) { const clientIp = ipConvert(request.ip) const serverIp = getIPv4IPAddress() const sn = request.header['device-serial-number'] let linkMode = LINK_MODE.NULL if (clientIp === '127.0.0.1') { linkMode = LINK_MODE.ADB } else if (clientIp !== '127.0.0.1' && clientIp !== serverIp) { linkMode = LINK_MODE.WIFI } return { clientIp, sn, linkMode, serverIp } }