question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It would be nice to have proxy support for Sequelize. Using Sequelize on a host with rotating IP addresses makes it near impossible to whitelist connections to an external database. I understand that this would depend on the connection package (node-mysql, pg). These packages do offer support for this feature, but there is no way to initialize Sequelize using those stream options. For example, with the pg package:

const pg = require('pg');
const SocksConnection = require('socksjs');

const pgServer = {
  host: 'YOUR-HOST',
  port: 5432
};

const proxyConnection = new SocksConnection(pgServer, {
  user: process.env.PROXY_USER,
  pass: process.env.PROXY_PASS,
  host: process.env.PROXY_HOST,
  port: process.env.PROXY_PORT,
});

const connectionConfig = {
  user: 'YOUR-DB-USERNAME',
  password: 'YOUR-DB-PASSWORD',
  database: 'YOUR-DATABASE',
  stream: proxyConnection,
  ssl: true // Optional, depending on db config
};

const client = new pg.Client(connectionConfig);

// client.connect() { ... }

Since we can’t pass a custom pg instance into Sequelize, how would we go about making this work?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mgoriacommented, Mar 6, 2020

stream option needs to be a function returning a new Socks connection, something like:

const createNewProxyStream = (proxyUrl) => {
 const proxyUrlObj = new URL(proxyUrl)

  const remoteOptions = {
    host: DB_HOST,
    port: DB_PORT,
  }

  const sockOptions = {
    host: proxyUrlObj.hostname,
    port: 1080,
    user: proxyUrlObj.username,
    pass: proxyUrlObj.password,
  }

  return new SocksConnection(remoteOptions, sockOptions)
}

const config = {
  host: DB_HOST,
  dialect: 'mysql',
  dialectOptions: {
    // IMPORTANT: the stream needs to be created all the time since new pooled connection needs new stream
    stream: (options) => {
      console.log('[sequelize-connection] - creating new proxy stream.')
      return createNewProxyStream()
    },
  },
}

const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASS, config);
2reactions
papbcommented, Jan 16, 2020

Related: #9780

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Set Up a Proxy Server on Your PC, Mac, or Web Browser
Open the IE toolbar and select Internet Options. · Open the Connections tab. · Select LAN Settings. · Check the “Use a proxy...
Read more >
Proxy Server - What They Are & How to Use
Forward proxies are the most common type of proxy server. The work by taking a request and passing it through from an isolated,...
Read more >
Connecting to a proxy server - IBM
Open Edge. · Click Settings and more > Advanced > Open proxy settings. · Under Manual proxy setup, select Use a proxy server....
Read more >
What is a Proxy Server? How does it work? - Fortinet
A proxy server performs the function of a firewall and filter. The end-user or a network administrator can choose a proxy designed to...
Read more >
What is a Proxy Server and How Does it Work? - Varonis
If you're using a proxy server, internet traffic flows through the proxy server on its way to the address you requested. The request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found