topnic-https 1.2.1 → 1.2.3
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/directServer.js +200 -0
- package/src/sharing/shareManager.js +26 -33
package/package.json
CHANGED
@@ -0,0 +1,200 @@
|
|
1
|
+
const http = require('http');
|
2
|
+
const WebSocket = require('ws');
|
3
|
+
const { nanoid } = require('nanoid/non-secure');
|
4
|
+
const os = require('os');
|
5
|
+
const crypto = require('crypto');
|
6
|
+
|
7
|
+
class DirectServer {
|
8
|
+
constructor() {
|
9
|
+
this.connections = new Map();
|
10
|
+
this.server = http.createServer(this.handleRequest.bind(this));
|
11
|
+
this.wss = new WebSocket.Server({ server: this.server });
|
12
|
+
this.publicIp = this.getPublicIp();
|
13
|
+
}
|
14
|
+
|
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 start(port = 3500) {
|
25
|
+
return new Promise((resolve) => {
|
26
|
+
this.server.listen(port, '0.0.0.0', () => {
|
27
|
+
console.log(`🚀 خادم المشاركة يعمل على المنفذ ${port}`);
|
28
|
+
console.log(`📡 عنوان IP: ${this.publicIp}:${port}`);
|
29
|
+
resolve();
|
30
|
+
});
|
31
|
+
});
|
32
|
+
}
|
33
|
+
|
34
|
+
generateAccessCode() {
|
35
|
+
// إنشاء كود وصول من 6 أرقام
|
36
|
+
return Math.floor(100000 + Math.random() * 900000).toString();
|
37
|
+
}
|
38
|
+
|
39
|
+
createConnection(targetPort) {
|
40
|
+
const id = nanoid(6);
|
41
|
+
const accessCode = this.generateAccessCode();
|
42
|
+
|
43
|
+
this.connections.set(id, {
|
44
|
+
port: targetPort,
|
45
|
+
createdAt: Date.now(),
|
46
|
+
visitors: 0,
|
47
|
+
accessCode
|
48
|
+
});
|
49
|
+
|
50
|
+
return {
|
51
|
+
id,
|
52
|
+
accessCode
|
53
|
+
};
|
54
|
+
}
|
55
|
+
|
56
|
+
async handleRequest(req, res) {
|
57
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
58
|
+
|
59
|
+
const connectionId = req.url.split('/')[1];
|
60
|
+
const connection = this.connections.get(connectionId);
|
61
|
+
|
62
|
+
if (!connection) {
|
63
|
+
res.writeHead(404);
|
64
|
+
res.end('رابط المشاركة غير صالح');
|
65
|
+
return;
|
66
|
+
}
|
67
|
+
|
68
|
+
// التحقق من صفحة تسجيل الدخول
|
69
|
+
if (req.url === `/${connectionId}/login`) {
|
70
|
+
const loginPage = this.generateLoginPage(connectionId);
|
71
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
72
|
+
res.end(loginPage);
|
73
|
+
return;
|
74
|
+
}
|
75
|
+
|
76
|
+
// التحقق من كود الوصول
|
77
|
+
const providedCode = req.headers['x-access-code'];
|
78
|
+
if (providedCode !== connection.accessCode) {
|
79
|
+
res.writeHead(401);
|
80
|
+
res.end(JSON.stringify({ error: 'كود الوصول غير صحيح' }));
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
|
84
|
+
// إعادة توجيه الطلب إلى المنفذ المحلي
|
85
|
+
const options = {
|
86
|
+
hostname: 'localhost',
|
87
|
+
port: connection.port,
|
88
|
+
path: req.url.replace(`/${connectionId}`, '') || '/',
|
89
|
+
method: req.method,
|
90
|
+
headers: {
|
91
|
+
...req.headers,
|
92
|
+
host: `localhost:${connection.port}`
|
93
|
+
}
|
94
|
+
};
|
95
|
+
|
96
|
+
connection.visitors++;
|
97
|
+
|
98
|
+
const proxyReq = http.request(options, (proxyRes) => {
|
99
|
+
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
100
|
+
proxyRes.pipe(res);
|
101
|
+
});
|
102
|
+
|
103
|
+
proxyReq.on('error', (error) => {
|
104
|
+
console.error('خطأ في التوجيه:', error);
|
105
|
+
res.writeHead(502);
|
106
|
+
res.end('خطأ في الاتصال بالتطبيق المحلي');
|
107
|
+
});
|
108
|
+
|
109
|
+
req.pipe(proxyReq);
|
110
|
+
}
|
111
|
+
|
112
|
+
generateLoginPage(connectionId) {
|
113
|
+
return `
|
114
|
+
<!DOCTYPE html>
|
115
|
+
<html dir="rtl">
|
116
|
+
<head>
|
117
|
+
<meta charset="UTF-8">
|
118
|
+
<title>صفحة الدخول</title>
|
119
|
+
<style>
|
120
|
+
body {
|
121
|
+
font-family: Arial, sans-serif;
|
122
|
+
display: flex;
|
123
|
+
justify-content: center;
|
124
|
+
align-items: center;
|
125
|
+
height: 100vh;
|
126
|
+
margin: 0;
|
127
|
+
background-color: #f5f5f5;
|
128
|
+
}
|
129
|
+
.login-container {
|
130
|
+
background: white;
|
131
|
+
padding: 2rem;
|
132
|
+
border-radius: 8px;
|
133
|
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
134
|
+
text-align: center;
|
135
|
+
}
|
136
|
+
input {
|
137
|
+
padding: 8px;
|
138
|
+
margin: 10px 0;
|
139
|
+
width: 200px;
|
140
|
+
text-align: center;
|
141
|
+
font-size: 1.2rem;
|
142
|
+
}
|
143
|
+
button {
|
144
|
+
padding: 10px 20px;
|
145
|
+
background-color: #007bff;
|
146
|
+
color: white;
|
147
|
+
border: none;
|
148
|
+
border-radius: 4px;
|
149
|
+
cursor: pointer;
|
150
|
+
}
|
151
|
+
button:hover {
|
152
|
+
background-color: #0056b3;
|
153
|
+
}
|
154
|
+
</style>
|
155
|
+
</head>
|
156
|
+
<body>
|
157
|
+
<div class="login-container">
|
158
|
+
<h2>أدخل كود الوصول</h2>
|
159
|
+
<input type="text" id="accessCode" placeholder="######" maxlength="6">
|
160
|
+
<br>
|
161
|
+
<button onclick="submitCode()">دخول</button>
|
162
|
+
</div>
|
163
|
+
<script>
|
164
|
+
async function submitCode() {
|
165
|
+
const code = document.getElementById('accessCode').value;
|
166
|
+
const response = await fetch('/${connectionId}', {
|
167
|
+
headers: {
|
168
|
+
'X-Access-Code': code
|
169
|
+
}
|
170
|
+
});
|
171
|
+
|
172
|
+
if (response.ok) {
|
173
|
+
localStorage.setItem('accessCode', code);
|
174
|
+
window.location.href = '/${connectionId}';
|
175
|
+
} else {
|
176
|
+
alert('كود الوصول غير صحيح');
|
177
|
+
}
|
178
|
+
}
|
179
|
+
</script>
|
180
|
+
</body>
|
181
|
+
</html>
|
182
|
+
`;
|
183
|
+
}
|
184
|
+
|
185
|
+
removeConnection(id) {
|
186
|
+
return this.connections.delete(id);
|
187
|
+
}
|
188
|
+
|
189
|
+
getConnectionStats(id) {
|
190
|
+
const connection = this.connections.get(id);
|
191
|
+
if (!connection) return null;
|
192
|
+
|
193
|
+
return {
|
194
|
+
visitors: connection.visitors,
|
195
|
+
uptime: Date.now() - connection.createdAt
|
196
|
+
};
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
module.exports = new DirectServer();
|
@@ -1,68 +1,61 @@
|
|
1
|
-
const
|
2
|
-
const { nanoid } = require('nanoid/non-secure');
|
1
|
+
const DirectServer = require('./directServer');
|
3
2
|
|
4
3
|
class ShareManager {
|
5
4
|
constructor() {
|
6
5
|
this.activeShares = new Map();
|
7
|
-
this.
|
6
|
+
this.server = DirectServer;
|
8
7
|
}
|
9
8
|
|
10
9
|
async init() {
|
11
|
-
await this.
|
10
|
+
await this.server.start();
|
12
11
|
}
|
13
12
|
|
14
|
-
async createShare(port
|
13
|
+
async createShare(port) {
|
15
14
|
try {
|
16
|
-
const
|
17
|
-
const
|
18
|
-
const shareUrl = `http://${publicIp}:8000`;
|
15
|
+
const { id, accessCode } = this.server.createConnection(port);
|
16
|
+
const shareUrl = `http://${this.server.publicIp}:3500/${id}/login`;
|
19
17
|
|
20
18
|
const share = {
|
21
|
-
id
|
19
|
+
id,
|
22
20
|
url: shareUrl,
|
23
|
-
|
24
|
-
|
21
|
+
accessCode,
|
22
|
+
port: port,
|
23
|
+
startTime: Date.now()
|
25
24
|
};
|
26
25
|
|
27
|
-
this.activeShares.set(
|
28
|
-
|
29
|
-
setTimeout(() => {
|
30
|
-
this.closeShare(tunnelId);
|
31
|
-
}, share.duration);
|
26
|
+
this.activeShares.set(id, share);
|
32
27
|
|
33
28
|
return {
|
34
|
-
id
|
29
|
+
id,
|
35
30
|
url: shareUrl,
|
36
|
-
|
31
|
+
accessCode,
|
32
|
+
localUrl: `http://localhost:${port}`
|
37
33
|
};
|
38
34
|
} catch (error) {
|
39
35
|
throw new Error(`فشل إنشاء المشاركة: ${error.message}`);
|
40
36
|
}
|
41
37
|
}
|
42
38
|
|
43
|
-
|
39
|
+
closeShare(shareId) {
|
44
40
|
const share = this.activeShares.get(shareId);
|
45
41
|
if (!share) {
|
46
42
|
throw new Error('لم يتم العثور على المشاركة');
|
47
43
|
}
|
48
44
|
|
49
|
-
this.
|
45
|
+
this.server.removeConnection(shareId);
|
50
46
|
this.activeShares.delete(shareId);
|
51
47
|
}
|
52
48
|
|
53
|
-
|
54
|
-
const
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
});
|
64
|
-
}
|
65
|
-
return shares;
|
49
|
+
getShareStats(shareId) {
|
50
|
+
const share = this.activeShares.get(shareId);
|
51
|
+
if (!share) return null;
|
52
|
+
|
53
|
+
const stats = this.server.getConnectionStats(shareId);
|
54
|
+
return {
|
55
|
+
...share,
|
56
|
+
...stats,
|
57
|
+
uptime: Math.floor((Date.now() - share.startTime) / 1000 / 60) // بالدقائق
|
58
|
+
};
|
66
59
|
}
|
67
60
|
}
|
68
61
|
|