1 |
|
2 |
|
3 | var BlockedError, Bottleneck, InvalidCFCookieError, InvalidUserAgentError, CloudCookie, KissHTTP, MaxRetryError, Promise, _, b, cheerio, cloudscraper, debug, got,
|
4 | extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
|
5 | hasProp = {}.hasOwnProperty;
|
6 |
|
7 | got = require('got');
|
8 |
|
9 | debug = require('debug')('test');
|
10 |
|
11 | Promise = require('bluebird');
|
12 |
|
13 | cloudscraper = Promise.promisifyAll(require('cloudscraper'));
|
14 |
|
15 | _ = require('lodash');
|
16 |
|
17 | cheerio = require('cheerio');
|
18 |
|
19 | Bottleneck = require('bottleneck');
|
20 |
|
21 | CloudCookie = require('./cookie-storage');
|
22 |
|
23 | b = new Bottleneck(1, 0);
|
24 |
|
25 | InvalidUserAgentError = (function(superClass) {
|
26 | extend(InvalidUserAgentError, superClass);
|
27 |
|
28 | function InvalidUserAgentError() {
|
29 | this.name = 'InvalidUserAgentError';
|
30 | this.message = 'The user-agent used to request content differs from the user-agent accepted by Cloudflare.';
|
31 | Error.captureStackTrace(this, InvalidUserAgentError);
|
32 | }
|
33 |
|
34 | return InvalidUserAgentError;
|
35 |
|
36 | })(Error);
|
37 |
|
38 | InvalidCFCookieError = (function(superClass) {
|
39 | extend(InvalidCFCookieError, superClass);
|
40 |
|
41 | function InvalidCFCookieError() {
|
42 | this.name = 'InvalidCFCookieError';
|
43 | this.message = 'The the cloudflare cookie used to request content is no longer valid.';
|
44 | Error.captureStackTrace(this, InvalidCFCookieError);
|
45 | }
|
46 |
|
47 | return InvalidCFCookieError;
|
48 |
|
49 | })(Error);
|
50 |
|
51 | BlockedError = (function(superClass) {
|
52 | extend(BlockedError, superClass);
|
53 |
|
54 | function BlockedError(reason, body) {
|
55 | var $;
|
56 | $ = cheerio.load(body);
|
57 | reason = $('.barContent').text();
|
58 | this.name = 'BlockedError';
|
59 | this.message = "You have been blocked, the page states: " + (reason.trim());
|
60 | Error.captureStackTrace(this, BlockedError);
|
61 | }
|
62 |
|
63 | return BlockedError;
|
64 |
|
65 | })(Error);
|
66 |
|
67 | MaxRetryError = (function(superClass) {
|
68 | extend(MaxRetryError, superClass);
|
69 |
|
70 | function MaxRetryError() {
|
71 | this.name = 'MaxRetryError';
|
72 | this.message = 'Retrieving page after 5 attempts failed. Something has most likely been broken by external forces.';
|
73 | Error.captureStackTrace(this, MaxRetryError);
|
74 | }
|
75 |
|
76 | return MaxRetryError;
|
77 |
|
78 | })(Error);
|
79 |
|
80 | KissHTTP = (function() {
|
81 | var DEFAULT_OPTIONS;
|
82 |
|
83 | DEFAULT_OPTIONS = {
|
84 | method: 'GET',
|
85 | headers: {
|
86 | 'user-agent': 'got/6.11 (https://github.com/sindresorhus/got)'
|
87 | },
|
88 | followRedirect: true,
|
89 | save_cookies: true
|
90 | };
|
91 |
|
92 | function KissHTTP(options) {
|
93 | this.options = _.merge(DEFAULT_OPTIONS, options);
|
94 | if (this.options.save_cookies) {
|
95 | this.cookie_storage = new CloudCookie();
|
96 | this.options.headers.cookie = this.cookie_storage.loadCookie();
|
97 | }
|
98 | }
|
99 |
|
100 | KissHTTP.prototype.setDelay = function(amount) {
|
101 | return b = new Bottleneck(1, amount);
|
102 | };
|
103 |
|
104 | KissHTTP.prototype.getFreshCookie = function() {
|
105 | debug('Retrieving fresh Cloudflare cookie.');
|
106 | return new Promise((function(_this) {
|
107 | return function(resolve, reject) {
|
108 | return cloudscraper.get('https://ww1.gogoanime.io/', function(err, resp) {
|
109 | if (err) {
|
110 | return reject(new Error('Unable to bypass Cloudflare protection.'));
|
111 | } else {
|
112 | _this.options.headers.cookie = resp.request.headers.cookie;
|
113 | if (_this.options.save_cookies) {
|
114 | _this.cookie_storage.saveCookie(resp.request.headers.cookie);
|
115 | }
|
116 | debug('Fresh Cloudflare cookie retrieved.');
|
117 | return resolve();
|
118 | }
|
119 | }, {
|
120 | 'User-Agent': _this.options.headers['user-agent']
|
121 | });
|
122 | };
|
123 | })(this));
|
124 | };
|
125 |
|
126 | KissHTTP.prototype.request = function(url, options) {
|
127 | var local_options;
|
128 | if (options == null) {
|
129 | options = {
|
130 | retries: 0
|
131 | };
|
132 | }
|
133 | local_options = _.merge(this.options, options);
|
134 | return got(url, local_options).then(function(resp) {
|
135 | if (resp.body.indexOf('Are you human?') > -1) {
|
136 | throw new BlockedError('Captcha Blocked', resp.body);
|
137 | } else if (resp.body.indexOf('does not allow unofficial apps') > -1) {
|
138 | throw new BlockedError('Blocked IP', resp.body);
|
139 | } else {
|
140 | return resp;
|
141 | }
|
142 | })["catch"](function(err) {
|
143 | debug(err);
|
144 | if (err instanceof BlockedError) {
|
145 | throw err;
|
146 | } else if (err.name === 'HTTPError') {
|
147 | debug("Received HTTP error retrieving URL: " + url);
|
148 | throw err;
|
149 | } else if (local_options.retries > 5) {
|
150 | throw new MaxRetryError();
|
151 | }
|
152 | if (err.name === 'InvalidCFCookieError') {
|
153 | throw new InvalidCFCookieError();
|
154 | } else {
|
155 | throw err;
|
156 | }
|
157 | })["catch"]((function(_this) {
|
158 | return function(err) {
|
159 | local_options.retries += 1;
|
160 | if (err.name === 'MaxRetryError' || err.name === 'BlockedError') {
|
161 | throw err;
|
162 | } else {
|
163 | return _this.getFreshCookie().then(function() {
|
164 | return _this.request(url, local_options);
|
165 | });
|
166 | }
|
167 | };
|
168 | })(this));
|
169 | };
|
170 |
|
171 | return KissHTTP;
|
172 |
|
173 | })();
|
174 |
|
175 | module.exports = KissHTTP;
|