topnic-https 1.2.4 → 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.4",
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,21 +76,23 @@ class DirectServer {
91
76
  const connectionId = urlParts[1];
92
77
  const connection = this.connections.get(connectionId);
93
78
 
94
- if (!connection) {
95
- res.writeHead(404);
96
- res.end('رابط المشاركة غير صالح');
79
+ res.setHeader('Access-Control-Allow-Origin', '*');
80
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
81
+ res.setHeader('Access-Control-Allow-Headers', '*');
82
+
83
+ if (req.method === 'OPTIONS') {
84
+ res.writeHead(200);
85
+ res.end();
97
86
  return;
98
87
  }
99
88
 
100
- // التحقق من أن الخدمة المحلية تعمل
101
- if (!(await this.verifyTargetPort(connection.port))) {
102
- res.writeHead(502);
103
- res.end('الخدمة المحلية غير متاحة');
89
+ if (!connection) {
90
+ res.writeHead(404);
91
+ res.end('رابط المشاركة غير صالح');
104
92
  return;
105
93
  }
106
94
 
107
- // إعادة بناء المسار الأصلي
108
- const originalPath = '/' + urlParts.slice(2).join('/');
95
+ const originalPath = urlParts.length > 2 ? '/' + urlParts.slice(2).join('/') : '/';
109
96
 
110
97
  const options = {
111
98
  hostname: 'localhost',
@@ -114,19 +101,28 @@ class DirectServer {
114
101
  method: req.method,
115
102
  headers: {
116
103
  ...req.headers,
104
+ 'X-Forwarded-Host': req.headers.host,
105
+ 'X-Forwarded-Proto': 'http',
106
+ 'X-Real-IP': req.socket.remoteAddress,
117
107
  host: `localhost:${connection.port}`
118
108
  }
119
109
  };
120
110
 
121
111
  const proxyReq = http.request(options, (proxyRes) => {
122
- res.writeHead(proxyRes.statusCode, proxyRes.headers);
112
+ Object.keys(proxyRes.headers).forEach(key => {
113
+ res.setHeader(key, proxyRes.headers[key]);
114
+ });
115
+
116
+ res.writeHead(proxyRes.statusCode);
123
117
  proxyRes.pipe(res);
124
118
  });
125
119
 
126
120
  proxyReq.on('error', (error) => {
127
121
  console.error('خطأ في التوجيه:', error);
128
- res.writeHead(502);
129
- res.end('خطأ في الاتصال بالتطبيق المحلي');
122
+ if (!res.headersSent) {
123
+ res.writeHead(502);
124
+ res.end('خطأ في الاتصال بالتطبيق المحلي');
125
+ }
130
126
  });
131
127
 
132
128
  if (!req.readableEnded) {
@@ -138,8 +134,10 @@ class DirectServer {
138
134
  connection.visitors++;
139
135
  } catch (error) {
140
136
  console.error('خطأ في معالجة الطلب:', error);
141
- res.writeHead(500);
142
- res.end('خطأ داخلي في الخادم');
137
+ if (!res.headersSent) {
138
+ res.writeHead(500);
139
+ res.end('خطأ داخلي في الخادم');
140
+ }
143
141
  }
144
142
  }
145
143