Konik Tooling

This commit is contained in:
firestar5683
2026-06-27 23:23:39 -05:00
parent a8ec45ec12
commit e82a4f034b
16 changed files with 472 additions and 119 deletions
+49 -4
View File
@@ -7,7 +7,10 @@
#include <openssl/sha.h>
#include <cassert>
#include <algorithm>
#include <cctype>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include "common/params.h"
@@ -16,6 +19,30 @@
namespace CommaApi2 {
const std::string COMMA_API_HOST = "https://api.commadotai.com";
const std::string COMMA_API_HOST_ALIAS = "https://api.comma.ai";
const std::string KONIK_API_HOST = "https://api.konik.ai";
std::string normalize_api_host(const std::string &host) {
std::string normalized = host.empty() ? COMMA_API_HOST : host;
while (!normalized.empty() && normalized.back() == '/') {
normalized.pop_back();
}
if (normalized.find("://") == std::string::npos) {
normalized = "https://" + normalized;
}
std::transform(normalized.begin(), normalized.end(), normalized.begin(), [](unsigned char c) { return std::tolower(c); });
return normalized == COMMA_API_HOST_ALIAS ? COMMA_API_HOST : normalized;
}
std::vector<std::string> route_api_hosts() {
const char *api_host = std::getenv("API_HOST");
if (api_host != nullptr && api_host[0] != '\0') {
return {normalize_api_host(api_host)};
}
return {COMMA_API_HOST, KONIK_API_HOST};
}
// Base64 URL-safe character set (uses '-' and '_' instead of '+' and '/')
static const std::string base64url_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -104,7 +131,25 @@ std::string create_jwt(const json11::Json &extra, int exp_time) {
return jwt + "." + base64url_encode(signature);
}
std::string create_token(bool use_jwt, const json11::Json &payloads, int expiry) {
std::string auth_token_for_host(const json11::Json &json, const std::string &api_host) {
const std::string normalized_host = normalize_api_host(api_host);
const json11::Json tokens = json["tokens"];
if (tokens.is_object()) {
const std::string token = tokens[normalized_host].string_value();
if (!token.empty()) {
return token;
}
}
const std::string env_host = normalize_api_host(util::getenv("API_HOST", COMMA_API_HOST));
if (normalized_host == COMMA_API_HOST || normalized_host == env_host) {
return json["access_token"].string_value();
}
return "";
}
std::string create_token(bool use_jwt, const json11::Json &payloads, int expiry, const std::string &api_host) {
if (use_jwt) {
return create_jwt(payloads, expiry);
}
@@ -116,15 +161,15 @@ std::string create_token(bool use_jwt, const json11::Json &payloads, int expiry)
std::cerr << "Error parsing auth.json " << err << std::endl;
return "";
}
return json["access_token"].string_value();
return auth_token_for_host(json, api_host);
}
std::string httpGet(const std::string &url, long *response_code) {
std::string httpGet(const std::string &url, long *response_code, const std::string &api_host) {
CURL *curl = curl_easy_init();
assert(curl);
std::string readBuffer;
const std::string token = CommaApi2::create_token(!Hardware::PC());
const std::string token = CommaApi2::create_token(!Hardware::PC(), {}, 3600, api_host);
// Set up the lambda for the write callback
// The '+' makes the lambda non-capturing, allowing it to be used as a C function pointer
+5 -2
View File
@@ -2,6 +2,7 @@
#include <curl/curl.h>
#include <string>
#include <vector>
#include "common/util.h"
#include "third_party/json11/json11.hpp"
@@ -9,7 +10,9 @@
namespace CommaApi2 {
const std::string BASE_URL = util::getenv("API_HOST", "https://api.commadotai.com").c_str();
std::string create_token(bool use_jwt, const json11::Json& payloads = {}, int expiry = 3600);
std::string httpGet(const std::string &url, long *response_code = nullptr);
std::string normalize_api_host(const std::string &host);
std::vector<std::string> route_api_hosts();
std::string create_token(bool use_jwt, const json11::Json& payloads = {}, int expiry = 3600, const std::string &api_host = BASE_URL);
std::string httpGet(const std::string &url, long *response_code = nullptr, const std::string &api_host = BASE_URL);
} // namespace CommaApi2
+28 -20
View File
@@ -15,7 +15,7 @@ Route::Route(const std::string &route, const std::string &data_dir, bool auto_so
RouteIdentifier Route::parseRoute(const std::string &str) {
RouteIdentifier identifier = {};
static const std::regex pattern(R"(^(([a-z0-9]{16})[|_/])?(.{20})((--|/)((-?\d+(:(-?\d+)?)?)|(:-?\d+)))?$)");
static const std::regex pattern(R"(^(([a-z0-9]{16})[|_/])?(.{20})((--|/)((-?\d+(:(-?\d+)?)?)|(:-?\d+)))?(/[qra])?$)");
std::smatch match;
if (std::regex_match(str, match, pattern)) {
@@ -104,30 +104,38 @@ bool Route::loadFromAutoSource() {
}
bool Route::loadFromServer(int retries) {
const std::string url = CommaApi2::BASE_URL + "/v1/route/" + route_.str + "/files";
for (int i = 1; i <= retries; ++i) {
long response_code = 0;
std::string result = CommaApi2::httpGet(url, &response_code);
if (response_code == 200) {
return loadFromJson(result);
const auto api_hosts = CommaApi2::route_api_hosts();
for (const auto &api_host : api_hosts) {
const std::string url = api_host + "/v1/route/" + route_.str + "/files";
for (int i = 1; i <= retries; ++i) {
long response_code = 0;
std::string result = CommaApi2::httpGet(url, &response_code, api_host);
if (response_code == 200) {
return loadFromJson(result);
}
if (response_code == 401 || response_code == 403) {
rWarning(">> Unauthorized. Authenticate with tools/lib/auth.py <<");
err_ = RouteLoadError::Unauthorized;
return false;
}
if (response_code == 404) {
err_ = RouteLoadError::FileNotFound;
break;
}
err_ = RouteLoadError::NetworkError;
rWarning("Retrying %d/%d", i, retries);
util::sleep_for(3000);
}
if (response_code == 401 || response_code == 403) {
rWarning(">> Unauthorized. Authenticate with tools/lib/auth.py <<");
err_ = RouteLoadError::Unauthorized;
break;
if (err_ == RouteLoadError::NetworkError) {
return false;
}
if (response_code == 404) {
rWarning("The specified route could not be found on the server.");
err_ = RouteLoadError::FileNotFound;
break;
}
err_ = RouteLoadError::NetworkError;
rWarning("Retrying %d/%d", i, retries);
util::sleep_for(3000);
}
rWarning(api_hosts.size() > 1 ? "The specified route could not be found on comma or Konik." : "The specified route could not be found on the server.");
err_ = RouteLoadError::FileNotFound;
return false;
}