// Server-side Node.js (18+) example. Never bundle credentials into browser code. import { createCipheriv, createHash, randomInt } from 'node:crypto'; const separator = '/' + '\\'.repeat(4) + '/'; const order = ['cagent','loginname','method','billno','type','credit','actype','flag','password','oddtype','dm','sid','lang','gameType','session_token','cur','ts','mh5','displayname']; export function encode(p, desKey, md5Key) { const keyBytes = Buffer.from(desKey,'utf8'); if(keyBytes.length !== 8) throw new Error('DES key must contain exactly eight bytes'); const keys = [...order.filter(k=>Object.hasOwn(p,k)), ...Object.keys(p).filter(k=>!order.includes(k)).sort()]; for(const k of keys) if(/[=/\\\r\n]/.test(k) || String(p[k]).includes(separator)) throw new Error('invalid parameter separator'); const plain = keys.map(k=>`${k}=${p[k]}`).join(separator); // EDE with K1=K2=K3 is exactly single DES: E_K(D_K(E_K(x)))=E_K(x). // This keeps the wire DES-ECB/PKCS5 algorithm while using Node's supported cipher. const cipher = createCipheriv('des-ede3',Buffer.concat([keyBytes,keyBytes,keyBytes]),null); const params = Buffer.concat([cipher.update(plain,'utf8'),cipher.final()]).toString('base64'); const key = createHash('md5').update(params+md5Key,'utf8').digest('hex'); return {params,key}; } // Parse the XML response with your server's XML parser; info is method-specific. // On tc/tcc timeout or network_error, query qos using the SAME billno. export async function doBusiness(endpoint,p,desKey,md5Key) { const response = await fetch(endpoint,{ method:'POST', headers:{'Content-Type':'application/x-www-form-urlencoded','User-Agent':'WEB_LIB_GI_'+p.cagent}, body:new URLSearchParams(encode(p,desKey,md5Key)),signal:AbortSignal.timeout(60000) }); if(!response.ok) throw new Error('SDK HTTP '+response.status); return response.text(); } // account needs cagent/loginname/password/actype/cur; optional mh5/lang/gameType. // Never cache this URL; each link has a fresh single-use SID. export function forwardURL(endpoint,account,desKey,md5Key) { const sequence=String(randomInt(10000000,100000000))+String(randomInt(0,100000000)).padStart(8,'0'); const p={...account,method:'fw',gameType:account.gameType||'0',sid:account.cagent+sequence,ts:String(Math.floor(Date.now()/1000))}; const url=new URL(endpoint);const envelope=encode(p,desKey,md5Key); url.searchParams.set('params',envelope.params);url.searchParams.set('key',envelope.key); return url.toString(); }