Skip to content

Commit

Permalink
feat: 🎸 anthropic support
Browse files Browse the repository at this point in the history
  • Loading branch information
monlor committed Aug 26, 2024
1 parent 9893939 commit 9c32594
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions media-unlock/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ docker run -d --name media-unlock -e SNI_IP=x.x.x.x -p 53:53 monlor/media-unlock

`OPENAI_IPS`: 可选,OPENAI IP列表

`ANTHROPIC_IPS`: 可选,Claude IP列表

`TEST_INTERVAL`: IP可用性测试间隔,默认60
4 changes: 4 additions & 0 deletions media-unlock/anthropic.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
anthropic.com
claude.ai
servd-anthropic-website.b-cdn.net
claude.site
28 changes: 28 additions & 0 deletions media-unlock/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CONFIG_FILE=/etc/dnsmasq.conf
# Define a list of SNI_IPs, separated by commas
IFS=',' read -r -a MEDIA_IP_LIST <<< "${MEDIA_IPS}" # Split the string into an array
IFS=',' read -r -a OPENAI_IP_LIST <<< "${OPENAI_IPS:-}" # Split the string into an array
IFS=',' read -r -a ANTHROPIC_IP_LIST <<< "${ANTHROPIC_IPS:-}" # Split the string into an array

# Set the interval for testing (in seconds)
# Default to 60 seconds if not set
Expand All @@ -16,6 +17,7 @@ INTERVAL=${TEST_INTERVAL:-60}
# Variable to hold the current IP
current_media_ip=""
current_openai_ip=""
current_anthropic_ip=""

# Function to test the current IP address
test_current_ip() {
Expand Down Expand Up @@ -53,6 +55,18 @@ switch_openai_ip() {
echo "openai: No available IP found."
}

# Function to switch to the next available IP
switch_anthropic_ip() {
for ip in "${ANTHROPIC_IP_LIST[@]}"; do
if curl -s --connect-timeout 5 "http://$ip:80" > /dev/null; then
echo "anthropic: Switching to $ip."
current_anthropic_ip="$ip"
return 0
fi
done
echo "anthropic: No available IP found."
}

# Function to generate dnsmasq configuration for all domains using a specified IP
generate_dnsmasq_config() {
# Clear the existing configuration file
Expand Down Expand Up @@ -82,13 +96,20 @@ EOF
done < /tmp/openai.txt
fi

if [ -n "${current_anthropic_ip}" ]; then
while read -r domain; do
echo "address=/$domain/$current_anthropic_ip" >> "/tmp/dnsmasq.conf"
done < /tmp/anthropic.txt
fi

# Move the generated configuration to the actual dnsmasq configuration file
mv -f "/tmp/dnsmasq.conf" "$CONFIG_FILE"
}

# Initialize with the first available IP
switch_media_ip || exit 1
switch_openai_ip
switch_anthropic_ip

# Generate the initial dnsmasq configuration
generate_dnsmasq_config
Expand All @@ -112,6 +133,13 @@ while true; do
generate_dnsmasq_config
fi

if [ -n "${current_anthropic_ip}" ] && ! test_current_ip "${current_anthropic_ip}"; then
switch_anthropic_ip

# Update the dnsmasq configuration with the new reachable IP
generate_dnsmasq_config
fi

# Sleep for the specified interval before the next test
sleep "$INTERVAL"
done

0 comments on commit 9c32594

Please sign in to comment.