topnic-https 1.2.7 → 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 +154 -32
package/package.json
CHANGED
@@ -1,57 +1,179 @@
|
|
1
|
-
const
|
1
|
+
const http = require('http');
|
2
|
+
const { nanoid } = require('nanoid/non-secure');
|
3
|
+
const os = require('os');
|
2
4
|
|
3
5
|
class ShareManager {
|
4
6
|
constructor() {
|
5
|
-
this.
|
6
|
-
this.server =
|
7
|
+
this.shares = new Map();
|
8
|
+
this.server = http.createServer(this.handleRequest.bind(this));
|
9
|
+
this.port = 3500;
|
7
10
|
}
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
getLocalIp() {
|
13
|
+
const interfaces = os.networkInterfaces();
|
14
|
+
for (const iface of Object.values(interfaces)) {
|
15
|
+
const nonLocalIpv4 = iface.find(addr =>
|
16
|
+
addr.family === 'IPv4' &&
|
17
|
+
!addr.internal &&
|
18
|
+
addr.address !== '127.0.0.1'
|
19
|
+
);
|
20
|
+
if (nonLocalIpv4) {
|
21
|
+
return nonLocalIpv4.address;
|
22
|
+
}
|
23
|
+
}
|
24
|
+
return 'localhost';
|
25
|
+
}
|
26
|
+
|
27
|
+
async start() {
|
28
|
+
return new Promise((resolve) => {
|
29
|
+
this.server.listen(this.port, '0.0.0.0', () => {
|
30
|
+
console.log(`🚀 خادم المشاركة يعمل على المنفذ ${this.port}`);
|
31
|
+
resolve();
|
32
|
+
});
|
33
|
+
});
|
34
|
+
}
|
35
|
+
|
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}`
|
46
|
+
};
|
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;
|
11
58
|
}
|
12
59
|
|
13
|
-
async
|
60
|
+
async handleRequest(req, res) {
|
14
61
|
try {
|
15
|
-
const
|
16
|
-
const
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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}`, '') || '/';
|
21
91
|
|
22
|
-
const
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
+
}
|
28
104
|
};
|
29
105
|
|
30
|
-
|
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
|
+
});
|
31
125
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
console.log('--------------------------------\n');
|
126
|
+
if (!req.readableEnded) {
|
127
|
+
req.pipe(proxyReq);
|
128
|
+
} else {
|
129
|
+
proxyReq.end();
|
130
|
+
}
|
38
131
|
|
39
|
-
return share;
|
40
132
|
} catch (error) {
|
41
|
-
|
133
|
+
console.error('❌ خطأ في معالجة الطلب:', error);
|
134
|
+
if (!res.headersSent) {
|
135
|
+
res.writeHead(500);
|
136
|
+
res.end('خطأ داخلي في الخادم');
|
137
|
+
}
|
42
138
|
}
|
43
139
|
}
|
44
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
|
+
|
45
166
|
closeShare(shareId) {
|
46
|
-
|
47
|
-
if (!share) {
|
167
|
+
if (!this.shares.has(shareId)) {
|
48
168
|
throw new Error('لم يتم العثور على المشاركة');
|
49
169
|
}
|
50
|
-
|
51
|
-
this.server.removeConnection(shareId);
|
52
|
-
this.activeShares.delete(shareId);
|
170
|
+
this.shares.delete(shareId);
|
53
171
|
console.log(`\n✅ تم إغلاق المشاركة: ${shareId}`);
|
54
172
|
}
|
173
|
+
|
174
|
+
getActiveShares() {
|
175
|
+
return Array.from(this.shares.values());
|
176
|
+
}
|
55
177
|
}
|
56
178
|
|
57
179
|
module.exports = new ShareManager();
|