Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / youpipe / src / main / assets / sabr_po_token.js
sabr_po_token.js265 lines · 6.5 KiBRaw
| 1 | /* global PipePipeWebViewBridge */ |
| 2 | |
| 3 | function pipepipeBridge() { |
| 4 | return window["PipePipeWebViewBridge"]; |
| 5 | } |
| 6 | |
| 7 | function loadBotGuard(root, challengeData, onReady, onError) { |
| 8 | var intervalId; |
| 9 | var finished = false; |
| 10 | |
| 11 | function fail(error) { |
| 12 | if (finished) { |
| 13 | return; |
| 14 | } |
| 15 | finished = true; |
| 16 | if (intervalId) { |
| 17 | clearInterval(intervalId); |
| 18 | } |
| 19 | onError(error); |
| 20 | } |
| 21 | |
| 22 | try { |
| 23 | root.vm = root[challengeData.globalName]; |
| 24 | root.program = challengeData.program; |
| 25 | root.vmFunctions = {}; |
| 26 | root.syncSnapshotFunction = null; |
| 27 | |
| 28 | if (!root.vm) { |
| 29 | throw new Error("[BotGuardClient]: VM not found in the global object"); |
| 30 | } |
| 31 | if (!root.vm.a) { |
| 32 | throw new Error("[BotGuardClient]: Could not load program"); |
| 33 | } |
| 34 | |
| 35 | var vmFunctionsCallback = function ( |
| 36 | asyncSnapshotFunction, |
| 37 | shutdownFunction, |
| 38 | passEventFunction, |
| 39 | checkCameraFunction |
| 40 | ) { |
| 41 | root.vmFunctions = { |
| 42 | asyncSnapshotFunction: asyncSnapshotFunction, |
| 43 | shutdownFunction: shutdownFunction, |
| 44 | passEventFunction: passEventFunction, |
| 45 | checkCameraFunction: checkCameraFunction, |
| 46 | }; |
| 47 | }; |
| 48 | |
| 49 | var noOp = function () {}; |
| 50 | var loggerFunctions = [noOp, noOp, noOp, noOp, noOp]; |
| 51 | |
| 52 | root.syncSnapshotFunction = root.vm.a( |
| 53 | root.program, |
| 54 | vmFunctionsCallback, |
| 55 | true, |
| 56 | root.userInteractionElement, |
| 57 | noOp, |
| 58 | [[], []], |
| 59 | undefined, |
| 60 | false, |
| 61 | loggerFunctions |
| 62 | )[0]; |
| 63 | |
| 64 | root._botGuardPolls = 0; |
| 65 | intervalId = setInterval(function () { |
| 66 | if (root.vmFunctions.asyncSnapshotFunction) { |
| 67 | finished = true; |
| 68 | clearInterval(intervalId); |
| 69 | onReady(root); |
| 70 | return; |
| 71 | } |
| 72 | if (root._botGuardPolls >= 10000) { |
| 73 | fail(new Error("asyncSnapshotFunction is null even after 10 seconds")); |
| 74 | return; |
| 75 | } |
| 76 | root._botGuardPolls = (root._botGuardPolls || 0) + 1; |
| 77 | }, 1); |
| 78 | } catch (error) { |
| 79 | fail(error); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | function snapshot(root, args, onSuccess, onError) { |
| 84 | try { |
| 85 | if (!root.vmFunctions.asyncSnapshotFunction) { |
| 86 | throw new Error("[BotGuardClient]: Async snapshot function not found"); |
| 87 | } |
| 88 | |
| 89 | root.vmFunctions.asyncSnapshotFunction( |
| 90 | function (response) { |
| 91 | onSuccess(response); |
| 92 | }, |
| 93 | [ |
| 94 | args.contentBinding, |
| 95 | args.signedTimestamp, |
| 96 | args.webPoSignalOutput, |
| 97 | args.skipPrivacyBuffer, |
| 98 | ] |
| 99 | ); |
| 100 | } catch (error) { |
| 101 | onError(error); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | function runBotGuard(challengeData, onSuccess, onError) { |
| 106 | var root = this; |
| 107 | try { |
| 108 | var interpreterJavascript = |
| 109 | challengeData.interpreterJavascript |
| 110 | .privateDoNotAccessOrElseSafeScriptWrappedValue; |
| 111 | |
| 112 | if (!interpreterJavascript) { |
| 113 | throw new Error("Could not load VM"); |
| 114 | } |
| 115 | |
| 116 | new Function(interpreterJavascript)(); |
| 117 | |
| 118 | var webPoSignalOutput = []; |
| 119 | loadBotGuard( |
| 120 | root, |
| 121 | { |
| 122 | globalName: challengeData.globalName, |
| 123 | globalObj: root, |
| 124 | program: challengeData.program, |
| 125 | }, |
| 126 | function (botguard) { |
| 127 | snapshot( |
| 128 | botguard, |
| 129 | { webPoSignalOutput: webPoSignalOutput }, |
| 130 | function (botguardResponse) { |
| 131 | onSuccess({ |
| 132 | webPoSignalOutput: webPoSignalOutput, |
| 133 | botguardResponse: botguardResponse, |
| 134 | }); |
| 135 | }, |
| 136 | onError |
| 137 | ); |
| 138 | }, |
| 139 | onError |
| 140 | ); |
| 141 | } catch (error) { |
| 142 | onError(error); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | function createPoTokenMinter(webPoSignalOutput, integrityToken) { |
| 147 | var getMinter = webPoSignalOutput[0]; |
| 148 | |
| 149 | if (!getMinter) { |
| 150 | throw new Error("PMD:Undefined"); |
| 151 | } |
| 152 | |
| 153 | var mintCallback = getMinter(integrityToken); |
| 154 | |
| 155 | if (!(mintCallback instanceof Function)) { |
| 156 | throw new Error("APF:Failed"); |
| 157 | } |
| 158 | |
| 159 | return mintCallback; |
| 160 | } |
| 161 | |
| 162 | function obtainPoToken(mintCallback, identifier) { |
| 163 | var result = mintCallback(identifier); |
| 164 | |
| 165 | if (!result) { |
| 166 | throw new Error("YNJ:Undefined"); |
| 167 | } |
| 168 | |
| 169 | if (!(result instanceof Uint8Array)) { |
| 170 | throw new Error("ODM:Invalid"); |
| 171 | } |
| 172 | |
| 173 | return result; |
| 174 | } |
| 175 | |
| 176 | function pipepipeSabrRunBotguard(sessionId, eventId, challengeData) { |
| 177 | var bridge = pipepipeBridge(); |
| 178 | try { |
| 179 | window.yt = window.yt || {}; |
| 180 | window.yt.config_ = window.yt.config_ || {}; |
| 181 | window.yt.config_.EVENT_ID = eventId; |
| 182 | runBotGuard( |
| 183 | challengeData, |
| 184 | function (result) { |
| 185 | window.__sabrLocalDomSessions = window.__sabrLocalDomSessions || {}; |
| 186 | window.__sabrLocalDomSessions[sessionId] = { |
| 187 | webPoSignalOutput: result.webPoSignalOutput, |
| 188 | }; |
| 189 | bridge.onSabrLocalDomRunBotguardResult( |
| 190 | sessionId, |
| 191 | result.botguardResponse |
| 192 | ); |
| 193 | }, |
| 194 | function (error) { |
| 195 | bridge.onSabrLocalDomJsInitializationError( |
| 196 | sessionId, |
| 197 | String(error) + "\n" + (error && error.stack ? error.stack : "") |
| 198 | ); |
| 199 | } |
| 200 | ); |
| 201 | } catch (error) { |
| 202 | bridge.onSabrLocalDomJsInitializationError( |
| 203 | sessionId, |
| 204 | String(error) + "\n" + (error && error.stack ? error.stack : "") |
| 205 | ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | function pipepipeSabrCreateMinter(sessionId, integrityToken) { |
| 210 | var bridge = pipepipeBridge(); |
| 211 | try { |
| 212 | var sessions = window.__sabrLocalDomSessions || {}; |
| 213 | var session = sessions[sessionId]; |
| 214 | if (!session || !session.webPoSignalOutput) { |
| 215 | throw new Error("Local DOM WebPO signal output is missing"); |
| 216 | } |
| 217 | session.integrityToken = integrityToken; |
| 218 | session.poTokenMinter = createPoTokenMinter( |
| 219 | session.webPoSignalOutput, |
| 220 | session.integrityToken |
| 221 | ); |
| 222 | bridge.onSabrLocalDomMinterReady(sessionId); |
| 223 | } catch (error) { |
| 224 | bridge.onSabrLocalDomJsInitializationError( |
| 225 | sessionId, |
| 226 | String(error) + "\n" + (error && error.stack ? error.stack : "") |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | function pipepipeSabrObtainPoToken(sessionId, identifier, identifierU8) { |
| 232 | var bridge = pipepipeBridge(); |
| 233 | try { |
| 234 | var sessions = window.__sabrLocalDomSessions || {}; |
| 235 | var session = sessions[sessionId]; |
| 236 | if (!session || !session.poTokenMinter) { |
| 237 | throw new Error("Local DOM PO token minter is not ready"); |
| 238 | } |
| 239 | var poTokenU8 = obtainPoToken(session.poTokenMinter, identifierU8); |
| 240 | var poTokenU8String = ""; |
| 241 | for (var i = 0; i < poTokenU8.length; i++) { |
| 242 | if (i !== 0) { |
| 243 | poTokenU8String += ","; |
| 244 | } |
| 245 | poTokenU8String += poTokenU8[i]; |
| 246 | } |
| 247 | bridge.onSabrLocalDomObtainPoTokenResult( |
| 248 | sessionId, |
| 249 | identifier, |
| 250 | poTokenU8String |
| 251 | ); |
| 252 | } catch (error) { |
| 253 | bridge.onSabrLocalDomObtainPoTokenError( |
| 254 | sessionId, |
| 255 | identifier, |
| 256 | String(error) + "\n" + (error && error.stack ? error.stack : "") |
| 257 | ); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | function pipepipeSabrDeleteSession(sessionId) { |
| 262 | if (window.__sabrLocalDomSessions) { |
| 263 | delete window.__sabrLocalDomSessions[sessionId]; |
| 264 | } |
| 265 | } |