all files / lib/ proxyParser.js

100% Statements 10/10
100% Branches 6/6
100% Functions 1/1
100% Lines 10/10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                                            
module.exports.consume = function (buffer) {
    //TODO: make this assert the ips and ports are correct
 
    var line = buffer.toString('utf8'),
        nextToken = line.indexOf('\r\n')
 
    if (nextToken < 0) {
        throw new Error('Expected \\r\\n but did not find one')
    }
 
    var parts = buffer.slice(0, nextToken).toString('utf8').split(' ')
 
    if (parts.length < 6) {
        throw new Error('Expected 6 parts for PROXY protocol got ' + parts.length)
    }
 
    if (parts[0] != 'PROXY') {
        throw new Error('Expect PROXY protocol but got ' + parts[0])
    }
 
    return {
        buffer: buffer.slice(nextToken + 2),
        proxy: {
            protocol: parts[1],
            clientIp: parts[2],
            proxyIp: parts[3],
            clientPort: parts[4],
            proxyPort: parts[5]
        }
    }
}