Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh2 not working #11947

Closed
lassejlv opened this issue Jun 18, 2024 · 6 comments
Closed

ssh2 not working #11947

lassejlv opened this issue Jun 18, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@lassejlv
Copy link

lassejlv commented Jun 18, 2024

What version of Bun is running?

1.1.13+bd6a60512

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

You can use this code:

import { Client } from "ssh2";

const IP_ADDRESS = process.env.VPS_IP_ADRESS;
const USERNAME = process.env.VPS_USERNAME;
const PASSWORD = process.env.VPS_PASSWORD;

export function runCommand(command) {
  const conn = new Client();

  conn.on("ready", () => {
    console.log("Client :: ready");

    conn.exec(command, (err, stream) => {
      if (err) throw err;

      stream
        .on("close", (code, signal) => {
          console.log(`Stream :: close :: code: ${code}, signal: ${signal}`);
          conn.end();
        })
        .on("data", (data) => {
          console.log(`STDOUT: ${data}`);
        })
        .stderr.on("data", (data) => {
          console.error(`STDERR: ${data}`);
        });
    });
  });

  conn.connect({
    host: IP_ADDRESS,
    port: 22,
    username: USERNAME,
    password: PASSWORD,
  });
}

What is the expected behavior?

I would expect it to connect and run commands. It works with node without any issues.

What do you see instead?

With bun it just exists with this error:

bun src/index.js
dyld[18758]: missing symbol called
[1] 18758 abort bun src/index.js

Additional information

Nope, love bun keep up the great work!

@lassejlv lassejlv added bug Something isn't working needs triage labels Jun 18, 2024
@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Jun 18, 2024

Duplicate of #8228

@Jarred-Sumner Jarred-Sumner marked this as a duplicate and then as not a duplicate of #4487 Jun 18, 2024
@BarakBinyamin
Copy link

BarakBinyamin commented Jun 27, 2024

on mac x86 13.5.2 (Ventura) the error was similar when trying launch an ssh2 server

dyld[26567]: missing symbol called
Abort trap: 6

on linux x86 ubuntu server

bun: symbol lookup error: /PATH/TO/src/node_modules/cpu-features/build/Release/cpufeatures.node: undefined symbol: node_module_register

@Tuanm
Copy link

Tuanm commented Jul 7, 2024

import { readFileSync } from 'fs';
import { Server, type AuthContext } from 'ssh2';
import net from 'net';

/**
 * SSH server's initial configurations.
 */
type ServerConfigurations<T extends AuthContext> = {
    /**
     * Path to the host key file.
     */
    hostKeyPath: string | undefined;
    /**
     * Authenticates an SSH connection.
     */
    authenticate: (context: T) => Promise<boolean>;
    /**
     * Retrieves an assigned port for local forwarding.
     */
    assignPort: (context: T) => Promise<number>;
};

/**
 * Creates an SSH server that supports local port forwarding with authentication.
 */
function createServer<T extends AuthContext>({
    hostKeyPath,
    authenticate,
    assignPort,
}: ServerConfigurations<T>) {
    const hostKeys = [] as string[];
    if (hostKeyPath) {
        hostKeys.push(Buffer.from(readFileSync(hostKeyPath)).toString());
    }
    return new Server(
        {
            hostKeys,
        },
        (client) => {
            client
                .on('authentication', async (ctx) => {
                    if (!(await authenticate(ctx as T))) {
                        return ctx.reject();
                    }
                    (client as any).assignedPort = await assignPort(ctx as T);
                    return ctx.accept();
                })
                .on('ready', () => {
                    client.on('request', (accept, reject, name, info) => {
                        if (name === 'tcpip-forward') {
                            const assignedPort = (client as any)
                                .assignedPort as number;
                            if (assignedPort === info.bindPort) {
                                const server = net
                                    .createServer((socket) => {
                                        socket.setEncoding('utf8');
                                        client.forwardOut(
                                            info.bindAddr,
                                            info.bindPort,
                                            String(socket.remoteAddress),
                                            Number(socket.remotePort),
                                            (err, upstream) => {
                                                if (err) {
                                                    console.log(err);
                                                    socket.end();
                                                    return;
                                                }
                                                upstream
                                                    .pipe(socket)
                                                    .pipe(upstream);
                                            },
                                        );
                                    })
                                    .listen(info.bindPort);
                                (client as any).server = server;
                                return accept && accept();
                            }
                        }
                        return reject && reject();
                    });
                    client.on('close', () => {
                        ((client as any).server as net.Server)?.close();
                    });
                });
        },
    );
}

/**
 * SSH functionalities.
 */
export default {
    createServer,
};

These scripts work well in my python:3.11-slim container with Bun 1.1.17 installed. The only drawback is that I could not build it 😭 by using the bun build command:

error: Could not resolve: "../build/Release/cpufeatures.node"

@maggiemoreno-synth
Copy link

I think this is effectively a duplicate of issue#158, see comment here #158 (comment)

I was able to work around this by installing dependencies with optional dependencies omitted, which then omits nan and cpu-features

npm install --omit=optional

@Jarred-Sumner
Copy link
Collaborator

Duplicate of #4290

@Jarred-Sumner Jarred-Sumner marked this as a duplicate of #4290 Aug 8, 2024
@Jarred-Sumner
Copy link
Collaborator

This is caused by ssh2's usage of cpu-features, which uses V8 C++ APIs.

@190n is actively working on supporting V8 C++ APIs in Bun

Please follow along in #4290. It will be fixed

@Jarred-Sumner Jarred-Sumner closed this as not planned Won't fix, can't repro, duplicate, stale Aug 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants