topnic-https 1.2.4 → 1.2.5

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.5",
4
4
  "main": "./src/main/index.js",
5
5
  "type": "commonjs",
6
6
  "scripts": {
@@ -91,6 +91,18 @@ class DirectServer {
91
91
  const connectionId = urlParts[1];
92
92
  const connection = this.connections.get(connectionId);
93
93
 
94
+ // إضافة CORS headers
95
+ res.setHeader('Access-Control-Allow-Origin', '*');
96
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
97
+ res.setHeader('Access-Control-Allow-Headers', '*');
98
+
99
+ // معالجة طلبات OPTIONS
100
+ if (req.method === 'OPTIONS') {
101
+ res.writeHead(200);
102
+ res.end();
103
+ return;
104
+ }
105
+
94
106
  if (!connection) {
95
107
  res.writeHead(404);
96
108
  res.end('رابط المشاركة غير صالح');
@@ -105,8 +117,9 @@ class DirectServer {
105
117
  }
106
118
 
107
119
  // إعادة بناء المسار الأصلي
108
- const originalPath = '/' + urlParts.slice(2).join('/');
120
+ const originalPath = urlParts.length > 2 ? '/' + urlParts.slice(2).join('/') : '/';
109
121
 
122
+ // إضافة معلومات التوجيه
110
123
  const options = {
111
124
  hostname: 'localhost',
112
125
  port: connection.port,
@@ -114,21 +127,34 @@ class DirectServer {
114
127
  method: req.method,
115
128
  headers: {
116
129
  ...req.headers,
130
+ 'X-Forwarded-Host': req.headers.host,
131
+ 'X-Forwarded-Proto': 'http',
132
+ 'X-Forwarded-For': req.socket.remoteAddress,
117
133
  host: `localhost:${connection.port}`
118
134
  }
119
135
  };
120
136
 
137
+ // إنشاء طلب التوجيه
121
138
  const proxyReq = http.request(options, (proxyRes) => {
122
- res.writeHead(proxyRes.statusCode, proxyRes.headers);
139
+ // نسخ headers الاستجابة
140
+ Object.keys(proxyRes.headers).forEach(key => {
141
+ res.setHeader(key, proxyRes.headers[key]);
142
+ });
143
+
144
+ res.writeHead(proxyRes.statusCode);
123
145
  proxyRes.pipe(res);
124
146
  });
125
147
 
148
+ // معالجة أخطاء التوجيه
126
149
  proxyReq.on('error', (error) => {
127
150
  console.error('خطأ في التوجيه:', error);
128
- res.writeHead(502);
129
- res.end('خطأ في الاتصال بالتطبيق المحلي');
151
+ if (!res.headersSent) {
152
+ res.writeHead(502);
153
+ res.end('خطأ في الاتصال بالتطبيق المحلي');
154
+ }
130
155
  });
131
156
 
157
+ // إرسال البيانات إلى التطبيق المحلي
132
158
  if (!req.readableEnded) {
133
159
  req.pipe(proxyReq);
134
160
  } else {
@@ -138,8 +164,10 @@ class DirectServer {
138
164
  connection.visitors++;
139
165
  } catch (error) {
140
166
  console.error('خطأ في معالجة الطلب:', error);
141
- res.writeHead(500);
142
- res.end('خطأ داخلي في الخادم');
167
+ if (!res.headersSent) {
168
+ res.writeHead(500);
169
+ res.end('خطأ داخلي في الخادم');
170
+ }
143
171
  }
144
172
  }
145
173