Skip to content

A secure, sandboxed Lua scripting platform built in Go that enables runtime task automation and dynamic configuration management. Write Lua scripts to automate tasks, configure application behavior, and process data - all within a secure, isolated environment.

Notifications You must be signed in to change notification settings

ab0626/ScriptConfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Lua Dynamic Configuration and Scripting Platform

A secure, sandboxed Lua scripting platform built in Go that allows users to write Lua scripts for task automation and dynamic configuration management.

Features

πŸ”’ Security & Sandboxing

  • 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

βš™οΈ Configuration Management

  • 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

πŸš€ Script Management

  • CLI Interface: Command-line tools for script management
  • Interactive Mode: Interactive shell for script development
  • File-based Storage: Scripts stored as .lua files in the scripts/ directory
  • Hot Reloading: Scripts are loaded dynamically

πŸ“ Built-in Functions

  • get_config(key): Retrieve configuration values
  • set_config(key, value): Set configuration values
  • log(message): Log messages with timestamps
  • Standard Lua functions (print, math, string, table, etc.)

Installation

  1. Prerequisites: Go 1.16 or later
  2. Clone/Setup: Navigate to your project directory
  3. Install Dependencies:
    go mod init scriptconfig
    go get github.com/yuin/gopher-lua
    go mod tidy

Usage

Command Line Interface

# 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

Interactive Mode

Start interactive mode for a more user-friendly experience:

go run main.go interactive

Available commands in interactive mode:

  • list - List all scripts
  • run <script> - Execute a script
  • add <name> - Add a new script (interactive input)
  • delete <script> - Delete a script
  • help - Show help
  • exit - Exit interactive mode

Writing Lua Scripts

Basic Script Example

-- 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")

Configuration Management

-- 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")

Task Automation Example

-- 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)

Security Model

Allowed Operations

  • βœ… Mathematical operations (math library)
  • βœ… String manipulation (string library)
  • βœ… Table operations (table library)
  • βœ… Basic I/O (print)
  • βœ… Configuration access (get_config, set_config)
  • βœ… Logging (log)

Blocked Operations

  • ❌ File system access (io library)
  • ❌ Operating system calls (os library)
  • ❌ Debug access (debug library)
  • ❌ Package loading (package library)
  • ❌ Network access
  • ❌ Process creation

Project Structure

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

Sample Scripts

1. Hello World (scripts/hello.lua)

Basic script demonstrating configuration access and logging.

2. Calculator (scripts/calculator.lua)

Mathematical operations with configuration-driven parameters.

3. Automation (scripts/automation.lua)

Task automation with progress tracking and configuration updates.

4. Advanced Demo (scripts/advanced_demo.lua)

Complex data processing, filtering, and configuration management demonstration.

5. Test Script (scripts/test_script.lua)

Example of a script created via the CLI interface.

Building and Running

# 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

Extending the Platform

Adding New Safe Functions

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
}))

Custom Configuration Types

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
})

External Configuration Files

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"
}

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is open source. Feel free to use and modify as needed.

Security Considerations

  • 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! πŸš€

About

A secure, sandboxed Lua scripting platform built in Go that enables runtime task automation and dynamic configuration management. Write Lua scripts to automate tasks, configure application behavior, and process data - all within a secure, isolated environment.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published