diff --git a/utils/auto-timezone/Makefile b/utils/auto-timezone/Makefile new file mode 100644 index 00000000000000..9b26408ae121a3 --- /dev/null +++ b/utils/auto-timezone/Makefile @@ -0,0 +1,41 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=auto-timezone +PKG_VERSION:=1.0.0 +PKG_RELEASE:=1 + +PKG_MAINTAINER:=Renn Akaza +PKG_LICENSE:=GPL-3.0-or-later + +include $(INCLUDE_DIR)/package.mk + +define Package/auto-timezone + SECTION:=utils + CATEGORY:=Utilities + TITLE:=Auto set timezone by IP + PKGARCH:=all + DEPENDS:=+zoneinfo-all +endef + +define Package/auto-timezone/description +Hotplug script to automatically configure the timezone UCI settings based on the IP address. + +The user needs to set their own endpoint to get the IANA timezone string, e.g.: + uci set auto-timezone.config.endpoint="https://domain.tld/timezone" + uci commit auto-timezone + +The endpoint is expected to respond GET requests by a plaintext one-line IANA timezone string, e.g. "Europe/London". +The endpoint can be verified by directly accessing the its URL from a browser by the address bar. +endef + +define Build/Compile +endef + +define Package/auto-timezone/install + $(INSTALL_DIR) $(1)/etc/hotplug.d/iface/ + $(INSTALL_DATA) ./files/99-auto-timezone $(1)/etc/hotplug.d/iface/ + $(INSTALL_DIR) $(1)/etc/config + $(INSTALL_CONF) ./files/auto-timezone.config $(1)/etc/config/auto-timezone +endef + +$(eval $(call BuildPackage,auto-timezone)) diff --git a/utils/auto-timezone/files/99-auto-timezone b/utils/auto-timezone/files/99-auto-timezone new file mode 100644 index 00000000000000..eed7668acf860c --- /dev/null +++ b/utils/auto-timezone/files/99-auto-timezone @@ -0,0 +1,58 @@ +#!/bin/sh + +if [ "${INTERFACE}" = "loopback" ]; then + exit 0 +fi +if [ "${ACTION}" != "ifup" ] && [ "${ACTION}" != "ifupdate" ]; then + exit 0 +fi +if [ "${ACTION}" = "ifupdate" ] && [ -z "${IFUPDATE_ADDRESSES}" ] && [ -z "${IFUPDATE_DATA}" ]; then + exit 0 +fi + +log() { + logger -t "auto-timezone" "$@" +} + +log_info() { + log -p user.info "$*" +} + +log_error() { + log -p user.err "$*" +} + +get_posix_timezone() { + iana_timezone=$1 + tail -n1 /usr/share/zoneinfo/${iana_timezone} +} + +endpoint="$(uci -q get auto-timezone.config.endpoint)" +if [ $? -ne 0 ]; then + log_error "Endpoint not set" + exit 1 +fi + +for i in {1..5}; do + timezone_iana="$(wget --timeout=5 -qO- "${endpoint}")" && break || sleep 1 +done + +if [ -z "${timezone_iana}" ]; then + log_error "Failed to get timezone" + exit 1 +fi + +timezone_posix="$(get_posix_timezone ${timezone_iana})" +if [ $? -ne 0 ]; then + log_error "Invalid timezone" + exit 1 +fi + +if [ "${timezone_iana}" != "$(uci get system.@system[0].zonename)" ] || [ "${timezone_posix}" != "$(uci get system.@system[0].timezone)" ]; then + log_info "Updating timezone, IANA: ${timezone_iana}, POSIX: ${timezone_posix}" + uci set system.@system[0].zonename="${timezone_iana}" + uci set system.@system[0].timezone="${timezone_posix}" + uci commit system + /etc/init.d/system reload + /etc/init.d/cron restart +fi diff --git a/utils/auto-timezone/files/auto-timezone.config b/utils/auto-timezone/files/auto-timezone.config new file mode 100644 index 00000000000000..21552dd7a97c39 --- /dev/null +++ b/utils/auto-timezone/files/auto-timezone.config @@ -0,0 +1 @@ +config auto-timezone 'config'