-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_cf_realip.sh
More file actions
136 lines (118 loc) · 3.11 KB
/
update_cf_realip.sh
File metadata and controls
136 lines (118 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 [-h] [-o output_file] [-v]"
echo "Options:"
echo " -h Show this help message"
echo " -o file Output file path (default: ./cf_real-ip.conf)"
echo " -v Verbose output"
echo
echo "Example: $0 -o /etc/nginx/conf.d/cf_real-ip.conf"
exit 1
}
# Function to log messages
log() {
local level=$1
shift
local msg="$@"
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
INFO)
[ "$VERBOSE" = true ] && echo "[$timestamp] INFO: $msg"
;;
WARN)
echo "[$timestamp] WARNING: $msg" >&2
;;
ERROR)
echo "[$timestamp] ERROR: $msg" >&2
;;
*)
echo "[$timestamp] $msg"
;;
esac
}
# Default values
OUTPUT_FILE="./cf_real-ip.conf"
VERBOSE=false
# Parse arguments
while getopts "ho:v" opt; do
case $opt in
h)
usage
;;
o)
OUTPUT_FILE="$OPTARG"
;;
v)
VERBOSE=true
;;
\?)
usage
;;
esac
done
shift $((OPTIND-1))
# Check if curl is installed
if ! command -v curl &>/dev/null; then
log ERROR "curl is not installed. Please install curl to proceed."
exit 1
fi
log INFO "Fetching Cloudflare IP ranges..."
log INFO "Output file: $OUTPUT_FILE"
# Create temporary file
TEMP_FILE=$(mktemp)
trap "rm -f $TEMP_FILE" EXIT
# Fetch IPv4 ranges
log INFO "Fetching IPv4 ranges..."
IPV4_RANGES=$(curl -s "https://www.cloudflare.com/ips-v4")
if [ $? -ne 0 ] || [ -z "$IPV4_RANGES" ]; then
log ERROR "Failed to fetch IPv4 ranges from Cloudflare"
exit 1
fi
# Fetch IPv6 ranges
log INFO "Fetching IPv6 ranges..."
IPV6_RANGES=$(curl -s "https://www.cloudflare.com/ips-v6")
if [ $? -ne 0 ] || [ -z "$IPV6_RANGES" ]; then
log ERROR "Failed to fetch IPv6 ranges from Cloudflare"
exit 1
fi
# Generate configuration file
log INFO "Generating configuration file..."
{
echo "$IPV4_RANGES" | while read -r ip; do
[ -n "$ip" ] && echo "set_real_ip_from $ip;"
done
echo "$IPV6_RANGES" | while read -r ip; do
[ -n "$ip" ] && echo "set_real_ip_from $ip;"
done
} > "$TEMP_FILE"
# Verify the file was created and has content
if [ ! -s "$TEMP_FILE" ]; then
log ERROR "Failed to generate configuration file"
exit 1
fi
# Create output directory if it doesn't exist
OUTPUT_DIR=$(dirname "$OUTPUT_FILE")
if [ ! -d "$OUTPUT_DIR" ]; then
log INFO "Creating output directory: $OUTPUT_DIR"
mkdir -p "$OUTPUT_DIR"
if [ $? -ne 0 ]; then
log ERROR "Failed to create output directory: $OUTPUT_DIR"
exit 1
fi
fi
# Move temp file to final location
mv "$TEMP_FILE" "$OUTPUT_FILE"
if [ $? -ne 0 ]; then
log ERROR "Failed to write to output file: $OUTPUT_FILE"
exit 1
fi
# Display results
IP_COUNT=$(wc -l < "$OUTPUT_FILE")
log INFO "Configuration file generated successfully!"
log INFO "Total IP ranges: $IP_COUNT"
if [ "$VERBOSE" = true ]; then
log INFO "Configuration file contents:"
cat "$OUTPUT_FILE"
fi
exit 0