topnic-https 0.2.13 → 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": "0.2.13",
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,84 +1,59 @@
1
- const ngrok = require('ngrok');
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();
6
+ this.server = DirectServer;
7
7
  }
8
8
 
9
- async createTunnel(port) {
10
- try {
11
- const url = await ngrok.connect({
12
- addr: port,
13
- onStatusChange: status => {
14
- if (status === 'closed') {
15
- console.log('🔄 تم إغلاق النفق');
16
- }
17
- },
18
- onLogEvent: data => {
19
- if (data.err) {
20
- console.error('❌ خطأ في النفق:', data.err);
21
- }
22
- }
23
- });
24
-
25
- return { url, disconnect: ngrok.disconnect };
26
- } catch (error) {
27
- throw new Error(`فشل إنشاء النفق: ${error.message}`);
28
- }
9
+ async init() {
10
+ await this.server.start();
29
11
  }
30
12
 
31
- async createShare(port, duration) {
13
+ async createShare(port) {
32
14
  try {
33
- const tunnel = await this.createTunnel(port);
34
- const shareId = nanoid(6);
15
+ const shareId = this.server.createConnection(port);
16
+ const shareUrl = `http://${this.server.publicIp}:3500/${shareId}`;
35
17
 
36
18
  const share = {
37
19
  id: shareId,
38
- url: tunnel.url,
39
- tunnel,
40
- startTime: Date.now(),
41
- duration: duration * 60 * 1000
20
+ url: shareUrl,
21
+ port: port,
22
+ startTime: Date.now()
42
23
  };
43
24
 
44
25
  this.activeShares.set(shareId, share);
45
26
 
46
- setTimeout(() => {
47
- this.closeShare(shareId);
48
- }, share.duration);
49
-
50
27
  return {
51
28
  id: shareId,
52
- url: tunnel.url
29
+ url: shareUrl,
30
+ localUrl: `http://localhost:${port}`
53
31
  };
54
32
  } catch (error) {
55
33
  throw new Error(`فشل إنشاء المشاركة: ${error.message}`);
56
34
  }
57
35
  }
58
36
 
59
- async closeShare(shareId) {
37
+ closeShare(shareId) {
60
38
  const share = this.activeShares.get(shareId);
61
39
  if (!share) {
62
40
  throw new Error('لم يتم العثور على المشاركة');
63
41
  }
64
42
 
65
- await share.tunnel.disconnect();
43
+ this.server.removeConnection(shareId);
66
44
  this.activeShares.delete(shareId);
67
45
  }
68
46
 
69
- getActiveShares() {
70
- const shares = [];
71
- for (const [id, share] of this.activeShares) {
72
- const remainingTime = share.duration - (Date.now() - share.startTime);
73
- const remainingMinutes = Math.max(0, Math.floor(remainingTime / (60 * 1000)));
74
-
75
- shares.push({
76
- id,
77
- url: share.url,
78
- remainingMinutes
79
- });
80
- }
81
- 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
+ };
82
57
  }
83
58
  }
84
59
 
@@ -0,0 +1,110 @@
1
+ const http = require('http');
2
+ const https = require('https');
3
+ const WebSocket = require('ws');
4
+ const { nanoid } = require('nanoid/non-secure');
5
+ const os = require('os');
6
+
7
+ class TunnelServer {
8
+ constructor() {
9
+ this.tunnels = 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
+ let address;
18
+
19
+ for (const iface of Object.values(interfaces)) {
20
+ for (const alias of iface) {
21
+ if (alias.family === 'IPv4' && !alias.internal) {
22
+ address = alias.address;
23
+ break;
24
+ }
25
+ }
26
+ if (address) break;
27
+ }
28
+
29
+ return address;
30
+ }
31
+
32
+ async start(port = 8000) {
33
+ return new Promise((resolve) => {
34
+ this.server.listen(port, '0.0.0.0', () => {
35
+ console.log(`🚀 خادم النفق يعمل على المنفذ ${port}`);
36
+ console.log(`📡 عنوان IP العام: ${this.publicIp}`);
37
+ resolve();
38
+ });
39
+ });
40
+ }
41
+
42
+ async createTunnel(targetPort) {
43
+ const tunnelId = nanoid(6);
44
+ const tunnel = {
45
+ id: tunnelId,
46
+ targetPort,
47
+ clients: new Set(),
48
+ createdAt: Date.now()
49
+ };
50
+
51
+ this.tunnels.set(tunnelId, tunnel);
52
+ return tunnelId;
53
+ }
54
+
55
+ async handleRequest(req, res) {
56
+ const tunnelId = req.headers.host.split('.')[0];
57
+ const tunnel = this.tunnels.get(tunnelId);
58
+
59
+ if (!tunnel) {
60
+ res.writeHead(404);
61
+ res.end('النفق غير موجود');
62
+ return;
63
+ }
64
+
65
+ // إعادة توجيه الطلب إلى الخادم المحلي
66
+ const options = {
67
+ hostname: 'localhost',
68
+ port: tunnel.targetPort,
69
+ path: req.url,
70
+ method: req.method,
71
+ headers: req.headers
72
+ };
73
+
74
+ const proxyReq = http.request(options, (proxyRes) => {
75
+ res.writeHead(proxyRes.statusCode, proxyRes.headers);
76
+ proxyRes.pipe(res);
77
+ });
78
+
79
+ req.pipe(proxyReq);
80
+
81
+ proxyReq.on('error', (error) => {
82
+ console.error('خطأ في النفق:', error);
83
+ res.writeHead(502);
84
+ res.end('خطأ في الاتصال بالخادم المحلي');
85
+ });
86
+ }
87
+
88
+ handleConnection(ws, req) {
89
+ const tunnelId = req.headers.host.split('.')[0];
90
+ const tunnel = this.tunnels.get(tunnelId);
91
+
92
+ if (tunnel) {
93
+ tunnel.clients.add(ws);
94
+
95
+ ws.on('close', () => {
96
+ tunnel.clients.delete(ws);
97
+ });
98
+ }
99
+ }
100
+
101
+ closeTunnel(tunnelId) {
102
+ const tunnel = this.tunnels.get(tunnelId);
103
+ if (tunnel) {
104
+ tunnel.clients.forEach(client => client.close());
105
+ this.tunnels.delete(tunnelId);
106
+ }
107
+ }
108
+ }
109
+
110
+ module.exports = new TunnelServer();