topnic-https 1.2.9 → 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.9",
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,155 +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) {
48
+ handleConnection(clientSocket) {
61
49
  try {
62
- const shareId = req.url.split('/')[1];
63
- const share = this.shares.get(shareId);
64
-
65
- // إضافة CORS headers
66
- res.setHeader('Access-Control-Allow-Origin', '*');
67
- res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
68
- res.setHeader('Access-Control-Allow-Headers', '*');
69
-
70
- if (req.method === 'OPTIONS') {
71
- res.writeHead(200);
72
- res.end();
73
- return;
74
- }
50
+ const targetSocket = net.connect(this.targetPort, 'localhost');
75
51
 
76
- if (!share) {
77
- res.writeHead(404);
78
- res.end('رابط المشاركة غير صالح');
79
- return;
80
- }
81
-
82
- // التحقق من أن التطبيق المحلي يعمل
83
- const isTargetAvailable = await this.checkTargetAvailability(share.targetPort);
84
- if (!isTargetAvailable) {
85
- res.writeHead(502);
86
- res.end(`التطبيق المحلي غير متاح على المنفذ ${share.targetPort}`);
87
- return;
88
- }
89
-
90
- const targetPath = req.url.replace(`/${shareId}`, '') || '/';
91
-
92
- const options = {
93
- hostname: 'localhost',
94
- port: share.targetPort,
95
- path: targetPath,
96
- method: req.method,
97
- headers: {
98
- ...req.headers,
99
- host: `localhost:${share.targetPort}`,
100
- 'x-forwarded-host': req.headers.host,
101
- 'x-forwarded-proto': 'http',
102
- 'x-forwarded-for': req.socket.remoteAddress
103
- }
104
- };
52
+ clientSocket.pipe(targetSocket);
53
+ targetSocket.pipe(clientSocket);
105
54
 
106
- console.log(`📡 توجيه الطلب: ${req.method} ${targetPath} -> localhost:${share.targetPort}`);
107
-
108
- const proxyReq = http.request(options, (proxyRes) => {
109
- // نسخ headers الاستجابة
110
- Object.keys(proxyRes.headers).forEach(key => {
111
- res.setHeader(key, proxyRes.headers[key]);
112
- });
113
-
114
- res.writeHead(proxyRes.statusCode);
115
- proxyRes.pipe(res);
55
+ clientSocket.on('error', (error) => {
56
+ console.error('❌ خطأ في اتصال العميل:', error.message);
57
+ targetSocket.destroy();
116
58
  });
117
59
 
118
- proxyReq.on('error', (error) => {
119
- console.error('❌ خطأ في التوجيه:', error.message);
120
- if (!res.headersSent) {
121
- res.writeHead(502);
122
- res.end(`خطأ في الاتصال بالتطبيق المحلي: ${error.message}`);
123
- }
60
+ targetSocket.on('error', (error) => {
61
+ console.error('❌ خطأ في الاتصال بالتطبيق المحلي:', error.message);
62
+ clientSocket.destroy();
124
63
  });
125
64
 
126
- if (!req.readableEnded) {
127
- req.pipe(proxyReq);
128
- } else {
129
- proxyReq.end();
130
- }
131
-
132
65
  } catch (error) {
133
- console.error('❌ خطأ في معالجة الطلب:', error);
134
- if (!res.headersSent) {
135
- res.writeHead(500);
136
- res.end('خطأ داخلي في الخادم');
137
- }
138
- }
139
- }
140
-
141
- async checkTargetAvailability(port) {
142
- return new Promise((resolve) => {
143
- const testReq = http.request({
144
- hostname: 'localhost',
145
- port: port,
146
- path: '/',
147
- method: 'HEAD',
148
- timeout: 1000
149
- }, (res) => {
150
- resolve(res.statusCode < 500);
151
- });
152
-
153
- testReq.on('error', () => {
154
- resolve(false);
155
- });
156
-
157
- testReq.on('timeout', () => {
158
- testReq.destroy();
159
- resolve(false);
160
- });
161
-
162
- testReq.end();
163
- });
164
- }
165
-
166
- closeShare(shareId) {
167
- if (!this.shares.has(shareId)) {
168
- throw new Error('لم يتم العثور على المشاركة');
66
+ console.error('❌ خطأ في معالجة الاتصال:', error);
67
+ clientSocket.destroy();
169
68
  }
170
- this.shares.delete(shareId);
171
- console.log(`\n✅ تم إغلاق المشاركة: ${shareId}`);
172
69
  }
173
70
 
174
- getActiveShares() {
175
- return Array.from(this.shares.values());
71
+ close() {
72
+ this.server.close();
73
+ console.log('\n✅ تم إغلاق خادم المشاركة');
176
74
  }
177
75
  }
178
76