diff --git a/config.js b/config.js index 86de704..eec40fd 100644 --- a/config.js +++ b/config.js @@ -1,6 +1,5 @@ const fs = require('fs'); -const request = require('request'); -const utf8 = require('utf8'); +const fetch = require('node-fetch'); const Markup = require('telegraf/markup'); const Extra = require('telegraf/extra'); @@ -10,43 +9,90 @@ const db_js = require('./db.js'); var db = new db_js("17track"); //=== TOOL FUNCTIONS + + const track_url = 'https://t.17track.net/restapi/track'; -function get_tracking(trck_ids){ - let options = { - url: track_url, - qs:{ - "data": [], - "guid":"", - "timeZoneOffset":-120 - } - }; - for(var id in trck_ids){ - options.qs.data.push({num:id+"",fc:0,sc:0}); + +var cnvrt = function(v) { + var str = ''; + for (var i = 0; i < v.length; i++) { + str += v.charCodeAt(i).toString(16); + } + return str; +}; +function acc(a, b) { + var c = 1315423911 ^ b << 16; + for (var i = a.length - 1; i >= 0; i--) { + c ^= (c << 5) + a.charCodeAt(i) + (c >> 2); + } + return Math.abs(c & 2147483647); +}; +function atb(a) { + var b = a; + while (b['length'] < 0x8) { + b = '0' + b; + } + return b; +}; +var token = function(data) { + var classes = ["navbar navbar-default yq-header", "nav navbar-toolbar navbar-right navbar-toolbar-right yq-navbar-toolbar", "yq-dropdown-menu-media-apps", "yq-navbar-menuIcon-24 yq-head-apps", "dropdown-menu yq-moreapp-container", "list-group yq-list-group-moreapp clearfix", "hide yq-dropdown-menu-media-msg", "yq-head-msg yq-navbar-menuIcon-24", "yq-msg-nomsg vertical-align panel text-center hide", "yq-msg-network-err vertical-align panel text-center hide", "dropdown-menu dropdown-menu-media yq-dropdown-menu-media-user", "dropdown-menu-footer yq-user-footer clearfix", "margin-horizontal-10 yq-input-container", "dropdown-menu dropdown-menu-media yq-track-dropdown", "yq-more-track-container clearfix", "yq-tools-small", "navbar-brand-logo hidden-xs yq-default-logo", "navbar-brand-logo visible-xs yq-default-logo", "tab-content yq-panel-tracklist scrollable is-enabled scrollable-vertical", "yq-panel-gad", "modal-dialog yq-modal-dialog"]; + var rnd = Math.round((Math.random()* classes.length)); + + var res = []; + var value = "yq-"; + var key = "Last-Event-ID"; + if (classes.length > rnd) { + value = classes[rnd]; + } + value += '/' + Date.now().toString(16) + '/18c/true'; + + res[0]= cnvrt(value.split('').reverse().join('')); + res[1] = rnd.toString(16); + res[2] = rnd.toString(16).length; + res[3] = 2; + res[4] = atb(acc(value, rnd).toString(16)); + res[5] = atb(acc(data,data.length).toString(16)); + return res.join('') +}; + + + +async function get_tracking(trck_ids){ + + var data = { "data": [],"guid":"","timeZoneOffset":-120 }; + for(var id of trck_ids){ + data.data.push({num:id,fc:0,sc:0}); } - return new Promise((resolve,reject)=>{ - return request(options, function (err, response, body) { - if (err) { - return reject(err); - } - - if (response.statusCode != 200 ){ - return reject(500); - } - - var resp = JSON.parse(body) - if(resp.reply==undefined || resp.reply=='') - return reject(500); - var results = []; - for(var rr in resp.dat){ - results.push({id:rr.no, date:rr.z0.a, loc:rr.z0.c, status:rr.z0.z}); - } - return resolve(results); - }); + var keyid = token(JSON.stringify(data)); + + const opts = { + method: 'POST', + body: JSON.stringify(data), + headers: { + 'Accept': 'application/json, text/javascript, */*; q=0.01', + 'Accept-Language': 'en-US,en;q=0.5', + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'X-Requested-With': 'XMLHttpRequest', + 'Referer': 'https://t.17track.net/en', + 'Cookie': 'v5_TranslateLang=en; v5_AppIconsVersion=1; Last-Event-ID='+keyid, + 'Cache-Control': 'no-cache', + }, + // agent: new http.Agent({ + // keepAlive: true + // }) + }; + return fetch(track_url, opts) + .then(res => res.json()) + .then(res => { + var results = []; + for(var rr of resp.dat){ + if(rr.track) + results.push({id:rr.no, date:rr.track.z0.a, loc:rr.track.z0.c, status:rr.track.z0.z}); + else + results.push({id:rr.no, date:'?', loc:'?', status:"Unknown"}); + } + return results; }); - // -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: en-US,en;q=0.5' - // --compressed - // -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' - // -H 'Referer: https://t.17track.net/en' } //=== MAIN MODULE CONFIG @@ -84,7 +130,7 @@ module.exports = function(){ if(res && res != []){ return get_tracking(res).then(res=>{ var text = "*Tracking*\n"; - for(var rr in res){ + for(var rr of res){ text+=`_${rr.id}_ (${rr.date} - ${rr.loc})\n${rr.status}\n\n`; } return event.reply( text, {"reply_markup":Markup.inlineKeyboard(kb),"parse_mode":"Markdown"}); @@ -158,7 +204,7 @@ module.exports = function(){ return db.get_v(event.chat.id).then(res =>{ if(res && res != []){ let kb = []; - for(var rr in res){ + for(var rr of res){ kb.push([bot.mkcb(rr,"del",rr)]) } kb.push([bot.mkcb_close("Close")]);