feat: initial commit
This commit is contained in:
commit
6179dea246
19 changed files with 693 additions and 0 deletions
13
legacy/Dockerfile
Normal file
13
legacy/Dockerfile
Normal file
|
@ -0,0 +1,13 @@
|
|||
FROM akorn/luarocks:lua5.1-alpine
|
||||
|
||||
RUN apk add build-base openssl-dev
|
||||
|
||||
RUN luarocks install luasocket \
|
||||
&& luarocks install luasec \
|
||||
&& luarocks install lua-cjson \
|
||||
&& luarocks install redis-lua \
|
||||
&& luarocks install luaposix
|
||||
|
||||
WORKDIR /opt/updater
|
||||
COPY updater.lua .
|
||||
CMD ["lua", "updater.lua"]
|
24
legacy/README.md
Normal file
24
legacy/README.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
Legacy Updater Tool
|
||||
===================
|
||||
|
||||
This updater exists to facilitate a transition from the legacy mirrors orchestration to its replacement.
|
||||
As far as I know, no one else has ever deployed the original orchestrator so this will almost certainly not be useful
|
||||
to you.
|
||||
|
||||
Configuration
|
||||
-------------
|
||||
|
||||
When working with the Docker Compose file, configure the sources via the environment file: `updater.env`.
|
||||
One environment variable per configured pool with an HTTP URL to the Bypass Censorship Mirrors JSON file for each pool.
|
||||
|
||||
For example:
|
||||
|
||||
```shell
|
||||
JASIMA_MIRRORS_POOL_example=https://www.example.com/mirrors.json
|
||||
```
|
||||
|
||||
### Per-site Configurations
|
||||
|
||||
In the configs directory, create a file named `default.json` to provide the default site configuration.
|
||||
Create files named `<host>.json` to override the default.
|
||||
This is a complete override so duplicate anything from the default you wanted to keep.
|
3
legacy/configs/default.json.example
Normal file
3
legacy/configs/default.json.example
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"matomo_site_id": 200
|
||||
}
|
4
legacy/configs/www.example.com.json.example
Normal file
4
legacy/configs/www.example.com.json.example
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"matomo_site_id": 300,
|
||||
"host_connect": "www.example.org"
|
||||
}
|
88
legacy/updater.lua
Normal file
88
legacy/updater.lua
Normal file
|
@ -0,0 +1,88 @@
|
|||
local cjson = require('cjson')
|
||||
local http = require('socket.http')
|
||||
local stdlib = require('posix.stdlib')
|
||||
local redis = require('redis')
|
||||
|
||||
local client = redis.connect('redis', 6379)
|
||||
|
||||
local function get_mirrors(source)
|
||||
local body, _, _, _ = http.request(source)
|
||||
return cjson.decode(body)
|
||||
end
|
||||
|
||||
local function prepare_pool_map(mirrors)
|
||||
local pool_map = {}
|
||||
for _, site in pairs(mirrors['sites']) do
|
||||
for _, alternative in pairs(site['available_alternatives']) do
|
||||
if alternative['type'] == 'mirror' then
|
||||
local canonical_host = site['main_domain']
|
||||
if select(2, canonical_host:gsub("%.", "")) == 1 then
|
||||
canonical_host = "www." .. canonical_host
|
||||
end
|
||||
pool_map[canonical_host] = alternative['url']:sub(9)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return pool_map
|
||||
end
|
||||
|
||||
local function redis_set(client, key, data)
|
||||
local res, err = client:set(key, data)
|
||||
if not res then
|
||||
print("Error setting key in Redis:", err)
|
||||
else
|
||||
print("Data successfully stored in Redis under '" .. key .. "'")
|
||||
end
|
||||
end
|
||||
|
||||
local function get_config(host)
|
||||
local function read_file(file_path)
|
||||
local file = io.open(file_path, "r")
|
||||
if file then
|
||||
local content = file:read("*a")
|
||||
file:close()
|
||||
return content
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
local file_paths = {
|
||||
"/configs/" .. host .. ".json", -- Check host-specific file
|
||||
"/configs/default.json" -- Check default file
|
||||
}
|
||||
for _, file_path in ipairs(file_paths) do
|
||||
local content = read_file(file_path)
|
||||
if content then
|
||||
print("Using " .. file_path .. " for " .. host .. " configuration.")
|
||||
return content
|
||||
end
|
||||
end
|
||||
return "{}"
|
||||
end
|
||||
|
||||
local function update_pool_data(pool_name, pool_source)
|
||||
local pool_map = prepare_pool_map(get_mirrors(pool_source))
|
||||
redis_set(client, 'jasima:poolmap:' .. pool_name, cjson.encode(pool_map))
|
||||
for host, _ in pairs(pool_map) do
|
||||
local redis_key = 'jasima:config:' .. host
|
||||
local config = get_config(host)
|
||||
redis_set(client, redis_key, config)
|
||||
end
|
||||
end
|
||||
|
||||
local interval = 5 * 60 -- 5 minutes in seconds
|
||||
local last_update_time = os.clock() - interval
|
||||
|
||||
while true do
|
||||
local current_time = os.clock()
|
||||
if current_time - last_update_time >= interval then
|
||||
for pool_name, pool_source in pairs(stdlib.getenv()) do
|
||||
if pool_name:match("^JASIMA_MIRRORS_POOL_") then
|
||||
local pool_name = pool_name:sub(21)
|
||||
update_pool_data(pool_name, pool_source)
|
||||
end
|
||||
end
|
||||
last_update_time = current_time
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue