Skip to content

Commit 4fbb8fd

Browse files
committed
proxy v0.013
1 parent 8f887d1 commit 4fbb8fd

File tree

13 files changed

+710
-599
lines changed

13 files changed

+710
-599
lines changed

assets_js_bitrequest_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const glob_apptitle = "Bitrequest",
1717
"https://www.bitrequest.app/"
1818
],
1919
glob_hosted_proxy = random_array_item(glob_proxy_list), // load balance proxies
20-
glob_proxy_version = "0.012",
20+
glob_proxy_version = "0.013",
2121
glob_firebase_dynamic_link_domain = "bitrequest.page.link",
2222
glob_firebase_shortlink = "https://" + glob_firebase_dynamic_link_domain + "/",
2323
glob_androidpackagename = "io.bitrequest.app",

assets_js_bitrequest_core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4692,7 +4692,7 @@ function proxy_alert(version) {
46924692
$("#alert > span").text("!").attr("title", translate("updateproxy", {
46934693
"version": version,
46944694
"proxy_version": glob_proxy_version
4695-
}));
4695+
}) + " " + d_proxy());
46964696
}
46974697
}
46984698

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<html class="firstload gdauth">
1+
<html class="firstload">
22
<head>
33
<meta http-equiv="cache-control" content="no-cache"/>
44
<meta http-equiv="expires" content="0"/>

proxy/v1/api.php

