Skip to content

Instantly share code, notes, and snippets.

@divyam234
Created December 24, 2025 14:19
Show Gist options
  • Select an option

  • Save divyam234/65cf46cdb59d420e451bd3b0cc63395f to your computer and use it in GitHub Desktop.

Select an option

Save divyam234/65cf46cdb59d420e451bd3b0cc63395f to your computer and use it in GitHub Desktop.
Patch aria2c libaria2.so.0 to change default max-connections-per-server
#!/bin/bash
# Patch system libaria2.so.0 to change max-connections-per-server default value
# Tested with aria2 version 1.37.0
set -e
# Default value
DEFAULT_MAX_CONN=256
# Show usage
usage() {
echo "Usage: $0 [MAX_CONNECTIONS]"
echo ""
echo "Patch aria2c libaria2.so.0 to change default max-connections-per-server."
echo ""
echo "Arguments:"
echo " MAX_CONNECTIONS Number of connections (default: $DEFAULT_MAX_CONN)"
echo ""
echo "Example:"
echo " $0 # Patch to 256 connections"
echo " $0 128 # Patch to 128 connections"
echo " $0 512 # Patch to 512 connections"
exit 1
}
# Parse arguments
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
fi
MAX_CONN=${1:-$DEFAULT_MAX_CONN}
# Validate input
if ! [[ "$MAX_CONN" =~ ^[0-9]+$ ]]; then
echo "Error: MAX_CONNECTIONS must be a number"
exit 1
fi
# Check aria2c version
echo "Checking aria2c version..."
if ! command -v aria2c &> /dev/null; then
echo "Error: aria2c not found"
exit 1
fi
VERSION=$(aria2c --version | head -1 | awk '{print $3}')
TARGET_VERSION="1.37.0"
echo "Found aria2 version: $VERSION"
if [ "$VERSION" != "$TARGET_VERSION" ]; then
echo "Version mismatch expected $TARGET_VERSION found $VERSION"
exit
fi
# Find library path from ldd
LIB_LOC=$(ldd $(which aria2c) | grep libaria2 | awk '{print $3}')
if [ -z "$LIB_LOC" ]; then
echo "Error: Could not find libaria2 via ldd"
exit 1
fi
# Handle symlink or regular file
if [ -L "$LIB_LOC" ]; then
LIB_PATH=$(readlink -f "$LIB_LOC")
else
LIB_PATH="$LIB_LOC"
fi
echo "Library link: $LIB_LOC"
echo "Actual file: $LIB_PATH"
echo ""
# Patch offsets
OFFSET=0x1d4e96
EXPECTED="10000000"
# Check current value
CURRENT=$(xxd -s $OFFSET -l 4 "$LIB_PATH" | awk '{print $2$3}')
CURRENT_DEC=$((16#${CURRENT}))
echo "Current value: 0x$CURRENT ($CURRENT_DEC connections)"
echo "Expected value: 0x$EXPECTED (16 connections)"
if [ "$CURRENT_DEC" != 16 ]; then
echo "Warning: Current value is not 16, file may already be patched"
fi
# Convert new value to hex
NEW_HEX=$(printf '%08x' $MAX_CONN)
echo "Patching to: $MAX_CONN connections (0x$NEW_HEX)"
echo ""
# Copy to tmp
TEMP_LIB="/tmp/libaria2.so.patched"
echo "Copying library to /tmp..."
cp "$LIB_PATH" "$TEMP_LIB"
# Patch temp file with xxd
echo "Patching temp file..."
printf "1d4e96: ${NEW_HEX:0:2} ${NEW_HEX:2:2} ${NEW_HEX:4:2} ${NEW_HEX:6:2}\n" | xxd -r - "$TEMP_LIB"
# Verify temp file
NEW=$(xxd -s $OFFSET -l 4 "$TEMP_LIB" | awk '{print $2$3}')
echo "New value in temp file: 0x$NEW"
# Verify decimal matches
NEW_DEC=$((16#${NEW}))
if [ "$NEW_DEC" != "$MAX_CONN" ]; then
echo "Failed to patch temp file! Expected $MAX_CONN, got $NEW_DEC"
rm -f "$TEMP_LIB"
exit 1
fi
# Backup original
echo "Backing up original: $LIB_PATH.bak"
sudo cp "$LIB_PATH" "$LIB_PATH.bak"
# Copy patched file
echo "Copying patched file back..."
sudo cp "$TEMP_LIB" "$LIB_PATH"
# Set permissions
sudo chmod 644 "$LIB_PATH"
sudo chown root:root "$LIB_PATH"
# Verify
FINAL=$(xxd -s $OFFSET -l 4 "$LIB_PATH" | awk '{print $2$3}')
FINAL_DEC=$((16#${FINAL}))
if [ "$FINAL_DEC" = "$MAX_CONN" ]; then
echo ""
echo "Success! Changed from $CURRENT_DEC to $FINAL_DEC connections"
echo "Backup saved to: $LIB_PATH.bak"
rm -f "$TEMP_LIB"
else
echo "Failed! Restoring backup..."
sudo cp "$LIB_PATH.bak" "$LIB_PATH"
rm -f "$TEMP_LIB"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment