Skip to content

Add connectionOptions support to createServer#77

Merged
brozeph merged 2 commits into
mainfrom
rc/v3.6.0
May 10, 2026
Merged

Add connectionOptions support to createServer#77
brozeph merged 2 commits into
mainfrom
rc/v3.6.0

Conversation

@brozeph
Copy link
Copy Markdown
Owner

@brozeph brozeph commented May 2, 2026

This PR adds options.connectionOptions as a narrower alternative to accepting arbitrary Duplex streams from connectionFilter.

The intent is to keep connectionFilter focused on allow/deny policy while giving callers a separate hook to customize the outbound TCP dial. simple-socks still owns the call to net.createConnection, so existing socket lifecycle behavior, timeouts, piping, and proxy events remain under the library’s control.

This supports use cases like destination rewriting, local bind customization, and routing through a local TCP adapter. For SSH forwarding or other non-net.Socket transports, callers can bridge through a local TCP listener and return those listener options from connectionOptions, rather than returning the stream directly.

Included:

  • TypeScript declarations for connectionOptions
  • README documentation and SSH adapter example
  • tests for host/port rewriting and hook error handling
  • v3.6.0 changelog entry
  • dependency refresh and audit cleanup

To accomplish what #76 is trying to address:

import net from "net";
import socks5 from "simple-socks";

// `ssh` is an already-connected ssh2 Client instance.
function createSshForwardTarget(ssh, destination, origin, callback) {
  const forwarder = net.createServer((localSocket) => {
    // This listener is for one SOCKS request only.
    forwarder.close();

    ssh.forwardOut(
      origin.address,
      origin.port,
      destination.address,
      destination.port,
      (err, sshStream) => {
        if (err) {
          localSocket.destroy(err);
          return;
        }

        localSocket.pipe(sshStream);
        sshStream.pipe(localSocket);

        localSocket.once("close", () => sshStream.destroy());
        localSocket.once("error", () => sshStream.destroy());
        sshStream.once("close", () => localSocket.destroy());
        sshStream.once("error", () => localSocket.destroy());
      },
    );
  });

  forwarder.once("error", callback);

  forwarder.listen(0, "127.0.0.1", () => {
    const address = forwarder.address();

    callback(null, {
      host: address.address,
      port: address.port,
    });
  });
}

const server = socks5.createServer({
  connectionOptions(destination, origin, defaults, callback) {
    createSshForwardTarget(ssh, destination, origin, (err, target) => {
      if (err) {
        return callback(err);
      }

      return callback(null, {
        ...defaults,
        ...target,
      });
    });
  },
});

server.listen(1080, "127.0.0.1");

@codecov
Copy link
Copy Markdown

codecov Bot commented May 2, 2026

Codecov Report

❌ Patch coverage is 90.47619% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.03%. Comparing base (641b108) to head (178aeda).

Files with missing lines Patch % Lines
src/socks5.js 90.47% 16 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #77      +/-   ##
==========================================
+ Coverage   86.75%   87.03%   +0.27%     
==========================================
  Files           2        2              
  Lines         657      702      +45     
==========================================
+ Hits          570      611      +41     
- Misses         87       91       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@brozeph brozeph changed the title adding support for connectionOptions the createServer function Add connectionOptions support to createServer May 2, 2026
@brozeph brozeph marked this pull request as ready for review May 3, 2026 17:20
@brozeph brozeph merged commit 2175c79 into main May 10, 2026
18 checks passed
@brozeph brozeph deleted the rc/v3.6.0 branch May 10, 2026 19:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant