diff --git a/main.cpp b/main.cpp index 8fd0bb8..65cbf21 100644 --- a/main.cpp +++ b/main.cpp @@ -4,76 +4,104 @@ #include #include -// Callback function to receive HTTP response +// http callback size_t write_callback(void* buffer, size_t size, size_t nmemb, std::string* userdata) { size_t totalSize = size * nmemb; userdata->append(static_cast(buffer), totalSize); return totalSize; } -void print_json(const rapidjson::Value& value, const std::string& prefix = "") { - if (value.IsObject()) { - for (auto it = value.MemberBegin(); it != value.MemberEnd(); ++it) { - std::cout << prefix << it->name.GetString() << ": "; - print_json(it->value, prefix + " "); - } - } - else if (value.IsString()) { - std::cout << value.GetString() << std::endl; - } - else if (value.IsNumber()) { - if (value.IsDouble()) - std::cout << value.GetDouble() << std::endl; - else if (value.IsInt()) - std::cout << value.GetInt() << std::endl; - else if (value.IsUint()) - std::cout << value.GetUint() << std::endl; - else if (value.IsInt64()) - std::cout << value.GetInt64() << std::endl; - else if (value.IsUint64()) - std::cout << value.GetUint64() << std::endl; +int main(int argc, char* argv[]) { + const char* aws_url = "https://checkip.amazonaws.com/"; + const char* ip_api_url = "http://ip-api.com/json/"; + std::string response; + + // int aws + CURL* aws_curl = curl_easy_init(); + if (!aws_curl) { + std::cerr << "Failed to initialize libcurl for AWS." << std::endl; + return 1; } -} -int main(int argc, char* argv[]) { - const char* url = "https://checkip.amazonaws.com/"; + // Fetch IP address from AWS endpoint + curl_easy_setopt(aws_curl, CURLOPT_URL, aws_url); + curl_easy_setopt(aws_curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(aws_curl, CURLOPT_WRITEDATA, &response); - // Initialize libcurl for each thread - CURL* curl = curl_easy_init(); - if (!curl) { - std::cerr << "Failed to initialize libcurl." << std::endl; + CURLcode aws_res = curl_easy_perform(aws_curl); + if (aws_res != CURLE_OK) { + std::cerr << "curl_easy_perform() for AWS failed: " << curl_easy_strerror(aws_res) << std::endl; + curl_easy_cleanup(aws_curl); return 1; } - std::string response; + std::string aws_ip = response; - // If there are no flags provided, get the IP only. - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response); + curl_easy_cleanup(aws_curl); - CURLcode res = curl_easy_perform(curl); - if (res != CURLE_OK) { - std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; - curl_easy_cleanup(curl); - return 1; + // Check for command-line flags + bool geo_flag = false; + bool json_flag = false; + if (argc > 1) { + for (int i = 1; i < argc; ++i) { + std::string flag = argv[i]; + if (flag == "-geo") { + geo_flag = true; + } + else if (flag == "-json") { + json_flag = true; + } + } } - // Parse JSON response. - rapidjson::Document document; - document.Parse(response.c_str()); + if (geo_flag) { + CURL* ip_api_curl = curl_easy_init(); + if (!ip_api_curl) { + std::cerr << "Failed to initialize libcurl for IP-API." << std::endl; + return 1; + } - if (document.HasParseError()) { - std::cerr << "JSON parse error: " << rapidjson::GetParseError_En(document.GetParseError()) << std::endl; - curl_easy_cleanup(curl); - return 1; + curl_easy_setopt(ip_api_curl, CURLOPT_URL, ip_api_url); + response.clear(); + curl_easy_setopt(ip_api_curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(ip_api_curl, CURLOPT_WRITEDATA, &response); + + CURLcode ip_api_res = curl_easy_perform(ip_api_curl); + if (ip_api_res != CURLE_OK) { + std::cerr << "curl_easy_perform() for IP-API failed: " << curl_easy_strerror(ip_api_res) << std::endl; + curl_easy_cleanup(ip_api_curl); + return 1; + } + + std::cout << response << std::endl; + + curl_easy_cleanup(ip_api_curl); } + else if (json_flag) { + CURL* ip_api_curl = curl_easy_init(); + if (!ip_api_curl) { + std::cerr << "Failed to initialize libcurl for IP-API." << std::endl; + return 1; + } + + curl_easy_setopt(ip_api_curl, CURLOPT_URL, ip_api_url); + response.clear(); + curl_easy_setopt(ip_api_curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(ip_api_curl, CURLOPT_WRITEDATA, &response); - std::cout << "Received data from IP-API:" << std::endl; - print_json(document); + CURLcode ip_api_res = curl_easy_perform(ip_api_curl); + if (ip_api_res != CURLE_OK) { + std::cerr << "curl_easy_perform() for IP-API failed: " << curl_easy_strerror(ip_api_res) << std::endl; + curl_easy_cleanup(ip_api_curl); + return 1; + } - // Cleanup libcurl for each thread - curl_easy_cleanup(curl); + std::cout << response << std::endl; + curl_easy_cleanup(ip_api_curl); + } + else { + std::cout << "IP: " << aws_ip << std::endl; + } return 0; }