Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions utils/auto-timezone/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=auto-timezone
PKG_VERSION:=1.0.0
PKG_RELEASE:=1

PKG_MAINTAINER:=Renn Akaza <openwrt.org-auto-timezone@akaza.ren>
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))
58 changes: 58 additions & 0 deletions utils/auto-timezone/files/99-auto-timezone
Original file line number Diff line number Diff line change
@@ -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() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would pass priority as well.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added two functions log_info and log_error to handle the priorities in 1dbe849.

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
1 change: 1 addition & 0 deletions utils/auto-timezone/files/auto-timezone.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config auto-timezone 'config'
Loading