diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..307387d --- /dev/null +++ b/.gitattributes @@ -0,0 +1,15 @@ +# Router files MUST use LF line endings. CRLF breaks nft (firewall ruleset is +# rejected wholesale), POSIX shell (init.d), and can confuse ucode parsing. +# Force LF on checkout regardless of the developer's core.autocrlf setting. + +*.ut text eol=lf +*.uc text eol=lf +*.nft text eol=lf +*.sh text eol=lf +*.js text eol=lf +*.json text eol=lf + +# Extensionless router scripts +root/etc/init.d/* text eol=lf +root/usr/share/rpcd/ucode/* text eol=lf +root/etc/uci-defaults/* text eol=lf diff --git a/htdocs/luci-static/resources/view/homeproxy/client.js b/htdocs/luci-static/resources/view/homeproxy/client.js index ebf9a9a..f57a9d0 100644 --- a/htdocs/luci-static/resources/view/homeproxy/client.js +++ b/htdocs/luci-static/resources/view/homeproxy/client.js @@ -658,6 +658,7 @@ return view.extend({ so.value('urltest', _('Separate URLTest')); for (let i in proxy_nodes) so.value(i, proxy_nodes[i]); + so.value('byedpi-out', _('ByeDPI')); so.rmempty = false; so.editable = true; diff --git a/htdocs/luci-static/resources/view/homeproxy/diagnostics.js b/htdocs/luci-static/resources/view/homeproxy/diagnostics.js index 38918ae..b8e80a9 100644 --- a/htdocs/luci-static/resources/view/homeproxy/diagnostics.js +++ b/htdocs/luci-static/resources/view/homeproxy/diagnostics.js @@ -124,9 +124,11 @@ function buildCoreSection(view) { dom.content(resultsEl, [ row(_('Hiddify-core'), statusBadge(ret.hiddify_installed, ret.hiddify_installed ? _('installed') : _('not found'))), row(_('sing-box'), statusBadge(ret.singbox_installed, ret.singbox_installed ? _('installed') : _('not found'))), + row(_('ByeDPI'), statusBadge(ret.byedpi_installed, ret.byedpi_installed ? _('installed') : _('not found'))), ret.binary ? row(_('Active binary'), E('code', {}, ret.binary)) : null, ret.version ? row(_('Version'), E('span', { 'class': 'diag-pre', 'style': 'max-height:5em' }, ret.version)) : null, row(_('Running'), statusBadge(ret.running, ret.running ? _('yes') + (ret.pid ? ' (PID ' + ret.pid + ')' : '') : _('no'))), + ret.byedpi_installed ? row(_('ByeDPI running'), statusBadge(ret.byedpi_running, ret.byedpi_running ? _('yes') + (ret.byedpi_pid ? ' (PID ' + ret.byedpi_pid + ')' : '') : _('no'))) : null, row(_('Listening ports'), E('div', {}, portLines)) ].filter(Boolean)); }); diff --git a/htdocs/luci-static/resources/view/homeproxy/node.js b/htdocs/luci-static/resources/view/homeproxy/node.js index fca97b0..8484013 100644 --- a/htdocs/luci-static/resources/view/homeproxy/node.js +++ b/htdocs/luci-static/resources/view/homeproxy/node.js @@ -2208,7 +2208,7 @@ return view.extend({ ss.addremove = false; o = ss.option(form.Flag, 'byedpi_udp_over_tcp', _('UDP over TCP'), - _('Wrap UDP traffic in TCP when routing to ciadpi.')); + _('Wrap UDP traffic in TCP when routing to ByeDPI.')); o.default = o.enabled; o.rmempty = false; @@ -2299,60 +2299,84 @@ return view.extend({ expect: { '': {} } }); + /* Reference to the Command-options option, set when it's created below. + * The preset dropdown drives this field through LuCI's own widget API. */ + let cmdOptsOpt = null; + function applyPreset(idx) { - if (BYEDPI_PRESETS[idx]) { + if (!BYEDPI_PRESETS[idx]) + return; + const val = BYEDPI_PRESETS[idx].args; + const widget = cmdOptsOpt ? cmdOptsOpt.getUIElement('config') : null; + if (widget) { + widget.setValue(val); + widget.node.dispatchEvent(new Event('change', { bubbles: true })); + } else { + /* fallback if the widget isn't registered yet */ const el = document.querySelector('[name*=".byedpi_cmd_opts"]'); if (el) { - el.value = BYEDPI_PRESETS[idx].args; - el.dispatchEvent(new Event('change')); + el.value = val; + el.dispatchEvent(new Event('change', { bubbles: true })); } } } - /* Preset selector with optgroup categories */ - o = ss.option(form.ListValue, 'byedpi_preset', - _('Strategy preset'), - _('Grouped by technique. Based on hufrea/byedpi and community testing. See also fatyzzz/Byedpi-Setup.')); - o.value('', _('— select a preset —')); - for (let i = 0; i < BYEDPI_PRESETS.length; i++) - o.value(String(i), BYEDPI_PRESETS[i].name); - o.renderWidget = function(section_id, option_index, cfgvalue) { + /* Preset selector — DummyValue so LuCI never tracks or resets it. + * It's a picker that fills byedpi_cmd_opts, not a stored value. On every + * render it pre-selects whichever preset matches the current options, so it + * reflects truth and survives section re-renders. */ + o = ss.option(form.DummyValue, '_byedpi_preset', _('Strategy preset')); + o.render = function(option_index, section_id) { + const cur = (uci.get('homeproxy', 'config', 'byedpi_cmd_opts') || '').trim(); + let curIdx = -1; + for (let i = 0; i < BYEDPI_PRESETS.length; i++) + if (BYEDPI_PRESETS[i].args === cur) { curIdx = i; break; } + const sel = E('select', { - 'id': this.cbid(section_id), - 'name': this.cbid(section_id), 'class': 'cbi-input-select', 'style': 'max-width:100%' }); - sel.appendChild(E('option', { value: '' }, _('— select a preset —'))); + sel.appendChild(E('option', { + value: '', + selected: curIdx < 0 ? '' : null + }, curIdx < 0 ? _('— custom / select a preset —') : _('— select a preset —'))); for (let g of BYEDPI_GROUPS) { const grp = E('optgroup', { label: g.label }); for (let i = g.range[0]; i <= g.range[1]; i++) { grp.appendChild(E('option', { - value: String(i), - selected: cfgvalue === String(i) ? '' : null + value: String(i), + selected: i === curIdx ? '' : null }, BYEDPI_PRESETS[i].name)); } sel.appendChild(grp); } sel.addEventListener('change', function() { - applyPreset(parseInt(sel.value)); + if (sel.value !== '') + applyPreset(parseInt(sel.value)); }); - return sel; + return E('div', { 'class': 'cbi-value' }, [ + E('label', { 'class': 'cbi-value-title' }, _('Strategy preset')), + E('div', { 'class': 'cbi-value-field' }, [ + sel, + E('div', { 'class': 'cbi-value-description' }, + _('Grouped by technique. Based on hufrea/byedpi and community testing. See also fatyzzz/Byedpi-Setup.')) + ]) + ]); }; + o.write = function() {}; - /* Command options — the actual value used by ciadpi */ o = ss.option(form.Value, 'byedpi_cmd_opts', _('Command options'), - _('Arguments passed to ciadpi after -i 127.0.0.1 -p <port>. ' + - 'Select a preset above or enter custom options. See ciadpi --help for full flag reference.')); + _('Arguments passed to ByeDPI. Select a preset above or enter custom options. See ciadpi --help for full flag reference.')); o.placeholder = '--disorder 1'; + cmdOptsOpt = o; /* Strategy tester */ o = ss.option(form.DummyValue, '_byedpi_tester', _('Test current strategy'), - _('Starts ciadpi with the current options on a temporary port and tests it. ' + - 'With curl installed: makes a real HTTP request through ciadpi to verify DPI bypass. ' + - 'Without curl: only confirms arguments are valid and ciadpi starts.')); + _('Starts ByeDPI with the current options on a temporary port and tests it. ' + + 'With curl installed: makes a real HTTP request through ByeDPI to verify DPI bypass. ' + + 'Without curl: only confirms arguments are valid and ByeDPI starts.')); o.render = function(option_index, section_id) { const msgEl = E('span', { style: 'margin-left:8px; font-size:0.9em; color:gray' }, ''); const btn = E('button', { @@ -2363,13 +2387,13 @@ return view.extend({ btn.disabled = true; msgEl.style.color = 'gray'; msgEl.textContent = _('Testing...'); - return L.resolveDefault(callByeDPITest(opts, 15335), {}).then((ret) => { + return L.resolveDefault(callByeDPITest(opts, '15335'), {}).then((ret) => { btn.disabled = false; if (ret.result) { msgEl.style.color = 'green'; msgEl.textContent = ret.method === 'curl' - ? _('✓ HTTP request succeeded through ciadpi') - : _('✓ ciadpi started (install curl for full test)'); + ? _('✓ HTTP request succeeded through ByeDPI') + : _('✓ ByeDPI started (install curl for full test)'); } else { msgEl.style.color = 'red'; msgEl.textContent = ret.error || _('Test failed'); @@ -2450,7 +2474,7 @@ return view.extend({ ': ' + BYEDPI_PRESETS[idx].name; statusCell.textContent = '⏳'; return L.resolveDefault( - callByeDPITest(BYEDPI_PRESETS[idx].args, 15335), {} + callByeDPITest(BYEDPI_PRESETS[idx].args, '15335'), {} ).then(function(ret) { if (ret.result) { statusCell.textContent = '✓'; diff --git a/root/etc/homeproxy/scripts/firewall_post.ut b/root/etc/homeproxy/scripts/firewall_post.ut index 04a0f6d..83b714e 100755 --- a/root/etc/homeproxy/scripts/firewall_post.ut +++ b/root/etc/homeproxy/scripts/firewall_post.ut @@ -79,6 +79,12 @@ let self_mark, redirect_port, tproxy_port, tproxy_mark, tun_name, tun_mark; +/* ByeDPI runs as root (needed for TCP_MD5SIG etc.) but under a dedicated group, so its + * egress can be excluded from the redirect by socket group (skgid) to break the proxy + * loop — ciadpi has no fwmark option, and root traffic can't be told apart by skuid. */ +const byedpi_enabled = uci.get(cfgname, 'config', 'byedpi_enabled') || '0'; +const byedpi_gid = uci.get(cfgname, 'infra', 'byedpi_gid') || '8181'; + if (match(proxy_mode, /redirect/)) { self_mark = uci.get(cfgname, 'infra', 'self_mark') || '100'; redirect_port = uci.get(cfgname, 'infra', 'redirect_port') || '5331'; @@ -371,6 +377,9 @@ chain homeproxy_redirect { chain homeproxy_output_redir { type nat hook output priority filter -105; policy accept + {% if (byedpi_enabled === '1'): %} + meta skgid {{ byedpi_gid }} counter return + {% endif %} meta nfproto { {{ (ipv6_support === '1') ? 'ipv4, ipv6' : 'ipv4' }} } meta l4proto tcp jump homeproxy_redirect } {% endif %} @@ -495,6 +504,9 @@ chain homeproxy_mangle_prerouting { } chain homeproxy_mangle_output { + {% if (byedpi_enabled === '1'): %} + meta skgid {{ byedpi_gid }} counter return + {% endif %} meta mark {{ self_mark }} counter return ip daddr @homeproxy_wan_proxy_addr_v4 counter goto homeproxy_mangle_mark diff --git a/root/etc/homeproxy/scripts/generate_client.uc b/root/etc/homeproxy/scripts/generate_client.uc index 3f0c3f7..6a44f4c 100755 --- a/root/etc/homeproxy/scripts/generate_client.uc +++ b/root/etc/homeproxy/scripts/generate_client.uc @@ -550,7 +550,9 @@ config.dns = { if (!isEmpty(main_node)) { if (routing_mode === 'proxy_banned_ru') { - /* Russia mode: direct-default routing, russia-dns for all, secure-dns for proxy lists */ + /* Russia mode: direct-default routing, russia-dns for all, secure-dns for proxy lists. + * secure-dns (Cloudflare DoH) is routed through main-out — including when main-out is + * ByeDPI: ByeDPI's desync carries the DoH connection, so DNS stays un-poisoned. */ push(config.dns.servers, { tag: 'russia-dns', detour: self_mark ? 'direct-out' : null, @@ -990,6 +992,20 @@ if (!isEmpty(main_node)) { idle_timeout: (strToInt(main_urltest_interval) > 1800) ? `${main_urltest_interval * 2}s` : null, }); urltest_nodes = main_urltest_nodes; + } else if (main_node === 'byedpi-out') { + /* ByeDPI as main node: route through the local ByeDPI socks proxy. + * byedpi-out is a synthetic tag, not a real node section, so build the + * socks outbound here. Fall back to direct if ByeDPI is disabled. */ + if (byedpi_enabled === '1') + push(config.outbounds, { + type: 'socks', + tag: 'main-out', + server: '127.0.0.1', + server_port: 5335, + udp_over_tcp: (uci.get(uciconfig, ucimain, 'byedpi_udp_over_tcp') !== '0') || null + }); + else + push(config.outbounds, { type: 'direct', tag: 'main-out' }); } else { const main_node_cfg = uci.get_all(uciconfig, main_node) || {}; if (main_node_cfg.type in ['wireguard', 'amneziawg']) { @@ -1015,6 +1031,18 @@ if (!isEmpty(main_node)) { idle_timeout: (strToInt(main_udp_urltest_interval) > 1800) ? `${main_udp_urltest_interval * 2}s` : null, }); urltest_nodes = [...urltest_nodes, ...filter(main_udp_urltest_nodes, (l) => !~index(urltest_nodes, l))]; + } else if (dedicated_udp_node && main_udp_node === 'byedpi-out') { + /* ByeDPI as dedicated UDP node — same synthetic-tag handling as above */ + if (byedpi_enabled === '1') + push(config.outbounds, { + type: 'socks', + tag: 'main-udp-out', + server: '127.0.0.1', + server_port: 5335, + udp_over_tcp: (uci.get(uciconfig, ucimain, 'byedpi_udp_over_tcp') !== '0') || null + }); + else + push(config.outbounds, { type: 'direct', tag: 'main-udp-out' }); } else if (dedicated_udp_node) { const main_udp_node_cfg = uci.get_all(uciconfig, main_udp_node) || {}; if (main_udp_node_cfg.type in ['wireguard', 'amneziawg']) { @@ -1335,11 +1363,19 @@ if (!isEmpty(main_node)) { for (let cfg in ru_rules) { - /* 'main-out' means route directly through the main proxy without a separate outbound */ - const effective_outbound = (cfg.node === 'main-out') ? 'main-out' : ('hp-ru-' + cfg.source + '-out'); + /* 'main-out' routes through the main proxy; 'byedpi-out' through the shared ByeDPI + * socks outbound (already created when ByeDPI is enabled). Both reuse an existing + * outbound, so no per-source outbound is generated. */ + let effective_outbound; + if (cfg.node === 'main-out') + effective_outbound = 'main-out'; + else if (cfg.node === 'byedpi-out') + effective_outbound = (byedpi_enabled === '1') ? 'byedpi-out' : 'direct-out'; + else + effective_outbound = 'hp-ru-' + cfg.source + '-out'; - if (cfg.node === 'main-out') { - /* no new outbound needed */ + if (cfg.node === 'main-out' || cfg.node === 'byedpi-out') { + /* no new outbound needed — main-out / byedpi-out already exist */ } else if (!has_outbound(effective_outbound)) { if (cfg.node === 'urltest') { const ut_nodes = filter(cfg.urltest_nodes || [], (k) => uci.get_all(uciconfig, k) != null); diff --git a/root/etc/init.d/homeproxy b/root/etc/init.d/homeproxy index 1e96a72..1a1a36e 100755 --- a/root/etc/init.d/homeproxy +++ b/root/etc/init.d/homeproxy @@ -152,23 +152,36 @@ start_service() { echo > "$RUN_DIR/hiddify-c.log" # Setup firewall + # Strip CR: a CRLF-tainted template (e.g. transferred from Windows) would otherwise + # emit CRLF into the nft file, which nft rejects wholesale — silently dropping every + # homeproxy chain since fw4 loads all-or-nothing. ucode "$HP_DIR/scripts/firewall_pre.uc" - utpl -S "$HP_DIR/scripts/firewall_post.ut" > "$RUN_DIR/fw4_post.nft" + utpl -S "$HP_DIR/scripts/firewall_post.ut" | tr -d '\r' > "$RUN_DIR/fw4_post.nft" fw4 reload >"/dev/null" 2>&1 # ByeDPI - local byedpi_enabled byedpi_cmd_opts byedpi_block_quic + local byedpi_enabled byedpi_cmd_opts byedpi_block_quic byedpi_gid config_get_bool byedpi_enabled "config" "byedpi_enabled" "0" if [ "$byedpi_enabled" = "1" ] && [ -x "/usr/bin/ciadpi" ]; then config_get byedpi_cmd_opts "config" "byedpi_cmd_opts" "--disorder 1" + config_get byedpi_gid "infra" "byedpi_gid" "8181" /etc/init.d/ciadpi stop 2>/dev/null; /etc/init.d/ciadpi disable 2>/dev/null; true + # ByeDPI runs as root (needed for TCP_MD5SIG and other desync) but under a dedicated + # group, so its egress can be excluded from the proxy redirect by socket group (skgid). + # ciadpi has no fwmark option, and without this exclusion its outbound connections + # loop back through sing-box (conntrack table full / packet flood). + if ! grep -q "^byedpi:" /etc/group; then + echo "byedpi:x:${byedpi_gid}:" >> /etc/group + fi + procd_open_instance "byedpi" procd_set_param command /usr/bin/ciadpi -i 127.0.0.1 -p 5335 for _arg in $byedpi_cmd_opts; do procd_append_param command "$_arg" done + procd_set_param group "byedpi" procd_set_param respawn procd_close_instance diff --git a/root/usr/share/rpcd/ucode/luci.homeproxy b/root/usr/share/rpcd/ucode/luci.homeproxy index c104ae2..0dda79c 100644 --- a/root/usr/share/rpcd/ucode/luci.homeproxy +++ b/root/usr/share/rpcd/ucode/luci.homeproxy @@ -500,9 +500,23 @@ const methods = { version: null, running: false, pid: null, + byedpi_installed: !!access('/usr/bin/ciadpi'), + byedpi_running: false, + byedpi_pid: null, listen_ports: [] }; + /* ByeDPI (ciadpi) runs independently of the core, so detect it regardless. + * Use pidof, not `pgrep -x` — BusyBox pgrep -x matches argv0 (/usr/bin/ciadpi), + * not the bare name, so it misses the process. */ + let bfd = popen('pidof ciadpi 2>/dev/null'); + if (bfd) { + const bpid = trim(bfd.read('all')); + bfd.close(); + result.byedpi_running = length(bpid) > 0; + result.byedpi_pid = length(bpid) > 0 ? bpid : null; + } + if (!binary) return result; @@ -521,7 +535,7 @@ const methods = { if (fd) { const ports = []; for (let line = fd.read('line'); length(line); line = fd.read('line')) { - if (match(line, /hiddify|sing.box/)) + if (match(line, /hiddify|sing.box|ciadpi/)) push(ports, trim(line)); } fd.close(); @@ -692,6 +706,16 @@ const methods = { } add(''); + add('## ByeDPI (ciadpi)'); + if (access('/usr/bin/ciadpi')) { + add('Installed: yes'); + fd = popen('pgrep -x ciadpi 2>/dev/null'); + if (fd) { const bpid = trim(fd.read('all')); fd.close(); add('PID: ' + (length(bpid) > 0 ? bpid : 'not running')); } + } else { + add('Installed: no'); + } + add(''); + add('## Config Stats'); const config_path = RUN_DIR + '/hiddify-c.json'; const stat = lstat(config_path); @@ -729,7 +753,7 @@ const methods = { fd = popen('netstat -tlnup 2>/dev/null'); if (fd) { for (let line = fd.read('line'); length(line); line = fd.read('line')) { - if (match(line, /hiddify|sing.box/)) + if (match(line, /hiddify|sing.box|ciadpi/)) add(trim(line)); } fd.close(); @@ -813,7 +837,7 @@ const methods = { } } let running = false; - const fd2 = popen('pgrep -x ciadpi 2>/dev/null'); + const fd2 = popen('pidof ciadpi 2>/dev/null'); if (fd2) { running = length(trim(fd2.read('all'))) > 0; fd2.close(); @@ -911,7 +935,7 @@ const methods = { if (!access('/usr/bin/ciadpi')) return { result: false, error: 'ciadpi not installed' }; const cmd_opts = req.args?.cmd_opts || '--disorder 1'; - const test_port = strToInt(req.args?.port) || 15335; + const test_port = req.args?.port || 15335; const test_url = req.args?.test_url || 'https://www.youtube.com'; const has_curl = !!access('/usr/bin/curl');