shadowsocks / shadowsocks-rust

A Rust port of shadowsocks
https://shadowsocks.org/
MIT License
8.06k stars 1.12k forks source link

[ASK] How to Encrypt Payload from ss-local to ss-remote? #1286

Closed kicut closed 3 weeks ago

kicut commented 10 months ago

First of all, sorry, this question might be a bit deviating from this repository, I'm asking for help here because I see this repository is quite active among the others.

So, recently I just got into network programming. I want to know how to encrypt data sent from ss-local to ss-remote.

image

I've been reading the documentation and diving into some ss-local tool libraries in the wild for a few days, but I don't get the answer it's still too abstract for me. I have tried it on Nodejs but I am stuck on this error, error message from Shadowsocks Go Server.

2023/09/03 20:56:57 tcp.go:126: failed to get target address from 127.0.0.1:36654: cipher: message authentication failed

Can someone please provide an example of how to send data from ss-local to ss-remote in Node.js, the following code below might work as a starting sample.

import net from "net";

const ssServer = {
  host: "127.0.0.1",
  password: "password",
  port: 8488,
  encryption: {
    method: "aes-256-gcm",
    keySize: 32,
    ivLen: 16,
    saltSize: 32,
    nonceSize: 12,
    tagSize: 16,
  },
};

const streamToServer = net.createConnection({
  host: ssServer.host,
  port: ssServer.port,
});

streamToServer.on("ready", () => {
  const data = Buffer.from("GET / HTTP/1.1\r\nHost: ifconfig.me\r\n\r\n");
  // ?????????????????????????????????????
  streamToServer.write(data);
});

streamToServer.on("data", (data) => {
  console.log("on data,", data);
});

streamToServer.on("connect", () => {
  console.log("connected");
});

streamToServer.on("error", (e) => {
  console.log("error", e);
});

I really appreciate any answer, thanks in advance.

zonyitoo commented 3 weeks ago

https://github.com/shadowsocks/shadowsocks-rust/tree/master/crates/shadowsocks/src/relay

Here are the core protocol implementation.