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
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netdb.h> #include <openssl/ssl.h> #include <openssl/err.h>
#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() { char response[BUFFER_SIZE]; int bytes_received;
SSL_library_init(); SSL_CTX* ctx = SSL_CTX_new(TLS_client_method()); SSL* ssl = NULL;
int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("Could not create socket"); return 1; }
struct sockaddr_in server; server.sin_family = AF_INET; server.sin_port = htons(443);
struct hostent* host = gethostbyname("www.bilibili.com"); if (host == NULL) { perror("Could not resolve hostname"); close(sock); return 1; } memcpy(&server.sin_addr, host->h_addr, host->h_length);
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) { perror("Connect failed"); close(sock); return 1; }
ssl = SSL_new(ctx); SSL_set_fd(ssl, sock);
if (SSL_connect(ssl) <= 0) { printf("SSL connect failed. Error Code: %ld\n", ERR_get_error()); SSL_free(ssl); SSL_CTX_free(ctx); close(sock); 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); close(sock);
sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("Could not create socket"); SSL_CTX_free(ctx); return 1; }
char* new_host = location; char* path = strchr(new_host, '/'); if (path) { *path = '\0'; path++; }
host = gethostbyname(new_host); if (host == NULL) { perror("Could not resolve hostname"); close(sock); SSL_CTX_free(ctx); 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) { perror("Connect failed"); close(sock); SSL_CTX_free(ctx); return 1; }
ssl = SSL_new(ctx); SSL_set_fd(ssl, sock); if (SSL_connect(ssl) <= 0) { printf("SSL connect failed. Error Code: %ld\n", ERR_get_error()); SSL_free(ssl); SSL_CTX_free(ctx); close(sock); return 1; }
send_request(ssl, new_host, path); } } break; } }
SSL_shutdown(ssl); SSL_free(ssl); SSL_CTX_free(ctx); close(sock); return 0; }
|