diff --git a/docs/host-your-own.md b/docs/host-your-own.md index 4953b16..41ebe9b 100644 --- a/docs/host-your-own.md +++ b/docs/host-your-own.md @@ -35,6 +35,14 @@ Set options by using the following flags in the `docker run` command: ``` > Limits clients to 1000 requests per 5 min +##### IPv6 Localization +```bash +-e IPV6_LOCALIZE=4 +``` +> To enable Peer Discovery among IPv6 peers, you can specify a reduced number of segments of the client IPv6 address to be evaluated as the peer's IP. This can be especially useful when using Cloudflare as a proxy. +> +> The flag must be set to an **integer** between `1` and `7`. The number represents the number of IPv6 [hextets](https://en.wikipedia.org/wiki/IPv6#Address_representation) to match the client IP against. The most common value would be `4`, which will group peers within the same `/64` subnet. + ##### Websocket Fallback (for VPN) ```bash -e WS_FALLBACK=true @@ -200,6 +208,12 @@ $env:PORT=3010; npm start ``` > Specify the port PairDrop is running on. (Default: 3000) +#### IPv6 Localization +```bash +IPV6_LOCALIZE=4 +``` +> Truncate a portion of the client IPv6 address to make peers more discoverable. See [Options/Flags](#options--flags) above. + #### Specify STUN/TURN Server On Unix based systems ```bash diff --git a/index.js b/index.js index cca34c8..f9a9a34 100644 --- a/index.js +++ b/index.js @@ -96,6 +96,16 @@ if (debugMode) { console.log("DEBUG_MODE is active. To protect privacy, do not use in production.") } +if (process.env.IPV6_LOCALIZE) { + let ipv6_lcl = parseInt(process.env.IPV6_LOCALIZE); + if (!ipv6_lcl || !(0 < ipv6_lcl && ipv6_lcl < 8)) { + console.error("IPV6_LOCALIZE must be an integer between 1 and 7"); + return; + } else { + console.log("IPv6 client IPs will be localized to", ipv6_lcl, ipv6_lcl > 1 ? "segments" : "segment"); + } +} + app.use(function(req, res) { res.redirect('/'); }); @@ -516,11 +526,19 @@ class Peer { if (this.ip.substring(0,7) === "::ffff:") this.ip = this.ip.substring(7); + let ipv6_was_localized = false; + if (ipv6_lcl && this.ip.includes(':')) { + this.ip = this.ip.split(':',ipv6_lcl).join(':'); + ipv6_was_localized = true; + } + if (debugMode) { console.debug("----DEBUGGING-PEER-IP-START----"); console.debug("remoteAddress:", request.connection.remoteAddress); console.debug("x-forwarded-for:", request.headers['x-forwarded-for']); console.debug("cf-connecting-ip:", request.headers['cf-connecting-ip']); + if (ipv6_was_localized) + console.debug("IPv6 client IP was localized to", ipv6_lcl, ipv6_lcl > 1 ? "segments" : "segment"); console.debug("PairDrop uses:", this.ip); console.debug("IP is private:", this.ipIsPrivate(this.ip)); console.debug("if IP is private, '127.0.0.1' is used instead");