topnic-https 1.2.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "topnic-https",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "main": "./src/main/index.js",
5
5
  "type": "commonjs",
6
6
  "scripts": {
@@ -2,6 +2,7 @@ const http = require('http');
2
2
  const WebSocket = require('ws');
3
3
  const { nanoid } = require('nanoid/non-secure');
4
4
  const os = require('os');
5
+ const crypto = require('crypto');
5
6
 
6
7
  class DirectServer {
7
8
  constructor() {
@@ -30,17 +31,31 @@ class DirectServer {
30
31
  });
31
32
  }
32
33
 
34
+ generateAccessCode() {
35
+ // إنشاء كود وصول من 6 أرقام
36
+ return Math.floor(100000 + Math.random() * 900000).toString();
37
+ }
38
+
33
39
  createConnection(targetPort) {
34
40
  const id = nanoid(6);
41
+ const accessCode = this.generateAccessCode();
42
+
35
43
  this.connections.set(id, {
36
44
  port: targetPort,
37
45
  createdAt: Date.now(),
38
- visitors: 0
46
+ visitors: 0,
47
+ accessCode
39
48
  });
40
- return id;
49
+
50
+ return {
51
+ id,
52
+ accessCode
53
+ };
41
54
  }
42
55
 
43
56
  async handleRequest(req, res) {
57
+ res.setHeader('Access-Control-Allow-Origin', '*');
58
+
44
59
  const connectionId = req.url.split('/')[1];
45
60
  const connection = this.connections.get(connectionId);
46
61
 
@@ -50,6 +65,22 @@ class DirectServer {
50
65
  return;
51
66
  }
52
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
+
53
84
  // إعادة توجيه الطلب إلى المنفذ المحلي
54
85
  const options = {
55
86
  hostname: 'localhost',
@@ -78,6 +109,79 @@ class DirectServer {
78
109
  req.pipe(proxyReq);
79
110
  }
80
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
+
81
185
  removeConnection(id) {
82
186
  return this.connections.delete(id);
83
187
  }
@@ -12,21 +12,23 @@ class ShareManager {
12
12
 
13
13
  async createShare(port) {
14
14
  try {
15
- const shareId = this.server.createConnection(port);
16
- const shareUrl = `http://${this.server.publicIp}:3500/${shareId}`;
15
+ const { id, accessCode } = this.server.createConnection(port);
16
+ const shareUrl = `http://${this.server.publicIp}:3500/${id}/login`;
17
17
 
18
18
  const share = {
19
- id: shareId,
19
+ id,
20
20
  url: shareUrl,
21
+ accessCode,
21
22
  port: port,
22
23
  startTime: Date.now()
23
24
  };
24
25
 
25
- this.activeShares.set(shareId, share);
26
+ this.activeShares.set(id, share);
26
27
 
27
28
  return {
28
- id: shareId,
29
+ id,
29
30
  url: shareUrl,
31
+ accessCode,
30
32
  localUrl: `http://localhost:${port}`
31
33
  };
32
34
  } catch (error) {