topnic-https 1.2.8 → 1.2.9
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 +1 -1
- package/src/sharing/shareManager.js +97 -22
package/package.json
CHANGED
@@ -58,34 +58,109 @@ class ShareManager {
|
|
58
58
|
}
|
59
59
|
|
60
60
|
async handleRequest(req, res) {
|
61
|
-
|
62
|
-
|
61
|
+
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
|
+
}
|
75
|
+
|
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
|
+
};
|
105
|
+
|
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);
|
116
|
+
});
|
117
|
+
|
118
|
+
proxyReq.on('error', (error) => {
|
119
|
+
console.error('❌ خطأ في التوجيه:', error.message);
|
120
|
+
if (!res.headersSent) {
|
121
|
+
res.writeHead(502);
|
122
|
+
res.end(`خطأ في الاتصال بالتطبيق المحلي: ${error.message}`);
|
123
|
+
}
|
124
|
+
});
|
125
|
+
|
126
|
+
if (!req.readableEnded) {
|
127
|
+
req.pipe(proxyReq);
|
128
|
+
} else {
|
129
|
+
proxyReq.end();
|
130
|
+
}
|
63
131
|
|
64
|
-
|
65
|
-
|
66
|
-
res.
|
67
|
-
|
132
|
+
} catch (error) {
|
133
|
+
console.error('❌ خطأ في معالجة الطلب:', error);
|
134
|
+
if (!res.headersSent) {
|
135
|
+
res.writeHead(500);
|
136
|
+
res.end('خطأ داخلي في الخادم');
|
137
|
+
}
|
68
138
|
}
|
139
|
+
}
|
69
140
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
+
});
|
77
152
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
});
|
153
|
+
testReq.on('error', () => {
|
154
|
+
resolve(false);
|
155
|
+
});
|
82
156
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
157
|
+
testReq.on('timeout', () => {
|
158
|
+
testReq.destroy();
|
159
|
+
resolve(false);
|
160
|
+
});
|
87
161
|
|
88
|
-
|
162
|
+
testReq.end();
|
163
|
+
});
|
89
164
|
}
|
90
165
|
|
91
166
|
closeShare(shareId) {
|