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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #include <ws2tcpip.h> #include <openssl/ssl.h> #include <openssl/err.h>
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "libssl.lib") #pragma comment(lib, "libcrypto.lib") #pragma comment(lib, "crypt32.lib") #pragma warning(disable:4996)
#define BUFFER_SIZE 4096
void send_request(SSL* ssl, const char* host, const char* path) { char request[BUFFER_SIZE]; sprintf(request, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" "Connection: close\r\n\r\n", path, host); SSL_write(ssl, request, strlen(request)); }
int main() { SetConsoleOutputCP(CP_UTF8); WSADATA wsaData; SOCKET sock; struct sockaddr_in server; char response[BUFFER_SIZE]; int bytes_received;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { printf("Failed to initialize WinSock. Error Code: %d\n", WSAGetLastError()); return 1; }
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == INVALID_SOCKET) { printf("Could not create socket. Error Code: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
server.sin_family = AF_INET; server.sin_port = htons(443);
struct hostent* host = gethostbyname("www.bilibili.com"); if (host == NULL) { printf("Could not resolve hostname. Error Code: %d\n", WSAGetLastError()); closesocket(sock); WSACleanup(); return 1; } memcpy(&server.sin_addr, host->h_addr, host->h_length);
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { printf("Connect failed. Error Code: %d\n", WSAGetLastError()); closesocket(sock); WSACleanup(); return 1; }
SSL_library_init(); SSL_CTX* ctx = SSL_CTX_new(TLS_client_method()); SSL* ssl = SSL_new(ctx); SSL_set_fd(ssl, sock);
if (SSL_connect(ssl) <= 0) { printf("SSL connect failed. Error Code: %d\n", ERR_get_error()); SSL_free(ssl); SSL_CTX_free(ctx); closesocket(sock); WSACleanup(); return 1; }
send_request(ssl, "www.bilibili.com", "/");
while ((bytes_received = SSL_read(ssl, response, sizeof(response) - 1)) > 0) { response[bytes_received] = '\0'; printf("%s", response);
if (strstr(response, "HTTP/1.1 301 Moved Permanently") || strstr(response, "HTTP/1.1 302 Found")) { char* location = strstr(response, "Location: "); if (location) { location += strlen("Location: "); char* end = strstr(location, "\r\n"); if (end) { *end = '\0'; printf("\nRedirecting to: %s\n", location);
SSL_shutdown(ssl); SSL_free(ssl); closesocket(sock);
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock == INVALID_SOCKET) { printf("Could not create socket. Error Code: %d\n", WSAGetLastError()); WSACleanup(); return 1; }
char* new_host = location; char* path = strchr(new_host, '/'); if (path) { *path = '\0'; path++; }
host = gethostbyname(new_host); if (host == NULL) { printf("Could not resolve hostname. Error Code: %d\n", WSAGetLastError()); closesocket(sock); WSACleanup(); return 1; } memcpy(&server.sin_addr, host->h_addr, host->h_length); server.sin_port = htons(443);
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { printf("Connect failed. Error Code: %d\n", WSAGetLastError()); closesocket(sock); WSACleanup(); return 1; }
ssl = SSL_new(ctx); SSL_set_fd(ssl, sock); if (SSL_connect(ssl) <= 0) { printf("SSL connect failed. Error Code: %d\n", ERR_get_error()); SSL_free(ssl); SSL_CTX_free(ctx); closesocket(sock); WSACleanup(); return 1; }
send_request(ssl, new_host, path); } } break; } }
SSL_shutdown(ssl); SSL_free(ssl); SSL_CTX_free(ctx); closesocket(sock); WSACleanup(); return 0; }
|