Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Add proxy support #134

Open
bahkadomos opened this issue Feb 2, 2023 · 4 comments · May be fixed by #162
Open

Add proxy support #134

bahkadomos opened this issue Feb 2, 2023 · 4 comments · May be fixed by #162
Labels
enhancement New feature or request

Comments

@bahkadomos
Copy link

bahkadomos commented Feb 2, 2023

Really need proxy support and saving it in the session. I suppose you need to add proxy field into the sessions/add endpoint. And add an endpoint like sessions/<:session>/update with field proxy that updates proxy in the session to the one passed.

I've done it myself so far. Everything works, but I'm not sure if the proxy is being set. Can you check it right?

// src/wa.ts
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';

type createSessionOptions = {
  sessionId: string;
  res?: Response;
  proxy?: string;
  SSE?: boolean;
  socketConfig?: SocketConfig;
};

export async function createSession(options: createSessionOptions) {
  const { sessionId, res, proxy = false, SSE = false, socketConfig } = options;
  ....
  const setProxy = (socket, proxy) => {
    if (proxy) {
      // replace 'socks5' -> 'socks'
      const proxyData = proxy.replace(/(?<=[a-z])(\d?)(?=://)/g, '');
      let agent;
      switch (true) {
        case /^socks/.test(proxyData):
          agent = SocksProxyAgent(proxyData);
          break;
        case /^http/.test(proxyData):
          agent = HttpsProxyAgent(proxyData);
          break;
        default:
          return;
      }
      Object.assign(socket, { agent });
  };

  const socket = makeWASocket({
    // your code here
  });
  ...
}

setProxy(socket, proxy);

I also added a proxy field to the sessions/add endpoint. In the MySQL database the proxy is inserted into the data column.

@ookamiiixd
Copy link
Owner

I've never used a proxy before. But sure, i'll take a look into this later

@ookamiiixd ookamiiixd added the enhancement New feature or request label Feb 5, 2023
@bahkadomos
Copy link
Author

I've never used a proxy before. But sure, i'll take a look into this later

Thanks for the answer. My code above doesn't work either, as it turns out. Typescript throws a type error when trying to compile. The baileys package uses the type agent: Agent, and passing an agent other than http-proxy-agent results in this error.

So I did not manage to connect the https-proxy-agent and socks-proxy-agent packages. The standard package http-proxy-agent works, but connection to whatsapp through the proxy raises an error. It looks like this package doesn't work with websocket over https protocol.

I can provide socks and http proxies for testing. My telegram: @shokoladny_zayac.

@ookamiiixd
Copy link
Owner

I've never used a proxy before. But sure, i'll take a look into this later

Thanks for the answer. My code above doesn't work either, as it turns out. Typescript throws a type error when trying to compile. The baileys package uses the type agent: Agent, and passing an agent other than http-proxy-agent results in this error.

So I did not manage to connect the https-proxy-agent and socks-proxy-agent packages. The standard package http-proxy-agent works, but connection to whatsapp through the proxy raises an error. It looks like this package doesn't work with websocket over https protocol.

I can provide socks and http proxies for testing. My telegram: @shokoladny_zayac.

Sure, i will reach you out later

@ookamiiixd ookamiiixd linked a pull request Mar 10, 2023 that will close this issue
@batmar125
Copy link

Hey Ookamiixd, have you reached out to that person? I would like to know how he fixed the proble. Thank you, have a good easter

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants