File size: 3,262 Bytes
2821330
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const localtunnel = require('localtunnel');
const axios = require('axios');

let tunnel = null;

module.exports.config = {
    name: "lt",
    version: "1.0.0",
    hasPermssion: 2,
    credits: "Jonell Magallanes",
    description: "Manage LocalTunnel connection",
    usePrefix: false,
    commandCategory: "System",
    usages: "lt <start|stop|pause> [subdomain] [port]",
    cooldowns: 5,
};

module.exports.run = async function ({ api, event, args }) {
    const { threadID, messageID } = event;
    const action = args[0]?.toLowerCase();
    const subdomain = args[1];
    const port = args[2] ? parseInt(args[2]) : 25622;

    if (action === 'start') {
        if (tunnel) {
            return api.sendMessage('⚠️ *Tunnel is already running!*\nUse `/lt stop` first to restart.', threadID, messageID);
        }

        if (isNaN(port) || port <= 0 || port > 65535) {
            return api.sendMessage('❌ *Invalid port number.* Please provide a valid port (1–65535).', threadID, messageID);
        }

        try {
            const statusMessage = await api.sendMessage("⏳ *Starting LocalTunnel...*", threadID, messageID);

            tunnel = await localtunnel({ 
                port: port, 
                subdomain: subdomain || undefined 
            });

            const publicUrl = tunnel.url;

            const passwordResponse = await axios.get('https://loca.lt/mytunnelpassword');
            const password = passwordResponse.data || 'No password provided';

            api.editMessage(`πŸš€ *LocalTunnel Started!*\n\nπŸ”— *URL:* ${publicUrl}\nπŸ“ *Port:* ${port}\nπŸ”’ *Password:* ${password}`, statusMessage.messageID, threadID);

            tunnel.on('close', () => {
                api.sendMessage('πŸ”’ *LocalTunnel connection closed.*', threadID);
                tunnel = null;
            });

        } catch (error) {
            console.error(error);
            return api.sendMessage('❌ *Failed to start LocalTunnel.* Make sure the custom subdomain and port are available or try again later.', threadID, messageID);
        }

    } else if (action === 'stop') {
        if (tunnel) {
            tunnel.close();
            return api.sendMessage('πŸ›‘ *LocalTunnel stopped successfully.*', threadID, messageID);
        } else {
            return api.sendMessage('⚠️ *No active LocalTunnel connection to stop.*', threadID, messageID);
        }

    } else if (action === 'pause') {
        return api.sendMessage('⏸️ *LocalTunnel paused.* You can restart it with `/lt start`.', threadID, messageID);

    } else {
        return api.sendMessage(
            '⚑ *LocalTunnel Command Guide:*\n\n' +
            '`/lt start` β†’ Start a tunnel with a random subdomain and default port (25622).\n' +
            '`/lt start <subdomain>` β†’ Start a tunnel with a custom subdomain and default port (25622).\n' +
            '`/lt start <subdomain> <port>` β†’ Start a tunnel with a custom subdomain and custom port.\n' +
            '`/lt stop` β†’ Stop the current tunnel.\n' +
            '`/lt pause` β†’ Pause the tunnel connection (temporarily).', 
            threadID, 
            messageID
        );
    }
};