topnic-https 1.2.6 → 1.2.8
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 -2
- package/src/sharing/shareManager.js +85 -43
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "topnic-https",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.8",
|
4
4
|
"main": "./src/main/index.js",
|
5
5
|
"type": "commonjs",
|
6
6
|
"scripts": {
|
@@ -95,7 +95,6 @@
|
|
95
95
|
"electron": "^33.2.0",
|
96
96
|
"fs": "^0.0.1-security",
|
97
97
|
"https": "^1.0.0",
|
98
|
-
"localtunnel": "^2.0.2",
|
99
98
|
"nanoid": "^5.0.8",
|
100
99
|
"net": "^1.0.2",
|
101
100
|
"ngrok": "^5.0.0-beta.2",
|
@@ -1,61 +1,103 @@
|
|
1
|
-
const
|
1
|
+
const http = require('http');
|
2
|
+
const { nanoid } = require('nanoid/non-secure');
|
3
|
+
const os = require('os');
|
2
4
|
|
3
5
|
class ShareManager {
|
4
6
|
constructor() {
|
5
|
-
this.
|
6
|
-
this.server =
|
7
|
+
this.shares = new Map();
|
8
|
+
this.server = http.createServer(this.handleRequest.bind(this));
|
9
|
+
this.port = 3500;
|
7
10
|
}
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
getLocalIp() {
|
13
|
+
const interfaces = os.networkInterfaces();
|
14
|
+
for (const iface of Object.values(interfaces)) {
|
15
|
+
const nonLocalIpv4 = iface.find(addr =>
|
16
|
+
addr.family === 'IPv4' &&
|
17
|
+
!addr.internal &&
|
18
|
+
addr.address !== '127.0.0.1'
|
19
|
+
);
|
20
|
+
if (nonLocalIpv4) {
|
21
|
+
return nonLocalIpv4.address;
|
22
|
+
}
|
23
|
+
}
|
24
|
+
return 'localhost';
|
11
25
|
}
|
12
26
|
|
13
|
-
async
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
url: shareUrl,
|
21
|
-
accessCode,
|
22
|
-
port: port,
|
23
|
-
startTime: Date.now()
|
24
|
-
};
|
25
|
-
|
26
|
-
this.activeShares.set(id, share);
|
27
|
-
|
28
|
-
return {
|
29
|
-
id,
|
30
|
-
url: shareUrl,
|
31
|
-
accessCode,
|
32
|
-
localUrl: `http://localhost:${port}`
|
33
|
-
};
|
34
|
-
} catch (error) {
|
35
|
-
throw new Error(`فشل إنشاء المشاركة: ${error.message}`);
|
36
|
-
}
|
27
|
+
async start() {
|
28
|
+
return new Promise((resolve) => {
|
29
|
+
this.server.listen(this.port, '0.0.0.0', () => {
|
30
|
+
console.log(`🚀 خادم المشاركة يعمل على المنفذ ${this.port}`);
|
31
|
+
resolve();
|
32
|
+
});
|
33
|
+
});
|
37
34
|
}
|
38
35
|
|
39
|
-
|
40
|
-
const
|
36
|
+
async createShare(targetPort) {
|
37
|
+
const id = nanoid(6);
|
38
|
+
const localIp = this.getLocalIp();
|
39
|
+
|
40
|
+
const share = {
|
41
|
+
id,
|
42
|
+
targetPort,
|
43
|
+
createdAt: Date.now(),
|
44
|
+
localUrl: `http://localhost:${this.port}/${id}`,
|
45
|
+
publicUrl: `http://${localIp}:${this.port}/${id}`
|
46
|
+
};
|
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
|
+
}
|
59
|
+
|
60
|
+
async handleRequest(req, res) {
|
61
|
+
const shareId = req.url.split('/')[1];
|
62
|
+
const share = this.shares.get(shareId);
|
63
|
+
|
41
64
|
if (!share) {
|
42
|
-
|
65
|
+
res.writeHead(404);
|
66
|
+
res.end('رابط المشاركة غير صالح');
|
67
|
+
return;
|
43
68
|
}
|
44
69
|
|
45
|
-
|
46
|
-
|
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
|
+
};
|
77
|
+
|
78
|
+
const proxyReq = http.request(options, (proxyRes) => {
|
79
|
+
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
80
|
+
proxyRes.pipe(res);
|
81
|
+
});
|
82
|
+
|
83
|
+
proxyReq.on('error', () => {
|
84
|
+
res.writeHead(502);
|
85
|
+
res.end('خطأ في الاتصال بالتطبيق المحلي');
|
86
|
+
});
|
87
|
+
|
88
|
+
req.pipe(proxyReq);
|
47
89
|
}
|
48
90
|
|
49
|
-
|
50
|
-
|
51
|
-
|
91
|
+
closeShare(shareId) {
|
92
|
+
if (!this.shares.has(shareId)) {
|
93
|
+
throw new Error('لم يتم العثور على المشاركة');
|
94
|
+
}
|
95
|
+
this.shares.delete(shareId);
|
96
|
+
console.log(`\n✅ تم إغلاق المشاركة: ${shareId}`);
|
97
|
+
}
|
52
98
|
|
53
|
-
|
54
|
-
return
|
55
|
-
...share,
|
56
|
-
...stats,
|
57
|
-
uptime: Math.floor((Date.now() - share.startTime) / 1000 / 60) // بالدقائق
|
58
|
-
};
|
99
|
+
getActiveShares() {
|
100
|
+
return Array.from(this.shares.values());
|
59
101
|
}
|
60
102
|
}
|
61
103
|
|