This commit is contained in:
samlm0
2022-07-09 11:45:52 +08:00
parent 544c4548fb
commit cf791e7b69
4 changed files with 37 additions and 29 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
backend/app/webspaces/speedtest-static/*

View File

@@ -1,30 +1,32 @@
<?php
Swoole\Timer::tick(1000, function () {
preg_match_all(
'/^\s*(?\'name\'[A-Za-z0-9]+):\s+(?\'recv\'\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(?\'send\'\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/m',
file_get_contents('/proc/net/dev'),
$matches,
PREG_SET_ORDER,
0
);
$interfaces = [];
foreach ($matches as $match) {
if (in_array($match['name'], ['lo', 'ip6tnl0', 'tunl0', 'docker0'])) continue;
if (strpos($match['name'], 'veth') !== false) continue;
$interfaces[$match['name']] = [
'recv' => $match['recv'],
'send' => $match['send'],
];
}
foreach (Client\Websocket::$clients as $client) {
foreach ($interfaces as $interface => $data) {
$client->send(implode("|", [
100,
$interface,
$data['recv'],
$data['send']
]));
if (env('DISPLAY_TRAFFIC', true)) {
Swoole\Timer::tick(1000, function () {
preg_match_all(
'/^\s*(?\'name\'[A-Za-z0-9]+):\s+(?\'recv\'\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(?\'send\'\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/m',
file_get_contents('/proc/net/dev'),
$matches,
PREG_SET_ORDER,
0
);
$interfaces = [];
foreach ($matches as $match) {
if (in_array($match['name'], ['lo', 'ip6tnl0', 'tunl0', 'docker0'])) continue;
if (strpos($match['name'], 'veth') !== false) continue;
$interfaces[$match['name']] = [
'recv' => $match['recv'],
'send' => $match['send'],
];
}
}
});
foreach (Client\Websocket::$clients as $client) {
foreach ($interfaces as $interface => $data) {
$client->send(implode("|", [
100,
$interface,
$data['recv'],
$data['send']
]));
}
}
});
}

View File

@@ -3,7 +3,11 @@ if (!function_exists('env')) {
function env($key, $default = false)
{
$result = getenv($key);
if ($result !== false) return $result;
if ($result !== false) {
if ($result === "true") return true;
if ($result === "false") return false;
return $result;
}
return $default;
}
}

View File

@@ -31,6 +31,7 @@ export default defineComponent({
},
methods: {
initWebsocket() {
if (this.isLoaded) return;
this.ws = new WebSocket(location.protocol.replace('http', 'ws') + '//' + location.host + '/ws')
this.ws.onopen = () => {
this.isLoaded = true