Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
luochongjun committed May 24, 2023
0 parents commit a3ee90b
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (C) luochognjun@gl-inet.com
#
# This is free software, licensed under the Apache License, Version 2.0 .
#

include $(TOPDIR)/rules.mk

LUCI_TITLE:=dynv6 ddns script
LUCI_DEPENDS:= +luci-compat

PKG_VERSION:=1.0.0
PKG_RELEASE:=
PKG_MAINTAINER:=luochongjun <luochongjun@gl-inet.com>

define Package/luci-app-dynv6/postinst
#!/bin/sh
uci -q get ucitrack.@dynv6[0] || uci add ucitrack dynv6
uci set ucitrack.@dynv6[0]=dynv6
uci set ucitrack.@dynv6[0].exec="/bin/sh /etc/rc.common /etc/init.d/dynv6 restart"
uci commit ucitrack
/etc/init.d/ucitrack restart

exit 0
endef

define Package/$(PKG_NAME)/conffiles
/etc/config/dynv6
endef


include $(TOPDIR)/feeds/luci/luci.mk

# call BuildPackage - OpenWrt buildroot signature
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# luci-app-dynv6
This repository is designed for the dynv6 ddns openwrt application.

## howto get http token
Log into your dynv6 account and go to this [link](https://dynv6.com/keys) to get the http token
3 changes: 3 additions & 0 deletions root/etc/config/dynv6
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
config site 'default'
option enabled '0'
option interval '60'
16 changes: 16 additions & 0 deletions root/etc/init.d/dynv6
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh /etc/rc.common

START=31


stop(){
for id in `pgrep -f dynv6-update.sh`;do
kill -9 $id
done
return 0
}

start(){
/usr/bin/dynv6-update.sh
return 0
}
102 changes: 102 additions & 0 deletions root/usr/bin/dynv6-update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/sh -e

. /lib/functions.sh


logmsg(){
logger -t "dynv6" $@
}

if [ -e /usr/bin/curl ]; then
bin="curl -fsS"
elif [ -e /usr/bin/wget ]; then
bin="wget -O-"
else
logmsg "neither curl nor wget found"
exit 1
fi

update_address(){
local hostname=$1
local device=$2
local token=$3
local site=$4
local file=$HOME/.dynv6.$site
[ -e $file ] && old=`cat $file`

if [ -z "$hostname" -o -z "$token" ]; then
return 0
fi

if [ -z "$netmask" ]; then
netmask=128
fi

if [ "$device" = "any" ]; then
device=""
fi

if [ -n "$device" ]; then
device="dev $device"
fi
address=$(ip -6 addr list scope global $device 2>/dev/null | grep -v " fd" | sed -n 's/.*inet6 \([0-9a-f:]\+\).*/\1/p' | head -n 1)


if [ -z "$address" ]; then
return 0
fi

# address with netmask
current=$address/$netmask

if [ "$old" = "$current" ]; then
#logmsg "$site IPv6 address unchanged"
return 0
fi

# send addresses to dynv6
ret6=`$bin "http://dynv6.com/api/update?hostname=$hostname&ipv6=$current&token=$token" 2>/dev/null`
ret4=`$bin "http://ipv4.dynv6.com/api/update?hostname=$hostname&ipv4=auto&token=$token" 2>/dev/null`

# save current address
if [ "$ret6" = "addresses updated" ];then
echo $current > $file
logmsg "$site update the new ipv6 address $current to dynv6 zone $hostname, ret=$ret6, success!"
else
logmsg "$site update the new ipv6 address $current to dynv6 zone $hostname, ret=$ret6, failed!"
fi

}

dynv6_update_loop()
{
local interval=$1
shift
while true;do
update_address $@
sleep "$interval"
done
}

site_foreach()
{
load_site_cb(){
local site=$1
local token zone device enabled interval token interface
config_get enabled "$site" "enabled" "1"
[ $enabled != '1' ] && return 0
config_get zone "$site" "zone"
config_get token "$site" "token"
config_get interface "$site" "interface" "any"
config_get interval "$site" "interval" "60"
device="$(ifstatus $interface 2>/dev/null| jsonfilter -e '@.l3_device' 2>/dev/null)"
[ -z $device ] && device="any"
[ -n "$zone" ] && [ -n "$device" ] && [ -n "$token" ] && {
dynv6_update_loop $interval $zone $device $token $site &
}
}
config_foreach load_site_cb site
}

config_load dynv6
site_foreach
5 changes: 5 additions & 0 deletions root/usr/lib/lua/luci/controller/admin/dynv6.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module("luci.controller.admin.dynv6", package.seeall)

function index()
entry({"admin", "network", "dynv6"}, cbi("dynv6/config"), translate("Dynv6"), 1)
end
19 changes: 19 additions & 0 deletions root/usr/lib/lua/luci/model/cbi/dynv6/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
m = Map("dynv6", translate("Dynv6"), translate("Dynv6 ddns script for luci (powerd by luochongjun@gl-inet.com)"));

s = m:section(TypedSection, "site", translate("Site")); s.addremove = true;
v1 = s:option(Flag, "enabled", translate("Enable")); v1.optional=false; v1.rmempty = false;
v2 = s:option(Value, "token", translate("Token")); v2.optional=false; v2.rmempty = false;
v3 = s:option(Value, "zone", translate("Zone")); v3.optional=false; v3.rmempty = false;
v4 = s:option(Value, "interval", translate("Interval")); v4.optional=false; v4.rmempty = false; v4.default = "60";
l = s:option(Value, "interface", translate("Interface")); l.optional=false; l.rmempty = false;
l:value("any")
l.default = "any"
local uci = require "luci.model.uci"
local _uci = uci.cursor()
_uci:foreach("network", "interface",
function(s)
if s['.name'] ~= "loopback" and s['.name'] ~= "lan" then
l:value(s['.name'],s['.name'])
end
end)
return m

0 comments on commit a3ee90b

Please sign in to comment.