aboutsummaryrefslogtreecommitdiffstats
path: root/lib/backend.php
blob: 68ac270cff4476792ada6187f86ad89ce77d907d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
class backend{
	
	public function __construct($scraper){
		
		$this->scraper = $scraper;
	}
	
	/*
		Proxy stuff
	*/
	public function get_ip($proxy_index_raw = null){
		
		$pool = constant("config::PROXY_" . strtoupper($this->scraper));
		if($pool === false){
			
			// we don't want a proxy, fuck off!
			return 'raw_ip::::';
		}
		
		// indent
		if($proxy_index_raw === null){
			
			$proxy_index_raw = apcu_inc("p." . $this->scraper);
		}
		
		$proxylist = file_get_contents("data/proxies/" . $pool . ".txt");
		$proxylist = explode("\n", $proxylist);

		// ignore empty or commented lines
		$proxylist = array_filter($proxylist, function($entry){
			$entry = ltrim($entry);
			return strlen($entry) > 0 && substr($entry, 0, 1) != "#";
		});
		
		$proxylist = array_values($proxylist);
		
		if(count($proxylist) === 0){
			
			throw new Exception("A proxy list was specified but it's empty!");
		}
		
		//echo $proxylist[$proxy_index_raw % count($proxylist)];
		return $proxylist[$proxy_index_raw % count($proxylist)];
	}
	
	// this function is also called directly on nextpage
	public function assign_proxy(&$curlproc, string $ip){
		
		// parse proxy line
		[
			$type,
			$address,
			$port,
			$username,
			$password
		] = explode(":", $ip, 5);
		
		switch($type){
			
			case "raw_ip":
				return;
				break;
			
			case "http":
			case "https":
				curl_setopt($curlproc, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
				curl_setopt($curlproc, CURLOPT_PROXY, $type . "://" . $address . ":" . $port);
				break;
			
			case "socks4":
				curl_setopt($curlproc, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
				curl_setopt($curlproc, CURLOPT_PROXY, $address . ":" . $port);
				break;
			
			case "socks5":
				curl_setopt($curlproc, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
				curl_setopt($curlproc, CURLOPT_PROXY, $address . ":" . $port);
				break;
			
			case "socks4a":
				curl_setopt($curlproc, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
				curl_setopt($curlproc, CURLOPT_PROXY, $address . ":" . $port);
				break;
			
			case "socks5_hostname":
			case "socks5h":
			case "socks5a":
				curl_setopt($curlproc, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
				curl_setopt($curlproc, CURLOPT_PROXY, $address . ":" . $port);
				break;
		}
		
		if($username != ""){
			
			curl_setopt($curlproc, CURLOPT_PROXYUSERPWD, $username . ":" . $password);
		}
	}
	
	// API key rotation
	public function get_key(){
		
		$keys = file_get_contents("data/api_keys/" . $this->scraper . ".txt");
		$keys = explode("\n", $keys);
		
		$keys = array_filter($keys, function($entry){
			$entry = ltrim($entry);
			return strlen($entry) > 0 && substr($entry, 0, 1) != "#";
		});
		
		$keys = array_values($keys);
		
		if(count($keys) === 0){
			
			throw new Exception("Please specify API keys in data/api_keys/" . $this->scraper . ".txt");
		}
		
		$increment = apcu_inc("s." . $this->scraper) % count($keys);
		return [
			"key" => $keys[$increment],
			"increment" => $increment
		];
	}
	
	
	/*
		Next page stuff
	*/
	public function store(string $payload, string $page, string $proxy){
		
		$key = sodium_crypto_secretbox_keygen();
		$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
		
		$requestid = apcu_inc("requestid");
		
		apcu_store(
			$page[0] . "." . // first letter of page name
			$this->scraper . // scraper name
			$requestid,
			[
				$nonce,
				$proxy,
				// compress and encrypt
				sodium_crypto_secretbox(
					gzdeflate($payload),
					$nonce,
					$key
				)
			],
			900 // cache information for 15 minutes
		);

		return 
			$this->scraper . $requestid . "." .
			rtrim(strtr(base64_encode($key), '+/', '-_'), '=');
	}
	
	public function get(string $npt, string $page){
		
		$page = $page[0];
		$explode = explode(".", $npt, 2);
		
		if(count($explode) !== 2){
			
			throw new Exception("Malformed nextPageToken!");
		}
		
		$apcu = $page . "." . $explode[0];
		$key = $explode[1];
		
		$payload = apcu_fetch($apcu);
		
		if($payload === false){
			
			throw new Exception("The next page token is invalid or has expired!");
		}
		
		$key =
			base64_decode(
				str_pad(
					strtr($key, '-_', '+/'),
					strlen($key) % 4,
					'=',
					STR_PAD_RIGHT
				)
			);
		
		// decrypt and decompress data
		$payload[2] =
			gzinflate(
				sodium_crypto_secretbox_open(
					$payload[2], // data
					$payload[0], // nonce
					$key
				)
			);
		
		if($payload[2] === false){
			
			throw new Exception("The next page token is invalid or has expired!");
		}
		
		// remove the key after using successfully
		apcu_delete($apcu);
		
		return [
			$payload[2], // data
			$payload[1] // proxy
		];
	}
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage