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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topnic-https",
3
- "version": "1.2.8",
3
+ "version": "1.2.10",
4
4
  "main": "./src/main/index.js",
5
5
  "type": "commonjs",
6
6
  "scripts": {
@@ -1,12 +1,10 @@
1
- const http = require('http');
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.shares = new Map();
8
- this.server = http.createServer(this.handleRequest.bind(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(this.port, '0.0.0.0', () => {
30
- console.log(`🚀 خادم المشاركة يعمل على المنفذ ${this.port}`);
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(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}`
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
- async handleRequest(req, res) {
61
- const shareId = req.url.split('/')[1];
62
- const share = this.shares.get(shareId);
48
+ handleConnection(clientSocket) {
49
+ try {
50
+ const targetSocket = net.connect(this.targetPort, 'localhost');
63
51
 
64
- if (!share) {
65
- res.writeHead(404);
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
- 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
- });
55
+ clientSocket.on('error', (error) => {
56
+ console.error('❌ خطأ في اتصال العميل:', error.message);
57
+ targetSocket.destroy();
58
+ });
87
59
 
88
- req.pipe(proxyReq);
89
- }
60
+ targetSocket.on('error', (error) => {
61
+ console.error('❌ خطأ في الاتصال بالتطبيق المحلي:', error.message);
62
+ clientSocket.destroy();
63
+ });
90
64
 
91
- closeShare(shareId) {
92
- if (!this.shares.has(shareId)) {
93
- throw new Error('لم يتم العثور على المشاركة');
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
- getActiveShares() {
100
- return Array.from(this.shares.values());
71
+ close() {
72
+ this.server.close();
73
+ console.log('\n✅ تم إغلاق خادم المشاركة');
101
74
  }
102
75
  }
103
76