Comprehensive Guide and API Examples for socks-proxy-agent to Elevate Your Node.js Applications

Introduction to socks-proxy-agent

The socks-proxy-agent is a versatile, powerful library designed to handle SOCKS proxy connections in Node.js applications. Leveraging this library can provide enhanced security and anonymity for your HTTP, HTTPS, and WebSocket communications. In this comprehensive guide, we will explore its various functionalities through detailed API explanations and code snippets, ensuring you can integrate it seamlessly into your projects.

Installing socks-proxy-agent

  npm install socks-proxy-agent

First, ensure you have socks-proxy-agent installed in your project.

Basic Usage

  const { SocksProxyAgent } = require('socks-proxy-agent');
  
  const options = {
    hostname: 'localhost',
    port: 1080,
    userId: 'username',
    password: 'password'
  };
  
  const agent = new SocksProxyAgent(options);
  console.log('Proxy agent created:', agent);

HTTP and HTTPS Examples

HTTP Request

  const http = require('http');
  const { SocksProxyAgent } = require('socks-proxy-agent');

  const url = 'http://example.com';
  const agent = new SocksProxyAgent('socks://localhost:1080');

  http.get(url, { agent }, (res) => {
    res.pipe(process.stdout);
  });

HTTPS Request

  const https = require('https');
  const { SocksProxyAgent } = require('socks-proxy-agent');

  const url = 'https://example.com';
  const agent = new SocksProxyAgent('socks://localhost:1080');

  https.get(url, { agent }, (res) => {
    res.pipe(process.stdout);
  });

Using with WebSocket

  const WebSocket = require('ws');
  const { SocksProxyAgent } = require('socks-proxy-agent');

  const agent = new SocksProxyAgent('socks://localhost:1080');
  const ws = new WebSocket('ws://example.com', { agent });

  ws.on('open', () => {
    console.log('WebSocket connection opened');
    ws.send('Hello!');
  });

  ws.on('message', (message) => {
    console.log('Received:', message);
  });

Complex Example: An Application with Multiple Proxies

  const http = require('http');
  const https = require('https');
  const { SocksProxyAgent } = require('socks-proxy-agent');

  const proxies = [
    'socks://proxy1:1080',
    'socks://proxy2:1080'
  ];

  proxies.forEach((proxy) => {
    const agent = new SocksProxyAgent(proxy);

    // HTTP Request
    http.get('http://example.com', { agent }, (res) => {
      console.log('HTTP request through proxy:', proxy);
      res.pipe(process.stdout);
    });

    // HTTPS Request
    https.get('https://example.com', { agent }, (res) => {
      console.log('HTTPS request through proxy:', proxy);
      res.pipe(process.stdout);
    });
  });

By following this guide, you will be able to utilize socks-proxy-agent in numerous scenarios, providing robust proxy support throughout your Node.js applications.

Hash: b8c0917b7797989baaa069484ecdb1a7c678975572bd9352f1431d0ec2bfc95c

Leave a Reply

Your email address will not be published. Required fields are marked *