Skip to content

Commit

Permalink
Merge pull request #279 from AltGrF13/vless-fixes.225
Browse files Browse the repository at this point in the history
[10+fixes] VLESS: подстройка цепочки и антипетель
  • Loading branch information
qzeleza authored Jan 8, 2025
2 parents f622003 + 456032b commit 3bb8cd1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion opt/bin/libs/debug
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ cmd_debug_iptables() {
output=$(ip route)
echo_debug 'Маршруты (подключитесь к гостевой заранее)' "${output}"

if is_proxy_enabled ; then
if is_shadowsocks_enabled ; then
title='Перенаправление в порт'
output="${CHAIN_DNAT_TO_PORT}"
else
Expand Down
19 changes: 9 additions & 10 deletions opt/bin/libs/main
Original file line number Diff line number Diff line change
Expand Up @@ -615,22 +615,21 @@ nets__get() {
echo "${find_interface}" | sed -n "s/${param_field}|${param_field}|${param_field}|${param_field}/${param_replace}/p"
}

# любой из видов proxy: хоть ShadowSocks, хоть VLESS
is_proxy_enabled() {
[ -f '/opt/etc/ndm/netfilter.d/100-proxy-redirect' ]
}

#ToDo: перейти везде на новые обёртки
has_ssr_enable() {
is_proxy_enabled
is_shadowsocks_enabled
}

is_shadowsocks_enabled() {
[ "$(get_config_value INFACE_CLI)" = 'shadowsocks' ]
local tunnel_keenetic_name
tunnel_keenetic_name=$(get_config_value 'INFACE_CLI')
[ "${tunnel_keenetic_name}" = 'shadowsocks' ]
}

is_vless_enabled() {
[ "$(get_config_value INFACE_ENT)" = "${PROXY_VLESS_NAME}" ]
is_vless_over_proxy_enabled() {
local tunnel_keenetic_name
tunnel_keenetic_name=$(get_config_value 'INFACE_CLI')
[ "${tunnel_keenetic_name}" = "${PROXY_VLESS_NAME}" ]
}


Expand Down Expand Up @@ -884,7 +883,7 @@ vpn__get_tunnel_addresses() {
get_tunnel_addresses() {
if is_shadowsocks_enabled ; then
echo $(get_from_json "${SHADOWSOCKS_CONF}" 'server')
elif is_vless_enabled ; then
elif is_vless_over_proxy_enabled ; then
echo $(get_from_json "${VLESS_CONFIG_FILE}" 'address' 'outbounds')
else
echo $(vpn__get_tunnel_addresses "$(get_config_value INFACE_CLI)")
Expand Down
43 changes: 24 additions & 19 deletions opt/etc/ndm/ndm
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,9 @@ ip4__dnat_to_port__create_chain() {
}

ip4__shadowsocks__create_chain() {
#local local_port=$(get_from_json "${VLESS_CONFIG_FILE}" 'port' 'inbounds')
local local_port=$(get_config_value SSR_DNS_PORT)
local local_port
local_port=$(get_config_value SSR_DNS_PORT)
#local_port=$(get_from_json "${VLESS_CONFIG_FILE}" 'port' 'inbounds')

ip4__dnat_to_port__create_chain "${TABLE_DNAT_TO_PORT}" "${CHAIN_DNAT_TO_PORT}" "${local_port}"
}
Expand All @@ -531,17 +532,25 @@ ip4__mark__create_chain() {
ip4__route__add_table
ip4__rule__add_mark_to_table

# выходим, если не новое соединение
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -m conntrack ! --ctstate NEW -j RETURN

# или маркируем соединение
#/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --set-mark ${MARK_NUM}

# или переносим маркер, проверяем и возвращаем обратно
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --restore-mark
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -m mark --mark ${MARK_NUM} -j RETURN
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j MARK --set-mark ${MARK_NUM}
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --save-mark
if is_vless_over_proxy_enabled ; then
# Теряет маркировку через раз, если не переносить для каждого пакета.
# Хотя домаркировки достаточно лишь для новых.
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --restore-mark
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -m mark --mark ${MARK_NUM} -j RETURN
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -m conntrack --ctstate NEW -j MARK --set-mark ${MARK_NUM}
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --save-mark
else
# Оптимизация: выходим сразу, если не новое соединение
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -m conntrack ! --ctstate NEW -j RETURN

# Нужно ли проверять наличие в новом соединении?
# Если нет, то можно схлопнуть до 2 правил
#/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --set-mark ${MARK_NUM}
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --restore-mark
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -m mark --mark ${MARK_NUM} -j RETURN
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j MARK --set-mark ${MARK_NUM}
/opt/sbin/iptables -w -t "${TABLE_MARK}" -A "${CHAIN_MARK}" -j CONNMARK --save-mark
fi
} &>/dev/null || error "[${FUNCNAME}] Возникли ошибки во время маркировки трафика для VPN"
}

Expand Down Expand Up @@ -595,9 +604,9 @@ ip4__shadowsocks__add_routing_for_home() {
ip4__add_routing_for_home "${TABLE_DNAT_TO_PORT}" "${CHAIN_DNAT_TO_PORT}" 'ShadowSocks'
}

# когда ускорение подключено
# VPN, VLESS
ip4__mark__add_routing_for_home() {
ip4__add_routing_for_home "${TABLE_MARK}" "${CHAIN_MARK}" 'VPN'
ip4__add_routing_for_home "${TABLE_MARK}" "${CHAIN_MARK}" 'сетевого интерфейса'
}


Expand Down Expand Up @@ -1004,8 +1013,6 @@ ip4__delete_routing_for_ip_from_config() {
ip4_firewall_flush_vpn_rules() {
{
ip4__delete_routing_by_list_for_net_from_config
# Для отключенного ускорения не сделает ничего, пока
# оно не будет переписано на цепочки
ip4__delete_routing_for_ip_from_config

# достаточно удалять правила для br0, но на всякий случай
Expand Down Expand Up @@ -1062,8 +1069,6 @@ ip4__flush() {
ip4__delete_routing_by_list_for_net_from_config
fi
if echo "${parts}" | grep -Fq ' ip ' ; then
# Для отключенного ускорения не сделает ничего, пока
# оно не будет переписано на цепочки
ip4__delete_routing_for_ip_from_config
fi

Expand Down

0 comments on commit 3bb8cd1

Please sign in to comment.