Lines changed: 156 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,179 @@
11
<?php
2-
// PROXY
3-
4-
const VERSION = "0.012";
5-
const CACHE_DURATIONS = [
6-
"2m" => 6220800,
7-
"1w" => 604800,
8-
"tx" => 604800,
9-
"1d" => 86400,
10-
"1h" => 3600
11-
];
12-
13-
// Main API function that handles caching and retrieval of data
14-
function api($url, $data, $headers, $ct, $cfd, $meta, $fn) {
15-
$cf = $cfd ?? false;
16-
if (!$cf) {
17-
return get_non_cached_result($url, $data, $headers);
18-
}
19-
20-
$cache_refresh = CACHE_DURATIONS[$cf] ?? 3600;
21-
$cache_time = $ct ?? $cache_refresh;
22-
$cache_folder = "cache/" . $cf . "/";
23-
$filename = $fn ?? md5($data . $url);
24-
$cache_file = $cache_folder . $filename;
2+
// PROXY
3+
4+
const VERSION = "0.013";
5+
const CACHE_DURATIONS = [
6+
"2m" => 6220800,
7+
"1w" => 604800,
8+
"tx" => 604800,
9+
"1d" => 86400,
10+
"1h" => 3600
11+
];
2512

26-
if (file_exists($cache_file) && is_cache_valid($cache_file, $cache_time)) {
27-
return get_cached_result($cache_file, $cache_time, $meta);
28-
}
13+
// Main API function that handles caching and retrieval of data
14+
function api($url, $data, $headers, $ct, $cfd, $meta, $fn) {
15+
$cf = $cfd ?? false;
16+
if (!$cf) {
17+
return get_non_cached_result($url, $data, $headers);
18+
}
2919

30-
return get_and_cache_result($url, $data, $headers, $cache_folder, $cache_file, $cache_time, $meta);
31-
}
20+
$cache_refresh = CACHE_DURATIONS[$cf] ?? 3600;
21+
$cache_time = $ct ?? $cache_refresh;
22+
$cache_folder = "cache/" . $cf . "/";
23+
$filename = $fn ?? md5($data . $url);
24+
$cache_file = $cache_folder . $filename;
3225

33-
// Retrieves non-cached result from the API
34-
function get_non_cached_result($url, $data, $headers) {
35-
$curlResult = curl_get($url, $data, $headers);
36-
return [
37-
"br_cache" => "no caching",
38-
"br_result" => json_decode($curlResult, true),
39-
];
40-
}
41-
42-
// Checks if the cached data is still valid
43-
function is_cache_valid($cache_file, $cache_time) {
44-
$filetime = filemtime($cache_file);
45-
return (time() - $filetime) < $cache_time;
46-
}
47-
48-
// Retrieves and formats cached result
49-
function get_cached_result($cache_file, $cache_time, $meta) {
50-
$filetime = filemtime($cache_file);
51-
$time_in_cache = time() - $filetime;
52-
$cache_object = [
53-
"filename" => basename($cache_file),
54-
"title" => $time_in_cache . " of " . $cache_time . " seconds in cache",
55-
"created" => gmdate("Y-m-d h:i:sa", $filetime) . " (UTC)",
56-
"created_utc" => $filetime,
57-
"cache_time" => $cache_time,
58-
"time_in_cache" => $time_in_cache,
59-
"utc_timestamp" => time(),
60-
"version" => VERSION
61-
];
62-
$cache_contents = json_decode(file_get_contents($cache_file), true);
63-
return $meta === false ? $cache_contents : [
64-
"br_cache" => $cache_object,
65-
"br_result" => $cache_contents
66-
];
67-
}
68-
69-
// Fetches new data from API and caches it
70-
function get_and_cache_result($url, $data, $headers, $cache_folder, $cache_file, $cache_time, $meta) {
71-
$api_result = $url ? curl_get($url, $data, $headers) : $data;
72-
if (!$api_result) {
73-
if (file_exists($cache_file)) {
74-
unlink($cache_file);
26+
if (file_exists($cache_file) && is_cache_valid($cache_file, $cache_time)) {
27+
return get_cached_result($cache_file, $cache_time, $meta);
7528
}
76-
return [
77-
"br_cache" => "no caching",
78-
"br_result" => ["error" => ["message" => "No API result"]]
79-
];
29+
30+
return get_and_cache_result($url, $data, $headers, $cache_folder, $cache_file, $cache_time, $meta);
8031
}
8132

82-
if (!is_dir($cache_folder) && !mkdir($cache_folder, 0777, true)) {
33+
// Retrieves non-cached result from the API
34+
function get_non_cached_result($url, $data, $headers) {
35+
$curlResult = curl_get($url, $data, $headers);
8336
return [
8437
"br_cache" => "no caching",
85-
"br_result" => ["error" => ["message" => "No write access"]]
38+
"br_result" => json_decode($curlResult),
8639
];
8740
}
8841

89-
file_put_contents($cache_file, $api_result);
90-
update_cache_monitor($cache_folder);
91-
92-
$cache_object = [
93-
"filename" => basename($cache_file),
94-
"title" => "0 of " . $cache_time . " seconds in cache",
95-
"created" => gmdate("Y-m-d h:i:sa", time()) . " (UTC)",
96-
"created_utc" => time(),
97-
"cache_time" => $cache_time,
98-
"time_in_cache" => 0,
99-
"utc_timestamp" => time(),
100-
"version" => VERSION
101-
];
102-
$api_contents = json_decode($api_result, true);
103-
return $meta === false ? $api_contents : [
104-
"br_cache" => $cache_object,
105-
"br_result" => $api_contents
106-
];
107-
}
108-
109-
// Updates the cache monitor file
110-
function update_cache_monitor($cache_folder) {
111-
$cacheMonitor = $cache_folder . "cachemonitor";
112-
$time = time();
113-
$cacheContent = json_encode([
114-
"created" => gmdate("Y-m-d h:i:sa", $time) . " (UTC)",
115-
"created_utc" => $time
116-
]);
117-
118-
if (!file_exists($cacheMonitor) || ($time - filemtime($cacheMonitor) > 3600)) {
119-
cleanup_old_cache($cache_folder);
120-
file_put_contents($cacheMonitor, $cacheContent);
42+
// Checks if the cached data is still valid
43+
function is_cache_valid($cache_file, $cache_time) {
44+
$filetime = filemtime($cache_file);
45+
return (time() - $filetime) < $cache_time;
12146
}
122-
}
123-
124-
// Removes old cache files
125-
function cleanup_old_cache(string $cache_folder) {
126-
$files = glob($cache_folder . "*");
127-
$cache_refresh = max(CACHE_DURATIONS);
128-
$time = time();
129-
foreach ($files as $file) {
130-
if ($time - filemtime($file) > $cache_refresh) {
131-
unlink($file);
132-
}
47+
48+
// Retrieves and formats cached result
49+
function get_cached_result($cache_file, $cache_time, $meta) {
50+
$filetime = filemtime($cache_file);
51+
$time_in_cache = time() - $filetime;
52+
$cache_object = [
53+
"filename" => basename($cache_file),
54+
"title" => $time_in_cache . " of " . $cache_time . " seconds in cache",
55+
"created" => gmdate("Y-m-d h:i:sa", $filetime) . " (UTC)",
56+
"created_utc" => $filetime,
57+
"cache_time" => $cache_time,
58+
"time_in_cache" => $time_in_cache,
59+
"utc_timestamp" => time(),
60+
"version" => VERSION
61+
];
62+
$cache_contents = json_decode(file_get_contents($cache_file));
63+
return $meta === false ? $cache_contents : [
64+
"br_cache" => $cache_object,
65+
"br_result" => $cache_contents
66+
];
13367
}
134-
}
13568

136-
// Performs a cURL request to fetch data from a URL
137-
function curl_get($url, $data, $headers) {
138-
if (strpos($url, ".onion") !== false) {
139-
require_once "ln/tor/index.php";
140-
return fetch_tor($url, $data, $headers);
69+
// Fetches new data from API and caches it
70+
function get_and_cache_result($url, $data, $headers, $cache_folder, $cache_file, $cache_time, $meta) {
71+
$api_result = $url ? curl_get($url, $data, $headers) : $data;
72+
if (!$api_result) {
73+
if (file_exists($cache_file)) {
74+
unlink($cache_file);
75+
}
76+
return [
77+
"br_cache" => "no caching",
78+
"br_result" => ["error" => ["message" => "No API result"]]
79+
];
80+
}
81+
82+
if (!is_dir($cache_folder) && !mkdir($cache_folder, 0777, true)) {
83+
return [
84+
"br_cache" => "no caching",
85+
"br_result" => ["error" => ["message" => "No write access"]]
86+
];
87+
}
88+
89+
file_put_contents($cache_file, $api_result);
90+
update_cache_monitor($cache_folder);
91+
92+
$cache_object = [
93+
"filename" => basename($cache_file),
94+
"title" => "0 of " . $cache_time . " seconds in cache",
95+
"created" => gmdate("Y-m-d h:i:sa", time()) . " (UTC)",
96+
"created_utc" => time(),
97+
"cache_time" => $cache_time,
98+
"time_in_cache" => 0,
99+
"utc_timestamp" => time(),
100+
"version" => VERSION
101+
];
102+
$api_contents = json_decode($api_result);
103+
return $meta === false ? $api_contents : [
104+
"br_cache" => $cache_object,
105+
"br_result" => $api_contents
106+
];
141107
}
142108

143-
$ch = curl_init();
144-
curl_setopt_array($ch, [
145-
CURLOPT_URL => $url,
146-
CURLOPT_RETURNTRANSFER => true,
147-
CURLOPT_CONNECTTIMEOUT => 0,
148-
CURLOPT_TIMEOUT => 5
149-
]);
150-
151-
if (!empty($headers)) {
152-
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
153-
if (isset($headers["tls_wildcard"])) {
154-
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
109+
// Updates the cache monitor file
110+
function update_cache_monitor($cache_folder) {
111+
$cacheMonitor = $cache_folder . "cachemonitor";
112+
$time = time();
113+
$cacheContent = json_encode([
114+
"created" => gmdate("Y-m-d h:i:sa", $time) . " (UTC)",
115+
"created_utc" => $time
116+
]);
117+
118+
if (!file_exists($cacheMonitor) || ($time - filemtime($cacheMonitor) > 3600)) {
119+
cleanup_old_cache($cache_folder);
120+
file_put_contents($cacheMonitor, $cacheContent);
155121
}
156122
}
157123

158-
if (!empty($data)) {
159-
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
124+
// Removes old cache files
125+
function cleanup_old_cache(string $cache_folder) {
126+
$files = glob($cache_folder . "*");
127+
$cache_refresh = max(array_values(CACHE_DURATIONS)); // Explicitly convert to array for max()
128+
$time = time();
129+
foreach ($files as $file) {
130+
if ($time - filemtime($file) > $cache_refresh) {
131+
unlink($file);
132+
}
133+
}
160134
}
161135

162-
$result = curl_exec($ch);
163-
if (curl_errno($ch)) {
164-
return error_object("411", curl_error($ch));
136+
// Performs a cURL request to fetch data from a URL
137+
function curl_get($url, $data, $headers) {
138+
if (strpos($url, ".onion") !== false) {
139+
require_once "ln/tor/index.php";
140+
return fetch_tor($url, $data, $headers);
141+
}
142+
143+
$ch = curl_init();
144+
curl_setopt_array($ch, [
145+
CURLOPT_URL => $url,
146+
CURLOPT_RETURNTRANSFER => true,
147+
CURLOPT_CONNECTTIMEOUT => 0,
148+
CURLOPT_TIMEOUT => 5
149+
]);
150+
151+
if (!empty($headers)) {
152+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
153+
if (isset($headers["tls_wildcard"])) {
154+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
155+
}
156+
}
157+
158+
if (!empty($data)) {
159+
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
160+
}
161+
162+
$result = curl_exec($ch);
163+
if (curl_errno($ch)) {
164+
return error_object("411", curl_error($ch));
165+
}
166+
curl_close($ch);
167+
168+
return $result ?: error_object("411", "no result");
165169
}
166-
curl_close($ch);
167-
168-
return $result ?: error_object("411", "no result");
169-
}
170-
171-
// Creates a JSON-encoded error object
172-
function error_object($code, $message) {
173-
return json_encode([
174-
"error" => [
175-
"code" => $code,
176-
"message" => $message
177-
]
178-
]);
179-
}
170+
171+
// Creates a JSON-encoded error object
172+
function error_object($code, $message) {
173+
return json_encode([
174+
"error" => [
175+
"code" => $code,
176+
"message" => $message
177+
]
178+
]);
179+
}

0 commit comments

Comments
 (0)