Skip to content

Lua Projects

Integrate abystrixLicense into your Lua-based applications and game scripts.

Using socket.http

lua
local http = require("socket.http")
local json = require("json")
local ltn12 = require("ltn12")

local abystrixLicense = {}
abystrixLicense.__index = abystrixLicense

function abystrixLicense.new(serverUrl)
    local self = setmetatable({}, abystrixLicense)
    self.serverUrl = serverUrl:gsub("/$", "")
    return self
end

function abystrixLicense:validate(licenseKey, productId, hwid)
    local body = json.encode({
        licenseKey = licenseKey,
        productId = productId,
        hwid = hwid or "unknown"
    })

    local responseBody = {}
    local res, code = http.request({
        url = self.serverUrl .. "/api/v1/validate",
        method = "POST",
        headers = {
            ["Content-Type"] = "application/json",
            ["Content-Length"] = #body
        },
        source = ltn12.source.string(body),
        sink = ltn12.sink.table(responseBody)
    })

    if code == 200 then
        local ok, result = pcall(json.decode, table.concat(responseBody))
        if ok then
            return result.valid == true
        end
    end
    return false
end

function abystrixLicense:getLicense(licenseKey)
    local responseBody = {}
    local res, code = http.request({
        url = self.serverUrl .. "/api/v2/licenses/by-license-key/" .. licenseKey,
        method = "GET",
        headers = {
            ["TOKEN"] = "your-api-token"
        },
        sink = ltn12.sink.table(responseBody)
    })

    if code == 200 then
        local ok, result = pcall(json.decode, table.concat(responseBody))
        if ok then return result end
    end
    return nil
end

-- Usage
local license = abystrixLicense.new("https://your-server.com")
if license:validate("XXXXX-XXXXX-XXXXX-XXXXX", 1) then
    print("License is valid!")
else
    print("License is invalid!")
end

GMod (Garry's Mod)

lua
-- Based on HTTP library available in Garry's Mod
local function validateLicense(licenseKey, productId)
    local body = util.TableToJSON({
        licenseKey = licenseKey,
        productId = productId
    })

    http.Post("https://your-server.com/api/v1/validate", body, function(data)
        local response = util.JSONToTable(data)
        if response and response.valid then
            print("[Abystrix] License validated!")
        else
            print("[Abystrix] Invalid license!")
        end
    end)
end

-- Usage
validateLicense("XXXXX-XXXXX-XXXXX-XXXXX", 1)

ComputerCraft / CC:Tweaked

lua
local function validateLicense(key, productId)
    local request = http.post(
        "https://your-server.com/api/v1/validate",
        textutils.serializeJSON({
            licenseKey = key,
            productId = productId
        }),
        { ["Content-Type"] = "application/json" }
    )
    
    if request then
        local data = textutils.unserializeJSON(request.readAll())
        request.close()
        return data and data.valid == true
    end
    return false
end

-- Usage
if validateLicense("XXXXX-XXXXX-XXXXX-XXXXX", 1) then
    print("License valid!")
else
    print("License invalid!")
end

Released under the MIT License.