Skip to content

mutagen-d/node-proxy-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Proxy Server

Http and Socks proxy server with zero dependencies

Content

Usage

const { createProxyServer } = require('@mutagen-d/node-proxy-server')
const port = 8080
const server = createProxyServer()
server.on('error', (error) => {
  console.log('server error', error)
})
server.listen(port, '0.0.0.0', () => console.log('proxy-server listening port', port))

Tls

create tls server

const fs = require('fs')
const path = require('path')
const server = createProxyServer({
  cert: fs.readFileSync(path.join(__dirname, './cert.pem'), 'utf-8'),
  key: fs.readFileSync(path.join(__dirname, './key.pem'), 'utf-8'),
})

Authorization

const server = createProxyServer({ auth: true })
server.on('proxy-auth', (username, password, callback) => {
  callback(username === 'login' && password === '1234')
})

Only first "proxy-auth" event listener will be envoked

KeepAlive

server.on('connection', (socket) => {
  socket.setTimeout(30 * 1000, () => socket.destroy())
})

Custom proxy connection

Use createProxyConnection method. By default net module is used to create connection, e.i.

const net = require('net')
const { createProxyServer } = require('./src')
const server = createProxyServer({
  createProxyConnection: async (info) => {
    const socket = net.createConnection({ host: info.dstHost, port: info.dstPort })
    return new Promise((resolve, reject) => {
      socket.on('connect', () => resolve(socket))
      socket.on('error', (error) => reject(error))
    })
  }
})

One can also use other methods to create connection, e.i ssh2-dynamic-port-forwarding

const { createProxyServer } = require('./src')
const { Client } = require('ssh2')
const util = require('util')

const sshClient = new Client()
const forwardOut = util.promisify(sshClient.forwardOut)

const server = createProxyServer({
  createProxyConnection: async (info) => {
    const stream = await forwardOut.call(sshClient, info.srcHost, info.srcPort, info.dstHost, info.dstPort)
    return stream
  },
})

sshClient.connect({
  host: 'localhost',
  username: 'username',
  password: '12345',
})
sshClient.on('ready', () => {
  const port = 8080
  server.listen(port, '0.0.0.0', () => console.log('proxy-server listening port', port))
})

Examples

see examples

About

Http and socks proxy server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published