"use strict";(function(){"use strict";var e=require("child_process").spawn;var n=require("path");var o=require("util");var r=require("request");var i=require("./jsUtil");var t=null;function d(e,n,o,d){if(e==undefined){e=i.getPkgInfo().spec.info.cloudUrl}var u=t!=undefined?t.getUserInfo().sessionId:"";if(o.appid==undefined){o.appid="0004";o.sign="rgcs"}console.log("request url:",e+n,"sessionId:",u," send data:",JSON.stringify(o));r.post({url:e+n,headers:{Cookie:"JSESSIONID="+u},json:true,timeout:12e3,body:o},function(e,n,o){try{if(e||n.statusCode!=200||o==undefined||o.code==undefined){if(d!=undefined){var r=n!=undefined?n.statusCode:"response is null";console.log("rsp: err:",e," statusCode:",r," body:",o,"response:",JSON.stringify(n));d("err:"+e+"statusCode:"+r,{code:-1})}}else{if(d!=undefined){console.log("body:",o);d(null,o)}}}catch(i){console.error("in _myPost: error:",i)}})}exports.init=function(e){t=e};exports.prepareTunnel=function(e,n,o,r,i,t,u){var s=o.getDeviceParams(e);if(s==null){i("cannot find deviceName:"+e);return}if(s.protocal!="telnet"&&s.protocal!="SSH"){i("cannot support orotocal:"+s.protocal);return}var l=function f(){if(s.port!=undefined){return s.port}if(s.protocal=="telnet"){return 23}if(s.protocal=="SSH"){return 22}return 0};var c=r.getUserInfo();n=!isNaN(n)&&n>=1&&n<=24?n:1;var a={validTime:n,device:{deviceName:e,ip:s.host!=undefined?s.host:"127.0.0.1",port:l(),protocol:s.protocal,deviceUserName:s.username!=undefined?s.username:"",devicePwd:s.password!=undefined?s.password:"",deviceEnablePwd:s.enablePassword?s.enablePassword:"",deviceSn:s.seriaNo!=undefined?s.seriaNo:"",deviceModel:s.deviceModel!=undefined?s.deviceModel:"",deviceType:s.deviceType!=undefined?s.deviceType:"",softVer:s.softVer!=undefined?s.softVer:"",hardVer:s.hardVer!=undefined?s.hardVer:"",encoding:s.encoding!=undefined?s.encoding:"",account:c.userName!=undefined?c.userName:"",trueName:c.name!=undefined?c.name:"",customer:""}};o.queryGroupInfoById(s.groupId,function(e,n){if(e==null&&n.length>0){a.device.customer=n[0].customer}if(u==true){i(null,{code:0,tunnelId:"111111",deviceSn:"SN8232334846",authMethod:"PASSWORD",authName:"sshTunnel",authKey:"ruijie2016",proxyHost:"172.18.111.72",proxyPort:9999})}else{d(t,"/api/tunnel/prepare",a,i)}})};exports.getConnectCode=function(e,n,o,r){if(r==true){n(null,{tunnelId:123456,verifyCode:"123456"})}else{d(o,"/api/tunnel/code",{tunnelId:e},n)}};function u(e,n,o){if(o!=true){d(n,"/api/tunnel/destroy",{tunnelId:e})}}exports.reportBuildTunnelFailure=function(e,n,o){u(e,n,o)};exports.reportCloseConnection=function(e,n){if(n!=true){d(e,"/api/tunnel/disconnect",{})}};exports.getTunnelInfo=function(e,n,o,r){if(r==true){n(null,{timeoutMinite:20,device:{deviceName:"deviceName",ip:"192.168.23.191",port:"23",protocol:"telnet",deviceUserName:"adm",devicePwd:"adm",deviceEnablePwd:"",encoding:"utf-8",account:"www",trueName:"www",customer:"ruijie"}})}else{d(o,"/api/tunnel/connect",{verityCode:e},n)}};exports.openTunnel=function(r,t,d){var u=false;if(t==undefined){return}try{var s=r.plinkPath!=undefined?r.plinkPath:n.join(i.getRootPath(),"/extra/plink.exe");var l=["-v","-ssh","-C","-N","-R",o.format("%d:%s:%d",r.port,r.deviceIp,r.devicePort),"-P","22",o.format("%s@%s",r.userName,r.host),"-pw",r.password];var c=e(s,l);var a=setTimeout(function(){if(c!=null){c.kill()}console.log("in openTunnel trigger timeout");t("timeout","timeout",null)},3e4);c.stdout.on("data",function(e){var n=String(e);console.log("in openTunnel stdout data:",n);if(n.indexOf("/n)")!=-1){c.stdin.write(new Buffer("n\r\n"))}if(n.indexOf("enabled")!=-1&&!u){clearTimeout(a);u=true;t(null,"success",c)}});c.stderr.on("data",function(e){var n=String(e);console.log("in openTunnel stderr data:",n);if(n.indexOf("/n)")!=-1){c.stdin.write(new Buffer("n\r\n"))}if(n.indexOf("enabled")!=-1&&!u){clearTimeout(a);u=true;t(null,"success",c)}});c.on("close",function(e){console.log("process exited with code",e);if(u&&d!=undefined){d(e,c)}});c.on("error",function(e){console.error("in openTunnel error:",e);if(u){c.kill()}})}catch(f){console.error("in openTunnel error:",f);t("exception",f,null)}};exports.closeTunnel=function(e,n){if(e!=undefined){try{e.kill();if(n!=undefined){u(n)}}catch(o){console.error("in closeTunnel error:",o)}}}})();