This is a basic module to help you lock HTML5 games to a specific domain (or a list of domains). This can be required for licensing for example, to keep other sites from hosting your games without your permission that easily. Most web browsers do not allow spoofing location.hostname for security reasons so it’s pretty safe to use for checking current domain.
You’ll want to provide a helpful message in case of domain check failure. When not running on HTML5 builds the default behavior is to always pass a domain check.
You will want to add every version of your domain if your games can be hosted on them. Such as www.yougamesdomain.com and yougamesdmain.com both. You’ll probably want to add “localhost” while debugging. You can use sys.get_engine_info to check if the engine is running in debug or not.
If you make improvements please post them here! For example, allowing wildcard for subdomains as an option to enable would be nice.
local domainlock = require("utils.domainlock")
domainlock.add_domain("localhost")
domainlock.add_domain("defold.com")
domainlock.add_domain("www.defold.com")
if domainlock.verify_domain() then print("hurray! we're on a verified domain!") end
domainlock.lua (457 Bytes)
local M = {}
M.domains = {}
function M.add_domain(domain)
table.insert(M.domains, domain)
end
function M.verify_domain()
if not html5 then return true end
local current_domain = html5.run("location.hostname")
for key, value in ipairs(M.domains) do
if value == current_domain then
return true
end
end
return false
end
function M.get_current_domain()
if html5 then
return html5.run("location.hostname")
else
return ""
end
end
return M