mirror of
https://github.com/0x5eal/wg-lua.git
synced 2024-12-12 04:40:36 +00:00
chore: include examples and runner
This commit is contained in:
parent
febab61221
commit
2a49e303f9
7 changed files with 130 additions and 3 deletions
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
|
@ -11,6 +11,9 @@
|
|||
"[luau][lua]": {
|
||||
"editor.defaultFormatter": "JohnnyMorganz.stylua"
|
||||
},
|
||||
"[shellscript]": {
|
||||
"editor.defaultFormatter": "mkhl.shfmt"
|
||||
},
|
||||
|
||||
// Linting
|
||||
"eslint.run": "onType",
|
||||
|
|
|
@ -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"
|
||||
|
|
17
examples/basic.luau
Normal file
17
examples/basic.luau
Normal file
|
@ -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()
|
70
examples/generate.luau
Normal file
70
examples/generate.luau
Normal file
|
@ -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 <output-file>")
|
||||
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()
|
36
lunew
Executable file
36
lunew
Executable file
|
@ -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 <example_name> [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 $?
|
|
@ -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": "",
|
||||
|
|
Loading…
Reference in a new issue