🍓 Sync 2026-09-15 02:13:06

This commit is contained in:
github-actions[bot] committed 2026-09-15 02:13:06 +08:00
1 parent 0e5393594c
commit 1dd96ac2bb
146 files changed
+26541 -508

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=luci-app-ap-switch
PKG_VERSION:=1.0.0
PKG_RELEASE:=1
PKG_LICENSE:=Apache-2.0
PKG_MAINTAINER:=Dante
LUCI_TITLE:=LuCI support for one-click AP mode switching
LUCI_DEPENDS:=+luci-base +ucode +ucode-mod-fs +ucode-mod-uci +ucode-mod-ubus
LUCI_PKGARCH:=all
include $(TOPDIR)/feeds/luci/luci.mk
# call BuildPackage - OpenWrt buildroot signature
@@ -0,0 +1,201 @@
'use strict';
'require view';
'require rpc';
'require ui';
'require uci';
var callGetStatus = rpc.declare({
object: 'luci.ap-switch',
method: 'get_status'
});
var callSetMode = rpc.declare({
object: 'luci.ap-switch',
method: 'set_mode',
params: ['mode']
});
var callProbeIP = rpc.declare({
object: 'luci.ap-switch',
method: 'probe_ip'
});
return L.view.extend({
i18n: 'ap-switch',
handleSaveApply: null,
handleSave: null,
handleReset: null,
load: function() {
return Promise.all([
callGetStatus(),
uci.load('ap-switch')
]);
},
render: function(data) {
var self = this;
var status = data[0];
var mode = status.mode || 'router';
var lan_ip = status.lan_ip || 'N/A';
var lan_proto = status.lan_proto || 'static';
var lan_mac = status.lan_mac || 'N/A';
var changeModeRows = [
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td left', 'width': '33%' }, _('Switch to %s').format(mode === 'ap' ? _('Router Mode') : _('AP Mode'))),
E('td', { 'class': 'td left' }, [
E('button', {
'class': 'cbi-button cbi-button-apply',
'click': ui.createHandlerFn(self, function() {
return self.handleSwitch(mode === 'ap' ? 'router' : 'ap', status);
})
}, [ (mode === 'ap' ? _('Switch to Router Mode') : _('Switch to AP Mode')) ])
])
])
];
var body = E('div', { 'class': 'cbi-map' }, [
E('h2', {}, _('AP Switch Title') + ' - ' + _('Switch Mode')),
E('div', { 'class': 'cbi-map-descr' }, _('Switch your system runtime configuration smoothly between standard Router and Access Point (AP) modes. Network interfaces and bridge rules will be adjusted automatically.')),
E('fieldset', { 'class': 'cbi-section' }, [
E('legend', {}, _('Current Status')),
E('div', { 'class': 'cbi-section-node' }, [
E('table', { 'class': 'table cbi-section-table' }, [
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td left', 'width': '33%' }, _('Operation Mode')),
E('td', { 'class': 'td left' }, E('strong', {}, (mode === 'ap' ? _('Access Point (AP)') : _('Router'))))
]),
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td left' }, _('Login URL')),
E('td', { 'class': 'td left' }, lan_ip ? E('a', {
'href': 'http://' + lan_ip,
'target': '_blank',
'style': 'text-decoration: underline; font-weight: bold; color: blue;'
}, 'http://' + lan_ip) : _('N/A'))
]),
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td left' }, _('LAN IP Address')),
E('td', { 'class': 'td left' }, [
E('strong', { 'style': 'color: #2196F3;' }, lan_ip || _('Pending...')),
E('span', { 'style': 'margin-left: 10px; color: #666;' }, '(' + lan_proto + ')')
])
]),
E('tr', { 'class': 'tr' }, [
E('td', { 'class': 'td left' }, _('LAN MAC Address')),
E('td', { 'class': 'td left', 'id': 'lan-mac' }, lan_mac)
])
])
])
]),
E('fieldset', { 'class': 'cbi-section' }, [
E('legend', {}, _('Change Mode')),
E('div', { 'class': 'cbi-section-node' }, [
E('table', { 'class': 'table cbi-section-table' }, changeModeRows)
])
])
]);
return body;
},
handleSwitch: function(target_mode, status) {
var self = this;
var executeSwitch = function() {
ui.showModal(null, [
E('p', { 'class': 'spinning' }, _('Applying changes and restarting network...')),
E('p', {}, _('The page will redirect or you may need to manually reconnect to the new IP address in a few moments.'))
]);
return callSetMode(target_mode).then(function(res) {
if (res && res.result === 'success') {
setTimeout(function() {
ui.hideModal();
ui.addNotification(null, E('p', _('Mode switched successfully. Please check your network connection.')), 'info');
}, 5000);
} else {
ui.hideModal();
ui.addNotification(null, E('p', _('Failed to switch mode: %s').format(res.error || 'Unknown error')), 'error');
}
}).catch(function(err) {
ui.hideModal();
ui.addNotification(null, E('p', _('Error calling RPC: %s').format(err.message)), 'error');
});
};
var showConfirmModal = function(title, messages, onConfirm) {
var content = [];
messages.forEach(function(msg) {
content.push(E('p', { 'style': 'margin-bottom: 10px; line-height: 1.5;' }, msg));
});
content.push(E('div', { 'class': 'right', 'style': 'margin-top: 20px;' }, [
E('button', {
'class': 'cbi-button cbi-button-reset',
'click': ui.hideModal
}, _('Cancel')),
E('button', {
'class': 'cbi-button cbi-button-action cbi-button-apply',
'style': 'margin-left: 10px;',
'click': function() {
ui.hideModal();
onConfirm();
}
}, _('Confirm'))
]));
ui.showModal(title, content);
};
if (target_mode === 'ap') {
ui.showModal(null, [
E('p', { 'class': 'spinning' }, _('Auto-probing for future AP IP via DHCP on br-lan...')),
E('p', {}, _('This ensures you can find your way back to the management panel after the switch.'))
]);
return callProbeIP().then(function(res) {
ui.hideModal();
var title = _('Confirm AP Mode Switch');
var messages = [];
if (res && res.ip) {
messages.push(E('span', {}, [
_('Successfully pre-fetched future management IP: '),
E('a', {
'href': 'http://' + res.ip,
'target': '_blank',
'style': 'color: #2196F3; font-weight: bold; text-decoration: underline; word-break: break-all;'
}, 'http://' + res.ip)
]));
messages.push(_('You can use this URL to access this dashboard after the switch.'));
messages.push(_('Are you sure you want to switch to AP mode and restart network?'));
} else {
messages.push(_('WARNING: Failed to pre-fetch IP address via DHCP probe!'));
messages.push(_('This may be because your LAN port is not connected to the main router.'));
messages.push(_('If you proceed, you must find the new IP from your main router client list using MAC: %s').format(status.lan_mac || 'N/A'));
messages.push(_('Do you still want to force the switch anyway?'));
}
showConfirmModal(title, messages, executeSwitch);
}).catch(function(err) {
ui.hideModal();
var title = _('Confirm AP Mode Switch');
var messages = [
_('WARNING: DHCP probe RPC error: %s').format(err.message),
_('If you proceed, you must find the new IP from your main router client list using MAC: %s').format(status.lan_mac || 'N/A'),
_('Do you still want to force the switch anyway?')
];
showConfirmModal(title, messages, executeSwitch);
});
} else {
var title = _('Confirm Router Mode Switch');
var messages = [
_('Switching to Router mode will restore the WAN port and local DHCP server. Your device will use the previous static IP address. Are you sure?')
];
showConfirmModal(title, messages, executeSwitch);
}
}
});
@@ -0,0 +1,116 @@
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"
msgid "AP Switch"
msgstr ""
msgid "Switch between Router and Access Point (AP) mode."
msgstr ""
msgid "Current Status"
msgstr ""
msgid "Operation Mode"
msgstr ""
msgid "Access Point (AP)"
msgstr ""
msgid "Router"
msgstr ""
msgid "LAN IP Address"
msgstr ""
msgid "Change Mode"
msgstr ""
msgid "Switch to %s"
msgstr ""
msgid "Router Mode"
msgstr ""
msgid "AP Mode"
msgstr ""
msgid "Switch to Router Mode"
msgstr ""
msgid "Switch to AP Mode"
msgstr ""
msgid "LAN MAC Address"
msgstr ""
msgid "Switching to AP mode will bridge the WAN port into \"br-lan\". The \"br-lan\" interface will become a DHCP client to your main router."
msgstr ""
msgid "Pre-fetch AP IP"
msgstr ""
msgid "Probing for DHCP IP on br-lan..."
msgstr ""
msgid "Successfully fetched IP: %s"
msgstr ""
msgid "Failed to fetch IP. Ensure a LAN port is connected to your main router."
msgstr ""
msgid "Probe IP"
msgstr ""
msgid "Not probed"
msgstr ""
msgid "A valid IP was pre-fetched: %s. You can use this address to log in after the switch."
msgstr ""
msgid "Are you sure you want to proceed?"
msgstr ""
msgid "WARNING: No IP address was pre-fetched via DHCP probe!"
msgstr ""
msgid "We strongly recommend connecting your LAN port to the main router and using \"Probe IP\" first."
msgstr ""
msgid "If you proceed now, you must find the new IP from your main router using MAC: %s"
msgstr ""
msgid "Do you want to proceed anyway or cancel to plug in the cable and try again?"
msgstr ""
msgid "Pending..."
msgstr ""
msgid "Login URL"
msgstr ""
msgid "N/A"
msgstr ""
msgid "None"
msgstr ""
msgid "Are you sure?"
msgstr ""
msgid "Switching to Router mode will restore the WAN port and local DHCP server. Your device will use the previous static IP address. Are you sure?"
msgstr ""
msgid "Applying changes and restarting network..."
msgstr ""
msgid "The page will redirect or you may need to manually reconnect to the new IP address in a few moments."
msgstr ""
msgid "Mode switched successfully. Please check your network connection."
msgstr ""
msgid "Failed to switch mode: %s"
msgstr ""
msgid "Error calling RPC: %s"
msgstr ""
+1
View File
@@ -0,0 +1 @@
zh_Hans
+169
View File
@@ -0,0 +1,169 @@
msgid ""
msgstr "Content-Type: text/plain; charset=UTF-8"
msgid "AP Switch"
msgstr "AP模式切换"
msgid "Switch between Router and Access Point (AP) mode."
msgstr "在路由器模式和访问点 (AP) 模式之间切换。"
msgid "Current Status"
msgstr "当前状态"
msgid "Operation Mode"
msgstr "运行模式"
msgid "Access Point (AP)"
msgstr "访问点 (AP)"
msgid "Router"
msgstr "路由器"
msgid "LAN IP Address"
msgstr "LAN IP 地址"
msgid "Change Mode"
msgstr "更改模式"
msgid "Switch to %s"
msgstr "切换到 %s"
msgid "Router Mode"
msgstr "路由器模式"
msgid "AP Mode"
msgstr "AP 模式"
msgid "Switch to Router Mode"
msgstr "切换到路由器模式"
msgid "Switch to AP Mode"
msgstr "切换到 AP 模式"
msgid "LAN MAC Address"
msgstr "LAN MAC 地址"
msgid "Switching to AP mode will bridge the WAN port into \"br-lan\". The \"br-lan\" interface will become a DHCP client to your main router."
msgstr "切换到 AP 模式会将 WAN 口桥接到 \"br-lan\"。\"br-lan\" 接口将成为主路由器的 DHCP 客户端。"
msgid "Pre-fetch AP IP"
msgstr "预获取 AP IP"
msgid "Probing for DHCP IP on br-lan..."
msgstr "正在 br-lan 接口探测 DHCP IP..."
msgid "Successfully fetched IP: %s"
msgstr "成功获取到 IP:%s"
msgid "Failed to fetch IP. Ensure a LAN port is connected to your main router."
msgstr "获取 IP 失败。请确保 LAN 口已连接到主路由器。"
msgid "Probe IP"
msgstr "开始探测"
msgid "Not probed"
msgstr "未探测"
msgid "A valid IP was pre-fetched: %s. You can use this address to log in after the switch."
msgstr "已预获取到有效的 IP:%s。切换后,您可以使用此地址登录。"
msgid "Are you sure you want to proceed?"
msgstr "确定要继续吗?"
msgid "WARNING: No IP address was pre-fetched via DHCP probe!"
msgstr "警告:未通过 DHCP 探测预获取到 IP 地址!"
msgid "We strongly recommend connecting your LAN port to the main router and using \"Probe IP\" first."
msgstr "强烈建议先将 LAN 口连接到主路由器,并使用“预获取 AP IP”功能。"
msgid "If you proceed now, you must find the new IP from your main router using MAC: %s"
msgstr "如果您现在继续,切换后必须通过 MAC 地址在主路由中查找新 IP:%s"
msgid "Do you want to proceed anyway or cancel to plug in the cable and try again?"
msgstr "您是坚持继续,还是取消操作并在插上网线后重新尝试?"
msgid "Pending..."
msgstr "等待中..."
msgid "Login URL"
msgstr "登录地址"
msgid "N/A"
msgstr "不适用"
msgid "None"
msgstr "无"
msgid "Are you sure?"
msgstr "确定要继续吗?"
msgid "Switching to Router mode will restore the WAN port and local DHCP server. Your device will use the previous static IP address. Are you sure?"
msgstr "切换到路由器模式将恢复 WAN 口和本地 DHCP 服务。您的设备将使用之前的静态 IP 地址。确定要继续吗?"
msgid "Applying changes and restarting network..."
msgstr "正在应用更改并重启网络..."
msgid "The page will redirect or you may need to manually reconnect to the new IP address in a few moments."
msgstr "页面将会重定向,或者您可能需要在片刻后手动连接到新的 IP 地址。"
msgid "Mode switched successfully. Please check your network connection."
msgstr "模式切换成功。请检查您的网络连接。"
msgid "Failed to switch mode: %s"
msgstr "切换模式失败:%s"
msgid "Error calling RPC: %s"
msgstr "调用 RPC 错误:%s"
msgid "Switch Mode"
msgstr "模式切换"
msgid "Switch your system runtime configuration smoothly between standard Router and Access Point (AP) modes. Network interfaces and bridge rules will be adjusted automatically."
msgstr "在标准路由器模式与 AP 桥接访问点模式之间平滑切换系统运行配置。切换后,网口物理桥接规则以及接口 DHCP 客户端行为将自动智能调整。"
msgid "AP Switch Title"
msgstr "AP 模式切换"
msgid "Auto-probing for future AP IP via DHCP on br-lan..."
msgstr "正在通过 br-lan 接口的 DHCP 自动探测未来的 AP 管理 IP 地址..."
msgid "This ensures you can find your way back to the management panel after the switch."
msgstr "这可以确保您在切换模式后,能够顺利找到并登录新的设备管理后台。"
msgid "Successfully pre-fetched future management IP: "
msgstr "成功预先获取到未来的管理 IP:"
msgid "You can use this URL to access this dashboard after the switch."
msgstr "您可以使用此 URL 地址在切换后访问本控制台。"
msgid "Are you sure you want to switch to AP mode and restart network?"
msgstr "您确定要切换到 AP 模式并重启网络吗?"
msgid "WARNING: Failed to pre-fetch IP address via DHCP probe!"
msgstr "警告:通过 DHCP 探测预获取 IP 地址失败!"
msgid "This may be because your LAN port is not connected to the main router."
msgstr "这可能是由于您设备的 LAN 物理端口尚未连接到上层主路由器。"
msgid "If you proceed, you must find the new IP from your main router client list using MAC: %s"
msgstr "如果您继续,切换后必须通过 MAC 地址在上级主路由的客户端列表中查找新 IP:%s"
msgid "Do you still want to force the switch anyway?"
msgstr "您是否仍要强制执行切换?"
msgid "WARNING: DHCP probe RPC error: %s"
msgstr "警告:DHCP 探测 RPC 请求发生错误:%s"
msgid "Confirm AP Mode Switch"
msgstr "确认切换至 AP 模式"
msgid "Confirm Router Mode Switch"
msgstr "确认切换至路由器模式"
msgid "Cancel"
msgstr "取消"
msgid "Confirm"
msgstr "确认"
@@ -0,0 +1,15 @@
config global 'settings'
option mode 'router'
config backup 'router_state'
option lan_proto 'static'
option lan_ipaddr '192.168.1.1'
option lan_netmask '255.255.255.0'
option lan_gateway ''
list lan_dns ''
option dhcp_ignore '0'
option wan_device ''
option wan_disabled '0'
option wan6_disabled '0'
@@ -0,0 +1,5 @@
#!/bin/sh
[ "$1" = "bound" ] || exit 0
echo "$ip" > /tmp/ap_switch_probed_ip
@@ -0,0 +1,13 @@
{
"admin/services/ap_switch": {
"title": "AP Switch",
"action": {
"type": "view",
"path": "ap-switch/ap-switch"
},
"order": 90,
"depends": {
"acl": [ "luci-app-ap-switch" ]
}
}
}
@@ -0,0 +1,19 @@
{
"luci-app-ap-switch": {
"description": "ACL for AP Switch",
"read": {
"uci": [ "ap-switch", "network", "dhcp" ],
"ubus": {
"luci.ap-switch": [ "get_status", "probe_ip" ],
"network.interface": [ "dump" ],
"network.device": [ "status" ]
}
},
"write": {
"uci": [ "ap-switch", "network", "dhcp" ],
"ubus": {
"luci.ap-switch": [ "set_mode" ]
}
}
}
}
@@ -0,0 +1,226 @@
'use strict';
import { cursor } from 'uci';
import { connect } from 'ubus';
import { readfile, unlink } from 'fs';
const UCI_CONFIG = 'ap-switch';
const PROBE_FILE = '/tmp/ap_switch_probed_ip';
const methods = {
get_status: {
call: function() {
let uci_cursor = cursor();
let mode = uci_cursor.get(UCI_CONFIG, 'settings', 'mode') || 'router';
let lan_proto = uci_cursor.get('network', 'lan', 'proto') || 'static';
let lan_ip = ''; // We will get the real-time IP
let ubus_conn = connect();
let lan_mac = '';
let wan_mac = '';
let wan_ip = '';
if (ubus_conn) {
// 1. Get real-time LAN IP and MAC
let lan_status = ubus_conn.call('network.interface.lan', 'status');
if (lan_status && type(lan_status['ipv4-address']) === 'array' && length(lan_status['ipv4-address']) > 0) {
lan_ip = lan_status['ipv4-address'][0].address;
}
let lan_dev = ubus_conn.call('network.device', 'status', { name: 'br-lan' });
lan_mac = lan_dev?.macaddr || '';
// 2. Get current WAN interface status and its physical device MAC
let wan_status = ubus_conn.call('network.interface.wan', 'status');
let wan_device_name = wan_status?.device || '';
if (wan_device_name) {
let wan_dev = ubus_conn.call('network.device', 'status', { name: wan_device_name });
wan_mac = wan_dev?.macaddr || '';
}
// 3. Get current WAN IP
if (wan_status && type(wan_status['ipv4-address']) === 'array' && length(wan_status['ipv4-address']) > 0) {
wan_ip = wan_status['ipv4-address'][0].address;
}
ubus_conn.disconnect();
}
let probed_ip = rtrim(readfile(PROBE_FILE) || '');
return {
mode: mode,
lan_proto: lan_proto,
lan_ip: lan_ip,
lan_mac: lan_mac,
wan_mac: wan_mac,
wan_ip: wan_ip,
probed_ip: probed_ip
};
}
},
probe_ip: {
call: function() {
unlink(PROBE_FILE);
// Run udhcpc on br-lan with a short timeout and our probe script
// -n: exit if no lease
// -q: quit after obtaining lease
// -t 3: send 3 discover packets
// -T 2: pause 2 seconds between packets
let rc = system('udhcpc -i br-lan -n -q -t 3 -T 2 -s /usr/share/luci-app-ap-switch/dhcp-probe.sh >/dev/null 2>&1');
let ip = rtrim(readfile(PROBE_FILE) || '');
return { success: (rc === 0), ip: ip };
}
},
set_mode: {
args: { mode: 'string' },
call: function(request) {
let target_mode = request.args?.mode;
if (target_mode !== 'router' && target_mode !== 'ap') {
return { error: 'Invalid mode' };
}
let uci_cursor = cursor();
let current_mode = uci_cursor.get(UCI_CONFIG, 'settings', 'mode') || 'router';
if (target_mode === current_mode) {
return { result: 'Already in target mode' };
}
if (target_mode === 'ap') {
// --- Switch to AP Mode (Full Lossless Snapshot) ---
// 1. Identify WAN device
let wan_device = uci_cursor.get('network', 'wan', 'device');
if (!wan_device) {
wan_device = uci_cursor.get('network', 'wan', 'ifname');
}
// 2. Full Snapshot Backup of Router Mode state
let lan_proto = uci_cursor.get('network', 'lan', 'proto') || 'static';
let lan_ipaddr = uci_cursor.get('network', 'lan', 'ipaddr') || '';
let lan_netmask = uci_cursor.get('network', 'lan', 'netmask') || '';
let lan_gateway = uci_cursor.get('network', 'lan', 'gateway') || '';
let lan_dns = uci_cursor.get('network', 'lan', 'dns') || [];
let dhcp_ignore = uci_cursor.get('dhcp', 'lan', 'ignore') || '0';
let wan_disabled = uci_cursor.get('network', 'wan', 'disabled') || '0';
let wan6_disabled = uci_cursor.get('network', 'wan6', 'disabled') || '0';
uci_cursor.set(UCI_CONFIG, 'router_state', 'lan_proto', lan_proto);
uci_cursor.set(UCI_CONFIG, 'router_state', 'lan_ipaddr', lan_ipaddr);
uci_cursor.set(UCI_CONFIG, 'router_state', 'lan_netmask', lan_netmask);
uci_cursor.set(UCI_CONFIG, 'router_state', 'lan_gateway', lan_gateway);
uci_cursor.set(UCI_CONFIG, 'router_state', 'lan_dns', lan_dns);
uci_cursor.set(UCI_CONFIG, 'router_state', 'dhcp_ignore', dhcp_ignore);
uci_cursor.set(UCI_CONFIG, 'router_state', 'wan_device', wan_device || '');
uci_cursor.set(UCI_CONFIG, 'router_state', 'wan_disabled', wan_disabled);
uci_cursor.set(UCI_CONFIG, 'router_state', 'wan6_disabled', wan6_disabled);
uci_cursor.set(UCI_CONFIG, 'settings', 'mode', 'ap');
// 3. Bridge WAN device to LAN bridge
if (wan_device) {
uci_cursor.foreach('network', 'device', (s) => {
if (s.name === 'br-lan') {
let ports = s.ports || [];
if (type(ports) === 'string') ports = [ports];
let exists = false;
for (let p in ports) if (p === wan_device) exists = true;
if (!exists) {
push(ports, wan_device);
uci_cursor.set('network', s['.name'], 'ports', ports);
}
}
});
}
// 4. Set LAN to DHCP client
uci_cursor.set('network', 'lan', 'proto', 'dhcp');
uci_cursor.delete('network', 'lan', 'ipaddr');
uci_cursor.delete('network', 'lan', 'netmask');
uci_cursor.delete('network', 'lan', 'gateway');
uci_cursor.delete('network', 'lan', 'dns');
// 5. Disable WAN interface
uci_cursor.set('network', 'wan', 'disabled', '1');
uci_cursor.set('network', 'wan6', 'disabled', '1');
// 6. Disable DHCP Server
uci_cursor.set('dhcp', 'lan', 'ignore', '1');
} else {
// --- Switch to Router Mode (Lossless State Restoration) ---
// 1. Read Snapshot
let wan_device = uci_cursor.get(UCI_CONFIG, 'router_state', 'wan_device');
let lan_proto = uci_cursor.get(UCI_CONFIG, 'router_state', 'lan_proto') || 'static';
let lan_ipaddr = uci_cursor.get(UCI_CONFIG, 'router_state', 'lan_ipaddr') || '192.168.1.1';
let lan_netmask = uci_cursor.get(UCI_CONFIG, 'router_state', 'lan_netmask') || '';
let lan_gateway = uci_cursor.get(UCI_CONFIG, 'router_state', 'lan_gateway') || '';
let lan_dns = uci_cursor.get(UCI_CONFIG, 'router_state', 'lan_dns') || [];
let dhcp_ignore = uci_cursor.get(UCI_CONFIG, 'router_state', 'dhcp_ignore') || '0';
let wan_disabled = uci_cursor.get(UCI_CONFIG, 'router_state', 'wan_disabled') || '0';
let wan6_disabled = uci_cursor.get(UCI_CONFIG, 'router_state', 'wan6_disabled') || '0';
uci_cursor.set(UCI_CONFIG, 'settings', 'mode', 'router');
// 2. Remove WAN device from LAN bridge
if (wan_device) {
uci_cursor.foreach('network', 'device', (s) => {
if (s.name === 'br-lan') {
let ports = s.ports || [];
if (type(ports) === 'string') ports = [ports];
let new_ports = [];
for (let p in ports) if (p !== wan_device) push(new_ports, p);
uci_cursor.set('network', s['.name'], 'ports', new_ports);
}
});
}
// 3. Restore LAN settings accurately without hardcoding
uci_cursor.set('network', 'lan', 'proto', lan_proto);
if (lan_ipaddr) uci_cursor.set('network', 'lan', 'ipaddr', lan_ipaddr);
if (lan_netmask) uci_cursor.set('network', 'lan', 'netmask', lan_netmask);
if (lan_gateway) uci_cursor.set('network', 'lan', 'gateway', lan_gateway);
if (type(lan_dns) === 'array' && length(lan_dns) > 0) {
uci_cursor.set('network', 'lan', 'dns', lan_dns);
} else if (type(lan_dns) === 'string' && lan_dns !== '') {
uci_cursor.set('network', 'lan', 'dns', lan_dns);
}
// 4. Restore WAN / WAN6 states
if (wan_disabled === '1') {
uci_cursor.set('network', 'wan', 'disabled', '1');
} else {
uci_cursor.delete('network', 'wan', 'disabled');
}
if (wan6_disabled === '1') {
uci_cursor.set('network', 'wan6', 'disabled', '1');
} else {
uci_cursor.delete('network', 'wan6', 'disabled');
}
// 5. Restore DHCP Server state
if (dhcp_ignore === '1') {
uci_cursor.set('dhcp', 'lan', 'ignore', '1');
} else {
uci_cursor.delete('dhcp', 'lan', 'ignore');
}
}
uci_cursor.commit('ap-switch');
uci_cursor.commit('network');
uci_cursor.commit('dhcp');
// Apply changes in background
system('/etc/init.d/network restart &');
system('/etc/init.d/dnsmasq restart &');
return { success: true, result: 'success' };
}
}
};
return { 'luci.ap-switch': methods };