@@ -29,6 +29,19 @@ LOCK_FILE="/tmp/lock/openclash.lock"
29
29
PROXY_FWMARK=" 0x162"
30
30
PROXY_ROUTE_TABLE=" 0x162"
31
31
32
+ # Assume we have bpftool sets would set ebpf marker to 1
33
+ KERNEL_EBPF_SUPPORT=$( bpftool version > /dev/null 2>&1 && echo ' 1' || echo ' 0' )
34
+
35
+ if [[ " ${KERNEL_EBPF_SUPPORT} " == " 1" ]]; then
36
+ # Enhanced capabilities
37
+ capabilties=" cap_sys_resource,cap_dac_override,cap_net_raw,cap_net_bind_service,cap_net_admin,cap_sys_ptrace,cap_sys_admin,CAP_PERFMON,cap_bpf"
38
+ else
39
+ # Regular capabilities
40
+ capabilties=" cap_sys_resource,cap_dac_override,cap_net_raw,cap_net_bind_service,cap_net_admin,cap_sys_ptrace,cap_sys_admin"
41
+ fi
42
+
43
+
44
+
32
45
set_lock () {
33
46
exec 888> " $LOCK_FILE " 2> /dev/null
34
47
flock -x 888 2> /dev/null
@@ -793,7 +806,6 @@ start_run_core()
793
806
chmod o+w /tmp/openclash.log 2> /dev/null
794
807
chown nobody:nogroup /etc/openclash/core/* 2> /dev/null
795
808
# 使用nobody启动内核方便代理路由自身流量
796
- capabilties=" cap_sys_resource,cap_dac_override,cap_net_raw,cap_net_bind_service,cap_net_admin,cap_sys_ptrace"
797
809
capsh --caps=" ${capabilties} +eip" -- -c " capsh --user=nobody --addamb='${capabilties} ' -- -c 'nohup $CLASH -d $CLASH_CONFIG -f \" $CONFIG_FILE \" >> $LOG_FILE 2>&1 &'" >> $LOG_FILE 2>&1
798
810
fi
799
811
uci -q set openclash.config.config_reload=1
@@ -1432,6 +1444,7 @@ if [ "$ipv6_enable" -eq 1 ]; then
1432
1444
esac
1433
1445
fi
1434
1446
1447
+ if [[ " ${ebpf_int_name} " == " 0" ]]; then
1435
1448
# NFTABLES
1436
1449
if [ -n " $FW4 " ]; then
1437
1450
LOG_OUT " Tip: Firewall4 was Detected, Use NFTABLE Rules..."
@@ -2060,10 +2073,7 @@ if [ -n "$FW4" ]; then
2060
2073
fi
2061
2074
nft add rule inet fw4 openclash_v6 meta nfproto {ipv6} tcp dport { 0-65535 } counter redirect to " $proxy_port "
2062
2075
nft ' add rule inet fw4 dstnat meta nfproto {ipv6} tcp dport { 0-65535 } counter jump openclash_v6'
2063
- fi
2064
2076
2065
- # tproxy not support output chain
2066
- if [ " $ipv6_mode " -eq 0 ] || [ " $ipv6_mode " -eq 1 ]; then
2067
2077
if [ " $router_self_proxy " = " 1" ]; then
2068
2078
nft ' add chain inet fw4 openclash_output_v6'
2069
2079
nft ' flush chain inet fw4 openclash_output_v6'
@@ -2081,12 +2091,18 @@ if [ -n "$FW4" ]; then
2081
2091
nft ' add rule inet fw4 openclash_output_v6 skuid != 65534 ip6 daddr @china_ip6_route counter return'
2082
2092
fi
2083
2093
fi
2084
- nft add rule inet fw4 openclash_output_v6 meta nfproto {ipv6} skuid ! = 65534 tcp dport { 0-65535 } counter redirect to " $proxy_port "
2094
+
2095
+ if [ " $ipv6_mode " -eq 2 ]; then
2096
+ nft add rule inet fw4 openclash_output_v6 meta nfproto {ipv6} skuid ! = 65534 tcp dport { 0-65535 } mark set " $PROXY_FWMARK " counter
2097
+ elif [ " $ipv6_mode " -eq 0 ]; then
2098
+ nft add rule inet fw4 openclash_output_v6 meta nfproto {ipv6} skuid ! = 65534 tcp dport { 0-65535 } mark set " $PROXY_FWMARK " tproxy ip6 to :" $tproxy_port " counter accept comment \" OpenClash TCP Tproxy\"
2099
+ fi
2100
+
2085
2101
nft ' add chain inet fw4 nat_output { type nat hook output priority -1; }'
2086
- nft ' add rule inet fw4 nat_output meta nfproto {ipv6} counter jump openclash_output_v6'
2102
+ nft ' add rule inet fw4 nat_output meta nfproto {ipv6} ip protocol tcp counter jump openclash_output_v6'
2087
2103
fi
2088
2104
fi
2089
-
2105
+
2090
2106
if [ " $enable_v6_udp_proxy " -eq 1 ] || [ " $ipv6_mode " -eq 0 ] || [ " $ipv6_mode " -eq 2 ]; then
2091
2107
nft ' add chain inet fw4 openclash_mangle_v6'
2092
2108
nft ' flush chain inet fw4 openclash_mangle_v6'
@@ -2127,26 +2143,28 @@ if [ -n "$FW4" ]; then
2127
2143
fi
2128
2144
2129
2145
if [ " $router_self_proxy " = " 1" ]; then
2130
- if [ " $ipv6_mode " -eq 2 ]; then
2131
- nft ' add chain inet fw4 openclash_mangle_output_v6'
2132
- nft ' flush chain inet fw4 openclash_mangle_output_v6'
2133
- nft ' add rule inet fw4 openclash_mangle_output_v6 ip6 daddr @localnetwork6 counter return'
2134
- nft ' add rule inet fw4 openclash_mangle_output_v6 ip6 saddr @localnetwork6 meta nfproto {ipv6} sport @lan_ac_black_ports counter return'
2135
- nft ' add rule inet fw4 openclash_mangle_output_v6 skuid != 65534 ip6 daddr @wan_ac_black_ipv6s counter return'
2136
- if [ " $en_mode " = " redir-host" ]; then
2137
- nft ' add rule inet fw4 openclash_mangle_output_v6 meta nfproto {ipv6} th dport != @common_ports skuid != 65534 counter return'
2138
- fi
2146
+ nft ' add chain inet fw4 openclash_mangle_output_v6'
2147
+ nft ' flush chain inet fw4 openclash_mangle_output_v6'
2148
+ nft ' add rule inet fw4 openclash_mangle_output_v6 ip6 daddr @localnetwork6 counter return'
2149
+ nft ' add rule inet fw4 openclash_mangle_output_v6 ip6 saddr @localnetwork6 meta nfproto {ipv6} sport @lan_ac_black_ports counter return'
2150
+ nft ' add rule inet fw4 openclash_mangle_output_v6 skuid != 65534 ip6 daddr @wan_ac_black_ipv6s counter return'
2151
+ if [ " $en_mode " = " redir-host" ]; then
2152
+ nft ' add rule inet fw4 openclash_mangle_output_v6 meta nfproto {ipv6} th dport != @common_ports skuid != 65534 counter return'
2153
+ fi
2139
2154
2140
- if [ " $china_ip6_route " = " 1" ]; then
2141
- if [ " $enable_redirect_dns " != " 2" ]; then
2142
- nft ' add rule inet fw4 openclash_mangle_output_v6 skuid != 65534 ip6 daddr @china_ip6_route ip6 daddr != @china_ip6_route_pass counter return'
2143
- else
2144
- nft ' add rule inet fw4 openclash_mangle_output_v6 skuid != 65534 ip6 daddr @china_ip6_route counter return'
2145
- fi
2155
+ if [ " $china_ip6_route " = " 1" ]; then
2156
+ if [ " $enable_redirect_dns " != " 2" ]; then
2157
+ nft ' add rule inet fw4 openclash_mangle_output_v6 skuid != 65534 ip6 daddr @china_ip6_route ip6 daddr != @china_ip6_route_pass counter return'
2158
+ else
2159
+ nft ' add rule inet fw4 openclash_mangle_output_v6 skuid != 65534 ip6 daddr @china_ip6_route counter return'
2146
2160
fi
2161
+ fi
2162
+ if [ " $ipv6_mode " -eq 2 ]; then
2147
2163
nft add rule inet fw4 openclash_mangle_output_v6 meta nfproto {ipv6} skuid ! = 65534 tcp dport { 0-65535 } mark set " $PROXY_FWMARK " counter
2148
- nft ' add rule inet fw4 mangle_output meta nfproto {ipv6} counter jump openclash_mangle_output_v6'
2164
+ elif [ " $ipv6_mode " -eq 0 ]; then
2165
+ nft add rule inet fw4 openclash_mangle_output_v6 meta nfproto {ipv6} skuid ! = 65534 tcp dport { 0-65535 } mark set " $PROXY_FWMARK " tproxy ip6 to :" $tproxy_port " counter accept comment \" OpenClash TCP Tproxy\"
2149
2166
fi
2167
+ nft ' add rule inet fw4 mangle_output meta nfproto {ipv6} counter jump openclash_mangle_output_v6'
2150
2168
fi
2151
2169
fi
2152
2170
@@ -2912,8 +2930,8 @@ if [ -z "$FW4" ]; then
2912
2930
ip6tables -t mangle -A openclash -p tcp -j MARK --set-mark " $PROXY_FWMARK "
2913
2931
fi
2914
2932
2915
- if [ " $router_self_proxy " = " 1 " ]; then
2916
- if [ " $ipv6_mode " -eq 2 ]; then
2933
+ if [ " $ipv6_mode " -eq 2 ]; then
2934
+ if [ " $router_self_proxy " = " 1 " ]; then
2917
2935
ip6tables -t mangle -N openclash_output
2918
2936
ip6tables -t mangle -F openclash_output
2919
2937
ip6tables -t mangle -A openclash_output -m set --match-set localnetwork6 dst -j RETURN
@@ -3021,6 +3039,10 @@ if [ -z "$FW4" ]; then
3021
3039
fi
3022
3040
fi
3023
3041
fi 2> /dev/null
3042
+ else
3043
+ LOG_OUT " Escaping firewall settings due to eBPF interface: ${ebpf_int_name} ..."
3044
+
3045
+ fi
3024
3046
3025
3047
# 端口转发
3026
3048
LOG_OUT " Tip: Start Add Port Bypassing Rules For Firewall Redirect and Firewall Rules..."
@@ -3148,11 +3170,21 @@ get_config()
3148
3170
fi
3149
3171
[ -z " $fakeip_range " ] && fakeip_range=" 198.18.0.1/16"
3150
3172
lan_interface_name=$( uci -q get openclash.config.lan_interface_name || echo " 0" )
3173
+
3151
3174
if [ " $lan_interface_name " = " 0" ]; then
3152
3175
lan_ip=$( uci -q get network.lan.ipaddr | awk -F ' /' ' {print $1}' 2> /dev/null || ip address show $( uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w " inet" 2> /dev/null | grep -Eo ' inet [0-9\.]+' | awk ' {print $2}' || ip addr show 2> /dev/null | grep -w ' inet' | grep ' global' | grep ' brd' | grep -Eo ' inet [0-9\.]+' | awk ' {print $2}' | head -n 1)
3153
3176
else
3154
3177
lan_ip=$( ip address show $lan_interface_name | grep -w " inet" 2> /dev/null | grep -Eo ' inet [0-9\.]+' | awk ' {print $2}' )
3155
3178
fi
3179
+
3180
+ # eBPF configuration
3181
+
3182
+ if [[ " $en_mode " == " redir-host" && " $en_mode_tun " == " 1" && " ${KERNEL_EBPF_SUPPORT} " == " 1" ]]; then
3183
+ ebpf_int_name=$( uci -q get openclash.config.ebpf_action_interface || echo " 0" )
3184
+ else
3185
+ ebpf_int_name=$( echo " 0" )
3186
+ fi
3187
+
3156
3188
wan_ip4s=$( /usr/share/openclash/openclash_get_network.lua " wanip" 2> /dev/null)
3157
3189
wan_ip6s=$( ifconfig | grep ' inet6 addr' | awk ' {print $3}' 2> /dev/null)
3158
3190
disable_masq_cache=$( uci -q get openclash.config.disable_masq_cache)
@@ -3220,7 +3252,7 @@ start()
3220
3252
if ! $quick_start ; then
3221
3253
LOG_OUT " Step 3: Modify The Config File..."
3222
3254
config_check
3223
- /usr/share/openclash/yml_change.sh 2> /dev/null " $en_mode " " $da_password " " $cn_port " " $proxy_port " " $TMP_CONFIG_FILE " " $ipv6_enable " " $http_port " " $socks_port " " $log_level " " $proxy_mode " " $en_mode_tun " " $stack_type " " $dns_port " " $mixed_port " " $tproxy_port " " $ipv6_dns " " $store_fakeip " " $stream_domains_prefetch " " $enable_meta_core " " $enable_meta_sniffer " " $enable_geoip_dat " " $geodata_loader " " $enable_meta_sniffer_custom " " $interface_name " " $enable_tcp_concurrent " " $core_type " " $append_default_dns " " $enable_meta_sniffer_pure_ip " " $find_process_mode " " $fakeip_range " " $global_client_fingerprint " " $ipv6_mode " " $stack_type_v6 " " $enable_unified_delay " " $keep_alive_interval " " $proxy_dns_group "
3255
+ /usr/share/openclash/yml_change.sh 2> /dev/null " $en_mode " " $da_password " " $cn_port " " $proxy_port " " $TMP_CONFIG_FILE " " $ipv6_enable " " $http_port " " $socks_port " " $log_level " " $proxy_mode " " $en_mode_tun " " $stack_type " " $dns_port " " $mixed_port " " $tproxy_port " " $ipv6_dns " " $store_fakeip " " $stream_domains_prefetch " " $enable_meta_core " " $enable_meta_sniffer " " $enable_geoip_dat " " $geodata_loader " " $enable_meta_sniffer_custom " " $interface_name " " $enable_tcp_concurrent " " $core_type " " $append_default_dns " " $enable_meta_sniffer_pure_ip " " $find_process_mode " " $fakeip_range " " $global_client_fingerprint " " $ipv6_mode " " $stack_type_v6 " " $enable_unified_delay " " $keep_alive_interval " " $proxy_dns_group " " $ebpf_int_name "
3224
3256
/usr/share/openclash/yml_rules_change.sh 2> /dev/null " $rule_source " " $enable_custom_clash_rules " " $TMP_CONFIG_FILE " " $enable_rule_proxy " " $CONFIG_NAME " " $router_self_proxy " " $lan_ip " " $proxy_port " " $tproxy_port " " $enable_meta_core " " $enable_redirect_dns " " $fakeip_range " " $en_mode "
3225
3257
/usr/share/openclash/openclash_custom_domain_dns.sh > /dev/null 2>&1
3226
3258
# Custom overwrite
0 commit comments