This repository was archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcr_handler.cpp
More file actions
195 lines (162 loc) · 5.11 KB
/
cr_handler.cpp
File metadata and controls
195 lines (162 loc) · 5.11 KB
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
#include <iostream>
#include <functional>
#include <map>
#include <vector>
#include <chrono>
#include <thread>
#include <memory>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include "lib/cr_handler.hpp"
#include "lib/proxy.hpp"
#include "lib/request.hpp"
using std::string, std::cout, std::map, std::vector;
// some headers should be avoided (origin, host, etc..)
// its not a proper proxy (but a http proxy) with hardcoded backend where we get the data
const vector<string> headers_avoid = {
"origin", "allow-control-allow-origin", "host",
};
const map<string, string> origin_rules = {
{ "Ly9wcm9kLmdjY3J1bmNoeXJvbGwuY29tL2", "d1.com" },
{ "t=exp=", "d2.com" },
{ "Ly9wbC5jcnVuY2h5cm9sbC5jb20", "d3.com" },
{ "Ly92LnZydi5jby", "d4.com" },
};
string get_original_host(string seed) {
string host = "undefined";
for (auto pair : origin_rules) {
if (seed.find(pair.first) != string::npos) {
host = pair.second;
break;
}
}
return host;
}
void clean_headers(map<string, string> &headers) {
// will remove headers listed by global headers_avoid
for (auto pair = headers.begin() ; pair != headers.end() ; ) {
string name;
bool remove = false;
for (int i = 0; i < pair->first.size(); i++) {
name += std::tolower(pair->first[i]);
}
for (string avoid : headers_avoid) {
if (name == avoid) {
remove = true;
break;
}
}
if (remove)
pair = headers.erase(pair);
else
++pair;
}
}
vector<string> split(string raw, string term) {
vector<string> parts;
string token;
size_t pos = 0;
size_t find_pos = 0;
while ((find_pos = raw.find(term, pos)) != string::npos) {
token = raw.substr(pos, find_pos - pos);
if (token.size() != 0)
parts.push_back(token);
pos = find_pos + term.size();
}
return parts;
}
void on_proxy_connection(int *sock, sockaddr_in *client) {
// drop POST requests
string headers_raw;
sockaddr_in client_addr = *client;
int client_sock = *sock;
int recv_total = 0; // capped at 8192 as it's the header limit
int recv_limit = 8192;
timeval timeout = { 10, 0 };
setsockopt(client_sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
while (true) {
if (recv_total >= recv_limit)
break;
if (headers_raw.find("\r\n\r\n") != string::npos)
break;
char buff[1024]; // 1KB chunk
int bytes = recv(client_sock, buff, sizeof(buff), 0);
if (bytes <= 0)
break;
recv_total += bytes;
headers_raw.append(buff);
}
if (headers_raw.size() < 5) {
close(client_sock);
delete client;
delete sock;
return;
}
std::function<void(void*, size_t)> on_curl_write_headers = [client_sock](void *data, size_t size) {
send(client_sock, data, size, 0);
};
std::function<int(void*, size_t)> on_curl_write = [client_sock](void *data, size_t size) {
void *tmp = malloc(size);
memcpy(tmp, data, size);
int ret, bytes = 0;
while (bytes < size) {
ret = send(client_sock, (void*)(tmp + bytes), size, 0);
bytes += ret;
}
free(tmp);
return ret < 0 ? ret : size;
};
//void (*on_curl_write_ptr)(void*, size_t) = static_cast<void (*)(void*, size_t)>(on_curl_write);
vector<string> header_lines = split(headers_raw, "\r\n");
string connection_method = header_lines[0];
header_lines.erase(header_lines.begin());
map<string, string> headers = Request::ParseHeaders(header_lines);
string path = connection_method.substr(connection_method.find("/") + 1, connection_method.find(" HTTP") - connection_method.find("/") - 1);
string original_host = get_original_host(path);
clean_headers(headers);
headers["Origin"] = "https://static.crunchyroll.com";
headers["Referer"] = "https://static.crunchyroll.com/";
headers["Host"] = original_host;
cout << "Host: " << original_host << "\n" << std::flush;
if (original_host == "undefined") {
close(client_sock);
delete client;
delete sock;
return;
}
RequestOptions options = {
.headers = headers,
.events = {
.on_curl_write_headers = on_curl_write_headers,
.on_curl_write = on_curl_write,
},
.save = {
.headers_raw = false, // CHECK
.data = false, // CHECK
},
};
string url = "https://" + original_host + "/" + path;
Response response = Request::Get(url, options);
//Response response = Request::Get("http://docs.evostream.com/sample_content/assets/bunny.mp4", options);
//Response response = Request::Get("http://127.0.0.1/djunk/trash.mp4", options);
//cout << "Request headers: \n" << headers_raw << "\n" << std::flush;
//cout << "Connection method: \n" << connection_method << "\n" << std::flush;
//cout << "Header linse: \n" << header_lines.size() << "\n" << std::flush;
cout << "Conexao respondida com sucesso!" << "\n" << std::flush;
sleep(5);
close(client_sock);
delete client;
delete sock;
}
CRProxy::CRProxy(string ip, int port) {
ProxyEvents events = {
.on_connection = on_proxy_connection,
};
this->server = Proxy(ip.c_str(), port, events);
}
void CRProxy::Start() {
this->server.Listen();
}