Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
https_proxy
/
src
/
main.cc
https_proxy
/
src
/
main.cc
main.cc 12.45 KB
Copy Edit Raw Blame History
Dzlua authored 2021年07月16日 16:53 +08:00 . add check is ssl
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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string>
#include <vector>
#include <functional>
#define MY_MAX(x,y) ((x)>(y)?(x):(y))
#define MY_MIN(x,y) ((x)>(y)?(y):(x))
#define MY_BUFFER_SIZE 128
#ifdef MY_OS_WINDOWS
#include <winsock2.h>
#include <ws2tcpip.h>
#define inet_aton(str,addr) inet_pton(AF_INET, str, addr)
typedef HANDLE task_handle_t;
static int fd_is_valid(int fd) {
char buffer[1];
recv(fd, buffer, 1, MSG_PEEK);
return (WSAECONNRESET != WSAGetLastError());
}
#define my_delay(ms) Sleep(ms)
#endif
#ifdef MY_OS_LINUX
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define closesocket close
typedef pthread_t task_handle_t;
typedef int SOCKET;
static int fd_is_valid(int fd) {
return fcntl(fd, F_GETFD, 0) != -1 || errno != EBADF;
}
#define my_delay(ms) sleep(ms/1000)
#endif
#define MY_LOG(tag, format, ...) printf("[%s] " format "\n", tag, ##__VA_ARGS__)
#define TAG_CLIENT "CLIENT"
#define TAG_SERVER "SERVER"
#define TAG_MAIN "MAIN"
struct str_t {
const char *data;
uint32_t size;
};
struct url_t {
const char *url;
str_t protocol;
str_t hostname;
str_t port;
str_t path;
str_t search;
str_t hash;
};
// http://www.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument
static bool parse_url(const char *url, url_t *val) {
if (!url || !*url || !val) return false;
memset(val, 0, sizeof(url_t));
val->url = url;
val->protocol.data = url;
url = strstr(url, "://");
if (!url) return false;
val->protocol.size = url - val->protocol.data;
val->hostname.data = url + 3;
url = strchr(val->hostname.data, ':');
if (url) {
val->hostname.size = url - val->hostname.data;
val->port.data = url + 1;
url = strchr(val->port.data, '/');
if (url) {
val->port.size = url - val->port.data;
val->path.data = url;
} else {
val->port.size = strlen(val->port.data);
return true;
}
} else {
url = strchr(val->hostname.data, '/');
if (url) {
val->hostname.size = url - val->hostname.data;
val->path.data = url;
} else {
val->hostname.size = strlen(val->hostname.data);
return true;
}
}
url = strchr(val->path.data, '?');
if (url) {
val->path.size = url - val->path.data;
val->search.data = url + 1;
} else {
val->path.size = strlen(val->path.data);
return true;
}
url = strchr(val->search.data, '#');
if (url) {
val->search.size = url - val->search.data;
val->hash.data = url + 1;
val->hash.size = strlen(val->hash.data);
} else {
val->search.size = strlen(val->search.data);
return true;
}
return true;
}
static void show_url(const url_t *url) {
if (!url) return;
printf("---------- show_url begin ----------\n");
printf("url: %s\n", url->url);
printf("protocol: %.*s\n", url->protocol.size, url->protocol.data);
printf("hostname: %.*s\n", url->hostname.size, url->hostname.data);
printf("port: %.*s\n", url->port.size, url->port.data);
printf("path: %.*s\n", url->path.size, url->path.data);
printf("search: %.*s\n", url->search.size, url->search.data);
printf("hash: %.*s\n", url->hash.size, url->hash.data);
printf("---------- show_url end ----------\n");
}
static void show_hex(const char *data, int len) {
for (int i = 0; i < len; ++i) {
if (i % 4 == 0) {
if (i % 8 == 0) {
if (i) printf("| \n");
}
else printf("| ");
}
printf("%02X ", (unsigned char)data[i]);
}
printf("\n");
}
static bool parse_url(const char *urlstr, std::string *hostname, std::string *path, uint16_t *port) {
url_t url = { 0 };
if (!parse_url(urlstr, &url)) return false;
std::string strport;
hostname->assign(url.hostname.data, url.hostname.size);
path->assign(url.path.data, url.path.size);
strport.assign(url.port.data, url.port.size);
if (hostname->empty()) return false;
if (strport.empty()) {
strport = 0 == strncmp("https", url.protocol.data, url.protocol.size)
? "443" : "80";
}
if (path->empty()) *path = "/";
*port = atoi(strport.c_str());
return true;
}
bool client(const char *hostname, uint16_t port, void *udata
, std::function<bool(SSL *ssl, void *udata)> callback) {
bool ret = false;
SSL *ssl = 0;
SSL_CTX *ctx = 0;
SOCKET fd = -1;
sockaddr_in addr = { 0 };
hostent *p = 0;
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ctx = SSL_CTX_new(SSLv23_client_method());
if (!ctx) {
MY_LOG(TAG_CLIENT, "SSL_CTX_new error.");
goto lb_err;
}
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
ssl = SSL_new(ctx);
if (!ssl) {
MY_LOG(TAG_CLIENT, "SSL_new error.");
goto lb_err;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
MY_LOG(TAG_CLIENT, "create socket error.");
goto lb_err;
}
addr.sin_addr.S_un.S_addr = 0;
addr.sin_port = htons(0);
addr.sin_family = AF_INET;
if (bind(fd, (const sockaddr*)&addr, sizeof(SOCKADDR_IN)) != NOERROR) {
MY_LOG(TAG_CLIENT, "bind error.");
goto lb_err;
}
p = gethostbyname(hostname);
if (!p) {
MY_LOG(TAG_CLIENT, "gethostbyname error.");
goto lb_err;
}
addr.sin_family = p->h_addrtype;
addr.sin_addr.S_un.S_addr = *(ULONG*)(p->h_addr_list[0]);
addr.sin_port = htons(port);
if (connect(fd, (sockaddr*)&addr, sizeof(addr)) != NOERROR) {
MY_LOG(TAG_CLIENT, "connect error.");
goto lb_err;
}
//MY_LOG(TAG_CLIENT, "connect ok.");
if (SSL_set_fd(ssl, fd) != SSL_SUCCESS) {
MY_LOG(TAG_CLIENT, "SSL_set_fd error.");
goto lb_err;
}
if (SSL_connect(ssl) != SSL_SUCCESS) {
MY_LOG(TAG_CLIENT, "SSL_connect error.");
goto lb_err;
}
//MY_LOG(TAG_CLIENT, "connected with %s encryption.", SSL_get_cipher(ssl));
if (callback)
ret = callback(ssl, udata);
ret = true;
lb_err:
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
}
if (fd) closesocket(fd);
if (ctx) SSL_CTX_free(ctx);
return ret;
}
bool http_get(const char *urlstr, void *udata, std::function<void(const char *data, int32_t size, void *udata)> callback) {
std::string hostname, path;
uint16_t port;
if (!parse_url(urlstr, &hostname, &path, &port))
return false;
char header[1024 * 8];
sprintf(header,
"GET %s HTTP/1.1 \r\n"
"Host: %s \r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0 \r\n"
"Accept-Type: */* \r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 \r\n"
"Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2 \r\n"
"Connection: Close \r\n\r\n"
, path.c_str()
, hostname.c_str());
return client(hostname.c_str(), port, udata
, [&] (SSL *ssl, void *udata) -> bool {
int header_len = strlen(header);
int ret = SSL_write(ssl, header, header_len);
if (ret != header_len) return false;
if (!callback) return true;
char buf[MY_BUFFER_SIZE];
while (true) {
ret = SSL_read(ssl, buf, MY_BUFFER_SIZE);
if (ret <= 0) break;
callback(buf, ret, udata);
}
return true;
});
}
typedef struct {
SOCKET fd;
SSL *ssl;
} conn_t;
bool server(const char *cert_file, const char *key_file, uint16_t port, void *udata
, std::function<bool(conn_t *conn, void *udata)> callback) {
bool ret = false;
SSL_CTX *ctx = 0;
SOCKET fd_listen;
sockaddr_in addr = { 0 };
if (!cert_file || !key_file) return false;
if (!callback) return false;
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ctx = SSL_CTX_new(SSLv23_server_method());
if (!ctx) {
MY_LOG(TAG_SERVER, "ssl create ctx error.");
goto lb_err;
}
if (SSL_CTX_use_certificate_file(ctx, cert_file, SSL_FILETYPE_PEM) != SSL_SUCCESS) {
MY_LOG(TAG_SERVER, "ssl load cert file error.");
goto lb_err;
}
if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) != SSL_SUCCESS) {
MY_LOG(TAG_SERVER, "ssl load key file error.");
goto lb_err;
}
if (SSL_CTX_check_private_key(ctx) != SSL_SUCCESS) {
MY_LOG(TAG_SERVER, "ssl check key error.");
goto lb_err;
}
SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
fd_listen = socket(AF_INET, SOCK_STREAM, 0);
if (fd_listen < 0) {
MY_LOG(TAG_SERVER, "create socket error.");
goto lb_err;
}
addr.sin_family = AF_INET;
addr.sin_addr.S_un.S_addr = INADDR_ANY;
addr.sin_port = htons(port);
if (bind(fd_listen, (const sockaddr*)&addr, sizeof(SOCKADDR_IN)) != NOERROR) {
MY_LOG(TAG_SERVER, "bind error.");
goto lb_err;
}
if (listen(fd_listen, 5) != NOERROR) {
MY_LOG(TAG_SERVER, "listen error.");
goto lb_err;
}
MY_LOG(TAG_SERVER, "listen in 0.0.0.0:%d.", port);
bool quit = false;
while (!quit) {
SSL *ssl = 0;
bool is_ssl = false;
char handshake[8];
conn_t conn = { 0 };
struct sockaddr_in addr_from;
socklen_t addr_from_len = sizeof(addr_from);
SOCKET fd_conn = accept(fd_listen, (struct sockaddr *)&addr_from, &addr_from_len);
if (fd_conn < 0) {
MY_LOG(TAG_SERVER, "accept error.");
goto lb_finish;
}
int ret = recv(fd_conn, (char*)&handshake, sizeof(handshake), MSG_PEEK);
if (ret < 0) {
MY_LOG(TAG_SERVER, "recv error.");
goto lb_finish;
} else if (ret == 0) {
MY_LOG(TAG_SERVER, "close fd %d.", (int)fd_conn);
goto lb_finish;
} else {
if ((unsigned char)handshake[0] == 0x16 // content type
&& (unsigned char)handshake[5] == 0x01) { // handshake type
is_ssl = true;
}
MY_LOG(TAG_SERVER, "check is ssl: %d", is_ssl);
show_hex((const char*)&handshake, ret);
}
if (is_ssl) {
ssl = SSL_new(ctx);
if (!ssl) {
MY_LOG(TAG_SERVER, "SSL_new error.");
goto lb_finish;
}
if (SSL_set_fd(ssl, fd_conn) != SSL_SUCCESS) {
MY_LOG(TAG_SERVER, "SSL_set_fd error.");
goto lb_finish;
}
if (SSL_accept(ssl) != SSL_SUCCESS) {
MY_LOG(TAG_SERVER, "SSL_accept error.");
goto lb_finish;
}
conn.fd = fd_conn;
conn.ssl = ssl;
} else {
conn.fd = fd_conn;
}
quit = !callback(&conn, udata);
lb_finish:
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
}
if (fd_conn >= 0) closesocket(fd_conn);
}
lb_err:
if (fd_listen >= 0) closesocket(fd_listen);
if (ctx) SSL_CTX_free(ctx);
return ret;
}
bool tcp_echo(const char *cert_file, const char *key_file, uint16_t port) {
return server(cert_file, key_file, port, 0
, [](conn_t *conn, void *udata) -> bool {
char buf[MY_BUFFER_SIZE];
while (true) {
int ret = SSL_read(conn->ssl, buf, MY_BUFFER_SIZE);
if (ret <= 0) break;
printf("%.*s", ret, buf);
SSL_write(conn->ssl, buf, ret);
}
return true;
});
}
str_t parse_request(const char *req) {
str_t ret = { 0 };
if (!req || !*req) return ret;
// GET /?url=https://www.baidu.com?a=1&b=2 HTTP/1.1
if (strncmp(req, "GET /", 5) != 0) return ret;
const char *bp = req + 4;
const char *ep = strchr(bp, ' ');
if (!ep) return ret;
ret.data = bp;
ret.size = ep - bp;
return ret;
}
int user_main(int argc, char *argv[]) {
if (argc != 5) {
MY_LOG(TAG_MAIN, "#> https <cert file> <cert key> <port> <dest>");
MY_LOG(TAG_MAIN, "such as: https server.cert server.key 8888 https://s.dzlua.top");
return 0;
}
const char *crt = argv[1];
const char *key = argv[2];
uint16_t port = atoi(argv[3]);
const char *dest = argv[4];
bool ok = server(crt, key, port, (void*)dest
, [](conn_t *conn, void *udata) -> bool {
std::string strRecv;
printf("---------- begin ----------\n");
printf("is ssl: %s\n", (conn->ssl ? "true" : "false"));
if (conn->ssl) {
std::string path;
char buf[MY_BUFFER_SIZE];
while (true) {
int ret = SSL_read(conn->ssl, buf, MY_BUFFER_SIZE);
if (ret <= 0) break;
strRecv.append(buf, ret);
printf("%.*s", ret, buf);
str_t tmp = parse_request(strRecv.c_str());
path.assign(tmp.data, tmp.size);
if (!path.empty()) break;
}
printf("\n----------\n");
path = (const char*)udata + path;
http_get(path.c_str(), conn->ssl
, [](const char *data, int32_t size, void *udata) {
printf("%.*s", size, data);
SSL_write((SSL*)udata, data, size);
});
} else {
return true;
}
printf("\n---------- end ----------\n");
return true;
});
return 0;
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

https代理
Cancel

Releases

No release

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/dzlua/https_proxy.git
git@gitee.com:dzlua/https_proxy.git
dzlua
https_proxy
https_proxy
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /