topnic-https 1.2.8 → 1.2.10
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/package.json +1 -1
- package/src/sharing/shareManager.js +36 -63
package/package.json
CHANGED
@@ -1,12 +1,10 @@
|
|
1
|
-
const
|
2
|
-
const { nanoid } = require('nanoid/non-secure');
|
1
|
+
const net = require('net');
|
3
2
|
const os = require('os');
|
4
3
|
|
5
4
|
class ShareManager {
|
6
5
|
constructor() {
|
7
|
-
this.
|
8
|
-
this.
|
9
|
-
this.port = 3500;
|
6
|
+
this.server = net.createServer(this.handleConnection.bind(this));
|
7
|
+
this.targetPort = null;
|
10
8
|
}
|
11
9
|
|
12
10
|
getLocalIp() {
|
@@ -24,80 +22,55 @@ class ShareManager {
|
|
24
22
|
return 'localhost';
|
25
23
|
}
|
26
24
|
|
27
|
-
async start() {
|
25
|
+
async start(port = 3500) {
|
28
26
|
return new Promise((resolve) => {
|
29
|
-
this.server.listen(
|
30
|
-
|
27
|
+
this.server.listen(port, '0.0.0.0', () => {
|
28
|
+
const localIp = this.getLocalIp();
|
29
|
+
console.log('\n🚀 تم بدء خادم المشاركة!');
|
30
|
+
console.log('--------------------------------');
|
31
|
+
console.log(`🏠 محلي: http://localhost:${port}`);
|
32
|
+
console.log(`🌍 شبكة محلية: http://${localIp}:${port}`);
|
33
|
+
console.log('--------------------------------\n');
|
31
34
|
resolve();
|
32
35
|
});
|
33
36
|
});
|
34
37
|
}
|
35
38
|
|
36
|
-
async createShare(
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
targetPort,
|
43
|
-
createdAt: Date.now(),
|
44
|
-
localUrl: `http://localhost:${this.port}/${id}`,
|
45
|
-
publicUrl: `http://${localIp}:${this.port}/${id}`
|
39
|
+
async createShare(port) {
|
40
|
+
this.targetPort = port;
|
41
|
+
console.log(`📡 جاري توجيه الاتصالات إلى المنفذ ${port}`);
|
42
|
+
return {
|
43
|
+
localUrl: `http://localhost:${this.server.address().port}`,
|
44
|
+
publicUrl: `http://${this.getLocalIp()}:${this.server.address().port}`
|
46
45
|
};
|
47
|
-
|
48
|
-
this.shares.set(id, share);
|
49
|
-
|
50
|
-
console.log('\n🌐 تم إنشاء المشاركة بنجاح!');
|
51
|
-
console.log('--------------------------------');
|
52
|
-
console.log('📡 الروابط المتاحة:');
|
53
|
-
console.log(`🏠 محلي: ${share.localUrl}`);
|
54
|
-
console.log(`🌍 شبكة محلية: ${share.publicUrl}`);
|
55
|
-
console.log('--------------------------------\n');
|
56
|
-
|
57
|
-
return share;
|
58
46
|
}
|
59
47
|
|
60
|
-
|
61
|
-
|
62
|
-
|
48
|
+
handleConnection(clientSocket) {
|
49
|
+
try {
|
50
|
+
const targetSocket = net.connect(this.targetPort, 'localhost');
|
63
51
|
|
64
|
-
|
65
|
-
|
66
|
-
res.end('رابط المشاركة غير صالح');
|
67
|
-
return;
|
68
|
-
}
|
69
|
-
|
70
|
-
const options = {
|
71
|
-
hostname: 'localhost',
|
72
|
-
port: share.targetPort,
|
73
|
-
path: req.url.replace(`/${shareId}`, '') || '/',
|
74
|
-
method: req.method,
|
75
|
-
headers: req.headers
|
76
|
-
};
|
52
|
+
clientSocket.pipe(targetSocket);
|
53
|
+
targetSocket.pipe(clientSocket);
|
77
54
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
proxyReq.on('error', () => {
|
84
|
-
res.writeHead(502);
|
85
|
-
res.end('خطأ في الاتصال بالتطبيق المحلي');
|
86
|
-
});
|
55
|
+
clientSocket.on('error', (error) => {
|
56
|
+
console.error('❌ خطأ في اتصال العميل:', error.message);
|
57
|
+
targetSocket.destroy();
|
58
|
+
});
|
87
59
|
|
88
|
-
|
89
|
-
|
60
|
+
targetSocket.on('error', (error) => {
|
61
|
+
console.error('❌ خطأ في الاتصال بالتطبيق المحلي:', error.message);
|
62
|
+
clientSocket.destroy();
|
63
|
+
});
|
90
64
|
|
91
|
-
|
92
|
-
|
93
|
-
|
65
|
+
} catch (error) {
|
66
|
+
console.error('❌ خطأ في معالجة الاتصال:', error);
|
67
|
+
clientSocket.destroy();
|
94
68
|
}
|
95
|
-
this.shares.delete(shareId);
|
96
|
-
console.log(`\n✅ تم إغلاق المشاركة: ${shareId}`);
|
97
69
|
}
|
98
70
|
|
99
|
-
|
100
|
-
|
71
|
+
close() {
|
72
|
+
this.server.close();
|
73
|
+
console.log('\n✅ تم إغلاق خادم المشاركة');
|
101
74
|
}
|
102
75
|
}
|
103
76
|
|