forked from l33ts/PUBG_test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
247 lines (235 loc) · 8.59 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<<<<<<< HEAD
const backend = require('./backend')
const PacketByPacket = require('p-by-p')
const PacketParser = require('./backend/packetparser')
const utils = require('./backend/utils')
const gamestate = require('./backend/gamestate')
const bunchStash = require('./backend/bunchstash')
const CONSTS = require('./backend/constants')
const Cap = require('cap').Cap
function printUsage () {
console.log(
`Usage:
* node index.js sniff <interface> <game_pc_ip>
* node index.js playback <pcapfile>
Example: node index.js playback '/SOME/COOL/DIR/pubg-game1.pcap' | pino
node index.js sniff en5 192.168.0.100 | pino`
)
process.exit(1)
}
function startWebServer () {
const apiServerPort = 20086
backend.listen(apiServerPort, () => {
console.log('Scientific Chicken Dinner listening on http://localhost:' + apiServerPort)
})
}
function printStateOnConsole() {
setInterval(() => {
console.log(`[${utils.toLocalISOString(new Date())}] - playbackState: ${gamestate.playbackState}, speed: ${gamestate.playbackSpeed}, playbackIndex: ${gamestate.playbackIndex}, processedEvents: ${gamestate.totalProcessedEvents}/${gamestate.playbackEvents.length}`)
}, 5000)
}
function main () {
const args = process.argv.slice(2)
if (args.length < 2 || !['sniff', 'playback'].includes(args[0])) {
printUsage()
}
if (args[0] === 'sniff') {
if (args.length !== 3) {
printUsage()
}
const c = new Cap()
const device = args[1]
const filter = `(src host ${args[2]} and udp dst portrange 7000-7999) or (dst host ${args[2]} and udp src portrange 7000-7999)`
const bufSize = 10 * 1024 * 1024
const capBuffer = Buffer.alloc(65535)
const linkType = c.open(device, filter, bufSize, capBuffer)
c.setMinBytes && c.setMinBytes(0)
const parser = PacketParser()
let pIndex = 0
c.on('packet', function (nbytes, trunc) {
// raw packet data === buffer.slice(0, nbytes)
pIndex++
const rawPacketData = capBuffer.slice(0, nbytes)
// one packet can generate 1 or 0 event
const result = parser.parse(rawPacketData, new Date().getTime(), pIndex)
if (result != null) {
// result can only be UEBunch event
const l2Evts = bunchStash.feedEvent(result)
if (l2Evts) {
for (const l2Evt of l2Evts) {
if (l2Evt.type === CONSTS.EventTypes.ENCRYPTIONKEY) {
console.log(`Got EncryptionToken ${l2Evt.data.EncryptionToken}`)
parser.setEncryptionToken(l2Evt.data.EncryptionToken)
} else {
if (l2Evt.type === CONSTS.EventTypes.GAMESTOP) {
parser.clearEncryptionToken()
}
gamestate.processPUBGEvent(l2Evt)
}
}
}
}
})
startWebServer()
} else if (args[0] === 'playback') {
const parser = PacketParser()
// read the file and get all the events out first
const pbyp = PacketByPacket(args[1])
const finalEvents = []
pbyp.on('packet', packet => {
const result = parser.parse(packet.data, packet.header.timestamp)
if (result != null) {
const l2Evts = bunchStash.feedEvent(result)
if (l2Evts) {
for (const l2Evt of l2Evts) {
if (l2Evt.type === CONSTS.EventTypes.ENCRYPTIONKEY) {
console.log(`Got EncryptionToken ${l2Evt.data.EncryptionToken}`)
parser.setEncryptionToken(l2Evt.data.EncryptionToken)
} else {
if (l2Evt.type === CONSTS.EventTypes.GAMESTOP) {
parser.clearEncryptionToken()
} else if (l2Evt.type === CONSTS.EventTypes.GAMESTART) {
// reset the state, so that stash can process further games
gamestate.resetGameState()
}
finalEvents.push(l2Evt)
}
}
}
}
})
pbyp.on('error', err => {
console.log('ERROR happened during parsing the pcap file', err)
})
pbyp.on('end', result => {
console.log('pcap file processing finished.', result)
if (finalEvents.length > 0) {
// now set the mode to playback and start the api server
gamestate.setPlaybackMode(finalEvents)
printStateOnConsole()
startWebServer()
gamestate.startPlayback(1.0, finalEvents.length)
}
})
pbyp.resume()
}
}
main()
=======
const backend = require('./backend')
const PacketByPacket = require('p-by-p')
const PacketParser = require('./backend/packetparser')
const utils = require('./backend/utils')
const gamestate = require('./backend/gamestate')
const bunchStash = require('./backend/bunchstash')
const CONSTS = require('./backend/constants')
const Cap = require('cap').Cap
function printUsage () {
console.log(
`Usage:
* node index.js sniff <interface> <game_pc_ip>
* node index.js playback <pcapfile>
Example: node index.js playback '/SOME/COOL/DIR/pubg-game1.pcap' | pino
node index.js sniff en5 192.168.0.100 | pino`
)
process.exit(1)
}
function startWebServer () {
const apiServerPort = 20086
backend.listen(apiServerPort, () => {
console.log('Scientific Chicken Dinner listening on http://localhost:' + apiServerPort)
})
}
function printStateOnConsole() {
setInterval(() => {
console.log(`[${utils.toLocalISOString(new Date())}] - playbackState: ${gamestate.playbackState}, speed: ${gamestate.playbackSpeed}, playbackIndex: ${gamestate.playbackIndex}, processedEvents: ${gamestate.totalProcessedEvents}/${gamestate.playbackEvents.length}`)
}, 5000)
}
function main () {
const args = process.argv.slice(2)
if (args.length < 2 || !['sniff', 'playback'].includes(args[0])) {
printUsage()
}
if (args[0] === 'sniff') {
if (args.length !== 3) {
printUsage()
}
const c = new Cap()
const device = args[1]
const filter = `(src host ${args[2]} and udp dst portrange 7000-7999) or (dst host ${args[2]} and udp src portrange 7000-7999)`
const bufSize = 10 * 1024 * 1024
const capBuffer = Buffer.alloc(65535)
const linkType = c.open(device, filter, bufSize, capBuffer)
c.setMinBytes && c.setMinBytes(0)
const parser = PacketParser()
let pIndex = 0
c.on('packet', function (nbytes, trunc) {
// raw packet data === buffer.slice(0, nbytes)
pIndex++
const rawPacketData = capBuffer.slice(0, nbytes)
// one packet can generate 1 or 0 event
const result = parser.parse(rawPacketData, new Date().getTime(), pIndex)
if (result != null) {
// result can only be UEBunch event
const l2Evts = bunchStash.feedEvent(result)
if (l2Evts) {
for (const l2Evt of l2Evts) {
if (l2Evt.type === CONSTS.EventTypes.ENCRYPTIONKEY) {
console.log(`Got EncryptionToken ${l2Evt.data.EncryptionToken}`)
parser.setEncryptionToken(l2Evt.data.EncryptionToken)
} else {
if (l2Evt.type === CONSTS.EventTypes.GAMESTOP) {
parser.clearEncryptionToken()
}
gamestate.processPUBGEvent(l2Evt)
}
}
}
}
})
startWebServer()
} else if (args[0] === 'playback') {
const parser = PacketParser()
// read the file and get all the events out first
const pbyp = PacketByPacket(args[1])
const finalEvents = []
pbyp.on('packet', packet => {
const result = parser.parse(packet.data, packet.header.timestamp)
if (result != null) {
const l2Evts = bunchStash.feedEvent(result)
if (l2Evts) {
for (const l2Evt of l2Evts) {
if (l2Evt.type === CONSTS.EventTypes.ENCRYPTIONKEY) {
console.log(`Got EncryptionToken ${l2Evt.data.EncryptionToken}`)
parser.setEncryptionToken(l2Evt.data.EncryptionToken)
} else {
if (l2Evt.type === CONSTS.EventTypes.GAMESTOP) {
parser.clearEncryptionToken()
} else if (l2Evt.type === CONSTS.EventTypes.GAMESTART) {
// reset the state, so that stash can process further games
gamestate.resetGameState()
}
finalEvents.push(l2Evt)
}
}
}
}
})
pbyp.on('error', err => {
console.log('ERROR happened during parsing the pcap file', err)
})
pbyp.on('end', result => {
console.log('pcap file processing finished.', result)
if (finalEvents.length > 0) {
// now set the mode to playback and start the api server
gamestate.setPlaybackMode(finalEvents)
printStateOnConsole()
startWebServer()
gamestate.startPlayback(1.0, finalEvents.length)
}
})
pbyp.resume()
}
}
main()
>>>>>>> 提交文件