2020-09-07 22:27:42 +02:00
|
|
|
/*
|
|
|
|
Code by blackb0x @ GBAtemp.net
|
|
|
|
This allows the Wii to download from servers that use SNI.
|
|
|
|
*/
|
2019-10-25 00:26:17 +02:00
|
|
|
#include <network.h>
|
|
|
|
#include <ogc/lwp_watchdog.h>
|
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
#include "base64.h"
|
2019-10-25 00:26:17 +02:00
|
|
|
#include "gecko/gecko.hpp"
|
2020-09-07 22:27:42 +02:00
|
|
|
#include "https.h"
|
2020-04-12 20:39:10 +02:00
|
|
|
#include "memory/mem2.hpp"
|
2020-09-07 22:27:42 +02:00
|
|
|
#include "proxysettings.h"
|
2019-10-25 00:26:17 +02:00
|
|
|
|
|
|
|
u8 loop;
|
2020-04-12 21:27:47 +02:00
|
|
|
WOLFSSL_SESSION *session;
|
2019-10-25 00:26:17 +02:00
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
int https_write(HTTP_INFO *httpinfo, char *buffer, int len, bool proxy)
|
2019-10-25 00:26:17 +02:00
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
int ret, pos = 0;
|
|
|
|
int rlen = len > BLOCK_SIZE ? BLOCK_SIZE : len;
|
|
|
|
u64 time = gettime();
|
|
|
|
while (ticks_to_millisecs(diff_ticks(time, gettime())) < READ_WRITE_TIMEOUT)
|
|
|
|
{
|
|
|
|
if (httpinfo->use_https && !proxy)
|
|
|
|
ret = wolfSSL_write(httpinfo->ssl, &buffer[pos], rlen);
|
|
|
|
else
|
|
|
|
ret = net_write(httpinfo->sock, &buffer[pos], rlen);
|
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
pos += ret;
|
|
|
|
rlen = len - pos > BLOCK_SIZE ? BLOCK_SIZE : len - pos;
|
|
|
|
if (pos >= len)
|
|
|
|
return pos;
|
|
|
|
time = gettime();
|
|
|
|
}
|
|
|
|
usleep(10000);
|
|
|
|
}
|
2020-09-07 22:27:42 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("The connection timed out (write)\n");
|
2020-09-07 22:27:42 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return -ETIMEDOUT;
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
int https_read(HTTP_INFO *httpinfo, char *buffer, int len, bool proxy)
|
2019-10-25 00:26:17 +02:00
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
int ret = -ETIMEDOUT;
|
|
|
|
u64 time = gettime();
|
|
|
|
if (len > BLOCK_SIZE)
|
|
|
|
len = BLOCK_SIZE;
|
|
|
|
while (ticks_to_millisecs(diff_ticks(time, gettime())) < READ_WRITE_TIMEOUT)
|
|
|
|
{
|
|
|
|
if (httpinfo->use_https && !proxy)
|
|
|
|
ret = wolfSSL_read(httpinfo->ssl, buffer, len);
|
|
|
|
else
|
|
|
|
ret = net_read(httpinfo->sock, buffer, len);
|
|
|
|
if (ret >= 0)
|
|
|
|
return ret;
|
|
|
|
usleep(10000);
|
|
|
|
}
|
2020-04-12 21:27:47 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("The connection timed out (read)\n");
|
2020-04-12 21:27:47 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return -ETIMEDOUT;
|
2020-09-07 22:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int send_callback(__attribute__((unused)) WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
int sent = net_write(*(int *)ctx, buf, sz);
|
|
|
|
if (sent < 0)
|
|
|
|
{
|
|
|
|
if (sent == -EAGAIN)
|
|
|
|
return WOLFSSL_CBIO_ERR_WANT_WRITE;
|
|
|
|
else if (sent == -ECONNRESET)
|
|
|
|
return WOLFSSL_CBIO_ERR_CONN_RST;
|
|
|
|
else if (sent == -EINTR)
|
|
|
|
return WOLFSSL_CBIO_ERR_ISR;
|
|
|
|
else if (sent == -EPIPE)
|
|
|
|
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
|
|
|
|
else
|
|
|
|
return WOLFSSL_CBIO_ERR_GENERAL;
|
|
|
|
}
|
|
|
|
return sent;
|
2020-09-07 22:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int recv_callback(__attribute__((unused)) WOLFSSL *ssl, char *buf, int sz, void *ctx)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
int recvd = net_read(*(int *)ctx, buf, sz);
|
|
|
|
if (recvd < 0)
|
|
|
|
{
|
|
|
|
if (recvd == -EAGAIN)
|
|
|
|
return WOLFSSL_CBIO_ERR_WANT_READ;
|
|
|
|
else if (recvd == -ECONNRESET)
|
|
|
|
return WOLFSSL_CBIO_ERR_CONN_RST;
|
|
|
|
else if (recvd == -EINTR)
|
|
|
|
return WOLFSSL_CBIO_ERR_ISR;
|
|
|
|
else if (recvd == -ECONNABORTED)
|
|
|
|
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
|
|
|
|
else
|
|
|
|
return WOLFSSL_CBIO_ERR_GENERAL;
|
|
|
|
}
|
|
|
|
else if (recvd == 0)
|
|
|
|
return WOLFSSL_CBIO_ERR_CONN_CLOSE;
|
|
|
|
return recvd;
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void https_close(HTTP_INFO *httpinfo)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
if (httpinfo->use_https)
|
|
|
|
{
|
|
|
|
wolfSSL_shutdown(httpinfo->ssl);
|
|
|
|
wolfSSL_free(httpinfo->ssl);
|
|
|
|
wolfSSL_CTX_free(httpinfo->ctx);
|
|
|
|
}
|
|
|
|
net_close(httpinfo->sock);
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Closed socket and cleaned up\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
bool get_header_value(struct phr_header *headers, size_t num_headers, char *dst, char *header)
|
2019-10-25 00:26:17 +02:00
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
for (size_t i = 0; i != num_headers; ++i)
|
|
|
|
{
|
|
|
|
if (strncasecmp(header, headers[i].name, headers[i].name_len) == 0)
|
|
|
|
{
|
|
|
|
strlcpy(dst, headers[i].value, headers[i].value_len + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
u64 get_header_value_int(struct phr_header *headers, size_t num_headers, char *header)
|
2019-10-25 00:26:17 +02:00
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
char header_value[30];
|
|
|
|
if (!get_header_value(headers, num_headers, header_value, header))
|
|
|
|
return 0;
|
|
|
|
return strtoull(header_value, NULL, 0);
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
bool is_chunked(struct phr_header *headers, size_t num_headers)
|
2019-10-25 00:26:17 +02:00
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
char encoding[9];
|
|
|
|
if (!get_header_value(headers, num_headers, encoding, "transfer-encoding"))
|
|
|
|
return false;
|
|
|
|
return (strcasecmp(encoding, "chunked") == 0);
|
2020-09-07 22:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool read_chunked(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
struct phr_chunked_decoder decoder = {0};
|
|
|
|
size_t rsize, capacity = 4096;
|
|
|
|
ssize_t pret;
|
|
|
|
int ret;
|
|
|
|
decoder.consume_trailer = true;
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Data is chunked\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (start_pos == capacity)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Increased buffer size\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
capacity *= 2;
|
|
|
|
buffer->data = MEM2_realloc(buffer->data, capacity);
|
2021-02-05 20:51:15 +01:00
|
|
|
if (!buffer->data) // A custom theme is using too much memory
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_NETWORK
|
|
|
|
gprintf("Out of memory!\n");
|
|
|
|
#endif
|
|
|
|
errno = ENOMEM;
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-17 19:29:03 +02:00
|
|
|
}
|
|
|
|
if ((ret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos, false)) < 1)
|
|
|
|
return false;
|
|
|
|
rsize = ret;
|
|
|
|
pret = phr_decode_chunked(&decoder, &buffer->data[start_pos], &rsize);
|
|
|
|
if (pret == -1)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Parse error\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
start_pos += rsize;
|
|
|
|
} while (pret == -2);
|
|
|
|
buffer->size = start_pos;
|
|
|
|
buffer->data = MEM2_realloc(buffer->data, buffer->size);
|
|
|
|
return true;
|
2020-09-07 22:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool read_all(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
size_t capacity = 4096;
|
|
|
|
int ret;
|
2020-09-07 22:27:42 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Data is not chunked\n");
|
2020-09-07 22:27:42 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (start_pos == capacity)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Increased buffer size\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
capacity *= 2;
|
|
|
|
buffer->data = MEM2_realloc(buffer->data, capacity);
|
2021-02-05 20:51:15 +01:00
|
|
|
if (!buffer->data) // A custom theme is using too much memory
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_NETWORK
|
|
|
|
gprintf("Out of memory!\n");
|
|
|
|
#endif
|
|
|
|
errno = ENOMEM;
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-17 19:29:03 +02:00
|
|
|
}
|
|
|
|
if ((ret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos, false)) == 0)
|
|
|
|
break;
|
|
|
|
if (ret < 0)
|
|
|
|
return false;
|
|
|
|
start_pos += ret;
|
|
|
|
};
|
|
|
|
buffer->size = start_pos;
|
|
|
|
buffer->data = MEM2_realloc(buffer->data, buffer->size);
|
|
|
|
return (buffer->content_length > 0 && buffer->content_length == start_pos);
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
bool get_response(HTTP_INFO *httpinfo, HTTP_RESPONSE *resp, bool proxy)
|
2019-10-25 00:26:17 +02:00
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
int rret, minor_version;
|
|
|
|
size_t msg_len, prevbuflen;
|
|
|
|
const char *msg;
|
2020-09-07 22:27:42 +02:00
|
|
|
|
2020-10-17 19:29:03 +02:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if ((rret = https_read(httpinfo, &resp->data[resp->buflen], 1, proxy)) < 1)
|
|
|
|
return false;
|
|
|
|
prevbuflen = resp->buflen;
|
|
|
|
resp->buflen += rret;
|
|
|
|
// Parse the response
|
|
|
|
resp->num_headers = sizeof(resp->headers) / sizeof(resp->headers[0]);
|
|
|
|
if ((resp->pret = phr_parse_response(resp->data, resp->buflen, &minor_version, &resp->status, &msg, &msg_len,
|
|
|
|
resp->headers, &resp->num_headers, prevbuflen)) > 0)
|
|
|
|
return true;
|
|
|
|
else if (resp->pret == -1)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("pret error %i\n", resp->pret);
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (resp->buflen == sizeof(resp->data))
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("buflen error %lu\n", (unsigned long)resp->buflen);
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
bool check_ip(char *str)
|
2019-10-25 00:26:17 +02:00
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
int partA, partB, partC, partD;
|
|
|
|
char extra;
|
|
|
|
// We avoid using regex because it increases the file size
|
|
|
|
return (sscanf(str, "%d.%d.%d.%d%c", &partA, &partB, &partC, &partD, &extra) == 4);
|
2020-09-07 22:27:42 +02:00
|
|
|
}
|
2019-10-25 00:26:17 +02:00
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
bool connect_proxy(HTTP_INFO *httpinfo, char *host, char *username, char *password)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
HTTP_RESPONSE response = {0};
|
|
|
|
char request[500];
|
|
|
|
char credentials[66];
|
|
|
|
char *auth;
|
|
|
|
int len;
|
|
|
|
if (username && password)
|
|
|
|
{
|
|
|
|
if (!snprintf(credentials, sizeof(credentials), "%s:%s", username, password))
|
|
|
|
return false;
|
|
|
|
if (!(auth = base64(credentials, strlen(credentials), &len)))
|
|
|
|
return false;
|
|
|
|
len = snprintf(request, sizeof(request),
|
|
|
|
"CONNECT %s:%i HTTP/1.1\r\nProxy-Authorization: Basic %s\r\nUser-Agent: curl/7.55.1\r\n\r\n",
|
|
|
|
host, httpinfo->use_https ? 443 : 80, auth);
|
|
|
|
MEM2_free(auth);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
len = snprintf(request, sizeof(request),
|
|
|
|
"CONNECT %s:%i HTTP/1.1\r\nUser-Agent: curl/7.55.1\r\n\r\n",
|
|
|
|
host, httpinfo->use_https ? 443 : 80);
|
|
|
|
if (len > 0 && https_write(httpinfo, request, len, true) != len)
|
|
|
|
return false;
|
|
|
|
if (get_response(httpinfo, &response, true))
|
|
|
|
{
|
|
|
|
if (response.status == 200)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2020-09-07 22:27:42 +02:00
|
|
|
}
|
2019-10-25 00:26:17 +02:00
|
|
|
|
2020-09-07 22:27:42 +02:00
|
|
|
int connect(char *host, u16 port)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
s32 sock, ret;
|
|
|
|
u32 ipaddress;
|
|
|
|
u64 time;
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Connecting to %s", host);
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
if ((ipaddress = check_ip(host) ? inet_addr(host) : getipbynamecached(host)) == 0)
|
|
|
|
return -EFAULT;
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
sin.sin_addr.s_addr = ipaddress;
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
if (!check_ip(host))
|
|
|
|
gprintf(" (%s)", inet_ntoa(sin.sin_addr));
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
if ((sock = net_socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
|
|
|
|
return sock;
|
|
|
|
net_fcntl(sock, F_SETFL, 4);
|
|
|
|
time = gettime();
|
|
|
|
while (ticks_to_millisecs(diff_ticks(time, gettime())) < CONNECT_TIMEOUT)
|
|
|
|
{
|
|
|
|
if ((ret = net_connect(sock, (struct sockaddr *)&sin, sizeof(sin))) < 0)
|
|
|
|
{
|
|
|
|
if (ret == -EISCONN)
|
|
|
|
return sock;
|
|
|
|
if (ret == -EINPROGRESS || ret == -EALREADY)
|
|
|
|
{
|
|
|
|
usleep(10000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
net_close(sock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
net_close(sock);
|
|
|
|
return -ETIMEDOUT;
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void downloadfile(const char *url, struct download *buffer)
|
|
|
|
{
|
2020-10-17 19:29:03 +02:00
|
|
|
HTTP_INFO httpinfo = {0};
|
|
|
|
// Always reset the size due to the image downloader looping
|
|
|
|
buffer->size = 0;
|
|
|
|
// Check if we're using HTTPS and set the path
|
|
|
|
char *path;
|
|
|
|
if (strncmp(url, "https://", 8) == 0)
|
|
|
|
{
|
|
|
|
httpinfo.use_https = 1;
|
|
|
|
path = strchr(url + 8, '/');
|
|
|
|
}
|
|
|
|
else if (strncmp(url, "http://", 7) == 0)
|
|
|
|
{
|
|
|
|
httpinfo.use_https = 0;
|
|
|
|
path = strchr(url + 7, '/');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return;
|
2021-02-05 20:51:15 +01:00
|
|
|
if (!path)
|
2020-10-17 19:29:03 +02:00
|
|
|
return;
|
|
|
|
// Get the host
|
|
|
|
int domainlength = path - url - 7 - httpinfo.use_https;
|
|
|
|
char host[domainlength + 1];
|
|
|
|
strlcpy(host, url + 7 + httpinfo.use_https, domainlength + 1);
|
|
|
|
// Start connecting
|
|
|
|
if (getProxyAddress() && getProxyPort() > 0)
|
|
|
|
httpinfo.sock = connect(getProxyAddress(), getProxyPort());
|
|
|
|
else
|
|
|
|
httpinfo.sock = connect(host, httpinfo.use_https ? 443 : 80);
|
2019-10-25 00:26:17 +02:00
|
|
|
|
2020-10-17 19:29:03 +02:00
|
|
|
if (httpinfo.sock < 0)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
if (httpinfo.sock == -ETIMEDOUT)
|
|
|
|
gprintf("\nFailed to connect (timed out)\n");
|
|
|
|
else
|
|
|
|
gprintf("\nFailed to connect (%i)\n", httpinfo.sock);
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("\nConnected\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
// Connect to a web proxy
|
|
|
|
if (getProxyAddress() && getProxyPort() > 0)
|
|
|
|
{
|
|
|
|
if (!connect_proxy(&httpinfo, host, getProxyUsername(), getProxyPassword()))
|
|
|
|
{
|
2020-09-07 22:27:42 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Failed to connect to proxy (%s:%i)\n", getProxyAddress(), getProxyPort());
|
2020-09-07 22:27:42 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
session = NULL; // Resume doesn't work with a proxy
|
2020-09-07 22:27:42 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Proxy is ready to receive\n");
|
2020-09-07 22:27:42 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
}
|
|
|
|
// Setup for HTTPS if it's necessary
|
|
|
|
if (httpinfo.use_https)
|
|
|
|
{
|
|
|
|
// Create a new SSL context
|
|
|
|
// wolfSSLv23_client_method() works but TLS 1.2 is slightly faster on Wii
|
|
|
|
if ((httpinfo.ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Failed to create WOLFSSL_CTX\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Don't verify certificates
|
|
|
|
wolfSSL_CTX_set_verify(httpinfo.ctx, WOLFSSL_VERIFY_NONE, 0);
|
|
|
|
// Enable SNI
|
|
|
|
if (wolfSSL_CTX_UseSNI(httpinfo.ctx, 0, host, strlen(host)) != WOLFSSL_SUCCESS)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Failed to set SNI\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Custom I/O is essential due to how libogc handles errors
|
|
|
|
wolfSSL_SetIOSend(httpinfo.ctx, send_callback);
|
|
|
|
wolfSSL_SetIORecv(httpinfo.ctx, recv_callback);
|
|
|
|
// Create a new wolfSSL session
|
|
|
|
if ((httpinfo.ssl = wolfSSL_new(httpinfo.ctx)) == NULL)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("SSL session creation failed\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set the file descriptor
|
|
|
|
if (wolfSSL_set_fd(httpinfo.ssl, httpinfo.sock) != SSL_SUCCESS)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Failed to set SSL file descriptor\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Attempt to resume the session
|
2021-02-05 20:51:15 +01:00
|
|
|
if (session && wolfSSL_set_session(httpinfo.ssl, session) != SSL_SUCCESS)
|
2020-10-17 19:29:03 +02:00
|
|
|
{
|
2020-04-12 21:27:47 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Failed to set session (session timed out?)\n");
|
2020-04-12 21:27:47 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
session = NULL;
|
|
|
|
}
|
|
|
|
// Initiate a handshake
|
|
|
|
u64 time = gettime();
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (ticks_to_millisecs(diff_ticks(time, gettime())) > CONNECT_TIMEOUT)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("SSL handshake failed\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (wolfSSL_connect(httpinfo.ssl) == SSL_SUCCESS)
|
|
|
|
break;
|
|
|
|
usleep(10000);
|
|
|
|
}
|
|
|
|
// Check if we resumed successfully
|
2021-02-05 20:51:15 +01:00
|
|
|
if (session && !wolfSSL_session_reused(httpinfo.ssl))
|
2020-10-17 19:29:03 +02:00
|
|
|
{
|
2020-04-12 21:27:47 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Failed to resume session\n");
|
2020-04-12 21:27:47 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
session = NULL;
|
|
|
|
}
|
|
|
|
// Cipher info
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
/*char ciphers[4096];
|
2019-10-25 00:26:17 +02:00
|
|
|
wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers));
|
|
|
|
gprintf("All supported ciphers: %s\n", ciphers);*/
|
2020-10-17 19:29:03 +02:00
|
|
|
WOLFSSL_CIPHER *cipher = wolfSSL_get_current_cipher(httpinfo.ssl);
|
|
|
|
gprintf("Using: %s - %s\n", wolfSSL_get_version(httpinfo.ssl), wolfSSL_CIPHER_get_name(cipher));
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
}
|
|
|
|
// Send our request
|
|
|
|
char request[2300];
|
|
|
|
int ret, len;
|
|
|
|
len = snprintf(request, sizeof(request),
|
|
|
|
"GET %s HTTP/1.1\r\n"
|
|
|
|
"Host: %s\r\n"
|
|
|
|
"User-Agent: WiiFlow-Lite\r\n"
|
|
|
|
"Connection: close\r\n"
|
|
|
|
"Pragma: no-cache\r\n"
|
|
|
|
"Cache-Control: no-cache\r\n\r\n",
|
|
|
|
path, host);
|
|
|
|
if ((ret = https_write(&httpinfo, request, len, false)) != len)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("https_write error: %i\n", ret);
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check if we want a response
|
|
|
|
if (buffer->skip_response)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Sent request to %s and skipping response\n", host);
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Get the response
|
|
|
|
HTTP_RESPONSE response = {0};
|
|
|
|
if (!get_response(&httpinfo, &response, false))
|
|
|
|
{
|
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The website wants to redirect us
|
|
|
|
if (response.status == 301 || response.status == 302)
|
|
|
|
{
|
|
|
|
https_close(&httpinfo);
|
|
|
|
if (loop == REDIRECT_LIMIT)
|
|
|
|
{
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Reached redirect limit\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
loop++;
|
|
|
|
char location[2049];
|
|
|
|
if (!get_header_value(response.headers, response.num_headers, location, "location"))
|
|
|
|
return;
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Redirect #%i - %s\n", loop, location);
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
downloadfile(location, buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// It's not 301 or 302, so reset the loop
|
|
|
|
loop = 0;
|
|
|
|
// We got what we wanted
|
|
|
|
if (response.status == 200)
|
|
|
|
{
|
|
|
|
buffer->data = MEM2_alloc(4096);
|
|
|
|
memcpy(buffer->data, &response.data[response.pret], response.buflen - response.pret);
|
|
|
|
// Determine how to read the data
|
|
|
|
bool dl_valid;
|
|
|
|
if (is_chunked(response.headers, response.num_headers))
|
|
|
|
dl_valid = read_chunked(&httpinfo, buffer, response.buflen - response.pret);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer->content_length = get_header_value_int(response.headers, response.num_headers, "content-length");
|
|
|
|
dl_valid = read_all(&httpinfo, buffer, response.buflen - response.pret);
|
|
|
|
}
|
|
|
|
// Check if the download is incomplete
|
|
|
|
if (!dl_valid || buffer->size < 1)
|
|
|
|
{
|
|
|
|
buffer->size = 0;
|
|
|
|
MEM2_free(buffer->data);
|
2020-04-14 22:40:38 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Removed incomplete download\n");
|
2020-04-14 22:40:38 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Save the session
|
|
|
|
if (httpinfo.use_https)
|
|
|
|
session = wolfSSL_get_session(httpinfo.ssl);
|
|
|
|
// Finished
|
|
|
|
https_close(&httpinfo);
|
2019-10-25 00:26:17 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Download size: %llu\n", (long long)buffer->size);
|
|
|
|
gprintf("------------- HEADERS -------------\n");
|
|
|
|
for (size_t i = 0; i != response.num_headers; ++i)
|
|
|
|
gprintf("%.*s: %.*s\n", (int)response.headers[i].name_len, response.headers[i].name,
|
|
|
|
(int)response.headers[i].value_len, response.headers[i].value);
|
|
|
|
gprintf("------------ COMPLETED ------------\n");
|
2019-10-25 00:26:17 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Close on all other status codes
|
2020-04-12 21:27:47 +02:00
|
|
|
#ifdef DEBUG_NETWORK
|
2020-10-17 19:29:03 +02:00
|
|
|
gprintf("Status code: %i - %s\n", response.status, url);
|
2020-04-12 21:27:47 +02:00
|
|
|
#endif
|
2020-10-17 19:29:03 +02:00
|
|
|
https_close(&httpinfo);
|
2019-10-25 00:26:17 +02:00
|
|
|
}
|