Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

How to use proxy in snoowrap? #375

Open
learnacadman opened this issue Jan 20, 2023 · 2 comments
Open

How to use proxy in snoowrap? #375

learnacadman opened this issue Jan 20, 2023 · 2 comments

Comments

@learnacadman
Copy link

Let's say I want to use a proxy server 123.456.789 with the port 8080, how do I pass that to snoowrap?

@learnacadman
Copy link
Author

@LucasianProfessor ok, and how to pass username password for the proxy?

@noc2spam
Copy link

This is how I made it work..

interface RequestOptions {
  json: boolean;
  baseUrl: string;
  uri: string;
  method: string;
  headers: object;
  qs?: object;
  form?: object;
  auth?: {
    bearer: string;
    user: string;
    pass: string;
  };
  formData: object;
  body: object;
  transform: Function;
  resolveWithFullResponse: boolean;
}

export default class SnoowrapWithProxy extends Snoowrap {
  proxy: {
    host: string;
    port: number;
    auth: {
      username: string;
      password: string;
    };
    protocol: "http";
  };
  constructor(
    options: SnoowrapOptions,
    proxy: {
      host: string;
      port: number;
      auth: {
        username: string;
        password: string;
      };
      protocol: "http";
    }
  ) {
    super(options);
    this.proxy = proxy;
  }
  rawRequest(options: RequestOptions) {
    const auth = options?.auth?.bearer
      ? `bearer ${options.auth.bearer}`
      : "basic " + btoa(`${options?.auth?.user}:${options?.auth?.pass}`);
    const agent = options?.headers!["user-agent"];
    const axiosOptions = {
      url: options.uri,
      method: options.method,
      baseURL: options.baseUrl,
      data: options.form,
      proxy: this.proxy,

      headers: {
        Authorization: auth,
        "user-agent": agent,
        "Content-Type": "application/x-www-form-urlencoded",
      },
    };
    const promise = axios(axiosOptions)
      .then((resp) => resp.data)
      .catch((r) => r.message);
    return promise;
  }
}

Note, this uses axios as the client, but it could be anything..

Not tested for all usages though. Please be sure to test it before you use it in important places.

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

No branches or pull requests

2 participants