-
Notifications
You must be signed in to change notification settings - Fork 11
/
socket.js
executable file
·61 lines (52 loc) · 1.77 KB
/
socket.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /usr/bin/env node
const program = require('commander')
const pkg = require('../package.json')
const Validator = require('..')
const path = require('path')
const net = require('net')
/// /////////////////////////////////////////////////////////////////
// Initialize CLI
program
.version(pkg.version)
.option('-H, --host [host]', 'host to bind [127.0.0.1]', '127.0.0.1')
.option('-p, --port [port]', 'port to bind [4445]', '4445')
.option('-c, --conf [path]', 'validator configuration file [rules.json]')
program.on('--help', function() {
console.log(' Examples:')
console.log('')
console.log(' $ node bin/http.js -H 0.0.0.0 -p 8888')
console.log('')
})
program.parse(process.argv)
/// /////////////////////////////////////////////////////////////////
// Initialize Validator
var config = null
if (program['conf']) {
var configPath = path.resolve(process.cwd(), program['conf'])
config = require(configPath)
}
var validator = Validator(config, {
fast: true
})
const DELEM = '__baidu_mip_validator__'
// TODO 使用 Stream 代替 String
const server = net.createServer((sock) => {
console.log('[socket] CONNECTED:', sock.remoteAddress, +sock.remotePort)
var str = ''
sock.on('data', function(data) {
str += data
var tokens = str.split(DELEM)
str = tokens.pop()
tokens.forEach(function(html) {
var result = validator.validate(html)
sock.write(JSON.stringify(result, null, 4) + DELEM)
})
})
sock.on('close', function() {
console.log('[socket] CLOSED:', sock.remoteAddress, sock.remotePort)
})
})
server.listen(program['port'], program['host'], () => {
console.log(`[socket] bound to ${program['host']}:${program['port']}`)
})
module.exports = server