-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtracker.js
98 lines (89 loc) · 3.81 KB
/
tracker.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
const fs = require('fs');
const readline = require('readline');
const mongoose = require('mongoose');
const {config} = require('dotenv');
const axios = require('axios');
const path = require('path');
const db = mongoose.connection;
/* commented */
setInterval(function(){
config({
path: __dirname + '/.env/'
})
async function downloadFile () {
// This will request file that will contain download link for log
console.log('{beginning GET request !}');
const url1 = 'https://api.nitrado.net/services/'
const url2 = '/gameservers/file_server/download?file=/games/'
const url3 = '/noftp/dayzxb/config/DayZServer_X1_x64.ADM'
const filePath = path.resolve('./logs', 'serverlog.ADM')
const writer = fs.createWriteStream(filePath)
const downloadlink = await axios.get(url1+'<GAMESERVER ID GOES HERE>'+url2+'<USERID GOES HERE>'+url3,{ responseType: 'application/json', headers: {'Authorization' : 'Bearer '+process.env.ADM, 'Accept': 'application/octet-stream'}});
const response = await axios.get(downloadlink.data.data.token.url,{ responseType: 'stream', headers: {'Authorization' : 'Bearer '+process.env.ADM, 'Accept': 'application/octet-stream'}});
response.data.pipe(writer)
return new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
}
// MONGODB USERNAME GOES HERE, ALSO INCLUDE CLUSTER NAME -
mongoose.connect('mongodb+srv://<MONGO USERNAME GOES HERE>:' + process.env.MONGO + '@<CLUSTER NAME GOES HERE>.mongodb.net/test?retryWrites=true&w=majority', { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, });
db.on('error', console.error.bind(console, 'connection error:'));
const playerSchema = mongoose.Schema({
playerValue: {
type: String,
unique: true
},
dayzId: {
type: String
}
});
let playerModel
try {
playerModel = mongoose.model('Player');
} catch (error) {
playerModel = mongoose.model('Player', playerSchema);
}
function auditFile () {
db.once('open', function(){
downloadFile().catch().then(()=>{
console.log('beginning audit of gameserver TEST');
const rl = readline.createInterface({
input: fs.createReadStream('./logs/serverlog.ADM',{encoding: 'utf8'}),
});
rl.on('line',(input)=>{
if (input.includes('is connected')) {
const playerInput = input.slice(19,-60);
const idInput = input.slice(-45)
playerModel.findOne({playerValue: playerInput}, (err, playerDoc)=> {
if (err) return
if(!playerDoc) {
const newPlayer = new playerModel({
playerValue: playerInput,
dayzId: idInput
});
newPlayer.save(function(err, saved){
if (err) return console.error(err);
if (saved) console.log(`${playerInput} was saved to the databse`)
});
}
else
console.log(`${playerInput} already exists in the database !`);
});
}
else return;
});
rl.on('close', () =>{
console.log(`{readline is closing}`);
rl.close();
});
}).then(()=>{
playerModel.find(function (err, players) {
if (err) return console.error(err);
console.log(`** TOTAL AMOUNT OF UNIQUE PLAYERS ** : ${players.length}`)
});
});
});
}
auditFile();
},1500000);