-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.js
259 lines (185 loc) · 4.93 KB
/
sync.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
248
249
250
251
252
253
254
255
256
257
258
259
// Allow require
import { createRequire } from "module";
const require = createRequire(import.meta.url);
import { Server } from "socket.io";
import strftime from "strftime";
import Database from "better-sqlite3";
const fs = require('fs');
var ip = require('ip');
let myip = null;
try {
myip = ip.address("Tailscale")
} catch {
myip = ip.address("tailscale0");
};
//init socketio
let clientList = [];
let ApprovedFiles = {};
function init_socketio() {
console.log(`Initializing socket.io`);
const io = new Server({
cors: {
origin: `*`,
}
});
io.on("connection", (socket) => {
console.log(`${socket.id} Joined the queue`);
socket.join("main");
socket.emit("message", `Welcome to Main of ${myip}`);
// io.to("main").emit("message", "New queue ping:");
ping(socket.id);
socket.on("pong", (ip) => {
// io.to("main").emit("message",`Client ${socket.id} is up`);
addClient(ip, socket.id);
});
socket.on("get_manifest", () => {
get_manifest(socket);
});
});
function ping() {
io.to("main").emit("ping");
};
function addClient(ip, id) {
let db = load_manifest();
const client_list = db.prepare("SELECT * FROM client_list;").all();
client_list.forEach((client)=> {
delete client.id;
});
if (JSON.stringify(client_list).includes(ip)) {
client_list.forEach((client, count) =>{
if (client.client_ip === ip) {
if (client.socket_id !== id) {
//update id in database
let query = `UPDATE client_list \
SET socket_id = ? \
WHERE client_ip = ? \
`;
let sent = db.prepare(query);
sent.run(id, ip);
console.log(`Updated client id`);
};
};
});
}
else {
let client = {
"client_ip": ip,
"socket_id": id,
"server_ip": myip,
};
clientList.push(client);
console.log(`New client`);
add_manifest(clientList);
};
};
io.listen(6200);
console.log("Server online");
};
function add_manifest(clientList) {
let manifest = {
"server": ip.address(),
"clientList": clientList
};
let db = load_manifest();
const client_list = db.prepare("SELECT * FROM client_list;").all();
try {
clientList.forEach((client)=> {
if (! (JSON.stringify(client_list).includes(client.socket_id))) {
let query = `INSERT INTO client_list(client_ip, socket_id, server_ip) VALUES(?, ?, ?)`;
let sent = db.prepare(query);
sent.run(client.client_ip, client.socket_id, client.server_ip);
}
});
}
catch (err) {
console.log(`ERROR ${err}`);
};
};
function get_manifest(socket) {
let db = load_manifest();
let manifest = {
"server_ip": myip,
"clientList": [],
"Approved_Files": {}
}
const client_list = db.prepare("SELECT * FROM client_list;").all();
client_list.forEach((client)=> {
delete client.id;
manifest.clientList.push(client);
});
db.close();
console.log(`THIS SERVER manifest: ${JSON.stringify(manifest, null, 2)}`);
socket.emit("return_manifest", manifest);
};
function does_manifest_exist() {
let manifest_file = "./manifest/manifest.db";
if (fs.existsSync(manifest_file)) {
console.log(`Found Manifest`);
let db = load_manifest();
return db;
}
else {
console.log(`Could not find Manifest, Creating one now`);
let db = create_manifest();
return db;
};
};
function create_manifest() {
let db;
try {
db = new Database('./manifest/manifest.db');
//Manifest exists
console.log(`Database created`);
let query = `CREATE TABLE IF NOT EXISTS client_list (\
client_ip TEXT NOT NULL,\
socket_id TEXT NOT NULL,\
server_ip TEXT NOT NULL,\
id INT AUTO_INCREMENT PRIMARY KEY\
);`
try {
db.exec(query);
console.log(`Created client_list table`);
}
catch (err) {
console.log(err);
};
let query2 = `CREATE TABLE IF NOT EXISTS Approved_Files ( \
id INT AUTO_INCREMENT PRIMARY KEY, \
manifest_hash TEXT NOT NULL, \
file_location TEXT NOT NULL, \
file_name TEXT NOT NULL, \
file_hash TEXT NOT NULL, \
name_hash TEXT NOT NULL, \
duplicate_no TEXT, \
last_modified TEXT, \
deleted TEXT, \
recycled TEXT, \
original_server_ip TEXT NOT NULL\
);`
try {
db.exec(query2);
console.log(`Created Approved_Files table`);
}
catch (err) {
console.log(err);
};
db.close();
return db;
}
catch (err){
console.log(err);
}
};
function load_manifest(){
const db = new Database('./manifest/manifest.db');
db.pragma('journal_mode = WAL');
return db;
};
//Start
const Start = new Promise((resolve, reject) => {
let db = does_manifest_exist();
resolve(db);
});
Start.then((db) => {
init_socketio();
});