From fa32bcfad8ce70808c1d3a9b0d1aeaded4d4ee73 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Tue, 21 Dec 2021 22:19:26 -0500 Subject: [PATCH] Add public ip to wifi page --- src/panel/wifi.cpp | 33 ++++++++++++++++++++++++++++++++- src/panel/wifi.hpp | 4 ++++ src/panel/wifi_config.hpp | 7 +++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/panel/wifi.cpp b/src/panel/wifi.cpp index 772f4b1..7d0c613 100644 --- a/src/panel/wifi.cpp +++ b/src/panel/wifi.cpp @@ -97,7 +97,7 @@ void wifi::update_texture(){ //show info about the network tgt.x = (SCREEN_WIDTH / 2) + 25; - tgt.y = 50; + tgt.y = 60; TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), network_string.c_str(), &tgt.w, &tgt.h); SDL_RenderCopy(board::getRenderer(), @@ -113,6 +113,31 @@ void wifi::update_texture(){ board::getString(password_string.c_str(), { "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), NULL, &tgt); + //Get public ip address and display it + if(WIFI_SHOW_PUBLIC_IP){ + std::string public_ip = "WAN IP: "; + CURL* curl; + curl = curl_easy_init(); + + if(curl){ + CURLcode res; + curl_easy_setopt(curl, CURLOPT_URL, WIFI_PUBLIC_IP_URL); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, + dashboard::panel::curl_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &public_ip); + res = curl_easy_perform(curl); + curl_easy_cleanup(curl); + } else { + public_ip += "Unkown"; + } + + tgt.y += tgt.h + 25; + TTF_SizeText(board::getFont({ "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), + public_ip.c_str(), &tgt.w, &tgt.h); + SDL_RenderCopy(board::getRenderer(), + board::getString(public_ip.c_str(), + { "Roboto_Mono/RobotoMono-Medium.ttf", 50 }), NULL, &tgt); + } SDL_SetRenderTarget(board::getRenderer(), NULL); @@ -133,3 +158,9 @@ void wifi::initTexture(){ SDL_SetTextureBlendMode(_texture, SDL_BLENDMODE_BLEND); } } + +size_t dashboard::panel::curl_callback(void* contents, size_t size, + size_t nmemb, void* userp){ + ((std::string*)userp)->append((char*)contents, size * nmemb); + return size * nmemb; +} diff --git a/src/panel/wifi.hpp b/src/panel/wifi.hpp index fbee419..7d21661 100644 --- a/src/panel/wifi.hpp +++ b/src/panel/wifi.hpp @@ -12,6 +12,8 @@ #include #include +#include + #include namespace dashboard::panel { @@ -26,6 +28,8 @@ namespace dashboard::panel { void update_texture(); void initTexture(); }; + + size_t curl_callback(void*, size_t, size_t, void*); } #include "../board.hpp" diff --git a/src/panel/wifi_config.hpp b/src/panel/wifi_config.hpp index 6735d22..c127e3d 100644 --- a/src/panel/wifi_config.hpp +++ b/src/panel/wifi_config.hpp @@ -15,9 +15,16 @@ namespace dashboard::panel { //Due to the nature of this panel, this value is ignored constexpr size_t WIFI_UPDATE_INTERVAL = 60000; + //Set to true to have your public IP be shown on the wifi page + //Will make a curl request to WIFI_PUBLIC_IP_URL when the frame + //is rendered. This this is a privacy concern disable this option + constexpr bool WIFI_SHOW_PUBLIC_IP = true; + constexpr char WIFI_PUBLIC_IP_URL[] = "http://ipinfo.io/ip"; + //All of the following options WILL be shown on the screen. //If this is a privacy concernt for you, set the value to be blank //(ie "") and it will not be shown constexpr char WIFI_NETWORK_NAME[] = "MyNetwork"; constexpr char WIFI_NETWORK_PASS[] = "MyPassword"; + }