UNPKG

the-wireguard-effect

Version:

Cross platform wireguard api client for nodejs built on wireguard-go with effect-ts

409 lines 25.2 kB
import type * as Cause from "effect/Cause"; import type * as PlatformError from "effect/PlatformError"; import type * as Scope from "effect/Scope"; import type * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; import type * as Socket from "effect/unstable/socket/Socket"; import * as Effect from "effect/Effect"; import * as FileSystem from "effect/FileSystem"; import * as Path from "effect/Path"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import * as InternetSchemas from "effect-schemas/Internet"; import * as WireguardControl from "../WireguardControl.ts"; import * as WireguardErrors from "../WireguardErrors.ts"; import * as WireguardPeer from "../WireguardPeer.ts"; declare const WireguardConfig_base: import("effect/unstable/schema/VariantSchema").Class<WireguardConfig, { /** The Address of this peer. */ readonly Address: Schema.decodeTo<Schema.Union<readonly [typeof InternetSchemas.IPv4CidrBlock, typeof InternetSchemas.IPv6CidrBlock]>, Schema.TemplateLiteral<readonly [Schema.String, "/", Schema.Number]>, never, never>; /** DNS for this peer. */ readonly Dns: Schema.optional<Schema.Union<readonly [Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv4">; readonly ip: Schema.brand<Schema.String, "IPv4">; }>, Schema.String, never, never>, Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv6">; readonly ip: Schema.brand<Schema.String, "IPv6">; }>, Schema.String, never, never>]>>; /** * The value for this is a decimal-string integer corresponding to the * listening port of the interface. */ readonly ListenPort: Schema.Union<readonly [Schema.brand<Schema.Int, "Port">, Schema.compose<Schema.brand<Schema.Int, "Port">, Schema.NumberFromString>]>; /** * The value for this is a decimal-string integer corresponding to the * fwmark of the interface. The value may 0 in the case of a set operation, * in which case it indicates that the fwmark should be removed. */ readonly FirewallMark: Schema.optional<Schema.NullOr<Schema.Number>>; /** * The value for this key should be a lowercase hex-encoded private key of * the interface. The value may be an all zero string in the case of a set * operation, in which case it indicates that the private key should be * removed. */ readonly PrivateKey: Schema.brand<Schema.String, "WireguardKey">; /** List of peers to add. */ readonly Peers: import("effect/unstable/schema/VariantSchema").Field<{ readonly json: Schema.withDecodingDefault<Schema.$Array<typeof WireguardPeer.WireguardPeer>, never>; readonly uapi: Schema.withDecodingDefault<Schema.$Array<Schema.Struct<{ readonly PersistentKeepalive: Schema.OptionFromOptionalNullOr<Schema.decodeTo<Schema.Duration, Schema.Int, never, never>>; readonly AllowedIPs: Schema.withDecodingDefault<Schema.$ReadonlySet<Schema.decodeTo<Schema.Union<readonly [typeof InternetSchemas.IPv4CidrBlock, typeof InternetSchemas.IPv6CidrBlock]>, Schema.TemplateLiteral<readonly [Schema.String, "/", Schema.Number]>, never, never>>, never>; readonly Endpoint: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.decodeTo<Schema.Struct<{ readonly address: Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv4">; readonly ip: Schema.brand<Schema.String, "IPv4">; }>, Schema.String, never, never>; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly ip: Schema.String; readonly port: Schema.Number; readonly family: Schema.Literal<"ipv4">; }>, Schema.Struct<{ readonly ip: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; readonly family: Schema.Literal<"ipv4">; }>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>, Schema.decodeTo<Schema.Struct<{ readonly address: Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv6">; readonly ip: Schema.brand<Schema.String, "IPv6">; }>, Schema.String, never, never>; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly ip: Schema.String; readonly port: Schema.Number; readonly family: Schema.Literal<"ipv6">; }>, Schema.Struct<{ readonly ip: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; readonly family: Schema.Literal<"ipv6">; }>, Schema.TemplateLiteral<readonly [Schema.Literal<"[">, Schema.String, Schema.Literal<"]">, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.Literal<"[">, Schema.String, Schema.Literal<"]">, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>, Schema.decodeTo<Schema.Struct<{ readonly host: Schema.String; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly host: Schema.String; readonly port: Schema.Number; }>, Schema.Struct<{ readonly host: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; }>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>]>>>; readonly PublicKey: Schema.brand<Schema.String, "WireguardKey">; readonly PresharedKey: Schema.OptionFromOptionalNullOr<Schema.brand<Schema.String, "WireguardKey">>; readonly rxBytes: Schema.NumberFromString; readonly txBytes: Schema.NumberFromString; readonly lastHandshake: Schema.compose<Schema.DateTimeUtcFromMillis, Schema.decodeTo<Schema.toType<Schema.NumberFromString>, Schema.NumberFromString, never, never>>; }>>, never>; }>; }, Schema.Struct<import("effect/unstable/schema/VariantSchema").ExtractFields<"json", { /** The Address of this peer. */ readonly Address: Schema.decodeTo<Schema.Union<readonly [typeof InternetSchemas.IPv4CidrBlock, typeof InternetSchemas.IPv6CidrBlock]>, Schema.TemplateLiteral<readonly [Schema.String, "/", Schema.Number]>, never, never>; /** DNS for this peer. */ readonly Dns: Schema.optional<Schema.Union<readonly [Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv4">; readonly ip: Schema.brand<Schema.String, "IPv4">; }>, Schema.String, never, never>, Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv6">; readonly ip: Schema.brand<Schema.String, "IPv6">; }>, Schema.String, never, never>]>>; /** * The value for this is a decimal-string integer corresponding to the * listening port of the interface. */ readonly ListenPort: Schema.Union<readonly [Schema.brand<Schema.Int, "Port">, Schema.compose<Schema.brand<Schema.Int, "Port">, Schema.NumberFromString>]>; /** * The value for this is a decimal-string integer corresponding to the * fwmark of the interface. The value may 0 in the case of a set operation, * in which case it indicates that the fwmark should be removed. */ readonly FirewallMark: Schema.optional<Schema.NullOr<Schema.Number>>; /** * The value for this key should be a lowercase hex-encoded private key of * the interface. The value may be an all zero string in the case of a set * operation, in which case it indicates that the private key should be * removed. */ readonly PrivateKey: Schema.brand<Schema.String, "WireguardKey">; /** List of peers to add. */ readonly Peers: import("effect/unstable/schema/VariantSchema").Field<{ readonly json: Schema.withDecodingDefault<Schema.$Array<typeof WireguardPeer.WireguardPeer>, never>; readonly uapi: Schema.withDecodingDefault<Schema.$Array<Schema.Struct<{ readonly PersistentKeepalive: Schema.OptionFromOptionalNullOr<Schema.decodeTo<Schema.Duration, Schema.Int, never, never>>; readonly AllowedIPs: Schema.withDecodingDefault<Schema.$ReadonlySet<Schema.decodeTo<Schema.Union<readonly [typeof InternetSchemas.IPv4CidrBlock, typeof InternetSchemas.IPv6CidrBlock]>, Schema.TemplateLiteral<readonly [Schema.String, "/", Schema.Number]>, never, never>>, never>; readonly Endpoint: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.decodeTo<Schema.Struct<{ readonly address: Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv4">; readonly ip: Schema.brand<Schema.String, "IPv4">; }>, Schema.String, never, never>; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly ip: Schema.String; readonly port: Schema.Number; readonly family: Schema.Literal<"ipv4">; }>, Schema.Struct<{ readonly ip: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; readonly family: Schema.Literal<"ipv4">; }>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>, Schema.decodeTo<Schema.Struct<{ readonly address: Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv6">; readonly ip: Schema.brand<Schema.String, "IPv6">; }>, Schema.String, never, never>; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly ip: Schema.String; readonly port: Schema.Number; readonly family: Schema.Literal<"ipv6">; }>, Schema.Struct<{ readonly ip: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; readonly family: Schema.Literal<"ipv6">; }>, Schema.TemplateLiteral<readonly [Schema.Literal<"[">, Schema.String, Schema.Literal<"]">, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.Literal<"[">, Schema.String, Schema.Literal<"]">, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>, Schema.decodeTo<Schema.Struct<{ readonly host: Schema.String; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly host: Schema.String; readonly port: Schema.Number; }>, Schema.Struct<{ readonly host: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; }>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>]>>>; readonly PublicKey: Schema.brand<Schema.String, "WireguardKey">; readonly PresharedKey: Schema.OptionFromOptionalNullOr<Schema.brand<Schema.String, "WireguardKey">>; readonly rxBytes: Schema.NumberFromString; readonly txBytes: Schema.NumberFromString; readonly lastHandshake: Schema.compose<Schema.DateTimeUtcFromMillis, Schema.decodeTo<Schema.toType<Schema.NumberFromString>, Schema.NumberFromString, never, never>>; }>>, never>; }>; }, true>>> & { readonly json: Schema.Struct<{ readonly Address: Schema.decodeTo<Schema.Union<readonly [typeof InternetSchemas.IPv4CidrBlock, typeof InternetSchemas.IPv6CidrBlock]>, Schema.TemplateLiteral<readonly [Schema.String, "/", Schema.Number]>, never, never>; readonly Dns: Schema.optional<Schema.Union<readonly [Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv4">; readonly ip: Schema.brand<Schema.String, "IPv4">; }>, Schema.String, never, never>, Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv6">; readonly ip: Schema.brand<Schema.String, "IPv6">; }>, Schema.String, never, never>]>>; readonly ListenPort: Schema.Union<readonly [Schema.brand<Schema.Int, "Port">, Schema.compose<Schema.brand<Schema.Int, "Port">, Schema.NumberFromString>]>; readonly FirewallMark: Schema.optional<Schema.NullOr<Schema.Number>>; readonly PrivateKey: Schema.brand<Schema.String, "WireguardKey">; readonly Peers: Schema.withDecodingDefault<Schema.$Array<typeof WireguardPeer.WireguardPeer>, never>; }>; readonly uapi: Schema.Struct<{ readonly Address: Schema.decodeTo<Schema.Union<readonly [typeof InternetSchemas.IPv4CidrBlock, typeof InternetSchemas.IPv6CidrBlock]>, Schema.TemplateLiteral<readonly [Schema.String, "/", Schema.Number]>, never, never>; readonly Dns: Schema.optional<Schema.Union<readonly [Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv4">; readonly ip: Schema.brand<Schema.String, "IPv4">; }>, Schema.String, never, never>, Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv6">; readonly ip: Schema.brand<Schema.String, "IPv6">; }>, Schema.String, never, never>]>>; readonly ListenPort: Schema.Union<readonly [Schema.brand<Schema.Int, "Port">, Schema.compose<Schema.brand<Schema.Int, "Port">, Schema.NumberFromString>]>; readonly FirewallMark: Schema.optional<Schema.NullOr<Schema.Number>>; readonly PrivateKey: Schema.brand<Schema.String, "WireguardKey">; readonly Peers: Schema.withDecodingDefault<Schema.$Array<Schema.Struct<{ readonly PersistentKeepalive: Schema.OptionFromOptionalNullOr<Schema.decodeTo<Schema.Duration, Schema.Int, never, never>>; readonly AllowedIPs: Schema.withDecodingDefault<Schema.$ReadonlySet<Schema.decodeTo<Schema.Union<readonly [typeof InternetSchemas.IPv4CidrBlock, typeof InternetSchemas.IPv6CidrBlock]>, Schema.TemplateLiteral<readonly [Schema.String, "/", Schema.Number]>, never, never>>, never>; readonly Endpoint: Schema.optional<Schema.NullOr<Schema.Union<readonly [Schema.decodeTo<Schema.Struct<{ readonly address: Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv4">; readonly ip: Schema.brand<Schema.String, "IPv4">; }>, Schema.String, never, never>; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly ip: Schema.String; readonly port: Schema.Number; readonly family: Schema.Literal<"ipv4">; }>, Schema.Struct<{ readonly ip: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; readonly family: Schema.Literal<"ipv4">; }>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>, Schema.decodeTo<Schema.Struct<{ readonly address: Schema.decodeTo<Schema.Struct<{ readonly family: Schema.Literal<"ipv6">; readonly ip: Schema.brand<Schema.String, "IPv6">; }>, Schema.String, never, never>; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly ip: Schema.String; readonly port: Schema.Number; readonly family: Schema.Literal<"ipv6">; }>, Schema.Struct<{ readonly ip: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; readonly family: Schema.Literal<"ipv6">; }>, Schema.TemplateLiteral<readonly [Schema.Literal<"[">, Schema.String, Schema.Literal<"]">, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.Literal<"[">, Schema.String, Schema.Literal<"]">, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>, Schema.decodeTo<Schema.Struct<{ readonly host: Schema.String; readonly natPort: Schema.brand<Schema.Int, "Port">; readonly listenPort: Schema.brand<Schema.Int, "Port">; }>, Schema.Union<readonly [Schema.Struct<{ readonly host: Schema.String; readonly port: Schema.Number; }>, Schema.Struct<{ readonly host: Schema.String; readonly natPort: Schema.Number; readonly listenPort: Schema.Number; }>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number]>, Schema.TemplateLiteral<readonly [Schema.String, Schema.Literal<":">, Schema.Number, Schema.Literal<":">, Schema.Number]>]>, never, never>]>>>; readonly PublicKey: Schema.brand<Schema.String, "WireguardKey">; readonly PresharedKey: Schema.OptionFromOptionalNullOr<Schema.brand<Schema.String, "WireguardKey">>; readonly rxBytes: Schema.NumberFromString; readonly txBytes: Schema.NumberFromString; readonly lastHandshake: Schema.compose<Schema.DateTimeUtcFromMillis, Schema.decodeTo<Schema.toType<Schema.NumberFromString>, Schema.NumberFromString, never, never>>; }>>, never>; }>; }; export declare class WireguardConfig extends WireguardConfig_base { /** * Writes a wireguard interface configuration to an INI file. * * @since 1.0.0 * @category Constructors * @param file - The path to the INI file. */ writeToFile: { (file: string): Effect.Effect<void, Schema.SchemaError | PlatformError.PlatformError, FileSystem.FileSystem | Path.Path>; }; /** * Starts a wireguard tunnel that will continue to run and serve traffic * even after the nodejs process exits. * * @since 1.0.0 * @category Wireguard */ up: { (wireguardInterface?: WireguardInterface | undefined): Effect.Effect<WireguardInterface, Socket.SocketError | Schema.SchemaError | Cause.TimeoutError | PlatformError.PlatformError | PlatformError.SystemError | PlatformError.BadArgument | WireguardErrors.WireguardError, FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner | WireguardControl.WireguardControl>; }; /** * Starts a wireguard tunnel that will be gracefully shutdown and stop * serving traffic once the scope is closed. * * @since 1.0.0 * @category Wireguard */ upScoped: { (wireguardInterface?: WireguardInterface | undefined): Effect.Effect<WireguardInterface, Socket.SocketError | Schema.SchemaError | Cause.TimeoutError | PlatformError.PlatformError | PlatformError.SystemError | PlatformError.BadArgument | WireguardErrors.WireguardError, FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner | WireguardControl.WireguardControl | Scope.Scope>; }; } export declare const WireguardIniConfig: Schema.decodeTo<Schema.String, typeof WireguardConfig, never, never>; declare const WireguardInterface_base: Schema.Class<WireguardInterface, Schema.Struct<{ /** * Ensures the interface name matches the platform's interface name regex. * These functions need to be fully typed as we are accessing a static * method on this same class and otherwise typescript really complains about * inference. */ readonly Name: Schema.String; }>, {}>; /** * A wireguard interface name. * * @since 1.0.0 * @category Datatypes */ export declare class WireguardInterface extends WireguardInterface_base { /** * @since 1.0.0 * @category Constructors */ static readonly getNextAvailableInterface: Effect.Effect<WireguardInterface, WireguardErrors.WireguardError, never>; /** * @since 1.0.0 * @category Static members */ static readonly InterfaceRegExpForPlatform: Result.Result<RegExp, WireguardErrors.WireguardError>; /** * @since 1.0.0 * @category Userspace api */ readonly SocketLocation: string; /** * Starts a wireguard tunnel that will be gracefully shutdown and stop * serving traffic once the scope is closed. * * @since 1.0.0 * @category Wireguard control */ upScoped: { (config: WireguardConfig): Effect.Effect<WireguardInterface, Socket.SocketError | Schema.SchemaError | PlatformError.PlatformError | PlatformError.SystemError | PlatformError.BadArgument | Cause.TimeoutError, FileSystem.FileSystem | Path.Path | Scope.Scope | ChildProcessSpawner.ChildProcessSpawner | WireguardControl.WireguardControl>; }; /** * Starts a wireguard tunnel that will continue to run and serve traffic * even after the nodejs process exits. * * @since 1.0.0 * @category Wireguard control */ up: { (config: WireguardConfig): Effect.Effect<WireguardInterface, Socket.SocketError | Schema.SchemaError | PlatformError.PlatformError | PlatformError.SystemError | PlatformError.BadArgument | Cause.TimeoutError, FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner | WireguardControl.WireguardControl>; }; /** * Stops a previously started wireguard tunnel. * * @since 1.0.0 * @category Wireguard control */ down: { (config: WireguardConfig): Effect.Effect<WireguardInterface, PlatformError.PlatformError | PlatformError.SystemError | PlatformError.BadArgument | Schema.SchemaError | Cause.TimeoutError, FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner | WireguardControl.WireguardControl>; }; /** * Sets the config for this wireguard interface. * * @since 1.0.0 * @category Wireguard control */ setConfig: { (wireguardConfig: WireguardConfig): Effect.Effect<WireguardInterface, Socket.SocketError | Schema.SchemaError, never>; }; /** * Retrieves the config from this wireguard interface. * * @since 1.0.0 */ getConfig: { (address: (typeof InternetSchemas.CidrBlockFromString)["Encoded"]): Effect.Effect<Schema.Schema.Type<(typeof WireguardConfig)["uapi"]>, Socket.SocketError | Schema.SchemaError, never>; }; /** * Adds a peer to this interface. * * @since 1.0.0 * @category Wireguard control */ addPeer: { (peer: WireguardPeer.WireguardPeer): Effect.Effect<void, Socket.SocketError | Schema.SchemaError, never>; }; /** * Removes a peer from this interface. * * @since 1.0.0 * @category Wireguard control */ removePeer: { (peer: WireguardPeer.WireguardPeer): Effect.Effect<void, Socket.SocketError | Schema.SchemaError, never>; }; /** * Streams the stats from all the peers on this interface. * * @since 1.0.0 * @category Wireguard control */ streamPeerStats: { (): Stream.Stream<ReadonlyArray<Schema.Schema.Type<(typeof WireguardPeer.WireguardPeer)["uapi"]>>, Socket.SocketError | Schema.SchemaError, never>; }; } export {}; //# sourceMappingURL=circular.d.ts.map