topnic-https 1.2.7 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topnic-https",
3
- "version": "1.2.7",
3
+ "version": "1.2.8",
4
4
  "main": "./src/main/index.js",
5
5
  "type": "commonjs",
6
6
  "scripts": {
@@ -1,57 +1,104 @@
1
- const DirectServer = require('./directServer');
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.activeShares = new Map();
6
- this.server = DirectServer;
7
+ this.shares = new Map();
8
+ this.server = http.createServer(this.handleRequest.bind(this));
9
+ this.port = 3500;
7
10
  }
8
11
 
9
- async init() {
10
- await this.server.start();
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';
25
+ }
26
+
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
+ });
34
+ }
35
+
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;
11
58
  }
12
59
 
13
- async createShare(port) {
14
- try {
15
- const id = this.server.createConnection(port);
16
- const serverPort = this.server.getPort();
17
- const serverIp = await this.server.getPublicIp();
18
-
19
- const shareUrl = `http://${serverIp}:${serverPort}/${id}`;
20
- const localUrl = `http://localhost:${serverPort}/${id}`;
21
-
22
- const share = {
23
- id,
24
- url: shareUrl,
25
- localUrl,
26
- port: port,
27
- startTime: Date.now()
28
- };
29
-
30
- this.activeShares.set(id, share);
31
-
32
- console.log('\n🌐 تم إنشاء المشاركة بنجاح!');
33
- console.log('--------------------------------');
34
- console.log('📡 الروابط المتاحة:');
35
- console.log(`🏠 محلي: ${localUrl}`);
36
- console.log(`🌍 خارجي: ${shareUrl}`);
37
- console.log('--------------------------------\n');
38
-
39
- return share;
40
- } catch (error) {
41
- throw new Error(`فشل إنشاء المشاركة: ${error.message}`);
60
+ async handleRequest(req, res) {
61
+ const shareId = req.url.split('/')[1];
62
+ const share = this.shares.get(shareId);
63
+
64
+ if (!share) {
65
+ res.writeHead(404);
66
+ res.end('رابط المشاركة غير صالح');
67
+ return;
42
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
+ };
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);
43
89
  }
44
90
 
45
91
  closeShare(shareId) {
46
- const share = this.activeShares.get(shareId);
47
- if (!share) {
92
+ if (!this.shares.has(shareId)) {
48
93
  throw new Error('لم يتم العثور على المشاركة');
49
94
  }
50
-
51
- this.server.removeConnection(shareId);
52
- this.activeShares.delete(shareId);
95
+ this.shares.delete(shareId);
53
96
  console.log(`\n✅ تم إغلاق المشاركة: ${shareId}`);
54
97
  }
98
+
99
+ getActiveShares() {
100
+ return Array.from(this.shares.values());
101
+ }
55
102
  }
56
103
 
57
104
  module.exports = new ShareManager();