topnic-https 1.2.1 → 1.2.2

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.1",
3
+ "version": "1.2.2",
4
4
  "main": "./src/main/index.js",
5
5
  "type": "commonjs",
6
6
  "scripts": {
@@ -0,0 +1,96 @@
1
+ const http = require('http');
2
+ const WebSocket = require('ws');
3
+ const { nanoid } = require('nanoid/non-secure');
4
+ const os = require('os');
5
+
6
+ class DirectServer {
7
+ constructor() {
8
+ this.connections = new Map();
9
+ this.server = http.createServer(this.handleRequest.bind(this));
10
+ this.wss = new WebSocket.Server({ server: this.server });
11
+ this.publicIp = this.getPublicIp();
12
+ }
13
+
14
+ getPublicIp() {
15
+ const interfaces = os.networkInterfaces();
16
+ for (const iface of Object.values(interfaces)) {
17
+ const nonLocalIpv4 = iface.find(addr => addr.family === 'IPv4' && !addr.internal);
18
+ if (nonLocalIpv4) return nonLocalIpv4.address;
19
+ }
20
+ return 'localhost';
21
+ }
22
+
23
+ async start(port = 3500) {
24
+ return new Promise((resolve) => {
25
+ this.server.listen(port, '0.0.0.0', () => {
26
+ console.log(`🚀 خادم المشاركة يعمل على المنفذ ${port}`);
27
+ console.log(`📡 عنوان IP: ${this.publicIp}:${port}`);
28
+ resolve();
29
+ });
30
+ });
31
+ }
32
+
33
+ createConnection(targetPort) {
34
+ const id = nanoid(6);
35
+ this.connections.set(id, {
36
+ port: targetPort,
37
+ createdAt: Date.now(),
38
+ visitors: 0
39
+ });
40
+ return id;
41
+ }
42
+
43
+ async handleRequest(req, res) {
44
+ const connectionId = req.url.split('/')[1];
45
+ const connection = this.connections.get(connectionId);
46
+
47
+ if (!connection) {
48
+ res.writeHead(404);
49
+ res.end('رابط المشاركة غير صالح');
50
+ return;
51
+ }
52
+
53
+ // إعادة توجيه الطلب إلى المنفذ المحلي
54
+ const options = {
55
+ hostname: 'localhost',
56
+ port: connection.port,
57
+ path: req.url.replace(`/${connectionId}`, '') || '/',
58
+ method: req.method,
59
+ headers: {
60
+ ...req.headers,
61
+ host: `localhost:${connection.port}`
62
+ }
63
+ };
64
+
65
+ connection.visitors++;
66
+
67
+ const proxyReq = http.request(options, (proxyRes) => {
68
+ res.writeHead(proxyRes.statusCode, proxyRes.headers);
69
+ proxyRes.pipe(res);
70
+ });
71
+
72
+ proxyReq.on('error', (error) => {
73
+ console.error('خطأ في التوجيه:', error);
74
+ res.writeHead(502);
75
+ res.end('خطأ في الاتصال بالتطبيق المحلي');
76
+ });
77
+
78
+ req.pipe(proxyReq);
79
+ }
80
+
81
+ removeConnection(id) {
82
+ return this.connections.delete(id);
83
+ }
84
+
85
+ getConnectionStats(id) {
86
+ const connection = this.connections.get(id);
87
+ if (!connection) return null;
88
+
89
+ return {
90
+ visitors: connection.visitors,
91
+ uptime: Date.now() - connection.createdAt
92
+ };
93
+ }
94
+ }
95
+
96
+ module.exports = new DirectServer();
@@ -1,68 +1,59 @@
1
- const TunnelServer = require('./tunnelServer');
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.tunnelServer = TunnelServer;
6
+ this.server = DirectServer;
8
7
  }
9
8
 
10
9
  async init() {
11
- await this.tunnelServer.start();
10
+ await this.server.start();
12
11
  }
13
12
 
14
- async createShare(port, duration) {
13
+ async createShare(port) {
15
14
  try {
16
- const tunnelId = await this.tunnelServer.createTunnel(port);
17
- const publicIp = this.tunnelServer.publicIp;
18
- const shareUrl = `http://${publicIp}:8000`;
15
+ const shareId = this.server.createConnection(port);
16
+ const shareUrl = `http://${this.server.publicIp}:3500/${shareId}`;
19
17
 
20
18
  const share = {
21
- id: tunnelId,
19
+ id: shareId,
22
20
  url: shareUrl,
23
- startTime: Date.now(),
24
- duration: duration * 60 * 1000
21
+ port: port,
22
+ startTime: Date.now()
25
23
  };
26
24
 
27
- this.activeShares.set(tunnelId, share);
28
-
29
- setTimeout(() => {
30
- this.closeShare(tunnelId);
31
- }, share.duration);
25
+ this.activeShares.set(shareId, share);
32
26
 
33
27
  return {
34
- id: tunnelId,
28
+ id: shareId,
35
29
  url: shareUrl,
36
- localUrl: `http://localhost:8000`
30
+ localUrl: `http://localhost:${port}`
37
31
  };
38
32
  } catch (error) {
39
33
  throw new Error(`فشل إنشاء المشاركة: ${error.message}`);
40
34
  }
41
35
  }
42
36
 
43
- async closeShare(shareId) {
37
+ closeShare(shareId) {
44
38
  const share = this.activeShares.get(shareId);
45
39
  if (!share) {
46
40
  throw new Error('لم يتم العثور على المشاركة');
47
41
  }
48
42
 
49
- this.tunnelServer.closeTunnel(shareId);
43
+ this.server.removeConnection(shareId);
50
44
  this.activeShares.delete(shareId);
51
45
  }
52
46
 
53
- getActiveShares() {
54
- const shares = [];
55
- for (const [id, share] of this.activeShares) {
56
- const remainingTime = share.duration - (Date.now() - share.startTime);
57
- const remainingMinutes = Math.max(0, Math.floor(remainingTime / (60 * 1000)));
58
-
59
- shares.push({
60
- id,
61
- url: share.url,
62
- remainingMinutes
63
- });
64
- }
65
- return shares;
47
+ getShareStats(shareId) {
48
+ const share = this.activeShares.get(shareId);
49
+ if (!share) return null;
50
+
51
+ const stats = this.server.getConnectionStats(shareId);
52
+ return {
53
+ ...share,
54
+ ...stats,
55
+ uptime: Math.floor((Date.now() - share.startTime) / 1000 / 60) // بالدقائق
56
+ };
66
57
  }
67
58
  }
68
59