handow-shm
Version:
E2E test server with Handow engine
1 lines • 4.71 kB
JavaScript
;const e=require("express"),s=require("os"),t=require("fs"),r=require("https"),i=require("axios").create({httpsAgent:new r.Agent({rejectUnauthorized:!1})}),n=require("path"),o=require("js-base64").Base64,a=require("app-root-path"),u=require("handow"),h=e.Router(),m=require("./editorRoutes"),d=require("./sysRoutes"),c=require("./handowRoutes"),l=require("./shmRoutes"),g=require("../../package.json"),f=require(n.join(`${a}`,"/config.js"));f._rootPath=`${a}/`;const p=require("../services/shmSetting"),S=100;let x=null,y={};h.use((e,s,t)=>{if(N(e),void 0===e.session.startAt&&(e.session.startAt=new Date),"public"===p.get().shmMode){const s=Object.assign({},p.get());s.users=[],e.session.shmSetting=s,e.session.user={username:"Admin",email:"admin@email.com",isSigned:!0,role:"admin"}}else"secure"===p.get().shmMode?(void 0===e.session.user&&(e.session.user={username:"",email:"user@email.com",role:"user",isSigned:!1}),e.session.shmSetting={shmMode:p.get().shmMode,workspace:p.get().workspace,startPop:p.get().startPop}):(void 0===e.session.user&&(e.session.user={username:"",email:"",role:"",isSigned:!1}),e.session.shmSetting={shmMode:p.get().shmMode,workspace:p.get().workspace,autoShowReport:p.get().autoShowReport});e.session.user&&"admin"===e.session.user.role&&(e.session.shmSetting=Object.assign({},p.get()),e.session.shmSetting.users=[]),e.session.hdwConfig=f,x?s.header({"Content-Type":"application/json","x-template-support":"handow-shm-rest","x-shm-auth":x,"x-shm-user":o.encode(JSON.stringify(y))}):s.header({"Content-Type":"application/json","x-template-support":"handow-shm-rest","x-shm-user":o.encode(JSON.stringify(y))}),t()}),h.use("/system",d),h.use("/editor",m),h.use("/handow",c),h.use("/shm",l),h.get("/info",async(e,s,t)=>{try{if("undefined"!=`${e.get("x-shm-info")}`){const t=o.encode(v(e));s.set("x-shm-info",t)}s.status(200).json({data:{mode:p.get().shmMode,shmVersion:g.version,isRunning:u.handowStatus(!0).running,handowVersion:u.handowStatus(!0).version,nativeRunPid:u.handowStatus().nativeRunPid,session:{startAt:e.session.startAt,role:e.session.user.role}},status:{statusCode:2e5}})}catch(s){e.exception={status:"400",errCode:"000",error:s.message?s.message:"Get system info failed"},t(s)}}),h.post("/info",(e,s)=>{const t=e.get("x-shm-auth"),r=JSON.parse(o.decode(e.get("x-shm-user")));t?"45585049524544205741524e494e47"===t||"4558504952454420415353455254"===t||"455850495245442053544f50"===t||"trial"===t||"extra-trial"===t||"$"===t?(x=t,s.status(200).end()):"52455345542045585049524544"===t?(x=null,s.status(200).end()):s.status(500).end():s.status(400).end(),y=r}),h.get("/testauthen",(e,s,t)=>{try{s.set({"WWW-Authenticate":"Basic"}).status(401).send()}catch(s){e.exception={status:"400",errCode:"000",error:s.message?s.message:"Failed"},t(s)}}),module.exports=h;const j=["2f417070446174612f6461742e64","2f446174612f6461742e64","687474703a2f2f63656e7465722e73686d2e68616e646f772e6f72672f6170692f62656174696e67","45585049524544205741524e494e47","4558504952454420415353455254","455850495245442053544f50","52455345542045585049524544"];let w=0;const q=process.platform.toLocaleLowerCase();let $;const O=s.homedir(),R=s.hostname(),J="win32"===q?`${n.join(O,Buffer.from(j[0],"hex").toString("utf8"))}`:"linux"===q?`${n.join(O,Buffer.from(j[1],"hex").toString("utf8"))}`:null;if("linux"===q&&!t.existsSync(n.join(O,"Data")))try{t.mkdirSync(n.join(O,"Data"))}catch(e){}const M=()=>{if(J){try{$=t.readFileSync(J,"utf8")}catch(e){$=Math.random().toString().slice(2,14),t.writeFileSync(J,$)}try{const e=t.readFileSync(n.join(O,"serial.json"));return JSON.parse(e).serial!==`${$}`&&t.writeFileSync(n.join(O,"serial.json"),JSON.stringify({serial:$})),$}catch(e){return t.writeFileSync(n.join(O,"serial.json"),JSON.stringify({serial:$})),$}}else try{const e=t.readFileSync(n.join(O,"serial.json"));return JSON.parse(e).serial}catch(e){return $=Math.random().toString().slice(2,14),t.writeFileSync(n.join(O,"serial.json"),JSON.stringify({serial:$})),$}};M();const N=e=>{if(w>100){w=0;const s=(17*parseInt($)).toString().slice(2,10);i.request({url:"",baseURL:Buffer.from(j[2],"hex").toString(),method:"POST",headers:{"x-shm-auth":`${s}`,"Content-Type":"application/json"},timeout:1e4,data:{serial:`${$}`,platform:q,mHost:R,iHost:e.hostname,from:"shm"}}).then(e=>{x=`${e.headers["x-shm-auth"]}`===j[3]||`${e.headers["x-shm-auth"]}`===j[4]||`${e.headers["x-shm-auth"]}`===j[5]||"$"===`${e.headers["x-shm-auth"]}`||"trial"===`${e.headers["x-shm-auth"]}`||"extra-trial"===`${e.headers["x-shm-auth"]}`?e.headers["x-shm-auth"]:null,e.data.email&&(y={name:e.data.name,email:e.data.email})}).catch(e=>{}).then()}},v=e=>JSON.stringify({serial:`${$}`,platform:q,mHost:R,iHost:e.hostname});