ookamiiixd / baileys-api

Simple WhatsApp REST API with multiple device support
MIT License
431 stars 327 forks source link

Add proxy support #134

Open bahkadomos opened 1 year ago

bahkadomos commented 1 year ago

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 commented 1 year ago

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

bahkadomos commented 1 year ago

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 commented 1 year ago

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

batmar125 commented 1 year ago

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