What is a proxy server

To put it simply, the request is sent to service A, which does some simple processing, and then sends the request to service B intact. Server B is the real target server.

implementation

The existing core code implements this functionality, and if there is any functionality that needs to be added, add it in the appropriate place, such as request blocking, logging, error handling, and so on.

const net = require('net')

// Create a TCP link if requested
net.createServer(client= > {
  client.on('error'.error= > console.log('client', error))

  client.on('data'.req= > {
    // Create a link to the b service
    const bSserve = new net.Socket()
    bSserve.connect(8088.'localhost')
    bSserve.on('error'.error= > console.log('bSserve', error))

    // Send the request to server B
    bSserve.write(req)
    
    bSserve.on('data'.res= > {
      // Send the response to the client
      client.write(res)
    })
  })
}).listen(80)
Copy the code

These few lines create a simple agent that can copy the code and try it out locally. You need to modify the port. Just change 8088 to the server port you want to proxy.

Intercept request and response data

The response is handled at the following location in the code

Const net = require('net') // create a TCP connection net.createserver (client => {client.on('error', error => console.log('client', error)) client.on('data', req => {+ // Request processing
+ const content = req.toString()
+ console.log(content)Const bSserve = new net.socket () bsserve.connect (8088, 'localhost') bsserve.on ('error', Log ('bSserve', error)) // Send the request to b server bSserve. Write (req) bSserve. On ('data', res => {+ // Response processing
+ const content = res.toString()
+ console.log(content)Write (res)})})}).listen(80)Copy the code

conclusion

The core function is to create a serve using the NODEJS NET module. Each request from the client establishes a TCP connection with the target server, and the data is forwarded one-to-one.

There’s time for a little more difficulty. This is just to do a simple data proxy, then how to do a network penetration function?