mirror of
https://github.com/EasyTier/EasyTier.git
synced 2025-09-27 04:56:07 +08:00

Some checks failed
EasyTier Core / pre_job (push) Has been cancelled
EasyTier GUI / pre_job (push) Has been cancelled
EasyTier Mobile / pre_job (push) Has been cancelled
EasyTier Test / pre_job (push) Has been cancelled
EasyTier Core / build (freebsd-13.2-x86_64, 13.2, ubuntu-22.04, x86_64-unknown-freebsd) (push) Has been cancelled
EasyTier Core / build (linux-aarch64, ubuntu-22.04, aarch64-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-arm, ubuntu-22.04, arm-unknown-linux-musleabi) (push) Has been cancelled
EasyTier Core / build (linux-armhf, ubuntu-22.04, arm-unknown-linux-musleabihf) (push) Has been cancelled
EasyTier Core / build (linux-armv7, ubuntu-22.04, armv7-unknown-linux-musleabi) (push) Has been cancelled
EasyTier Core / build (linux-armv7hf, ubuntu-22.04, armv7-unknown-linux-musleabihf) (push) Has been cancelled
EasyTier Core / build (linux-mips, ubuntu-22.04, mips-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-mipsel, ubuntu-22.04, mipsel-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (linux-x86_64, ubuntu-22.04, x86_64-unknown-linux-musl) (push) Has been cancelled
EasyTier Core / build (macos-aarch64, macos-latest, aarch64-apple-darwin) (push) Has been cancelled
EasyTier Core / build (macos-x86_64, macos-latest, x86_64-apple-darwin) (push) Has been cancelled
EasyTier Core / build (windows-x86_64, windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
EasyTier Core / core-result (push) Has been cancelled
EasyTier GUI / build-gui (linux-aarch64, aarch64-unknown-linux-gnu, ubuntu-22.04, aarch64-unknown-linux-musl) (push) Has been cancelled
EasyTier GUI / build-gui (linux-x86_64, x86_64-unknown-linux-gnu, ubuntu-22.04, x86_64-unknown-linux-musl) (push) Has been cancelled
EasyTier GUI / build-gui (macos-aarch64, aarch64-apple-darwin, macos-latest, aarch64-apple-darwin) (push) Has been cancelled
EasyTier GUI / build-gui (macos-x86_64, x86_64-apple-darwin, macos-latest, x86_64-apple-darwin) (push) Has been cancelled
EasyTier GUI / build-gui (windows-x86_64, x86_64-pc-windows-msvc, windows-latest, x86_64-pc-windows-msvc) (push) Has been cancelled
EasyTier GUI / gui-result (push) Has been cancelled
EasyTier Mobile / build-mobile (android, ubuntu-22.04, android) (push) Has been cancelled
EasyTier Mobile / mobile-result (push) Has been cancelled
EasyTier Test / test (push) Has been cancelled
109 lines
3.2 KiB
TypeScript
109 lines
3.2 KiB
TypeScript
import { IPv4, IPv6 } from 'ip-num/IPNumber'
|
|
import { Ipv4Addr, Ipv4Inet, Ipv6Addr } from '../types/network'
|
|
|
|
export function ipv4ToString(ip: Ipv4Addr) {
|
|
return IPv4.fromNumber(ip.addr).toString()
|
|
}
|
|
|
|
export function ipv4InetToString(ip: Ipv4Inet | undefined) {
|
|
if (ip?.address === undefined) {
|
|
return 'undefined'
|
|
}
|
|
return `${ipv4ToString(ip.address)}/${ip.network_length}`
|
|
}
|
|
|
|
export function ipv6ToString(ip: Ipv6Addr) {
|
|
return IPv6.fromBigInt(
|
|
(BigInt(ip.part1) << BigInt(96))
|
|
+ (BigInt(ip.part2) << BigInt(64))
|
|
+ (BigInt(ip.part3) << BigInt(32))
|
|
+ BigInt(ip.part4),
|
|
)
|
|
}
|
|
|
|
function toHexString(uint64: bigint, padding = 9): string {
|
|
let hexString = uint64.toString(16);
|
|
while (hexString.length < padding) {
|
|
hexString = '0' + hexString;
|
|
}
|
|
return hexString;
|
|
}
|
|
|
|
function uint32ToUuid(part1: number, part2: number, part3: number, part4: number): string {
|
|
// 将两个 uint64 转换为 16 进制字符串
|
|
const part1Hex = toHexString(BigInt(part1), 8);
|
|
const part2Hex = toHexString(BigInt(part2), 8);
|
|
const part3Hex = toHexString(BigInt(part3), 8);
|
|
const part4Hex = toHexString(BigInt(part4), 8);
|
|
|
|
// 构造 UUID 格式字符串
|
|
const uuid = `${part1Hex.substring(0, 8)}-${part2Hex.substring(0, 4)}-${part2Hex.substring(4, 8)}-${part3Hex.substring(0, 4)}-${part3Hex.substring(4, 8)}${part4Hex.substring(0, 12)}`;
|
|
|
|
return uuid;
|
|
}
|
|
|
|
export interface UUID {
|
|
part1: number;
|
|
part2: number;
|
|
part3: number;
|
|
part4: number;
|
|
}
|
|
|
|
export function UuidToStr(uuid: UUID): string {
|
|
return uint32ToUuid(uuid.part1, uuid.part2, uuid.part3, uuid.part4);
|
|
}
|
|
|
|
export interface DeviceInfo {
|
|
hostname: string;
|
|
public_ip: string;
|
|
running_network_count: number;
|
|
report_time: string;
|
|
easytier_version: string;
|
|
running_network_instances?: Array<string>;
|
|
machine_id: string;
|
|
}
|
|
|
|
export function buildDeviceInfo(device: any): DeviceInfo {
|
|
let dev_info: DeviceInfo = {
|
|
hostname: device.info?.hostname,
|
|
public_ip: device.client_url,
|
|
running_network_instances: device.info?.running_network_instances.map((instance: any) => UuidToStr(instance)),
|
|
running_network_count: device.info?.running_network_instances.length,
|
|
report_time: device.info?.report_time,
|
|
easytier_version: device.info?.easytier_version,
|
|
machine_id: UuidToStr(device.info?.machine_id),
|
|
};
|
|
|
|
return dev_info;
|
|
}
|
|
|
|
// write a class to run a function periodically and can be stopped by calling stop(), use setTimeout to trigger the function
|
|
export class PeriodicTask {
|
|
private interval: number;
|
|
private task: (() => Promise<void>) | undefined;
|
|
private timer: any;
|
|
|
|
constructor(task: () => Promise<void>, interval: number) {
|
|
this.interval = interval;
|
|
this.task = task;
|
|
}
|
|
|
|
_runTaskHelper(nextInterval: number) {
|
|
this.timer = setTimeout(async () => {
|
|
if (this.task) {
|
|
await this.task();
|
|
this._runTaskHelper(this.interval);
|
|
}
|
|
}, nextInterval);
|
|
}
|
|
|
|
start() {
|
|
this._runTaskHelper(0);
|
|
}
|
|
|
|
stop() {
|
|
this.task = undefined;
|
|
clearTimeout(this.timer);
|
|
}
|
|
}
|