cky-image-public
Version:
public image to public host
253 lines (209 loc) • 7.81 kB
JavaScript
const fs = require("fs");
const async = require('async');
const cheerio = require('cheerio');
let log = require('debug')('CKY-IMAGE-PUBLIC').extend('IMGGMI');
const request = require('request').defaults({
headers: {
// // 'upgrade-insecure-requests': 1,
// 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
// origin: 'https://imggmi.com',
// 'accept-language': 'en-US,en;q=0.9,vi;q=0.8',
// accept: '*/*',
// referer: 'https://imggmi.com/',
// authority: 'imggmi.com',
},
jar: true
})
let helper = require('../helper');
class Imgeto {
constructor (options = {}) {
let { cookie } = options;
this.cookie_str = cookie;
this.cookie = request.jar();
}
upload (imgPath, callback) {
log('this.cookie_str= %s', this.cookie_str);
async.waterfall([
(next) => {
request({
url: 'https://imggmi.com/upload',
method: 'POST',
headers: {
'sec-fetch-mode': 'cors',
'dnt': 1,
'accept-language': 'en-US,en;q=0.9,vi;q=0.8,fr-FR;q=0.7,fr;q=0.6,la;q=0.5',
'x-requested-with': 'XMLHttpRequest',
'cookie': this.cookie_str,
'content-type': 'multipart/form-data',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'accept': '*/*',
'referer': 'https://imggmi.com/',
'authority': 'imggmi.com',
'sec-fetch-site': 'same-origin',
'Origin': 'https://imggmi.com'
},
jar: this.cookie,
formData: {
file: {
value: fs.createReadStream(imgPath),
options: {
filename: imgPath,
contentType: 'image/jpeg'
}
},
img: 'file',
url: '',
selected_input: 'file',
size: 640,
preview_size: 180,
rotation_type: 0
}
}, (err, response, body) => {
if (err) return next('EUPLOAD', err);
// if (response.statusCode != 200) return next('ESTATUSCODE');
body = helper.safeParse(body);
if (!body || !body.redirect_path) return next('ENOREDIRECTPATH', body);
body.redirect_path = `https://imggmi.com${body.redirect_path}`;
setTimeout(next, 1000, null, body);
// return next(null, body);
});
},
(result, next) => {
/*
{"error":false,"redirect_path":"\/uploaded\/2019\/8\/8\/2cfd548758ea991c07ba614f00af714e-full.jpg.html"}
*/
console.log('-----', result);
let _opts = {
url: result.redirect_path,
method: 'GET',
headers: {
'upgrade-insecure-requests': 1,
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'accept-language': 'en-US,en;q=0.9,vi;q=0.8,fr-FR;q=0.7,fr;q=0.6,la;q=0.5',
'cookie': this.cookie_str,
'dtn': 1,
'sec-fetch-mode': 'navigate',
'sec-fetch-site': 'same-origin',
'upgrade-insecure-requests': 1,
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36',
'referer': result.redirect_path
},
// jar: this.cookie
}
log('_opts= %o', _opts);
request(_opts, (err, response, body) => {
if (err) return next('EGETREDIRECTPATH', err);
let $ = cheerio.load(body);
let _viewerLink = $('#code_link_1 input').attr('value');
let _prevLink = $('#img_photo img').attr('src');
result['viewerLink'] = _viewerLink;
result['previewLink'] = _prevLink;
// {"error":false,"redirect_path":"/uploaded/2019/8/8/f95314347cb5462f7f5fd77450a43350-full.jpg.html","viewerLink":"https://cdn1.imggmi.com/uploads/2019/8/8/f95314347cb5462f7f5fd77450a43350-full.jpg","previewLink":"https://cdn3.imggmi.com/uploads/2019/8/8/f95314347cb5462f7f5fd77450a43350-prev.jpg"}
return next(null, result);
});
}
], callback);
}
getCookie () {
async.waterfall([
(next) => {
request({
url: 'https://imggmi.com',
method: 'GET',
jar: this.cookie
}, (err, response, body) => {
if (err) return next('EGETHOMEPAGE', err);
fs.writeFileSync('imggmi_cloud_fare_def.html', body);
let $ = cheerio.load(body);
let s = $('[name="s"]').attr('value');
let jschl_vc = $('[name="jschl_vc"]').attr('value');
let pass = $('[name="pass"]').attr('value');
let cf_dn_str = $('[id*="cf-dn"]').text();
let jschl_answer = '';
let cf_dn = 0;
cf_dn_str = 'cf_dn = ' + cf_dn_str;
eval(cf_dn_str);
console.log('cf_dn=', cf_dn);
let script = $('script').html();
script = script.replace('//<![CDATA[', '').replace('(function(){', '').replace('})();', '').replace('//]]>', '').trim();
// console.log('script=', script);
let script_split = script.split('\n');
console.log('script_split=', script_split);
script_split[0] = '';
script_split[1] = '';
script_split[2] = '';
script_split[3] = '';
script_split[4] = '';
script_split[27] = '';
script_split[28] = '';
script_split[29] = '';
script_split[30] = '';
// script_split[26] = script_split[26].replace('a.value', 'jschl_answer');
script_split[26] += '; console.log("a.value=", a.value); jschl_answer = a.value;';
script = script_split.join('');
console.log('script=', script);
let document = {};
document.createElement = () => {
return {
firstChild: {
href: "https://imggmi.com/"
}
};
}
document.getElementById = (s) => {
return {
innerHTML: cf_dn
}
}
setTimeout(() => {
eval(script);
let chk_jschl = {
s,
jschl_vc,
pass,
jschl_answer
}
console.log('\njschl_answer=', jschl_answer);
console.log('chk_jschl=', chk_jschl);
return next(null, chk_jschl);
}, 5000)
});
},
(chk_jschl, next) => {
let { s, jschl_vc, pass, jschl_answer } = chk_jschl;
let cookie_string = this.cookie.getCookieString(`https://imggmi.com`);
cookie_string += COOKIE_STR;
console.log('cookie_string=', cookie_string);
request({
url: 'https://imggmi.com/cdn-cgi/l/chk_jschl',
method: 'GET',
headers: {
cookie: cookie_string
},
jar: this.cookie,
qs: {
s,
jschl_vc,
pass,
jschl_answer
}
}, (err, response, body) => {
if (err) return next('ECDNCGI', err);
console.log('\n\n chk_jschl=======\n', body);
return next(null);
});
},
(next) => {
request({
url: 'https://imggmi.com',
method: 'GET',
jar: this.cookie
}, (err, response, body) => {
if (err) return next('EGETHOMEPAGE_2', err);
return next(null);
});
},
], callback);
}
}
module.exports = Imgeto;