Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI

on:
push:
pull_request:

permissions:
contents: read

jobs:
test:
name: Node ${{ matrix.node }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
node: [22, 24, 26]
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm run typecheck
- run: npm test
76 changes: 76 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Changelog

## 2.0.0 — 2026-11-19

A rewrite. The protocol is not compatible with 1.x: update all machines.

### Security

- **Messages are authenticated and encrypted with AES-256-GCM.** 1.x used AES-CTR without a MAC.
A man-in-the-middle could change text (for example `ls -la` into `rm -rf`) without the passphrase.
Any machine on the network could also forge clipboard text and terminal escape sequences:
it only had to take the keystream from the HELLO message, whose plaintext is known.
- **Mutual passphrase check before any data.** 1.x sent HELLO and every clipboard change to any
machine that connected. That also gave material for offline passphrase guessing.
- **Strong key derivation:** scrypt (128 MiB) once per connection.
1.x ran PBKDF2 with 10,000 iterations for every message, on the main thread.
- **Forward secrecy and replay protection:** ephemeral X25519 keys and per-direction message counters.
- **Strangers cannot make the hub work for free or lock out real clients:**
- the hub runs scrypt only after it receives a passphrase proof;
- connections without the passphrase are limited to 4 per address and 32 in total;
- unread input is limited by TCP flow control, so nobody can fill the hub's memory.
- **Online guessing is slowed down:** after a wrong passphrase, the hub blocks the address (IPv6: the /64):
1 s, then doubling up to 60 s. Parallel connections from that address are closed, so one address can
test at most 2 passphrases per block period.
- Handshake ERROR frames are not authenticated, so a "wrong passphrase" reply stops the client only when
you gave the hub address, and only after the client sent its proof.
- Peer-supplied names are cleaned of control characters before they are printed.

### Fixed

- **Any 4 bytes (`NetP`) from the network crashed the server or the client.** A crafted header did the same.
- **Text larger than one TCP segment arrived corrupted, or was lost.** The receiver assumed that every
`data` event starts with a whole fragment. On Wi-Fi/Ethernet a 3,000-character text arrived as 1,822
characters of garbage. The integrity check passed it, so the garbage went into the clipboard.
- **The client crashed on its first reconnect attempt:** `setTimeout(createConnection)` passed no host or port.
- **Leading and trailing whitespace was removed** from received text (`trim()`): indentation and final newlines were lost.
- **Texts above ~130 KB (~65,000 Cyrillic characters) were never sent:** the part counter was one byte.
- **Copies were lost or echoed back** because of the 2-second "ignore" window. A copy made within 2 s after a
received one was never sent. Two copies about 2 s apart were echoed back and could overwrite a newer copy.
One failed clipboard write blocked all later copies.
- **Every connection started another clipboard poller that never stopped.** After N reconnects, `pbpaste` ran N
times per second, and copies went to dead sockets with an "Ok." message.
- **The hub did not relay** text from one client to the other clients.
- **All clients shared one message assembler**, so fragments of different clients could mix.
- **The HELLO message was never processed** (it had no framing), so passphrase or version mismatches were not detected.
- **Passphrase input:**
- Backspace added a `\x7f` character, and arrow keys added the text `undefined`. A typo made the
passphrases on the two machines different, without an error message.
- Input from a pipe never finished.
- An empty passphrase was accepted.
- **Dead connections were not detected** after sleep or a network change; copies were lost with an "Ok." message.
- **Command line:**
- A port that was not a number silently became 8134.
- `--no-clipboard` before the host was taken as the host name.
- Arguments were checked only after the passphrase prompt.
- A busy port ended the program with exit code 0.
- On Linux under Wayland, clipboardy 4 used only `xsel` through XWayland.
- Unknown messages were printed in full, including clipboard text.

### New

- One command: `netpaste serve`, `netpaste connect [HOST[:PORT]]`, `netpaste discover`.
- Hub for many machines; `--no-clipboard` on the hub makes it a relay.
- mDNS discovery: `netpaste connect` without an address finds the hub.
- Passphrase from `--passphrase-file` or `NETPASTE_PASSPHRASE`, for autostart. Templates for launchd and systemd.
- Heartbeats, idle timeout, TCP keepalive, reconnect with exponential backoff and jitter.
- `--max-size` (default 10 MB, up to 64 MB), `--interval`, `--host`, `--no-mdns`, `--verbose`.
- The clipboard is read only while a peer is connected.
- Clear messages for a wrong passphrase, an old protocol version, a busy port, and a missing terminal.

### Internal

- TypeScript (strict), run directly by Node.js 22.18+ without a build step.
- `chalk` replaced by `util.styleText`; argument parsing with `util.parseArgs`; clipboardy 5 (native Wayland support).
- Tests with `node:test`: framing at every split point, crypto, handshake (including a man-in-the-middle),
clipboard races, passphrase input, CLI, and end-to-end hub tests. CI on Linux and macOS, Node.js 22/24/26.
199 changes: 166 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,193 @@
# netpaste

Netpaste is a utility crafted for developers who frequently work across Linux and macOS environments, providing a seamless method to share clipboard data between machines. Created out of necessity by a full-stack developer tired of the lack of simple tools for this task, **netpaste** offers an efficient, peer-to-peer clipboard sharing solution that's both secure and easy to use.
Encrypted clipboard sync between your machines on a local network.
Copy on one machine, paste on another. Works on macOS and Linux (X11 and Wayland).

## Features
- **Encrypted and authenticated.** Each machine proves that it knows the shared passphrase.
Messages use AES-256-GCM with new keys for every connection (X25519 + scrypt + HKDF).
A machine that has not proved the passphrase receives nothing.
- **One hub, many machines.** One machine runs `netpaste serve`, the others run `netpaste connect`.
The hub relays text between all clients. A headless machine can be a pure relay (`--no-clipboard`).
- **Finds the hub by itself.** `netpaste connect` without an address looks for the hub on the local network (mDNS).
- **Stays connected.** Heartbeats detect dead connections (laptop sleep, Wi-Fi change).
The client reconnects automatically.
- **Exact text.** Spaces, line endings and Unicode arrive unchanged. Up to 10 MB per copy (configurable).
- **No cloud, no account.** The traffic stays on your network.

- **Encryption:** All data shared between clients is encrypted.
- **Peer-to-Peer (P2P):** Directly shares clipboard data between machines.
- **Automatic Clipboard Monitoring:** Actively monitors your clipboard for changes.
## Requirements

## Installation
- Node.js 22.18 or newer. netpaste runs its TypeScript sources directly, without a build step.
- macOS: nothing else.
- Linux: `wl-clipboard` on Wayland, or `xsel` on X11 (a bundled `xsel` is used when it is missing).

Before you dive into using Netpaste, there are a few prerequisites and steps to follow:
## Install

### Dependencies
```bash
git clone https://github.com/leplik/netpaste.git
cd netpaste
npm install
npm install -g . # adds the `netpaste` command (a link to this folder)
```

Without the global install, use `node bin/netpaste.js` instead of `netpaste`.

- Node.js and npm (Node Package Manager)
## Use

### Steps
Use the same passphrase on all machines. Make it long: 4 or more random words, or 12+ characters.

1. Clone the Netpaste repository to your local machine.
```bash
git clone https://github.com/leplik/netpaste.git;
# On the hub (any machine that stays on):
netpaste serve

# On every other machine:
netpaste connect # finds the hub on the local network
netpaste connect 192.168.1.10 # or give the address (HOST or HOST:PORT)
```
2. Navigate into the Netpaste directory and run `npm install` to install the required Node.js dependencies.

```text
$ netpaste connect
Passphrase: ****************************
14:02:11 netpaste 2.0.0; clipboard sync is on
14:02:14 found server "studio" at 192.168.1.10:8134
14:02:14 connected to studio (192.168.1.10:8134)
14:02:31 → 1,204 chars sent to studio (192.168.1.10:8134)
14:03:05 ← 87 chars from studio (192.168.1.10:8134)
```

netpaste prints only the size of the text, never the text.
Copies are sent only while a connection is open; a copy made while disconnected is not sent later.

### Commands and options

| Command | What it does |
|---|---|
| `netpaste serve` | Start the hub. Listens on port 8134 and announces itself with mDNS. |
| `netpaste connect [HOST[:PORT]]` | Connect to the hub. Without HOST it finds the hub with mDNS. |
| `netpaste discover` | List the hubs on the local network. |

| Option | Default | Meaning |
|---|---|---|
| `-p, --port N` | 8134 | TCP port. |
| `--host ADDR` | all interfaces | `serve`: listen only on this address (the hub is then not announced with mDNS). |
| `--no-clipboard` | | Do not use the local clipboard. With `serve`: only relay between clients. |
| `--no-mdns` | | `serve`: do not announce the hub on the network. |
| `--passphrase-file F` | | Read the passphrase from file F. |
| `--interval MS` | 1000 | How often to check the clipboard. |
| `--max-size SIZE` | 10mb | Largest text to send or accept, from `1kb` to `64mb`. |
| `--timeout SEC` | 3 | `discover`: how long to search. |
| `--verbose` | | Show debug messages. |

### Passphrase without a prompt

netpaste takes the passphrase from the first source that is set:

1. `--passphrase-file FILE`: the content of the file (one final newline is removed);
2. the `NETPASTE_PASSPHRASE` environment variable;
3. a prompt on the terminal.

```bash
cd netpaste
npm install
mkdir -p ~/.config/netpaste
touch ~/.config/netpaste/passphrase && chmod 600 ~/.config/netpaste/passphrase
nano ~/.config/netpaste/passphrase # type the passphrase and save
netpaste connect --passphrase-file ~/.config/netpaste/passphrase
```
3. Allow Netpaste server through your firewall to ensure it can communicate over the network. This process varies by operating system:
- **Linux:** Use UFW or your distro's firewall settings to allow traffic on the default port (8134): `sudo ufw allow 8134`
- **macOS:** Go to `System Preferences > Security & Privacy > Firewall`, and add an exception for Netpaste.

## Usage
netpaste warns when other users can read the file.

### Firewall

Netpaste is comprised of two parts: the server and the client. Each machine should run its instance of the utility.
- **macOS:** when macOS asks whether `node` may accept incoming connections, allow it (only on the hub).
- **Linux:** open TCP 8134 on the hub, and UDP 5353 for mDNS, for example `sudo ufw allow 8134/tcp && sudo ufw allow 5353/udp`.

### Server
## Start automatically

Start the server on the machine that will be... server (no matters for copy-pasting process):
Both templates run `netpaste connect` (find the hub with mDNS) with a passphrase file.
For the hub, replace `connect` with `serve`. To connect to a fixed address, add it after `connect`.

### macOS (launchd)

```bash
node server.js [port]
cd netpaste
sed -e "s|__NODE__|$(command -v node)|" -e "s|__NETPASTE__|$PWD/bin/netpaste.js|" -e "s|__HOME__|$HOME|g" \
contrib/launchd/io.github.leplik.netpaste.plist > ~/Library/LaunchAgents/io.github.leplik.netpaste.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/io.github.leplik.netpaste.plist
tail -f ~/Library/Logs/netpaste.log
```
If no port is specified, Netpaste uses the default port 8134.

### Client
On other machine start the client:
Stop: `launchctl bootout gui/$(id -u)/io.github.leplik.netpaste`.
The template contains the full path to `node`. If you change the Node.js version (for example with nvm), install the template again.

Tip: to keep the passphrase in the Keychain instead of a file, store it with
`security add-generic-password -a "$USER" -s netpaste -w` and start netpaste through
`/bin/sh -c 'NETPASTE_PASSPHRASE="$(security find-generic-password -s netpaste -w)" exec …'`.

### Linux (systemd user service)

```bash
cd netpaste
mkdir -p ~/.config/systemd/user
sed -e "s|__NODE__|$(command -v node)|" -e "s|__NETPASTE__|$PWD/bin/netpaste.js|" \
contrib/systemd/netpaste.service > ~/.config/systemd/user/netpaste.service
systemctl --user daemon-reload
systemctl --user enable --now netpaste.service
journalctl --user -u netpaste -f
```

The service starts with the graphical session, because it needs the display for the clipboard.
If netpaste cannot read the clipboard, run
`systemctl --user import-environment DISPLAY WAYLAND_DISPLAY XAUTHORITY` and restart the service.
For a headless relay hub, use `serve --no-clipboard`, change `WantedBy=` to `default.target`,
and run `loginctl enable-linger $USER` so that the service runs without a login.

## Security

The full design is in [docs/PROTOCOL.md](docs/PROTOCOL.md). In short:

**Protects against:**

- eavesdroppers: they cannot read the text, and they cannot test passphrase guesses;
- changed, replayed or reordered messages: they fail authentication and close the connection;
- strangers who connect to the hub: they receive nothing until they prove the passphrase.
The hub limits their connections (4 per address, 32 in total) and runs scrypt only for a real passphrase attempt.
After a wrong passphrase, the hub blocks that address (IPv6: the /64) for 1 s, then 2 s, 4 s … up to 60 s;
- malformed input and floods: they close that one connection, never the program.

**Does not protect against:**

- **a weak passphrase.** An attacker who pretends to be the hub (a fake mDNS announcement or a
man-in-the-middle) can try passphrase guesses offline. Each guess costs one scrypt run (128 MiB, about
0.2 s), so a long random passphrase makes this impractical, and a short one does not;
- text sizes: the size of each copy is visible on the network;
- machines that know the passphrase: they can read your copies and write to your clipboard;
- password managers: netpaste syncs a copied password like any other text, and the other
machine keeps it after the password manager clears its own clipboard.

**Known limitation:** if you copy on two machines within about a second, the copies can cross:
each machine keeps the other's text until the next copy. See [docs/PROTOCOL.md](docs/PROTOCOL.md#known-limitations).

## Upgrading from 1.x

Version 2 uses a new protocol. It cannot talk to 1.x, so update all machines.

| 1.x | 2.x |
|---|---|
| `node server.js [port]` | `netpaste serve [--port N]` |
| `node client.js HOST [port]` | `netpaste connect HOST[:PORT]` |
| `--no-clipboard` | `--no-clipboard` (on the hub it now means "relay only") |

The list of fixed problems is in [CHANGELOG.md](CHANGELOG.md).

## Development

```bash
node client.js <hostname> [port]
npm run typecheck # tsc --noEmit, strict
npm test # node:test: unit, protocol, security and end-to-end tests
node bin/netpaste.js serve --no-clipboard --verbose
```
`<hostname>`: The IP address or hostname of the server.
`[port]`: The port number (optional, defaults to 8134 if not provided).

### Supported Platforms
Netpaste is tested and supported on Linux and macOS platforms. It leverages platform-independent technologies but adheres closely to Unix-like operating system behaviors.
The tests never touch the real clipboard: they use an in-memory clipboard.
The mDNS test uses multicast on the loopback interface only.

## Author

### Author
Aliaksei Smirnou
Aliaksei Smirnou. MIT License.
10 changes: 10 additions & 0 deletions bin/netpaste.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env node
// Plain JavaScript on purpose: it runs on any Node.js version and explains what is missing.
// Node.js runs TypeScript without flags from 22.18 (22.x line), 23.6 and every later version.
const [major, minor] = process.versions.node.split('.').map(Number);
const supported = major > 23 || (major === 23 && minor >= 6) || (major === 22 && minor >= 18);
if (!supported) {
console.error(`netpaste needs Node.js 22.18 or newer (it runs TypeScript directly); this is Node.js ${process.versions.node}.`);
process.exit(1);
}
await import('../src/cli.ts');
Loading
Loading