A secure, sandboxed Lua scripting platform built in Go that allows users to write Lua scripts for task automation and dynamic configuration management.
- Sandboxed Execution: Lua scripts run in a restricted environment
- Safe Libraries Only: Access to safe Lua libraries (math, string, table, etc.)
- No Dangerous Access: Blocked access to OS, file I/O, debug, and package loading
- Resource Isolation: Each script runs in its own isolated environment
- Dynamic Configuration: Scripts can read and write configuration values
- Type Safety: Support for strings, numbers, booleans, and tables
- Persistent State: Configuration changes persist across script executions
- CLI Interface: Command-line tools for script management
- Interactive Mode: Interactive shell for script development
- File-based Storage: Scripts stored as
.lua
files in thescripts/
directory - Hot Reloading: Scripts are loaded dynamically
get_config(key)
: Retrieve configuration valuesset_config(key, value)
: Set configuration valueslog(message)
: Log messages with timestamps- Standard Lua functions (print, math, string, table, etc.)
- Prerequisites: Go 1.16 or later
- Clone/Setup: Navigate to your project directory
- Install Dependencies:
go mod init scriptconfig go get github.com/yuin/gopher-lua go mod tidy
# List all available scripts
go run main.go list
# Run a specific script
go run main.go run hello.lua
# Add a new script
go run main.go add my_script "print('Hello World')"
# Delete a script
go run main.go delete my_script.lua
# Start interactive mode
go run main.go interactive
# Show help
go run main.go help
# Or use the built executable (Windows)
./scriptconfig.exe list
./scriptconfig.exe run hello.lua
Start interactive mode for a more user-friendly experience:
go run main.go interactive
Available commands in interactive mode:
list
- List all scriptsrun <script>
- Execute a scriptadd <name>
- Add a new script (interactive input)delete <script>
- Delete a scripthelp
- Show helpexit
- Exit interactive mode
-- Simple script
print("Hello from Lua!")
-- Access configuration
local value = get_config("my_key")
if value then
print("Config value:", value)
end
-- Set configuration
set_config("new_key", "new_value")
-- Log messages
log("Script completed successfully")
-- Get configuration with defaults
local max_retries = get_config("max_retries") or 3
local timeout = get_config("timeout") or 30
-- Set configuration values
set_config("last_execution", 0) -- Note: os.time() not available in sandbox
set_config("status", "completed")
-- Automation script
local tasks = {"task1", "task2", "task3"}
for i, task in ipairs(tasks) do
print("Executing:", task)
set_config("current_task", i)
log("Completed task: " .. task)
end
set_config("all_completed", true)
- β
Mathematical operations (
math
library) - β
String manipulation (
string
library) - β
Table operations (
table
library) - β
Basic I/O (
print
) - β
Configuration access (
get_config
,set_config
) - β
Logging (
log
)
- β File system access (
io
library) - β Operating system calls (
os
library) - β Debug access (
debug
library) - β Package loading (
package
library) - β Network access
- β Process creation
ScriptConfig/
βββ main.go # Main application entry point
βββ scriptconfig.exe # Built executable (Windows)
βββ go.mod # Go module file
βββ go.sum # Go dependencies checksum
βββ scripts/ # Lua scripts directory
β βββ hello.lua # Basic hello world script
β βββ calculator.lua # Mathematical operations example
β βββ automation.lua # Task automation example
β βββ advanced_demo.lua # Complex features demonstration
β βββ test_script.lua # CLI-created script example
βββ config/ # Configuration directory
β βββ default.json # Sample configuration file
βββ README.md # This documentation
Basic script demonstrating configuration access and logging.
Mathematical operations with configuration-driven parameters.
Task automation with progress tracking and configuration updates.
Complex data processing, filtering, and configuration management demonstration.
Example of a script created via the CLI interface.
# Build the application
go build -o scriptconfig.exe main.go # Windows
go build -o scriptconfig main.go # Linux/Mac
# Run the built binary
./scriptconfig.exe list # Windows
./scriptconfig list # Linux/Mac
./scriptconfig.exe run hello.lua # Windows
./scriptconfig run hello.lua # Linux/Mac
To add new safe functions to the Lua environment, modify the createSandboxedLuaState()
function in main.go
:
L.SetGlobal("my_function", L.NewFunction(func(L *lua.LState) int {
// Your function implementation
return 0
}))
The platform supports basic Lua types. For complex data structures, use Lua tables:
-- Store complex data
set_config("user_data", {
name = "John",
age = 30,
active = true
})
The platform includes a sample configuration file (config/default.json
) that can be extended for external configuration management:
{
"app_name": "Lua Scripting Platform",
"version": "1.0.0",
"max_retries": 3,
"timeout": 30,
"debug_mode": false,
"default_script": "hello.lua",
"allowed_libraries": ["math", "string", "table"],
"security_level": "high"
}
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source. Feel free to use and modify as needed.
- Always review Lua scripts before execution
- The sandbox provides basic security but is not foolproof
- Consider additional security measures for production use
- Monitor script execution for resource usage
- Implement rate limiting for script execution if needed
Happy Scripting! π