#!/bin/sh
# Define variables
APP_NAME="ipchanger"
APP_DIR="/usr/lib/lua/luci"
CONTROLLER_DIR="$APP_DIR/controller/$APP_NAME"
MODEL_DIR="$APP_DIR/model/cbi/$APP_NAME"
CONFIG_FILE="/etc/config/$APP_NAME"
CURRENT_THEME=$(uci get luci.themes.Theme_argon 2>/dev/null)
# Create directories
mkdir -p "$CONTROLLER_DIR"
mkdir -p "$MODEL_DIR"
# Create configuration file
if [ ! -f "$CONFIG_FILE" ]; then
cat <<EOF > "$CONFIG_FILE"
config ipchanger 'settings'
option new_ip ''
EOF
echo "Configuration file created: $CONFIG_FILE"
else
# Check if the configuration file has correct content
if ! grep -q "config ipchanger 'settings'" "$CONFIG_FILE"; then
cat <<EOF > "$CONFIG_FILE"
config ipchanger 'settings'
option new_ip ''
EOF
echo "Configuration file repaired: $CONFIG_FILE"
else
echo "Configuration file already exists and is correct: $CONFIG_FILE"
fi
fi
# Set permissions for the configuration file
chmod 644 "$CONFIG_FILE"
echo "Configuration file permissions set to 644"
# Create controller file
cat <<EOF > "$CONTROLLER_DIR/index.lua"
module("luci.controller.$APP_NAME.index", package.seeall)
function index()
entry({"admin", "system", "$APP_NAME"}, cbi("$APP_NAME/ipchanger"), _("IP Changer"), 60)
end
EOF
echo "Controller file created: $CONTROLLER_DIR/index.lua"
# Create CBI model file
cat <<EOF > "$MODEL_DIR/ipchanger.lua"
local uci = require "luci.model.uci".cursor()
m = Map("$APP_NAME", translate("IP Address Changer"), translate("Modify the router's IP address. After saving and applying, please access the new address."))
s = m:section(NamedSection, "settings", "ipchanger", "")
s.addremove = false
s.anonymous = true
-- Get current IP address
local current_ip = uci:get("network", "lan", "ipaddr") or "192.168.1.1"
-- Input field 1: Display current IP address
o = s:option(DummyValue, "current_ip", translate("Current IP Address"))
o.default = current_ip
o.readonly = true
-- Input field 2: Enter new IP address
o = s:option(Value, "new_ip", translate("New IP Address"))
o.datatype = "ip4addr"
-- Logic for handling submit button
function o.write(self, section, value)
if value ~= current_ip then
-- Replace all old IP addresses in /www/tv.m3u with the new IP address using sed
os.execute("sed -i 's/" .. current_ip .. "/" .. value .. "/g' /www/tv.m3u")
-- Replace all old IP addresses in /etc/config/network with the new IP address using sed
os.execute("sed -i 's/" .. current_ip .. "/" .. value .. "/g' /etc/config/network")
-- Replace all old IP addresses in /etc/config/dhcp with the new IP address using sed
os.execute("sed -i 's/" .. current_ip .. "/" .. value .. "/g' /etc/config/dhcp")
-- Calculate the new gateway address (replace the last byte of the new IP address with 1)
local new_gateway = value:gsub("%d+$", "1")
-- Update the gateway address
uci:set("network", "lan", "gateway", new_gateway)
-- Commit network configuration changes
uci:commit("network")
-- Update ipchanger configuration
uci:set("$APP_NAME", "settings", "new_ip", value)
uci:commit("$APP_NAME")
-- Restart network services
os.execute("/etc/init.d/network restart")
end
end
return m
EOF
echo "CBI model file created: $MODEL_DIR/ipchanger.lua"
# Clear LuCI cache
rm -rf /tmp/luci-*
echo "LuCI cache cleared"
# Output completion message
echo "LuCI application '$APP_NAME' has been successfully installed."
echo "You can access it via the OpenWRT web interface: 'Admin > System > IP Changer'."
No Comments