Skip to content

Instantly share code, notes, and snippets.

@PawanOsman
Created August 3, 2023 07:16
Show Gist options
  • Save PawanOsman/a12d8ed1c31eb0c8d023c8318817273c to your computer and use it in GitHub Desktop.
Save PawanOsman/a12d8ed1c31eb0c8d023c8318817273c to your computer and use it in GitHub Desktop.
This Gist contains instructions for configuring specific IP addresses and routing rules for network interfaces. It covers steps to validate IP connectivity, set up outgoing IP addresses, and update application code to manage source IPs for requests.

Step 1: Validate Routing to the Desired IP Address:

To ensure connectivity to your intended IP address, execute the following command to display network interface IP addresses:

ip addr show

In case the IP address is not visible under the relevant network interface, utilize the following command to add it:

sudo ip addr add IP_ADDRESS dev NETWORK_INTERFACE

Example:

sudo ip addr add 127.0.0.1 dev eth0

Step 2: Configuration of Outgoing IP Addresses:

  1. Identify the network interfaces associated with each dedicated IP address. You can utilize the 'ip addr' or 'ifconfig' command to enumerate your network interfaces.

  2. Establish policy routing rules to direct traffic from individual Nest applications through their corresponding interfaces. Employ the 'ip rule' command for this purpose.

  3. Revise your Nest application code to guarantee that each application dispatches requests from the appropriate source IP address.

Below is a simplified illustration using the ip command:

# Generate distinct routing tables for each IP address
echo "1 myapp1" >> /etc/iproute2/rt_tables
echo "2 myapp2" >> /etc/iproute2/rt_tables

# Configure routes for each application
ip route add your.app.1.ip dev eth0 table myapp1
ip route add your.app.2.ip dev eth1 table myapp2

# Set up routing regulations
ip rule add from your.app.1.ip table myapp1
ip rule add from your.app.2.ip table myapp2

Step 3: Application Code Update:

Within your application code, explicitly specify the local IP address to employ as the source for outgoing requests. This is typically accomplished by configuring your HTTP client (e.g., Axios) to bind to the specific local IP address linked with the application. Here's an example employing Axios:

import axios from "axios";
import http from "http";
import https from "https";

const options = { localAddress: 'some.ip.add.ress' } // The desired IP address for the HTTP request
const httpAgent = new http.Agent(options)
const httpsAgent = new https.Agent(options)

let response = await axios.get('https://api.ipify.org', { httpAgent, httpsAgent });

console.log(response.data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment