Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

http_proxy

const http = require('http')
const port = process.env.PORT || 9191
const net = require('net')
const url = require('url')

const requestHandler = (req, res) => { // discard all request to proxy server except HTTP/1.1 CONNECT method
  res.writeHead(405, {'Content-Type': 'text/plain'})
  res.end('Method not allowed')
}

const server = http.createServer(requestHandler)

const listener = server.listen(port, (err) => {
  if (err) {
    return console.error(err)
  }
  const info = listener.address()
  console.log(`Server is listening on address ${info.address} port ${info.port}`)
})

server.on('connect', (req, clientSocket, head) => { // listen only for HTTP/1.1 CONNECT method
  console.log(clientSocket.remoteAddress, clientSocket.remotePort, req.method, req.url)
  if (!req.headers['proxy-authorization']) { // here you can add check for any username/password, I just check that this header must exist!
    clientSocket.write([
      'HTTP/1.1 407 Proxy Authentication Required',
      'Proxy-Authenticate: Basic realm="proxy"',
      'Proxy-Connection: close',
    ].join('
'))
    clientSocket.end('

')  // empty body
    return
  }
  const {port, hostname} = url.parse(`//${req.url}`, false, true) // extract destination host and port from CONNECT request
  if (hostname && port) {
    const serverErrorHandler = (err) => {
      console.error(err.message)
      if (clientSocket) {
        clientSocket.end(`HTTP/1.1 500 ${err.message}
`)
      }
    }
    const serverEndHandler = () => {
      if (clientSocket) {
        clientSocket.end(`HTTP/1.1 500 External Server End
`)
      }
    }
    const serverSocket = net.connect(port, hostname) // connect to destination host and port
    const clientErrorHandler = (err) => {
      console.error(err.message)
      if (serverSocket) {
        serverSocket.end()
      }
    }
    const clientEndHandler = () => {
      if (serverSocket) {
        serverSocket.end()
      }
    }
    clientSocket.on('error', clientErrorHandler)
    clientSocket.on('end', clientEndHandler)
    serverSocket.on('error', serverErrorHandler)
    serverSocket.on('end', serverEndHandler)
    serverSocket.on('connect', () => {
      clientSocket.write([
        'HTTP/1.1 200 Connection Established',
        'Proxy-agent: Node-VPN',
      ].join('
'))
      clientSocket.write('

') // empty body
      // "blindly" (for performance) pipe client socket and destination socket between each other
      serverSocket.pipe(clientSocket, {end: false})
      clientSocket.pipe(serverSocket, {end: false})
    })
  } else {
    clientSocket.end('HTTP/1.1 400 Bad Request
')
    clientSocket.destroy()
  }
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: database for javascript 
Javascript :: how to use hash in javascript 
Javascript :: javascript event 
Javascript :: what is auth guard in angular 
Javascript :: polymer js tutorial 
Javascript :: how to download array of files from aws s3 using aws sdk in nodejs 
Javascript :: vue on page link or anchor 
Javascript :: var = {} js 
Javascript :: destructuring an array 
Javascript :: access object property dynamically javascript 
Javascript :: adding cors in angular 
Javascript :: switch statement 
Javascript :: Geometery parsing GeoJSON 
Javascript :: get the last element of array javascript 
Javascript :: use next() in node js 
Javascript :: AJAX in reload a div container 
Javascript :: arrow function in javascript 
Javascript :: set timer 
Javascript :: react native update helper 
Javascript :: new file shortcut vscode 
Javascript :: add value to object 
Javascript :: excel json to table 
Javascript :: ondedrive 
Javascript :: js multibyte string length 
Javascript :: javascript firestore autoID 
Javascript :: c# from javascript with callback 
Javascript :: check event target jquery outside 
Javascript :: chartjs angular bar beginAtZero 
Javascript :: what does tilde (~) and caret (^) mens in package.json file 
Javascript :: serverresponse 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =