# UDP Forwarder

A simple Node.js application that listens to UDP packets on a given port and forwards them to one or more destinations defined in a settings file.

## Features

- Listen on a configurable UDP port
- Forward packets to multiple destinations simultaneously
- Load configuration from a JSON settings file
- Optional labels for destinations
- Logging of all forwarded packets
- Graceful shutdown handling (Ctrl+C)

## Installation

```bash
npm install
```

## Configuration

Edit `settings.json` to configure the forwarder:

```json
{
  "listenPort": 5000,
  "listenAddress": "0.0.0.0",
  "destinations": [
    {
      "host": "192.168.1.100",
      "port": 5001,
      "label": "Primary Destination"
    },
    {
      "host": "192.168.1.101",
      "port": 5002,
      "label": "Secondary Destination"
    }
  ]
}
```

### Settings Properties

- **listenPort** (required): The UDP port to listen on
- **listenAddress** (optional): The address to bind to, defaults to `0.0.0.0` (all interfaces)
- **destinations** (required): Array of forwarding destinations
  - **host** (required): IP address or hostname of the destination
  - **port** (required): Port number on the destination
  - **label** (optional): Friendly name for the destination

## Usage

### Start the forwarder with default settings

```bash
npm start
```

Or directly:

```bash
node server.js
```

### Use a custom settings file

```bash
node server.js path/to/custom-settings.json
```

### Development mode with auto-reload

```bash
npm run dev
```

## Example

To test the forwarder locally:

1. Start the forwarder:
```bash
npm start
```

2. In another terminal, listen on a destination port (e.g., 5001):
```bash
ncat -u -l 127.0.0.1 5001
```

3. In another terminal, send a test packet:
```bash
echo "Hello UDP" | ncat -u 127.0.0.1 5000
```

You should see the message appear in the listener terminal.

## Output

The forwarder logs:
- All incoming packets (source, size)
- Successful forwards to each destination
- Any errors during forwarding
- Startup information with destination list

## Architecture

The application:
1. Loads the settings file on startup
2. Creates a UDP server listening on the configured port
3. For each incoming UDP packet:
   - Logs the packet details
   - Creates a temporary UDP client for each destination
   - Sends the packet to all destinations
   - Closes the client connection

## Notes

- Each packet is forwarded independently to all destinations
- If a destination is unreachable, an error is logged but forwarding continues to other destinations
- The forwarder preserves the original packet content
- UDP is connectionless, so no acknowledgment is guaranteed

## License

ISC
