Created
August 4, 2017 02:11
-
-
Save shenjunru/9ad59d3efccf420dedd73d35137ce440 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.textnow.com | |
// https://www.google.com/voice/?setup=1#setup/ | |
// node >= 8 | |
// usage: node script.js "curl ..." | |
const https = require('https'); | |
const http = require('http'); | |
const zlib = require('zlib'); | |
const { URL } = require('url'); | |
const zlibOptions = { | |
flush: zlib.Z_SYNC_FLUSH, | |
finishFlush: zlib.Z_SYNC_FLUSH, | |
}; | |
function request(url, headers, cookies, content){ | |
return new Promise((resolve, reject)=>{ | |
const _client = 'https:' === url.protocol ? https : http; | |
const _cookies = Object.entries(cookies).map((e)=>e.join('=')).join('; '); | |
const _headers = Object.assign({}, headers, { 'Cookie': _cookies }); | |
const _options = { | |
method: null == content ? 'GET' : 'POST', | |
hostname: url.hostname, | |
port: url.port, | |
path: url.pathname + url.search, | |
headers: _headers, | |
}; | |
const _request = _client.request(_options, (res)=>{ | |
if (200 !== res.statusCode) { | |
res.resume(); | |
return reject(new Error(`code: ${res.statusCode}`)); | |
} | |
const cookies = {}; | |
res.headers['set-cookie'].forEach((t)=>{ | |
const s = t.split('; ').shift(); | |
let i = s.indexOf('='); | |
let v = s.slice(i + 1); | |
let k = s.slice(0, i); | |
cookies[k] = v; | |
}); | |
let encoding = (res.headers['content-encoding'] || 'identity').trim().toLowerCase(); | |
let response; | |
if (encoding === 'gzip') { | |
response = zlib.createGunzip(zlibOptions); | |
res.pipe(response); | |
} else if (encoding === 'deflate') { | |
response = zlib.createInflate(zlibOptions); | |
res.pipe(response); | |
} else { | |
response = res; | |
} | |
let content = ''; | |
response.on('data', function(chunk){ | |
content += chunk.toString(); | |
}); | |
response.on('end', () => { | |
resolve({ cookies, content }); | |
}); | |
}); | |
_request.on('error', reject); | |
null == content || _request.write(content); | |
_request.end(); | |
}); | |
} | |
async function loop(url, headers, cookies, content, verify){ | |
let _remains = 100; | |
let _cookies = Object.assign({}, cookies); | |
while (1) { | |
try { | |
const res = await request(url, headers, _cookies, content); | |
_cookies = Object.assign({}, _cookies, res.cookies); | |
console.log('cookies', JSON.stringify(res.cookies)); | |
console.log('content', res.content.trim()); | |
if (verify(res.content.trim())) { | |
return; | |
} | |
} catch(error) { | |
console.error('error', error); | |
if (!--_remains) { | |
return; | |
} | |
} | |
} | |
} | |
// parse headers | |
const headers = {}; | |
process.argv[2].replace(/ -H ['"]([^'"]+)['"]/g, (m, s)=>{ | |
let i = s.indexOf(': '); | |
let v = s.slice(i + 2); | |
let k = s.slice(0, i); | |
headers[k] = v; | |
}); | |
// parse cookies | |
const cookies = {}; | |
((s)=>(s && s.split('; ').forEach((s)=>{ | |
let i = s.indexOf('='); | |
let v = s.slice(i + 1); | |
let k = s.slice(0, i); | |
cookies[k] = v; | |
})))(headers['Cookie'] || headers['cookie']); | |
// parse post data | |
const content = (process.argv[2].match(/ --data ['"]([^'"]+)['"]/) || []).pop(); | |
// start looping | |
loop( | |
new URL((process.argv[2].match(/^curl ['"]([^'"]+)['"]/) || []).pop()), | |
headers, | |
cookies, | |
content, | |
(s)=>s!== '[[null,null,"There was an error with your request. Please try again."]]', | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment