Covers and banners download faster

This commit is contained in:
wiidev 2020-04-12 20:27:47 +01:00
parent 4d6f75112b
commit 28ae5daf18
2 changed files with 62 additions and 25 deletions

View File

@ -1,7 +1,6 @@
/* // Code by blackb0x @ GBAtemp.net
Code by blackb0x @ GBAtemp.net // This allows the Wii to download from servers that use SNI.
This allows the Wii to download from servers that use SNI.
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -15,6 +14,7 @@
#include "memory/mem2.hpp" #include "memory/mem2.hpp"
u8 loop; u8 loop;
WOLFSSL_SESSION *session;
int https_write(HTTP_INFO *httpinfo, char *buffer, int len) int https_write(HTTP_INFO *httpinfo, char *buffer, int len)
{ {
@ -40,12 +40,32 @@ int https_write(HTTP_INFO *httpinfo, char *buffer, int len)
int https_read(HTTP_INFO *httpinfo, char *buffer, int len) int https_read(HTTP_INFO *httpinfo, char *buffer, int len)
{ {
struct pollsd fds[1];
fds[0].socket = httpinfo->sock;
fds[0].events = POLLIN;
net_fcntl(httpinfo->sock, F_SETFL, 4);
switch (net_poll(fds, 1, READ_WRITE_TIMEOUT))
{
case -1:
#ifdef DEBUG_NETWORK
gprintf("net_poll error\n");
#endif
return -1;
case 0:
#ifdef DEBUG_NETWORK
gprintf("The connection timed out\n");
#endif
return -ETIMEDOUT;
default:
net_fcntl(httpinfo->sock, F_SETFL, 0);
if (len > 8192) if (len > 8192)
len = 8192; // 16KB is the max on a Wii, but 8KB is safe len = 8192; // 16KB is the max on a Wii, but 8KB is safe
if (httpinfo->use_https) if (httpinfo->use_https)
return wolfSSL_read(httpinfo->ssl, buffer, len); return wolfSSL_read(httpinfo->ssl, buffer, len);
return net_read(httpinfo->sock, buffer, len); return net_read(httpinfo->sock, buffer, len);
} }
}
void https_close(HTTP_INFO *httpinfo) void https_close(HTTP_INFO *httpinfo)
{ {
@ -182,7 +202,7 @@ int connect(char *host, u16 port)
if (ticks_to_millisecs(diff_ticks(t, gettime())) > TCP_CONNECT_TIMEOUT) if (ticks_to_millisecs(diff_ticks(t, gettime())) > TCP_CONNECT_TIMEOUT)
{ {
#ifdef DEBUG_NETWORK #ifdef DEBUG_NETWORK
gprintf("The connection has timed out\n"); gprintf("The connection timed out\n");
#endif #endif
net_close(sock); net_close(sock);
return -ETIMEDOUT; return -ETIMEDOUT;
@ -203,11 +223,6 @@ int connect(char *host, u16 port)
break; break;
} }
net_fcntl(sock, F_SETFL, 0); net_fcntl(sock, F_SETFL, 0);
// Set a read and write timeout
struct timeval timeout;
timeout.tv_sec = READ_WRITE_TIMEOUT;
net_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
net_setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
return sock; return sock;
} }
@ -256,8 +271,9 @@ void downloadfile(const char *url, struct download *buffer)
if (httpinfo.use_https) if (httpinfo.use_https)
{ {
// Create a new SSL context and use the highest possible protocol version // Create a new SSL context
if ((httpinfo.ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())) == NULL) // wolfSSLv23_client_method() works, but resume would require further changes
if ((httpinfo.ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL)
{ {
#ifdef DEBUG_NETWORK #ifdef DEBUG_NETWORK
gprintf("Failed to create WOLFSSL_CTX\n"); gprintf("Failed to create WOLFSSL_CTX\n");
@ -294,6 +310,14 @@ void downloadfile(const char *url, struct download *buffer)
https_close(&httpinfo); https_close(&httpinfo);
return; return;
} }
// Attempt to resume the session
if (session != NULL && wolfSSL_set_session(httpinfo.ssl, session) != SSL_SUCCESS)
{
#ifdef DEBUG_NETWORK
gprintf("Failed to set session (session timed out?)\n");
#endif
session = NULL;
}
// Initiate a handshake // Initiate a handshake
if (wolfSSL_connect(httpinfo.ssl) != SSL_SUCCESS) if (wolfSSL_connect(httpinfo.ssl) != SSL_SUCCESS)
{ {
@ -303,6 +327,14 @@ void downloadfile(const char *url, struct download *buffer)
https_close(&httpinfo); https_close(&httpinfo);
return; return;
} }
// Check if we resumed successfully
if (session != NULL && !wolfSSL_session_reused(httpinfo.ssl))
{
#ifdef DEBUG_NETWORK
gprintf("Failed to resume session\n");
#endif
session = NULL;
}
// Cipher info // Cipher info
#ifdef DEBUG_NETWORK #ifdef DEBUG_NETWORK
/*char ciphers[4096]; /*char ciphers[4096];
@ -314,13 +346,13 @@ void downloadfile(const char *url, struct download *buffer)
} }
// Send our request // Send our request
char request[2048]; char request[2200];
char isgecko[36] = "Cookie: challenge=BitMitigate.com\r\n"; char isgecko[36] = "Cookie: challenge=BitMitigate.com\r\n";
int ret, len; int ret, len;
if (strcmp(host, "www.geckocodes.org") != 0) if (strcmp(host, "www.geckocodes.org") != 0)
memset(isgecko, 0, sizeof(isgecko)); // Not geckocodes, so don't set a cookie memset(isgecko, 0, sizeof(isgecko)); // Not geckocodes, so don't set a cookie
len = snprintf(request, 2048, len = snprintf(request, 2200,
"GET %s HTTP/1.1\r\n" "GET %s HTTP/1.1\r\n"
"Host: %s\r\n" "Host: %s\r\n"
"User-Agent: WiiFlow-Lite\r\n" "User-Agent: WiiFlow-Lite\r\n"
@ -429,6 +461,9 @@ void downloadfile(const char *url, struct download *buffer)
read_chunked(&httpinfo, buffer, buflen - pret); read_chunked(&httpinfo, buffer, buflen - pret);
else else
read_all(&httpinfo, buffer, buflen - pret); read_all(&httpinfo, buffer, buflen - pret);
// Save the session
if (httpinfo.use_https)
session = wolfSSL_get_session(httpinfo.ssl);
// Finished // Finished
https_close(&httpinfo); https_close(&httpinfo);
#ifdef DEBUG_NETWORK #ifdef DEBUG_NETWORK
@ -440,5 +475,8 @@ void downloadfile(const char *url, struct download *buffer)
return; return;
} }
// Close on all other status codes // Close on all other status codes
#ifdef DEBUG_NETWORK
gprintf("Status code: %i - %s\n", status, url);
#endif
https_close(&httpinfo); https_close(&httpinfo);
} }

View File

@ -1,7 +1,6 @@
/* // Code by blackb0x @ GBAtemp.net
Code by blackb0x @ GBAtemp.net // This allows the Wii to download from servers that use SNI.
This allows the Wii to download from servers that use SNI.
*/
#ifndef _HTTPS_H_ #ifndef _HTTPS_H_
#define _HTTPS_H_ #define _HTTPS_H_
@ -16,7 +15,7 @@ extern "C"
// #define DEBUG_NETWORK // #define DEBUG_NETWORK
#define REDIRECT_LIMIT 3 #define REDIRECT_LIMIT 3
#define TCP_CONNECT_TIMEOUT 5000 #define TCP_CONNECT_TIMEOUT 5000
#define READ_WRITE_TIMEOUT 10 #define READ_WRITE_TIMEOUT 5000
struct download struct download
{ {