[World] Fix slow world bootup bug (#4461)

* [World] Fix slow world bootup bug

* Update ip_util.cpp

* Add timeout

* Update ip_util.cpp

* Cross platform timeout
This commit is contained in:
Chris Miles 2024-08-26 20:59:43 -05:00 committed by GitHub
parent 7dfda95d86
commit 1f9c4b3a22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -81,45 +81,79 @@ bool IpUtil::IsIpInPrivateRfc1918(const std::string &ip)
); );
} }
/**
* Gets local address - pings google to inspect what interface was used locally #ifdef _WIN32
* @return #include <winsock2.h>
*/ #include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#endif
#include <iostream>
#include <string>
#include <cstring>
std::string IpUtil::GetLocalIPAddress() std::string IpUtil::GetLocalIPAddress()
{ {
char my_ip_address[16]; #ifdef _WIN32
unsigned int my_port; WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
return "";
}
#endif
char my_ip_address[INET_ADDRSTRLEN];
struct sockaddr_in server_address{}; struct sockaddr_in server_address{};
struct sockaddr_in my_address{}; struct sockaddr_in my_address{};
int sockfd; int sockfd;
// Connect to server // Create a UDP socket
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { #ifdef _WIN32
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == INVALID_SOCKET) {
WSACleanup();
return "";
}
#else
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
return ""; return "";
} }
#endif
// Set server_addr // Set server_addr (dummy address)
memset(&server_address, 0, sizeof(server_address)); memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET; server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("172.217.160.99"); server_address.sin_addr.s_addr = inet_addr("8.8.8.8"); // Google DNS
server_address.sin_port = htons(80); server_address.sin_port = htons(53); // DNS port
// Connect to server // Perform a dummy connection to the server (UDP)
if (connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) { connect(sockfd, (struct sockaddr *) &server_address, sizeof(server_address));
close(sockfd);
return "";
}
// Get my ip address and port // Get my IP address
memset(&my_address, 0, sizeof(my_address)); memset(&my_address, 0, sizeof(my_address));
socklen_t len = sizeof(my_address); socklen_t len = sizeof(my_address);
getsockname(sockfd, (struct sockaddr *) &my_address, &len); getsockname(sockfd, (struct sockaddr *) &my_address, &len);
inet_ntop(AF_INET, &my_address.sin_addr, my_ip_address, sizeof(my_ip_address)); inet_ntop(AF_INET, &my_address.sin_addr, my_ip_address, sizeof(my_ip_address));
my_port = ntohs(my_address.sin_port);
return fmt::format("{}", my_ip_address); #ifdef _WIN32
closesocket(sockfd);
WSACleanup();
#else
close(sockfd);
#endif
LogInfo("Local IP Address [{}]", my_ip_address);
return std::string(my_ip_address);
} }
/** /**
* Gets public address * Gets public address
* Uses various websites as options to return raw public IP back to the client * Uses various websites as options to return raw public IP back to the client