Droping rebory and migrate to cmake-js #14
@ -6,7 +6,7 @@ export const projectRoot: string = !process["resourcesPath"] ? path.resolve(__di
|
||||
|
||||
declare global {
|
||||
namespace NodeJS {
|
||||
export interface DlopenModule {
|
||||
export interface Moduledlopen {
|
||||
exports: any;
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ declare global {
|
||||
* @default flags `os.constants.dlopen.RTLD_LAZY`
|
||||
* @since v9.0.0
|
||||
*/
|
||||
dlopen(module: DlopenModule, filename: string, flags: number): void;
|
||||
dlopen(module: Moduledlopen, filename: string, flags: number): void;
|
||||
/**
|
||||
* The `process.dlopen()` method allows dynamically loading shared objects. It is primarily used by `require()` to load C++ Addons, and should not be used directly, except in special cases. In other words, `require()` should be preferred over `process.dlopen()` unless there are specific reasons such as custom dlopen flags or loading from ES modules.
|
||||
*
|
||||
@ -30,7 +30,7 @@ declare global {
|
||||
* @param filename - Addon path
|
||||
* @since v0.1.16
|
||||
*/
|
||||
dlopen(module: DlopenModule, filename: string): void;
|
||||
dlopen(module: Moduledlopen, filename: string): void;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -49,7 +49,7 @@ export async function LoadAddon<T = any>(addonFile: string, exports?: Record<st
|
||||
else if (await exists(path.resolve(projectRoot, "build/Debug", addonFile))) _addonFile = path.resolve(projectRoot, "build/Debug", addonFile)
|
||||
else if (await exists(path.resolve(projectRoot, "build/Debug", addonFile+".node"))) _addonFile = path.resolve(projectRoot, "build/Debug", addonFile+".node")
|
||||
if (!_addonFile) throw new Error("Cannot load required addon")
|
||||
let ext: NodeJS.DlopenModule = {exports: Object.assign({}, exports)}
|
||||
let ext: NodeJS.Moduledlopen = {exports: Object.assign({}, exports)}
|
||||
process.dlopen(ext, _addonFile)
|
||||
return ext.exports
|
||||
}
|
@ -106,10 +106,6 @@ function keyToBase64(key: Uint8Array): string {
|
||||
return Buffer.from(key).toString("base64");
|
||||
}
|
||||
|
||||
export function keyToHex(key: string): string {
|
||||
return Buffer.from(key, "base64").toString("hex");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate preshared key
|
||||
*/
|
||||
|
79
src/maneger.ts
Normal file
79
src/maneger.ts
Normal file
@ -0,0 +1,79 @@
|
||||
import * as wginterface from "./wginterface.js"
|
||||
import { publicKey, privateKey } from "./key.js"
|
||||
import * as quick from "./quick.js"
|
||||
import { isIP } from "net"
|
||||
|
||||
export class Wireguard extends Map<string, wginterface.Peer> {
|
||||
private _privateKey: string
|
||||
|
||||
/** Current privateKey */
|
||||
get privateKey(): string {
|
||||
return this._privateKey
|
||||
}
|
||||
|
||||
/** Get publicKey from privateKey */
|
||||
get publicKey(): string {
|
||||
if (!this._privateKey) throw new Error("Set private key to get publicKey")
|
||||
return publicKey(this._privateKey)
|
||||
}
|
||||
|
||||
/** @deprecated set privateKey only */
|
||||
set publicKey(_key: string) {
|
||||
throw new Error("Set privateKey only")
|
||||
}
|
||||
|
||||
/** Set interface privateKey */
|
||||
set privateKey(key: string) {
|
||||
this._privateKey = key
|
||||
}
|
||||
|
||||
/** Generate privateKey to interface */
|
||||
async generatePrivateKey() {
|
||||
this._privateKey = await privateKey()
|
||||
}
|
||||
|
||||
private _portListen: number
|
||||
get portListen() { return this._portListen; }
|
||||
set portListen(port: number) {
|
||||
if (port < 0 || port > 65534) throw new Error("Invalid port to listening");
|
||||
this._portListen = port
|
||||
}
|
||||
|
||||
/** Get config */
|
||||
toJSON(wgName: string): wginterface.Config {
|
||||
const config: wginterface.Config = {
|
||||
name: wgName,
|
||||
portListen: this._portListen,
|
||||
privateKey: this.privateKey,
|
||||
peers: {},
|
||||
}
|
||||
for (const [publicKey, peerSettings] of this.entries()) {
|
||||
config.peers[publicKey] = {
|
||||
keepInterval: peerSettings.keepInterval,
|
||||
presharedKey: peerSettings.presharedKey,
|
||||
endpoint: peerSettings.endpoint,
|
||||
allowedIPs: peerSettings.allowedIPs
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
/** Get quick config from current config */
|
||||
toString(extraConfig?: Omit<quick.QuickConfig, keyof wginterface.SetConfig>) {
|
||||
return quick.stringify(Object.assign({}, extraConfig, this.toJSON("wg0")))
|
||||
}
|
||||
|
||||
/** Deploy config to wireguard interface */
|
||||
async deployConfig(wgName: string) {
|
||||
return wginterface.setConfig(this.toJSON(wgName))
|
||||
}
|
||||
|
||||
/** Add peer to Map and check config */
|
||||
async addPeer(publicKey: string, peerConfig: wginterface.Peer) {
|
||||
if (peerConfig.allowedIPs?.length > 0) {
|
||||
for (const ipAddress of peerConfig.allowedIPs) if (isIP(ipAddress.split("/")[0]) === 0) throw new Error("Invalid ip address in allowedIPs")
|
||||
}
|
||||
peerConfig.keepInterval
|
||||
this.set(publicKey, peerConfig)
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@ import { isIP } from "net";
|
||||
import { format } from "util";
|
||||
import { SetConfig } from "./wginterface.js";
|
||||
|
||||
export interface QuickConfig extends SetConfig, Partial<Record<`${"Post" | "Pre"}${"Up" | "Down"}`, string[]>> {
|
||||
export interface QuickConfig extends Omit<SetConfig, "name">, Partial<Record<`${"Post" | "Pre"}${"Up" | "Down"}`, string[]>> {
|
||||
DNS?: string[];
|
||||
Table?: number;
|
||||
MTU?: number;
|
||||
|
@ -24,7 +24,6 @@ PublicKey = 15PMkuIeQNM9AlknHb+c10y8e3fzOihZJpuD23y+d0c=
|
||||
AllowedIPs = 10.88.198.220/32, 192.168.15.112/32, 2002:0A58:C6DC::/128, 2002:C0A8:0F70::/128`;
|
||||
|
||||
const StaticConfigJson: QuickConfig = {
|
||||
name: "wg0",
|
||||
privateKey: "2Ll/2LCXDlLVZcBCBZ6QeXB4qEF+bTzmuOBxnpu57WY=",
|
||||
portListen: 38451,
|
||||
PostUp: [
|
||||
|
@ -1,7 +1,7 @@
|
||||
import path from "node:path";
|
||||
import { LoadAddon, projectRoot } from "./addons.js";
|
||||
|
||||
interface Peer {
|
||||
export interface Peer {
|
||||
/** Preshared key to peer */
|
||||
presharedKey?: string;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user