topnic-https 1.2.5 → 1.2.6

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.5",
3
+ "version": "1.2.6",
4
4
  "main": "./src/main/index.js",
5
5
  "type": "commonjs",
6
6
  "scripts": {
@@ -91,6 +91,7 @@
91
91
  "chokidar": "^4.0.1",
92
92
  "commander": "^12.1.0",
93
93
  "crypto": "^1.0.1",
94
+ "dns": "^0.2.2",
94
95
  "electron": "^33.2.0",
95
96
  "fs": "^0.0.1-security",
96
97
  "https": "^1.0.0",
@@ -3,75 +3,60 @@ const WebSocket = require('ws');
3
3
  const { nanoid } = require('nanoid/non-secure');
4
4
  const os = require('os');
5
5
  const net = require('net');
6
+ const dns = require('dns');
6
7
 
7
8
  class DirectServer {
8
9
  constructor() {
9
10
  this.connections = new Map();
10
11
  this.server = http.createServer(this.handleRequest.bind(this));
11
12
  this.wss = new WebSocket.Server({ server: this.server });
12
- this.publicIp = this.getPublicIp();
13
+ this.publicIp = null;
13
14
  }
14
15
 
15
- getPublicIp() {
16
- const interfaces = os.networkInterfaces();
17
- for (const iface of Object.values(interfaces)) {
18
- const nonLocalIpv4 = iface.find(addr => addr.family === 'IPv4' && !addr.internal);
19
- if (nonLocalIpv4) return nonLocalIpv4.address;
20
- }
21
- return 'localhost';
22
- }
23
-
24
- async isPortAvailable(port) {
16
+ async getPublicIp() {
25
17
  return new Promise((resolve) => {
26
- const tester = net.createServer()
27
- .once('error', () => resolve(false))
28
- .once('listening', () => {
29
- tester.once('close', () => resolve(true)).close();
30
- })
31
- .listen(port);
32
- });
33
- }
34
-
35
- async start(port = 3500) {
36
- if (!(await this.isPortAvailable(port))) {
37
- console.log(`المنفذ ${port} مشغول، جاري المحاولة على منفذ آخر...`);
38
- port = await this.findAvailablePort(3501);
39
- }
18
+ // أولاً نحاول الحصول على IP المحلي
19
+ const interfaces = os.networkInterfaces();
20
+ let localIp = 'localhost';
21
+
22
+ for (const iface of Object.values(interfaces)) {
23
+ const nonLocalIpv4 = iface.find(addr =>
24
+ addr.family === 'IPv4' &&
25
+ !addr.internal &&
26
+ addr.address !== '127.0.0.1'
27
+ );
28
+ if (nonLocalIpv4) {
29
+ localIp = nonLocalIpv4.address;
30
+ break;
31
+ }
32
+ }
40
33
 
41
- return new Promise((resolve) => {
42
- this.server.listen(port, '0.0.0.0', () => {
43
- console.log(`🚀 خادم المشاركة يعمل على المنفذ ${port}`);
44
- console.log(`📡 عنوان IP: ${this.publicIp}:${port}`);
45
- resolve(port);
34
+ // نحاول الحصول على IP العام
35
+ dns.lookup(os.hostname(), (err, address, family) => {
36
+ if (!err && address && address !== '127.0.0.1') {
37
+ resolve(address);
38
+ } else {
39
+ resolve(localIp);
40
+ }
46
41
  });
47
42
  });
48
43
  }
49
44
 
50
- async findAvailablePort(startPort) {
51
- let port = startPort;
52
- while (!(await this.isPortAvailable(port))) {
53
- port++;
54
- }
55
- return port;
56
- }
57
-
58
- async verifyTargetPort(port) {
45
+ async start(port = 3500) {
59
46
  try {
60
- const response = await new Promise((resolve, reject) => {
61
- const req = http.request({
62
- hostname: 'localhost',
63
- port: port,
64
- path: '/',
65
- method: 'GET',
66
- timeout: 2000
67
- }, resolve);
68
-
69
- req.on('error', reject);
70
- req.end();
47
+ this.publicIp = await this.getPublicIp();
48
+
49
+ return new Promise((resolve) => {
50
+ this.server.listen(port, '0.0.0.0', () => {
51
+ console.log(`🚀 خادم المشاركة يعمل على المنفذ ${port}`);
52
+ console.log(`📡 عنوان IP: ${this.publicIp}:${port}`);
53
+ console.log(`🌐 يمكنك الوصول محلياً عبر: http://localhost:${port}`);
54
+ resolve(port);
55
+ });
71
56
  });
72
- return true;
73
57
  } catch (error) {
74
- return false;
58
+ console.error('خطأ في بدء الخادم:', error);
59
+ throw error;
75
60
  }
76
61
  }
77
62
 
@@ -91,12 +76,10 @@ class DirectServer {
91
76
  const connectionId = urlParts[1];
92
77
  const connection = this.connections.get(connectionId);
93
78
 
94
- // إضافة CORS headers
95
79
  res.setHeader('Access-Control-Allow-Origin', '*');
96
80
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
97
81
  res.setHeader('Access-Control-Allow-Headers', '*');
98
82
 
99
- // معالجة طلبات OPTIONS
100
83
  if (req.method === 'OPTIONS') {
101
84
  res.writeHead(200);
102
85
  res.end();
@@ -109,17 +92,8 @@ class DirectServer {
109
92
  return;
110
93
  }
111
94
 
112
- // التحقق من أن الخدمة المحلية تعمل
113
- if (!(await this.verifyTargetPort(connection.port))) {
114
- res.writeHead(502);
115
- res.end('الخدمة المحلية غير متاحة');
116
- return;
117
- }
118
-
119
- // إعادة بناء المسار الأصلي
120
95
  const originalPath = urlParts.length > 2 ? '/' + urlParts.slice(2).join('/') : '/';
121
96
 
122
- // إضافة معلومات التوجيه
123
97
  const options = {
124
98
  hostname: 'localhost',
125
99
  port: connection.port,
@@ -129,14 +103,12 @@ class DirectServer {
129
103
  ...req.headers,
130
104
  'X-Forwarded-Host': req.headers.host,
131
105
  'X-Forwarded-Proto': 'http',
132
- 'X-Forwarded-For': req.socket.remoteAddress,
106
+ 'X-Real-IP': req.socket.remoteAddress,
133
107
  host: `localhost:${connection.port}`
134
108
  }
135
109
  };
136
110
 
137
- // إنشاء طلب التوجيه
138
111
  const proxyReq = http.request(options, (proxyRes) => {
139
- // نسخ headers الاستجابة
140
112
  Object.keys(proxyRes.headers).forEach(key => {
141
113
  res.setHeader(key, proxyRes.headers[key]);
142
114
  });
@@ -145,7 +117,6 @@ class DirectServer {
145
117
  proxyRes.pipe(res);
146
118
  });
147
119
 
148
- // معالجة أخطاء التوجيه
149
120
  proxyReq.on('error', (error) => {
150
121
  console.error('خطأ في التوجيه:', error);
151
122
  if (!res.headersSent) {
@@ -154,7 +125,6 @@ class DirectServer {
154
125
  }
155
126
  });
156
127
 
157
- // إرسال البيانات إلى التطبيق المحلي
158
128
  if (!req.readableEnded) {
159
129
  req.pipe(proxyReq);
160
130
  } else {