From 2a49e303f97d5e5d55b2ab4d3739fdcd9d7498c7 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Thu, 28 Mar 2024 15:57:29 +0530 Subject: [PATCH] chore: include examples and runner --- .lune/{tests.luau => test.luau} | 0 .vscode/settings.json | 3 ++ aftman.toml | 1 + examples/basic.luau | 17 ++++++++ examples/generate.luau | 70 +++++++++++++++++++++++++++++++++ lunew | 36 +++++++++++++++++ package.json | 6 +-- 7 files changed, 130 insertions(+), 3 deletions(-) rename .lune/{tests.luau => test.luau} (100%) create mode 100644 examples/basic.luau create mode 100644 examples/generate.luau create mode 100755 lunew diff --git a/.lune/tests.luau b/.lune/test.luau similarity index 100% rename from .lune/tests.luau rename to .lune/test.luau diff --git a/.vscode/settings.json b/.vscode/settings.json index 2bed2cd..1d1ff35 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,6 +11,9 @@ "[luau][lua]": { "editor.defaultFormatter": "JohnnyMorganz.stylua" }, + "[shellscript]": { + "editor.defaultFormatter": "mkhl.shfmt" + }, // Linting "eslint.run": "onType", diff --git a/aftman.toml b/aftman.toml index c4d67bc..208aba1 100644 --- a/aftman.toml +++ b/aftman.toml @@ -2,3 +2,4 @@ lune = "lune-org/lune@0.8.2" stylua = "JohnnyMorganz/StyLua@0.20.0" luau-lsp = "JohnnyMorganz/luau-lsp@1.27.0" +darklua = "seaofvoices/darklua@0.13.0" diff --git a/examples/basic.luau b/examples/basic.luau new file mode 100644 index 0000000..33dc4d3 --- /dev/null +++ b/examples/basic.luau @@ -0,0 +1,17 @@ +local datetime = require("@lune/datetime") + +local wg = require("../out").wireguard + +function main() + print(`{datetime.now():formatLocalTime()} | Generating keypair`) + + local start = os.clock() + local keypair = wg:generateKeypair() + + print(`{datetime.now():formatLocalTime()} | Generated keypair in {os.clock() - start}s`) + print("---------------------------------------------------------------") + print(`Private Key: {keypair.privateKey}`) + print(`Public Key: {keypair.publicKey}`) +end + +return main() diff --git a/examples/generate.luau b/examples/generate.luau new file mode 100644 index 0000000..f97be2f --- /dev/null +++ b/examples/generate.luau @@ -0,0 +1,70 @@ +local fs = require("@lune/fs") +local process = require("@lune/process") +local stdio = require("@lune/stdio") + +local wg = require("../out").wireguard + +local BASE64_PAT = "^[%w%+%/=]-=?%w=*$" +local CODEGEN_TEMPLATE = [[[Interface] +PrivateKey = %s +PublicKey = %s +Address = %s +DNS = %s + +[Peer] +PublicKey = %s +AllowedIPs = 0.0.0.0/0, ::/0 +Endpoint = %s +]] + +function main() + if process.args[1] == nil or process.args[1] == "" then + print("USAGE: ./lunew example generate ") + process.exit(1) + end + + local outputFile = process.args[1] + + local opts = { + privateKey = "", + address = "", + dns = "", + peerPublicKey = "", + peerEndpoint = "", + peerAllowedIPs = "", + } + + for opt, _ in opts do + local resp: string? = stdio.prompt("text", opt) + + opts[opt] = resp + end + + if opts.privateKey ~= "" and string.match(opts.privateKey, BASE64_PAT) then + opts.publicKey = wg:generatePublicKey(opts.privateKey) + else + local keypair = wg:generateKeypair() + opts.publicKey = keypair.publicKey + opts.privateKey = keypair.privateKey + end + + if opts.address == "" or opts.dns == "" or opts.peerPublicKey == "" or opts.peerEndpoint == "" then + print("ERROR: address, dns, peerPublicKey, peerEndpoint are required") + process.exit(1) + end + + local generatedConfig = string.format( + CODEGEN_TEMPLATE, + opts.privateKey, + opts.publicKey, + opts.address, + opts.dns, + opts.peerPublicKey, + opts.peerEndpoint + ) + + fs.writeFile(outputFile, generatedConfig) + print("Generated config file: " .. outputFile) +end + +return main() diff --git a/lunew b/lunew new file mode 100755 index 0000000..3ea5076 --- /dev/null +++ b/lunew @@ -0,0 +1,36 @@ +#!/bin/bash + +######################################################################### +# A handy bash script which emulates pre 0.8 lune execution behavior. # +# This script checks if there is a lua(u) script at discoverable # +# paths, and if so, executes them with `lune run`, allowing for # +# implicit script execution as before. # +# USAGE: ./lunew [ARGS] [PARAMETERS] # +######################################################################### + +lune=$(which lune) + +if (($(lune --version | cut -d ' ' -f 2 | cut -d '.' -f 2) >= 8)); then + ## CUSTOM EXAMPLE RUNNER BEGIN ## + if [ "$1" == "example" ]; then + filename="$2" + + if [ -z "$filename" ]; then + echo "USAGE: $0 example [ARGS]" + exit 1 + fi + + shift 2 + lune run "examples/$filename.luau" "$@" + exit $? + fi + ## CUSTOM EXAMPLE RUNNER END ## + + if compgen -G "$1*" >/dev/null || compgen -G ".lune/$1*" >/dev/null; then + lune run "$@" + exit $? + fi +fi + +lune "$@" +exit $? diff --git a/package.json b/package.json index b830f31..4215567 100644 --- a/package.json +++ b/package.json @@ -4,15 +4,15 @@ "description": "A lua implementation of the wireguard keygen algorithm.", "main": "out/init.lua", "scripts": { - "build": "rbxtsc", + "build": "rbxtsc --verbose", "watch": "rbxtsc -w", "lint": "eslint src", "check_fmt": "prettier -c src/", "check": "pnpm lint && pnpm check_fmt", "fmt": "prettier -w src/", - "test": "lune run tests", + "test": "./lunew test", "postinstall": "patch -f node_modules/@rbxts/types/include/roblox.d.ts < lune_require_patch.diff", - "prepublishOnly": "pnpm run build" + "prepublishOnly": "pnpm build" }, "keywords": [], "author": "",