Skip to content

Commit d5e4854

Browse files
committed
remove unifi api it's doesnt work, unifi learn how to make an api...
1 parent 5ee2f79 commit d5e4854

File tree

3 files changed

+15
-68
lines changed

3 files changed

+15
-68
lines changed

handle-api.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,6 @@ function invalidateCache(env) {
2121
}
2222
}
2323

24-
// Helper to fetch Unifi API data
25-
export async function fetchUnifiData(env) {
26-
const apiKey = await env.UNIFI_API_KEY; // Secret API key
27-
const response = await fetch('https://api.ui.com/v1/sites?pageSize=1', {
28-
method: 'GET',
29-
headers: {
30-
'Accept': 'application/json',
31-
'X-API-Key': apiKey
32-
}
33-
});
34-
35-
if (!response.ok) {
36-
throw new Error(`Unifi API call failed with status ${response.status}`);
37-
}
38-
39-
const data = await response.json();
40-
const wanLTEFailover = data?.data?.[0]?.statistics?.wans?.WAN_LTE_FAILOVER;
41-
const wanUptime = wanLTEFailover?.wanUptime;
42-
43-
return wanUptime !== undefined && wanUptime < 100; // 4G is active if uptime < 100
44-
}
45-
4624
export async function handleApi(request, url, host, env, state) {
4725
// Restrict API access to the maintenance domain
4826
if (host !== env.MAINTENANCE_DOMAIN) {

worker.js

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { c_redirect } from './custom-redirect.js'
22
import maintenanceHtml from './html/maintenance.js'
3-
import { handleApi, fetchUnifiData } from './handle-api.js'
3+
import { handleApi } from './handle-api.js'
44

55
// Helper to safely parse JSON
66
function safeJsonParse(str, fallback) {
@@ -45,44 +45,18 @@ async function getMaintenanceState(env, host, useCache = true) {
4545
});
4646
}
4747

48-
// Determine 4G mode only if ENABLE_4G_BANNER is true
49-
let is4gMode = false;
50-
if (env.ENABLE_4G_BANNER === true || env.ENABLE_4G_BANNER === 'true') {
51-
if (env.UNIFI_USER === true || env.UNIFI_USER === 'true') {
52-
if (useCache && cacheEnabled) {
53-
if (cache.is4g.value !== null && (now - cache.is4g.ts < cacheTtl)) {
54-
is4gMode = cache.is4g.value;
55-
} else {
56-
try {
57-
is4gMode = await fetchUnifiData(env);
58-
cache.is4g.value = is4gMode;
59-
cache.is4g.ts = now;
60-
} catch (err) {
61-
console.error('Failed to fetch Unifi data:', err);
62-
is4gMode = false; // Default to false if API call fails
63-
}
64-
}
65-
} else {
66-
try {
67-
is4gMode = await fetchUnifiData(env);
68-
} catch (err) {
69-
console.error('Failed to fetch Unifi data:', err);
70-
is4gMode = false; // Default to false if API call fails
71-
}
72-
}
48+
// Cache for wan-is-4g
49+
let is4gMode;
50+
if (useCache && cacheEnabled) {
51+
if (cache.is4g.value !== null && (now - cache.is4g.ts < cacheTtl)) {
52+
is4gMode = cache.is4g.value;
7353
} else {
74-
if (useCache && cacheEnabled) {
75-
if (cache.is4g.value !== null && (now - cache.is4g.ts < cacheTtl)) {
76-
is4gMode = cache.is4g.value;
77-
} else {
78-
is4gMode = await env.MAINTENANCE_KV.get('wan-is-4g');
79-
cache.is4g.value = is4gMode;
80-
cache.is4g.ts = now;
81-
}
82-
} else {
83-
is4gMode = await env.MAINTENANCE_KV.get('wan-is-4g');
84-
}
54+
is4gMode = await env.MAINTENANCE_KV.get('wan-is-4g');
55+
cache.is4g.value = is4gMode;
56+
cache.is4g.ts = now;
8557
}
58+
} else {
59+
is4gMode = await env.MAINTENANCE_KV.get('wan-is-4g');
8660
}
8761

8862
return {

wrangler.toml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ ENABLE_CACHE = true
2323
# Cache duration in ms (default: 60000 = 1 minute)
2424
CACHE_TTL_MS = 60000
2525

26-
# Enable or disable 4G banner functionality
27-
ENABLE_4G_BANNER = true
28-
# Message for 4G banner when fiber is down
29-
TEXT_4G_BANNER_MESSAGE = "Attention des lenteurs peuvent survenir, ma fibre est H.S. Le serveur fonctionne en 4G !"
30-
31-
32-
# Enable/disable Unifi integration (true/false)
33-
UNIFI_USER = false
34-
3526

3627

3728

@@ -66,3 +57,7 @@ TEXT_CONTAINER_ERROR_TYPE = "Service indisponible"
6657
TEXT_CONTAINER_ERROR_MESSAGE = "Service non démarré ou planté ! <br> N’hésitez pas à me le signaler au cas où !"
6758
TEXT_CONTAINER_ERROR_GIF = "https://raw.githubusercontent.com/JamesDAdams/cloudflare-worker-error-page/refs/heads/main/assets/container_error.gif"
6859

60+
# Enable or disable 4G banner functionality
61+
ENABLE_4G_BANNER = true
62+
# Message for 4G banner when fiber is down
63+
TEXT_4G_BANNER_MESSAGE = "Attention des lenteurs peuvent survenir, ma fibre est H.S. Le serveur fonctionne en 4G !"

0 commit comments

Comments
 (0)