topnic-https 1.2.3 → 1.2.4
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 +2 -1
- package/readme +1 -1
- package/src/sharing/directServer.js +95 -134
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "topnic-https",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.4",
|
4
4
|
"main": "./src/main/index.js",
|
5
5
|
"type": "commonjs",
|
6
6
|
"scripts": {
|
@@ -96,6 +96,7 @@
|
|
96
96
|
"https": "^1.0.0",
|
97
97
|
"localtunnel": "^2.0.2",
|
98
98
|
"nanoid": "^5.0.8",
|
99
|
+
"net": "^1.0.2",
|
99
100
|
"ngrok": "^5.0.0-beta.2",
|
100
101
|
"os": "^0.1.2",
|
101
102
|
"path": "^0.12.7",
|
package/readme
CHANGED
@@ -25,7 +25,7 @@ $ npm install -g topnic-https
|
|
25
25
|
|
26
26
|
## أمثلة
|
27
27
|
```bash
|
28
|
-
$ httpst share -p 3000
|
28
|
+
$ httpst share -p 3000 # مشاركة لمدة 60 دقيقة
|
29
29
|
$ httpst run #تشغيل الخادم
|
30
30
|
$ httpst stop #إيقاف الخادم
|
31
31
|
$ httpst restart #إعادة تشغيل الخادم
|
@@ -2,7 +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
|
5
|
+
const net = require('net');
|
6
6
|
|
7
7
|
class DirectServer {
|
8
8
|
constructor() {
|
@@ -21,165 +21,126 @@ class DirectServer {
|
|
21
21
|
return 'localhost';
|
22
22
|
}
|
23
23
|
|
24
|
+
async isPortAvailable(port) {
|
25
|
+
return new Promise((resolve) => {
|
26
|
+
const tester = net.createServer()
|
27
|
+
.once('error', () => resolve(false))
|
28
|
+
.once('listening', () => {
|
29
|
+
tester.once('close', () => resolve(true)).close();
|
30
|
+
})
|
31
|
+
.listen(port);
|
32
|
+
});
|
33
|
+
}
|
34
|
+
|
24
35
|
async start(port = 3500) {
|
36
|
+
if (!(await this.isPortAvailable(port))) {
|
37
|
+
console.log(`المنفذ ${port} مشغول، جاري المحاولة على منفذ آخر...`);
|
38
|
+
port = await this.findAvailablePort(3501);
|
39
|
+
}
|
40
|
+
|
25
41
|
return new Promise((resolve) => {
|
26
42
|
this.server.listen(port, '0.0.0.0', () => {
|
27
43
|
console.log(`🚀 خادم المشاركة يعمل على المنفذ ${port}`);
|
28
44
|
console.log(`📡 عنوان IP: ${this.publicIp}:${port}`);
|
29
|
-
resolve();
|
45
|
+
resolve(port);
|
30
46
|
});
|
31
47
|
});
|
32
48
|
}
|
33
49
|
|
34
|
-
|
35
|
-
|
36
|
-
|
50
|
+
async findAvailablePort(startPort) {
|
51
|
+
let port = startPort;
|
52
|
+
while (!(await this.isPortAvailable(port))) {
|
53
|
+
port++;
|
54
|
+
}
|
55
|
+
return port;
|
56
|
+
}
|
57
|
+
|
58
|
+
async verifyTargetPort(port) {
|
59
|
+
try {
|
60
|
+
const response = await new Promise((resolve, reject) => {
|
61
|
+
const req = http.request({
|
62
|
+
hostname: 'localhost',
|
63
|
+
port: port,
|
64
|
+
path: '/',
|
65
|
+
method: 'GET',
|
66
|
+
timeout: 2000
|
67
|
+
}, resolve);
|
68
|
+
|
69
|
+
req.on('error', reject);
|
70
|
+
req.end();
|
71
|
+
});
|
72
|
+
return true;
|
73
|
+
} catch (error) {
|
74
|
+
return false;
|
75
|
+
}
|
37
76
|
}
|
38
77
|
|
39
78
|
createConnection(targetPort) {
|
40
79
|
const id = nanoid(6);
|
41
|
-
const accessCode = this.generateAccessCode();
|
42
|
-
|
43
80
|
this.connections.set(id, {
|
44
81
|
port: targetPort,
|
45
82
|
createdAt: Date.now(),
|
46
|
-
visitors: 0
|
47
|
-
accessCode
|
83
|
+
visitors: 0
|
48
84
|
});
|
49
|
-
|
50
|
-
return {
|
51
|
-
id,
|
52
|
-
accessCode
|
53
|
-
};
|
85
|
+
return id;
|
54
86
|
}
|
55
87
|
|
56
88
|
async handleRequest(req, res) {
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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}`
|
89
|
+
try {
|
90
|
+
const urlParts = req.url.split('/');
|
91
|
+
const connectionId = urlParts[1];
|
92
|
+
const connection = this.connections.get(connectionId);
|
93
|
+
|
94
|
+
if (!connection) {
|
95
|
+
res.writeHead(404);
|
96
|
+
res.end('رابط المشاركة غير صالح');
|
97
|
+
return;
|
93
98
|
}
|
94
|
-
};
|
95
99
|
|
96
|
-
|
100
|
+
// التحقق من أن الخدمة المحلية تعمل
|
101
|
+
if (!(await this.verifyTargetPort(connection.port))) {
|
102
|
+
res.writeHead(502);
|
103
|
+
res.end('الخدمة المحلية غير متاحة');
|
104
|
+
return;
|
105
|
+
}
|
97
106
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
107
|
+
// إعادة بناء المسار الأصلي
|
108
|
+
const originalPath = '/' + urlParts.slice(2).join('/');
|
109
|
+
|
110
|
+
const options = {
|
111
|
+
hostname: 'localhost',
|
112
|
+
port: connection.port,
|
113
|
+
path: originalPath,
|
114
|
+
method: req.method,
|
115
|
+
headers: {
|
116
|
+
...req.headers,
|
117
|
+
host: `localhost:${connection.port}`
|
118
|
+
}
|
119
|
+
};
|
120
|
+
|
121
|
+
const proxyReq = http.request(options, (proxyRes) => {
|
122
|
+
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
123
|
+
proxyRes.pipe(res);
|
124
|
+
});
|
102
125
|
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
126
|
+
proxyReq.on('error', (error) => {
|
127
|
+
console.error('خطأ في التوجيه:', error);
|
128
|
+
res.writeHead(502);
|
129
|
+
res.end('خطأ في الاتصال بالتطبيق المحلي');
|
130
|
+
});
|
108
131
|
|
109
|
-
|
110
|
-
|
132
|
+
if (!req.readableEnded) {
|
133
|
+
req.pipe(proxyReq);
|
134
|
+
} else {
|
135
|
+
proxyReq.end();
|
136
|
+
}
|
111
137
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
-
`;
|
138
|
+
connection.visitors++;
|
139
|
+
} catch (error) {
|
140
|
+
console.error('خطأ في معالجة الطلب:', error);
|
141
|
+
res.writeHead(500);
|
142
|
+
res.end('خطأ داخلي في الخادم');
|
143
|
+
}
|
183
144
|
}
|
184
145
|
|
185
146
|
removeConnection(id) {
|