Library sync OsmAnd-style: brick is the server, a client (the car's maptool, SYNC_SERVER_URL) syncs on its Sync button — pulls the server's rows, pushes its own with the source files the server lacks, newer updated_at wins; /api/sync/* guarded by SYNC_TOKEN (written into brick's .env by CI). TRUSTED_NETWORKS logs in the car's LAN (no Gitea offline); TAKEOUT_HOST_DIR for a server without the Takeout; bin/pull-maps.sh pulls map data one way while at home
This commit is contained in:
1 parent
850a5946ad
commit
dbe742321a
13 files changed
+460
-5
No files matched your search
@@ -12,6 +12,8 @@ require_relative "lib/maptool/google_saved"
|
||||
require_relative "lib/maptool/routing"
|
||||
require_relative "lib/maptool/mqtt_client"
|
||||
require_relative "lib/maptool/can"
|
||||
require_relative "lib/maptool/sync"
|
||||
require "ipaddr"
|
||||
require_relative "lib/maptool/live_store"
|
||||
require_relative "lib/maptool/livetrack_store"
|
||||
require_relative "lib/maptool/livetrack_client"
|
||||
@@ -205,9 +207,38 @@ module Maptool
|
||||
token_url: "/login/oauth/access_token"
|
||||
)
|
||||
|
||||
# The car's server (no internet, so no Gitea login): requests from
|
||||
# these networks (comma-separated CIDRs) count as logged in. By the
|
||||
# socket's own peer address, never X-Forwarded-For — and only for a
|
||||
# server no reverse proxy sits in front of (behind one every request
|
||||
# comes from the proxy's address).
|
||||
TRUSTED_NETWORKS = ENV.fetch("TRUSTED_NETWORKS", "").split(",").map(&:strip).reject(&:empty?).map { |cidr| IPAddr.new(cidr) }
|
||||
|
||||
# Library sync (Sync), OsmAnd-style: brick is the server, a client
|
||||
# (the car's) has SYNC_SERVER_URL and syncs on its Sync button. The same
|
||||
# SYNC_TOKEN on both: the server's /api/sync/* answer only to it.
|
||||
SYNC_TOKEN = ENV.fetch("SYNC_TOKEN", "")
|
||||
SYNC_SERVER_URL = ENV.fetch("SYNC_SERVER_URL", "")
|
||||
SYNC_CLIENT = SYNC_TOKEN.empty? || SYNC_SERVER_URL.empty? ? nil : Sync.new(server: SYNC_SERVER_URL, token: SYNC_TOKEN, library: LIBRARY, state_path: File.join(DATA_DIR, "sync_state.json"))
|
||||
SYNC_MUTEX = Mutex.new
|
||||
|
||||
helpers do
|
||||
def logged_in?
|
||||
!!session[:user]
|
||||
!!session[:user] || trusted_network?
|
||||
end
|
||||
|
||||
def trusted_network?
|
||||
return false if TRUSTED_NETWORKS.empty?
|
||||
|
||||
address = IPAddr.new(request.env["REMOTE_ADDR"].to_s.delete_prefix("::ffff:"))
|
||||
TRUSTED_NETWORKS.any? { |network| network.include?(address) }
|
||||
rescue IPAddr::Error
|
||||
false
|
||||
end
|
||||
|
||||
def require_sync_token!
|
||||
given = request.env["HTTP_AUTHORIZATION"].to_s.delete_prefix("Bearer ")
|
||||
halt 401, json(error: "sync token required") if SYNC_TOKEN.empty? || !Rack::Utils.secure_compare(given, SYNC_TOKEN)
|
||||
end
|
||||
|
||||
def require_login!
|
||||
@@ -372,7 +403,7 @@ module Maptool
|
||||
end
|
||||
|
||||
get "/" do
|
||||
erb :index, locals: { logged_in: logged_in?, user: session[:user], revision: REVISION }
|
||||
erb :index, locals: { logged_in: logged_in?, user: session[:user] || "local network", revision: REVISION, sync_client: !SYNC_CLIENT.nil? }
|
||||
end
|
||||
|
||||
# Map legend + symbol browser. A view, not a static public/ page, so it
|
||||
@@ -800,6 +831,55 @@ module Maptool
|
||||
halt 404
|
||||
end
|
||||
|
||||
# The client's side of a sync (Sync): its pull (whole rows after
|
||||
# ?since=<rev>, an item's source file) and its push (rows, then the
|
||||
# source files we said we lack).
|
||||
get "/api/sync/changes" do
|
||||
require_sync_token!
|
||||
json LIBRARY.sync_changes(params[:since] || 0)
|
||||
rescue ArgumentError, TypeError
|
||||
halt 400, json(error: "since must be a number")
|
||||
end
|
||||
|
||||
get "/api/sync/source/:id" do
|
||||
require_sync_token!
|
||||
path = LIBRARY.item_source(params[:id])
|
||||
halt 404 unless path && File.file?(path)
|
||||
send_file path
|
||||
end
|
||||
|
||||
post "/api/sync/apply" do
|
||||
require_sync_token!
|
||||
missing = LIBRARY.apply_sync(JSON.parse(request.body.read))
|
||||
json missing: missing.map { |item| item["id"] }
|
||||
rescue JSON::ParserError, Library::InvalidValue
|
||||
halt 400, json(error: "bad changes")
|
||||
end
|
||||
|
||||
put "/api/sync/source/:id" do
|
||||
require_sync_token!
|
||||
row = LIBRARY.source_row(params[:id]) or halt 404
|
||||
# Source files never change: one already here stays.
|
||||
LIBRARY.store_source(row, request.body.read) unless File.exist?(LIBRARY.source_file(row["type"], row["source_path"]))
|
||||
json ok: true
|
||||
rescue Library::InvalidValue
|
||||
halt 400, json(error: "bad source path")
|
||||
end
|
||||
|
||||
# The Sync button (a client only): pull, then push, now.
|
||||
post "/api/sync/run" do
|
||||
require_login!
|
||||
halt 404, json(error: "not a sync client") unless SYNC_CLIENT
|
||||
halt 409, json(error: "a sync is already running") unless SYNC_MUTEX.try_lock
|
||||
begin
|
||||
json SYNC_CLIENT.run
|
||||
rescue *Sync::NETWORK_ERRORS => e
|
||||
halt 502, json(error: "sync with #{SYNC_SERVER_URL} failed: #{e.message}")
|
||||
ensure
|
||||
SYNC_MUTEX.unlock
|
||||
end
|
||||
end
|
||||
|
||||
# The sync feed (Library#changes): everything after ?since=<rev>.
|
||||
# Login-only, like the other exports.
|
||||
get "/api/library/changes" do
|
||||
|
||||
Reference in new issue
Block a user