Skip to content

Commit 78afe59

Browse files
author
Theodikes
committed
Improve mask parsing algorithm, now allowed all subnets divisible by 4. Resolve #10
1 parent 64e901b commit 78afe59

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ Proxy server will stopped, all configuration files, firewalls, shedulers and so
4343

4444
**Command line arguments:**
4545

46-
- `-s` or `--subnet` - IPv6 [subnet](https://docs.netgate.com/pfsense/en/latest/network/ipv6/subnets.html), fully dedicated for your server. `16`, `32`, `48`, `64`, `80`, `96` or `112`, default `64`
46+
- `-s` or `--subnet` - IPv6 [subnet](https://docs.netgate.com/pfsense/en/latest/network/ipv6/subnets.html), fully allocated on your server. Any subnet divisible by 4 (for example, `48` or `56`), default `64`
4747
- `-c` or `--proxy-count` - The total number of proxies you want to have (from 1 to 10000)
4848
- `-t` or `--proxies-type` - Proxies type - `http` or `socks5`. Default `http`, if no value provided
4949
- `-u` or `--username` - All proxies auth login
50-
- `-p` or `--password` - All proxies auth password (if you specify neither username not password, proxy will run without authentification)
50+
- `-p` or `--password` - All proxies auth password (if you specify neither username not password, proxy will run without authentication)
5151
- `--random` - bool parameter without value, if used, each backconnect proxy will have random username and password, that will be written in backconnect proxies file (`-f` argument)
5252
- `--start-port` - backconnect IPv4 start port. If you create 1500 proxies and `start-port` is `20000`, and server external IPv4 is, e.g,`180.113.14.28` you can connect to proxies using `180.113.14.28:20000`, `180.113.14.28:20001` and so on until `180.113.14.28:21500`
5353
- `-r` or `--rotating-interval` - rotation interval of entire proxy pool in minutes. At the end of each interval, output (external IPv6) addresses of all proxies are changed and proxy server is restarted, which breaks existing connections for a few seconds. From 0 to 59, default value - `0` (rotating disabled)

ipv6-proxy-server.sh

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ function is_auth_used(){
9393
if [ -z $user ] && [ -z $password] && [ $use_random_auth = false ]; then false; return; else true; return; fi;
9494
}
9595
96-
function get_subnet_mask(){
97-
if [ -z $subnet_mask ]; then
98-
blocks_count=$((($subnet / 16) - 1));
99-
subnet_mask="$(ip -6 addr|awk '{print $2}'|grep -m1 -oP '^(?!fe80)([0-9a-fA-F]{1,4}:){'$blocks_count'}[0-9a-fA-F]{1,4}'|cut -d '/' -f1)";
100-
fi;
101-
102-
echo $subnet_mask;
103-
}
104-
10596
function check_startup_parameters(){
10697
# Check validity of user provided arguments
10798
re='^[0-9]+$'
@@ -125,8 +116,8 @@ function check_startup_parameters(){
125116
usage;
126117
fi;
127118
128-
if [ $(expr $subnet % 16) != 0 ]; then
129-
echo_log_err "Error: invalid value of '-s' (subnet) parameter";
119+
if [ $(expr $subnet % 4) != 0 ]; then
120+
echo_log_err "Error: invalid value of '-s' (subnet) parameter, must be divisible by 4";
130121
usage;
131122
fi;
132123
@@ -194,6 +185,45 @@ function create_random_string(){
194185
tr -dc A-Za-z0-9 </dev/urandom | head -c $1 ; echo ''
195186
}
196187

188+
function kill_3proxy(){
189+
ps -ef | awk '/[3]proxy/{print $2}' | while read -r pid; do
190+
kill $pid
191+
done;
192+
}
193+
194+
function remove_ipv6_addresses_from_iface(){
195+
if test -f $random_ipv6_list_file; then
196+
# Remove old ips from interface
197+
for ipv6_address in $(cat $random_ipv6_list_file); do ip -6 addr del $ipv6_address dev $interface_name; done;
198+
rm $random_ipv6_list_file;
199+
fi;
200+
}
201+
202+
function get_subnet_mask(){
203+
if [ -z $subnet_mask ]; then
204+
# If we parse addresses from iface and want to use lower subnets, we need to clean existing proxy from interface before parsing
205+
if is_proxyserver_running; then kill_3proxy; fi;
206+
if is_proxyserver_installed; then remove_ipv6_addresses_from_iface; fi;
207+
208+
full_blocks_count=$(($subnet / 16));
209+
# Full external ipv6 address, allocated to the interface
210+
ipv6=$(ip -6 addr | awk '{print $2}' | grep -m1 -oP '^(?!fe80)([0-9a-fA-F]{1,4}:)+[0-9a-fA-F]{1,4}' | cut -d '/' -f1);
211+
212+
subnet_mask=$(echo $ipv6 | grep -m1 -oP '^(?!fe80)([0-9a-fA-F]{1,4}:){'$(($full_blocks_count-1))'}[0-9a-fA-F]{1,4}');
213+
if [ $(expr $subnet % 16) -ne 0 ]; then
214+
# Get last "uncomplete" block: if we want /68 subnet, get block from 64 to 80
215+
block_part=$(echo $ipv6 | awk -v block=$(($full_blocks_count + 1)) -F ':' '{print $block}' | tr -d ' ');
216+
# Because leading zeros can be skipped in the block, we need to add them if needed
217+
while ((${#block_part} < 4)); do block_part="0$block_part"; done;
218+
# Get part of block needed for subnet mask: if we want /72 subnet, we get 2 symbols - (72 (subnet) - 64 (full 4 blocks)) / 4 (2^4) in one hex digit
219+
symbols_to_include=$(echo $block_part | head -c $(($(expr $subnet % 16) / 4)));
220+
subnet_mask="$subnet_mask:$symbols_to_include";
221+
fi;
222+
fi;
223+
224+
echo $subnet_mask;
225+
}
226+
197227
function delete_file_if_exists(){
198228
if test -f $1; then rm $1; fi;
199229
}
@@ -296,7 +326,7 @@ function configure_ipv6(){
296326
required_options=("conf.$interface_name.proxy_ndp" "conf.all.proxy_ndp" "conf.default.forwarding" "conf.all.forwarding" "ip_nonlocal_bind");
297327
for option in ${required_options[@]}; do
298328
full_option="net.ipv6.$option=1";
299-
if ! cat /etc/sysctl.conf | grep -v "#" | grep $full_option; then echo $full_option >> /etc/sysctl.conf; fi;
329+
if ! cat /etc/sysctl.conf | grep -v "#" | grep -q $full_option; then echo $full_option >> /etc/sysctl.conf; fi;
300330
done;
301331
sysctl -p &>> $script_log_file;
302332

@@ -352,20 +382,6 @@ function generate_random_users_if_needed(){
352382
done;
353383
}
354384

355-
function kill_3proxy(){
356-
ps -ef | awk '/[3]proxy/{print $2}' | while read -r pid; do
357-
kill $pid
358-
done;
359-
}
360-
361-
function remove_ipv6_addresses_from_iface(){
362-
if test -f $random_ipv6_list_file; then
363-
# Remove old ips from interface
364-
for ipv6_address in $(cat $random_ipv6_list_file); do ip -6 addr del $ipv6_address dev $interface_name; done;
365-
rm $random_ipv6_list_file;
366-
fi;
367-
}
368-
369385
function create_startup_script(){
370386
delete_file_if_exists $startup_script_path;
371387

0 commit comments

Comments
 (0)