Skip to content

Commit

Permalink
Merge pull request #170 from timoschlueter/bugfix/fix-connection-error
Browse files Browse the repository at this point in the history
Fix for connection errors starting November 2024
  • Loading branch information
timoschlueter authored Nov 15, 2024
2 parents cfa81b1 + 420b340 commit 18a7b87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {HttpCookieAgent} from "http-cookie-agent/http";
import {Agent as HttpAgent} from "node:http";
import {Agent as HttpsAgent} from "node:https";
import * as crypto from "crypto";
import {jwtDecode} from "jwt-decode";
import {Jwt} from "./interfaces/librelink/jwt";

// Generate new Ciphers for stealth mode in order to bypass SSL fingerprinting used by Cloudflare.
// The new Ciphers are then used in the HTTPS Agent for Axios.
Expand Down Expand Up @@ -95,7 +97,8 @@ const libreLinkUpHttpHeaders: LibreLinkUpHttpHeaders = {
"User-Agent": USER_AGENT,
"Content-Type": "application/json;charset=UTF-8",
"version": LIBRE_LINK_UP_VERSION,
"product": LIBRE_LINK_UP_PRODUCT
"product": LIBRE_LINK_UP_PRODUCT,
"account-id": "",
}

if (config.singleShot)
Expand Down Expand Up @@ -141,7 +144,7 @@ async function main(): Promise<void>
export async function login(): Promise<AuthTicket | null>
{
config = readConfig()

try
{
const url = "https://" + LIBRE_LINK_UP_URL + "/llu/auth/login"
Expand Down Expand Up @@ -353,6 +356,19 @@ function getLluAuthHeaders(): LibreLinkUpHttpHeaders
{
const authenticatedHttpHeaders = libreLinkUpHttpHeaders;
authenticatedHttpHeaders.Authorization = "Bearer " + getAuthenticationToken();

if (authTicket)
{
try
{
let jwt: Jwt = jwtDecode(authTicket.token);
let hashedAccountId: string = crypto.createHash("sha256").update(jwt.id).digest("hex");
authenticatedHttpHeaders["account-id"] = hashedAccountId;
} catch (error)
{
logger.error("Error getting accountId: ", error);
}
}
logger.debug("authenticatedHttpHeaders: " + JSON.stringify(authenticatedHttpHeaders));
return authenticatedHttpHeaders;
}
Expand Down
20 changes: 20 additions & 0 deletions src/interfaces/librelink/jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Interfaces for the JSON Web Token (JWT)
*
* SPDX-License-Identifier: MIT
*/

export interface Jwt
{
id: string;
firstName: string;
lastName: string;
country: string;
region: string;
role: string;
units: number;
practices: Array<string>;
c: number;
s: string;
exp: number;
}

0 comments on commit 18a7b87

Please sign in to comment.