How to configure squid proxy with IP rotation with cellular modem

USB Cellular Modem Proxy Server Setup Guide


This guide provides step-by-step instructions to configure a proxy server using USB cellular modems. The system ensures:

Overview

squid configuration
  1. Unique IP addresses per modem sourced from the modems.
  2. Automatic IP rotation via modem reboots when an IP is blocked.
  3. Port-based proxy management, with each modem assigned a unique port.
  4. Scalability to support up to 40 modems and multiple servers.

  1. System Requirements

Hardware

USB hub with 20–40 USB ports (or multiple hubs).

20–40 USB cellular modems.

A Linux server with at least 4 GB RAM and 2 CPU cores.

Software

Operating System: Ubuntu 20.04/22.04 or Debian-based distributions.

Required Software:

squid (proxy server)

usb-modeswitch (modem configuration)

wvdial or network-manager (managing modem connections)

uhubctl (USB port power control)


  1. Install Required Software

Update and install the required packages:

sudo apt update && sudo apt upgrade -y
sudo apt install squid usb-modeswitch wvdial net-tools network-manager uhubctl -y


  1. Configure Modems for Internet Access

Step 1: Verify Modem Connections

List all connected USB modems:

lsusb

Identify modem names and ports (e.g., /dev/ttyUSB0).

Step 2: Configure Modem with wvdial

For each modem, create a configuration file in /etc:

sudo nano /etc/wvdial-modem1.conf

Example configuration for Modem 1:

[Dialer Defaults]
Init1 = ATZ
Init2 = AT+CGDCONT=1,”IP”,””
Modem = /dev/ttyUSB0
Baud = 115200
Phone = *99#
Username = “”
Password = “”
Stupid Mode = 1

Repeat this step for each modem, adjusting the Modem value (e.g., /dev/ttyUSB1, /dev/ttyUSB2).

Step 3: Start Modem Connections

Run the wvdial command to connect each modem:

sudo wvdial -C /etc/wvdial-modem1.conf


  1. Configure SQUID Proxy

Step 1: Install SQUID

sudo apt install squid -y

Step 2: Configure SQUID

Edit the configuration file:

sudo nano /etc/squid/squid.conf

Add the following to define unique ports and assign outgoing IPs:

Define ports for each modem

http_port 8001
http_port 8002
http_port 8003

Continue for all modems…

Assign outgoing IPs for each port

acl modem1 localport 8001
tcp_outgoing_address 192.168.1.101 modem1

acl modem2 localport 8002
tcp_outgoing_address 192.168.1.102 modem2

acl modem3 localport 8003
tcp_outgoing_address 192.168.1.103 modem3

Repeat for all modems…

Save the file and restart SQUID:

sudo systemctl restart squid


  1. Automatic IP Rotation

Step 1: Install uhubctl

Use uhubctl to power cycle USB ports for modems:

sudo apt install uhubctl -y

Step 2: Test USB Port Control

List USB hubs and ports:

uhubctl

Turn off and on a specific port:

uhubctl -l 1-1 -p 2 -a off
sleep 5
uhubctl -l 1-1 -p 2 -a on

Step 3: Automate IP Rotation

Create a script to reset a modem (e.g., /usr/local/bin/reset_modem.sh):

!/bin/bash

MODEM_PORT=$1
uhubctl -l 1-1 -p $MODEM_PORT -a off
sleep 5
uhubctl -l 1-1 -p $MODEM_PORT -a on

Make it executable:

chmod +x /usr/local/bin/reset_modem.sh


  1. Manual IP Reset via Ports

To manually reset a modem, use netcat to create an HTTP-based trigger. For example, to listen on port 9001 for Modem 1:

while true; do echo -e “HTTP/1.1 200 OK\n\nResetting Modem 1” | nc -l -p 9001 -q 1; /usr/local/bin/reset_modem.sh 1; done

Repeat for other modems by changing the port and modem number.


  1. Scalability

Adding More Modems

  1. Connect the new modems to the USB hub.
  2. Add new wvdial configuration files (e.g., /etc/wvdial-modem21.conf).
  3. Add new ports and IPs in squid.conf:

http_port 8021
acl modem21 localport 8021
tcp_outgoing_address 192.168.1.121 modem21

  1. Restart SQUID:

sudo systemctl restart squid

Adding More Servers

  1. Set up another server using this guide.
  2. Use a load balancer (e.g., HAProxy) to distribute traffic across servers.

  1. Testing and Monitoring

Test Proxies

Verify each proxy’s IP using:

curl –proxy localhost:8001 http://ipinfo.io
curl –proxy localhost:8002 http://ipinfo.io

Monitor Modem Connections

Use the following commands to monitor modem IPs and status:

ifconfig
ip addr show


  1. Documentation for Scaling

Adding a New Modem

  1. Physically connect the modem.
  2. Configure /etc/wvdial-modemX.conf.
  3. Add the modem to /etc/squid/squid.conf.
  4. Restart the modem connection:

sudo wvdial -C /etc/wvdial-modemX.conf

  1. Restart SQUID:

sudo systemctl restart squid

Adding a New Server

  1. Set up the server using this guide.
  2. Add the server to your load balancer configuration.

This setup provides a scalable, port-based proxy server system with automatic IP rotation using USB cellular modems. It ensures high availability and allows easy expansion as more modems or servers are added. For any issues, review modem logs (/var/log/messages) and proxy logs (/var/log/squid/access.log).

Scroll to Top