This project implements a firewall-resistant, peer-to-peer (P2P) DNS system that leverages UDP hole-punching to bypass common network restrictions, including firewalls, NAT, and CGNAT. Built in Node.js, it integrates with [Holesail](https://holesail.io) for tunneling, provides seamless DNS and HTTP proxying, and supports both public and private P2P networks.
```bash
[DEBUG 2024-10-14T21:04:20.875Z] Public DNS returned records for www.google.com
[DEBUG 2024-10-14T21:04:28.465Z] DNS query received: Domain = example.tld, Type = A
Virtual interface dummy2 created with CIDR 192.168.100.2.
[DEBUG 2024-10-14T21:04:28.527Z] Assigned virtual interface IP: 192.168.100.2 for domain: example.tld
Create a `.env` file in the root directory to set a custom discovery key, `masterNetworkDiscoveryKey`, for your private network.
```plaintext
masterNetworkDiscoveryKey=<your_custom_hex_key>
```
Replace `<your_custom_hex_key>` with a unique hexadecimal string. This key allows you to create a private, isolated P2P DNS network, so only peers with the same `masterNetworkDiscoveryKey` can discover each other.
To add a domain, use the `addDomain` function in the script, or add it programmatically. You’ll need to provide a unique connection hash for each domain, generated by Holesail.
#### Generating a Connection Hash
Use the following command to generate a live, publicly accessible connection hash from Holesail:
```bash
holesail --live 80 --public
```
This command will create a P2P tunnel on port 80 and output a connection hash. Example output:
To create an isolated, private P2P DNS network, you can configure the system with a custom `masterNetworkDiscoveryKey`. This key acts as a unique identifier for your private DNS network, so only peers using the same `masterNetworkDiscoveryKey` will be able to discover and connect to each other.
When defined, the code will use `masterNetworkDiscoveryKey` as the main discovery key, enabling only clients with this key to participate in the network. If `masterNetworkDiscoveryKey` is not set, the system defaults to using `dnsCore.discoveryKey`.
This configuration ensures that only authorized clients with matching keys can join your network, providing a private, secure DNS namespace.
### Starting Holesail Clients for Each Domain
The system will automatically start or reuse a Holesail client for each domain as requests come in, ensuring the connection stays alive and accessible over P2P.
The P2P DNS server listens on port 53 for DNS requests and automatically proxies HTTP requests on port 80. This means you can access domains in your network without needing direct IPs or proxy servers.
Example:
```bash
curl http://example.tld
```
The system will route the request through the P2P network to the correct local IP, based on the domain's connection hash and DNS record.
## Troubleshooting
- **Binding Issues on Port 53**: Run the DNS server with elevated permissions (`sudo`).
- **Connection Timeout**: Verify that the DNS core topic is correctly set and that the Holesail client can connect.
## Configuration Options
- **masterNetworkDiscoveryKey**: Used to create private, isolated DNS networks. Set this value in your `.env` file.
- **Port Configurations**: Adjust the DNS and HTTP server ports in `index.js` if needed.
- **Logging**: Add custom logs to track DNS queries and Holesail connections more granularly.
## Security Considerations
- **Environment Variables**: Keep `masterNetworkDiscoveryKey` and other sensitive configuration values in the `.env` file, which should be excluded from version control.
- **Firewall**: Restrict external access to the DNS server to maintain control over the network.
- **Key Rotation**: Periodically update `masterNetworkDiscoveryKey` to maintain network security.