socketon 1.8.16 → 1.8.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Socket/index.js +0 -3
- package/package.json +1 -1
- package/lib/Socket/api-server.js +0 -257
package/lib/Socket/index.js
CHANGED
|
@@ -2,12 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const Defaults_1 = require("../Defaults");
|
|
4
4
|
const registration_1 = require("./registration");
|
|
5
|
-
const api_server_1 = require("./api-server");
|
|
6
5
|
const makeWASocket = (config) => ((0, registration_1.makeRegistrationSocket)({
|
|
7
6
|
...Defaults_1.DEFAULT_CONNECTION_CONFIG,
|
|
8
7
|
...config
|
|
9
8
|
}));
|
|
10
9
|
exports.default = makeWASocket;
|
|
11
10
|
exports.makeWASocket = makeWASocket;
|
|
12
|
-
exports.createAPIServer = api_server_1.createAPIServer;
|
|
13
|
-
exports.makeAPIServer = api_server_1.makeAPIServer;
|
package/package.json
CHANGED
package/lib/Socket/api-server.js
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makeAPIServer = exports.createAPIServer = void 0;
|
|
4
|
-
const http = require('http');
|
|
5
|
-
const crypto = require('crypto');
|
|
6
|
-
const axios = require('axios');
|
|
7
|
-
|
|
8
|
-
const VALID_USERS = {
|
|
9
|
-
'ibradecode': '088103'
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
let _apiKeys = new Map();
|
|
13
|
-
|
|
14
|
-
const generateAPIKey = () => {
|
|
15
|
-
const randomPart = crypto.randomBytes(12).toString('hex');
|
|
16
|
-
return `socketon_${randomPart}`;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const sendJSON = (res, statusCode, data) => {
|
|
20
|
-
res.writeHead(statusCode, { 'Content-Type': 'application/json' });
|
|
21
|
-
res.end(JSON.stringify(data));
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const parseBody = (req) => {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
let body = '';
|
|
27
|
-
req.on('data', chunk => body += chunk);
|
|
28
|
-
req.on('end', () => {
|
|
29
|
-
try {
|
|
30
|
-
resolve(body ? JSON.parse(body) : {});
|
|
31
|
-
} catch (err) {
|
|
32
|
-
resolve({});
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
req.on('error', reject);
|
|
36
|
-
});
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const authenticate = (authHeader) => {
|
|
40
|
-
if (!authHeader || !authHeader.startsWith('Basic ')) {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
const base64Credentials = authHeader.split(' ')[1];
|
|
45
|
-
const credentials = Buffer.from(base64Credentials, 'base64').toString('utf-8');
|
|
46
|
-
const [username, password] = credentials.split(':');
|
|
47
|
-
if (VALID_USERS[username] && VALID_USERS[username] === password) {
|
|
48
|
-
return { username, valid: true };
|
|
49
|
-
}
|
|
50
|
-
} catch (err) {}
|
|
51
|
-
return null;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const validateAPIKey = (apiKey) => {
|
|
55
|
-
if (!apiKey) return null;
|
|
56
|
-
const user = _apiKeys.get(apiKey);
|
|
57
|
-
return user || null;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const makeAPIServer = () => {
|
|
61
|
-
const handlers = {};
|
|
62
|
-
|
|
63
|
-
handlers.createAPIKey = async (body, auth) => {
|
|
64
|
-
if (!auth) {
|
|
65
|
-
return { status: 401, data: { error: 'Unauthorized - Invalid credentials' } };
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const apiKey = generateAPIKey();
|
|
69
|
-
_apiKeys.set(apiKey, {
|
|
70
|
-
username: auth.username,
|
|
71
|
-
createdAt: Date.now()
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
status: 201,
|
|
76
|
-
data: {
|
|
77
|
-
success: true,
|
|
78
|
-
apiKey: apiKey,
|
|
79
|
-
username: auth.username,
|
|
80
|
-
message: 'API Key created successfully'
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
handlers.deleteAPIKey = async (body, auth) => {
|
|
86
|
-
if (!auth) {
|
|
87
|
-
return { status: 401, data: { error: 'Unauthorized - Invalid credentials' } };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const apiKey = body.apiKey;
|
|
91
|
-
if (!apiKey) {
|
|
92
|
-
return { status: 400, data: { error: 'API Key is required' } };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (_apiKeys.has(apiKey)) {
|
|
96
|
-
_apiKeys.delete(apiKey);
|
|
97
|
-
return { status: 200, data: { success: true, message: 'API Key deleted' } };
|
|
98
|
-
}
|
|
99
|
-
return { status: 404, data: { error: 'API Key not found' } };
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
handlers.listAPIKeys = async (body, auth) => {
|
|
103
|
-
if (!auth) {
|
|
104
|
-
return { status: 401, data: { error: 'Unauthorized - Invalid credentials' } };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
const keys = [];
|
|
108
|
-
for (const [key, value] of _apiKeys.entries()) {
|
|
109
|
-
if (value.username === auth.username) {
|
|
110
|
-
keys.push({
|
|
111
|
-
apiKey: key,
|
|
112
|
-
createdAt: new Date(value.createdAt).toISOString()
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return { status: 200, data: { keys } };
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
handlers.stickerQuery = async (body, apiKey) => {
|
|
121
|
-
if (!apiKey) {
|
|
122
|
-
return { status: 401, data: { error: 'Invalid or missing API Key' } };
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const { query } = body;
|
|
126
|
-
if (!query) {
|
|
127
|
-
return { status: 400, data: { error: 'Query is required' } };
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const queries = query.split(',').map(q => q.trim()).filter(q => q);
|
|
131
|
-
if (queries.length === 0) {
|
|
132
|
-
return { status: 400, data: { error: 'No valid queries provided' } };
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const results = [];
|
|
136
|
-
for (const q of queries) {
|
|
137
|
-
try {
|
|
138
|
-
const response = await axios.get(`https://api.dhikaaa.me/search/sticker?q=${encodeURIComponent(q)}`, {
|
|
139
|
-
timeout: 10000
|
|
140
|
-
});
|
|
141
|
-
results.push({
|
|
142
|
-
query: q,
|
|
143
|
-
status: 'success',
|
|
144
|
-
data: response.data.results || []
|
|
145
|
-
});
|
|
146
|
-
} catch (err) {
|
|
147
|
-
results.push({
|
|
148
|
-
query: q,
|
|
149
|
-
status: 'error',
|
|
150
|
-
error: err.message
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return { status: 200, data: { results } };
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
handlers.newsletterMetadata = async (body, apiKey) => {
|
|
159
|
-
if (!apiKey) {
|
|
160
|
-
return { status: 401, data: { error: 'Invalid or missing API Key' } };
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const { urlCh } = body;
|
|
164
|
-
if (!urlCh) {
|
|
165
|
-
return { status: 400, data: { error: 'urlCh is required' } };
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const channelRegex = /whatsapp\.com\/channel\/([A-Za-z0-9_-]+)(?:\/(\d+))?/;
|
|
169
|
-
const match = urlCh.match(channelRegex);
|
|
170
|
-
|
|
171
|
-
if (!match) {
|
|
172
|
-
return { status: 400, data: { error: 'Invalid channel URL format' } };
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const inviteCode = match[1];
|
|
176
|
-
const serverId = match[2] || null;
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
const response = await axios.get(`https://api.dhikaaa.me/newsletter/metadata/${inviteCode}`, {
|
|
180
|
-
timeout: 10000
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
return {
|
|
184
|
-
status: 200,
|
|
185
|
-
data: {
|
|
186
|
-
success: true,
|
|
187
|
-
inviteCode: inviteCode,
|
|
188
|
-
serverId: serverId,
|
|
189
|
-
data: response.data
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
} catch (err) {
|
|
193
|
-
return {
|
|
194
|
-
status: 500,
|
|
195
|
-
data: {
|
|
196
|
-
success: false,
|
|
197
|
-
error: err.message,
|
|
198
|
-
inviteCode: inviteCode,
|
|
199
|
-
serverId: serverId
|
|
200
|
-
}
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
handlers.checkAPIKey = async (body, apiKey) => {
|
|
206
|
-
if (!apiKey) {
|
|
207
|
-
return { status: 401, data: { valid: false, error: 'Invalid API Key' } };
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return { status: 200, data: { valid: true, username: apiKey.username } };
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
return handlers;
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
const createAPIServer = (port = 3000) => {
|
|
217
|
-
const handlers = makeAPIServer();
|
|
218
|
-
|
|
219
|
-
const server = http.createServer(async (req, res) => {
|
|
220
|
-
const url = req.url.split('?')[0];
|
|
221
|
-
const method = req.method;
|
|
222
|
-
const authHeader = req.headers.authorization;
|
|
223
|
-
const apiKey = req.headers['x-api-key'] || (new URL(req.url, `http://localhost`).searchParams.get('apiKey'));
|
|
224
|
-
|
|
225
|
-
const auth = authenticate(authHeader);
|
|
226
|
-
const keyData = validateAPIKey(apiKey);
|
|
227
|
-
|
|
228
|
-
const body = await parseBody(req);
|
|
229
|
-
|
|
230
|
-
let result = { status: 404, data: { error: 'Not Found' } };
|
|
231
|
-
|
|
232
|
-
if (method === 'POST' && url === '/api/key/create') {
|
|
233
|
-
result = await handlers.createAPIKey(body, auth);
|
|
234
|
-
} else if (method === 'DELETE' && url === '/api/key/delete') {
|
|
235
|
-
result = await handlers.deleteAPIKey(body, auth);
|
|
236
|
-
} else if (method === 'GET' && url === '/api/key/list') {
|
|
237
|
-
result = await handlers.listAPIKeys(body, auth);
|
|
238
|
-
} else if (method === 'POST' && url === '/api/sticker/query') {
|
|
239
|
-
result = await handlers.stickerQuery(body, keyData);
|
|
240
|
-
} else if (method === 'POST' && url === '/api/newsletter/metadata') {
|
|
241
|
-
result = await handlers.newsletterMetadata(body, keyData);
|
|
242
|
-
} else if (method === 'GET' && url === '/api/key/check') {
|
|
243
|
-
result = await handlers.checkAPIKey(body, keyData);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
sendJSON(res, result.status, result.data);
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
server.listen(port, () => {
|
|
250
|
-
console.log(`[API Server] Running on port ${port}`);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
return { server, handlers };
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
exports.makeAPIServer = makeAPIServer;
|
|
257
|
-
exports.createAPIServer = createAPIServer;
|