function parseServiceStatus(d){var b={};var c=d.Deref(0,4).IntVal;b.isFileSystemDriver=((c&2)==2);b.isKernelDriver=((c&1)==1);b.isSharedProcess=((c&32)==32);b.isOwnProcess=((c&16)==16);b.isInteractive=((c&256)==256);switch(d.Deref((1*4),4).toBuffer().readUInt32LE()){case 5:b.state="CONTINUE_PENDING";break;case 6:b.state="PAUSE_PENDING";break;case 7:b.state="PAUSED";break;case 4:b.state="RUNNING";break;case 2:b.state="START_PENDING";break;case 3:b.state="STOP_PENDING";break;case 1:b.state="STOPPED";break}var a=d.Deref((2*4),4).toBuffer().readUInt32LE();b.controlsAccepted=[];if((a&16)==16){b.controlsAccepted.push("SERVICE_CONTROL_NETBINDADD");b.controlsAccepted.push("SERVICE_CONTROL_NETBINDREMOVE");b.controlsAccepted.push("SERVICE_CONTROL_NETBINDENABLE");b.controlsAccepted.push("SERVICE_CONTROL_NETBINDDISABLE")}if((a&8)==8){b.controlsAccepted.push("SERVICE_CONTROL_PARAMCHANGE")}if((a&2)==2){b.controlsAccepted.push("SERVICE_CONTROL_PAUSE");b.controlsAccepted.push("SERVICE_CONTROL_CONTINUE")}if((a&256)==256){b.controlsAccepted.push("SERVICE_CONTROL_PRESHUTDOWN")}if((a&4)==4){b.controlsAccepted.push("SERVICE_CONTROL_SHUTDOWN")}if((a&1)==1){b.controlsAccepted.push("SERVICE_CONTROL_STOP")}if((a&32)==32){b.controlsAccepted.push("SERVICE_CONTROL_HARDWAREPROFILECHANGE")}if((a&64)==64){b.controlsAccepted.push("SERVICE_CONTROL_POWEREVENT")}if((a&128)==128){b.controlsAccepted.push("SERVICE_CONTROL_SESSIONCHANGE")}b.pid=d.Deref((7*4),4).toBuffer().readUInt32LE();return(b)}function serviceManager(){this._ObjectID="service-manager";if(process.platform=="win32"){this.GM=require("_GenericMarshal");this.proxy=this.GM.CreateNativeProxy("Advapi32.dll");this.proxy.CreateMethod("OpenSCManagerA");this.proxy.CreateMethod("EnumServicesStatusExA");this.proxy.CreateMethod("OpenServiceA");this.proxy.CreateMethod("QueryServiceStatusEx");this.proxy.CreateMethod("ControlService");this.proxy.CreateMethod("StartServiceA");this.proxy.CreateMethod("CloseServiceHandle");this.proxy.CreateMethod("CreateServiceA");this.proxy.CreateMethod("ChangeServiceConfig2A");this.proxy.CreateMethod("DeleteService");this.proxy.CreateMethod("AllocateAndInitializeSid");this.proxy.CreateMethod("CheckTokenMembership");this.proxy.CreateMethod("FreeSid");this.proxy2=this.GM.CreateNativeProxy("Kernel32.dll");this.proxy2.CreateMethod("GetLastError");this.isAdmin=function e(){var j=this.GM.CreateVariable(6);j.toBuffer().writeInt8(5,5);var h=this.GM.CreatePointer();var g=false;if(this.proxy.AllocateAndInitializeSid(j,2,32,544,0,0,0,0,0,0,h).Val!=0){var i=this.GM.CreateInteger();if(this.proxy.CheckTokenMembership(0,h.Deref(),i).Val!=0){if(i.toBuffer().readUInt32LE()!=0){g=true}}this.proxy.FreeSid(h.Deref())}return g};this.getProgramFolder=function a(){if(require("os").arch()=="x64"){if(this.GM.PointerSize==4){return process.env["ProgramFiles(x86)"]}return process.env.ProgramFiles}return process.env.ProgramFiles};this.getServiceFolder=function b(){return this.getProgramFolder()+"\\mesh"};this.enumerateService=function(){var o=this.GM.CreatePointer();var k=this.GM.CreatePointer();var l=this.proxy.OpenSCManagerA(0,0,1|4);var h=this.GM.CreatePointer();var t=this.GM.CreatePointer();var q=this.GM.CreatePointer();var u=this.proxy.EnumServicesStatusExA(l,0,48,3,0,0,h,t,q,0);if(h.IntVal<=0){throw ("error enumerating services")}var v=h.IntVal;var s=this.GM.CreateVariable(v);this.proxy.EnumServicesStatusExA(l,0,48,3,s,v,h,t,q,0);console.log("servicesReturned",t.IntVal);var p=k._size;var g=36+(2*p);g+=((p-(g%p))%p);var r=[];for(var m=0;mStandardOutPath\n"+r.stdout+""):"");var i=(r.startType=="AUTO_START"?"":"");var s=" ProgramArguments\n";s+=" \n";s+=(" /usr/local/mesh_services/"+r.name+"/"+r.name+"\n");if(r.parameters){for(var p in r.parameters){s+=(" "+r.parameters[p]+"\n")}}s+=" \n";var t='\n';t+='\n';t+='\n';t+=" \n";t+=" Label\n";t+=(" "+r.name+"\n");t+=(s+"\n");t+=" WorkingDirectory\n";t+=(" /usr/local/mesh_services/"+r.name+"\n");t+=(y+"\n");t+=" RunAtLoad\n";t+=(i+"\n");t+=" \n";t+="";if(!require("fs").existsSync("/usr/local/mesh_services")){require("fs").mkdirSync("/usr/local/mesh_services")}if(!require("fs").existsSync("/Library/LaunchDaemons/"+r.name+".plist")){if(!require("fs").existsSync("/usr/local/mesh_services/"+r.name)){require("fs").mkdirSync("/usr/local/mesh_services/"+r.name)}if(r.binary){require("fs").writeFileSync("/usr/local/mesh_services/"+r.name+"/"+r.name,r.binary)}else{require("fs").copyFileSync(r.servicePath,"/usr/local/mesh_services/"+r.name+"/"+r.name)}require("fs").writeFileSync("/Library/LaunchDaemons/"+r.name+".plist",t);var q=require("fs").statSync("/usr/local/mesh_services/"+r.name+"/"+r.name).mode;q|=(require("fs").CHMOD_MODES.S_IXUSR|require("fs").CHMOD_MODES.S_IXGRP);require("fs").chmodSync("/usr/local/mesh_services/"+r.name+"/"+r.name,q)}else{throw ("Service: "+r.name+" already exists")}}};this.uninstallService=function f(i){if(!this.isAdmin()){throw ("Uninstalling a service, requires admin")}if(typeof(i)=="object"){i=i.name}if(process.platform=="win32"){var j=this.getService(i);if(j.status.state==undefined||j.status.state=="STOPPED"){if(this.proxy.DeleteService(j._service)==0){throw ("Uninstall Service for: "+i+", failed with error: "+this.proxy2.GetLastError())}else{try{require("fs").unlinkSync(this.getServiceFolder()+"\\"+i+".exe")}catch(h){}}}else{throw ("Cannot uninstall service: "+i+", because it is: "+j.status.state)}}else{if(process.platform=="linux"){switch(this.getServiceType()){case"init":this._update=require("child_process").execFile("/bin/sh",["sh"],{type:require("child_process").SpawnTypes.TERM});this._update.stdout.on("data",function(k){});this._update.stdin.write("service "+i+" stop\n");this._update.stdin.write("update-rc.d -f "+i+" remove\n");this._update.stdin.write("exit\n");this._update.waitExit();try{require("fs").unlinkSync("/etc/init.d/"+i);console.log(i+" uninstalled")}catch(h){console.log(i+" could not be uninstalled",h)}break;case"systemd":this._update=require("child_process").execFile("/bin/sh",["sh"],{type:require("child_process").SpawnTypes.TERM});this._update.stdout.on("data",function(k){});this._update.stdin.write("systemctl stop "+i+".service\n");this._update.stdin.write("systemctl disable "+i+".service\n");this._update.stdin.write("exit\n");this._update.waitExit();try{require("fs").unlinkSync("/usr/local/mesh/"+i);require("fs").unlinkSync("/lib/systemd/system/"+i+".service");console.log(i+" uninstalled")}catch(h){console.log(i+" could not be uninstalled",h)}break;default:break}}else{if(process.platform=="darwin"){if(require("fs").existsSync("/Library/LaunchDaemons/"+i+".plist")){var g=require("child_process").execFile("/bin/sh",["sh"]);g.stdout.on("data",function(k){});g.stdin.write("launchctl stop "+i+"\n");g.stdin.write("launchctl unload /Library/LaunchDaemons/"+i+".plist\n");g.stdin.write("exit\n");g.waitExit();try{require("fs").unlinkSync("/usr/local/mesh_services/"+i+"/"+i);require("fs").unlinkSync("/Library/LaunchDaemons/"+i+".plist")}catch(h){throw ("Error uninstalling service: "+i+" => "+h)}try{require("fs").rmdirSync("/usr/local/mesh_services/"+i)}catch(h){}}else{throw ("Service: "+i+" does not exist")}}}}};if(process.platform=="linux"){this.getServiceType=function c(){return(require("process-manager").getProcessInfo(1).Name)}}}module.exports=serviceManager;