File size: 6,315 Bytes
a8d9584
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// VPN Controller Logic
class VPNController {
    constructor() {
        this.isConnected = false;
        this.connectionTime = 0;
        this.timerInterval = null;
        this.process = null;
        this.logs = [];
        
        this.initElements();
        this.bindEvents();
        this.simulateMetrics();
    }

    initElements() {
        this.mainToggle = document.getElementById('mainToggle');
        this.connectionStatus = document.getElementById('connectionStatus');
        this.connectionSubtitle = document.getElementById('connectionSubtitle');
        this.logConsole = document.getElementById('logConsole');
        this.downloadStat = document.getElementById('downloadStat');
        this.uploadStat = document.getElementById('uploadStat');
        this.timeStat = document.getElementById('timeStat');
        this.clearLogsBtn = document.getElementById('clearLogs');
    }

    bindEvents() {
        // Power button toggle
        document.addEventListener('power-toggle', (e) => {
            this.toggleConnection();
        });

        // Clear logs
        this.clearLogsBtn.addEventListener('click', () => {
            this.logConsole.clear();
            this.addLog('Logs cleared by user', 'info');
        });

        // Keyboard shortcut (Space to toggle)
        document.addEventListener('keydown', (e) => {
            if (e.code === 'Space' && e.target === document.body) {
                e.preventDefault();
                this.toggleConnection();
            }
        });
    }

    toggleConnection() {
        if (this.isConnected) {
            this.disconnect();
        } else {
            this.connect();
        }
    }

    connect() {
        this.isConnected = true;
        this.updateUI();
        this.startTimer();
        this.addLog('Initializing Psiphon connection...', 'info');
        this.addLog('Loading configuration from /home/hossein/Files/psiphon/psiphon.config', 'system');
        
        // Simulate connection process
        setTimeout(() => {
            this.addLog('Starting tunnel core...', 'process');
        }, 500);

        setTimeout(() => {
            this.addLog('Connecting to nearest available server...', 'network');
        }, 1500);

        setTimeout(() => {
            this.addLog('Handshake successful', 'success');
            this.addLog('Tunnel established via SSH+ protocol', 'success');
            this.addLog('Local SOCKS proxy started on port 1080', 'info');
        }, 2500);

        // Update stats simulation
        this.simulateTraffic();
    }

    disconnect() {
        this.isConnected = false;
        this.updateUI();
        this.stopTimer();
        this.addLog('Terminating connection...', 'warning');
        
        setTimeout(() => {
            this.addLog('Tunnel closed gracefully', 'info');
            this.addLog('Process terminated', 'system');
        }, 500);

        // Reset stats
        this.downloadStat.updateValue('0 MB/s');
        this.uploadStat.updateValue('0 MB/s');
    }

    updateUI() {
        if (this.isConnected) {
            this.connectionStatus.textContent = 'Connected';
            this.connectionStatus.className = 'text-2xl font-bold text-secondary-400 mb-1';
            this.connectionSubtitle.textContent = 'Secure connection active';
            this.mainToggle.setAttribute('active', 'true');
            document.body.classList.add('connection-active');
        } else {
            this.connectionStatus.textContent = 'Disconnected';
            this.connectionStatus.className = 'text-2xl font-bold text-gray-400 mb-1';
            this.connectionSubtitle.textContent = 'Tap to connect';
            this.mainToggle.setAttribute('active', 'false');
            document.body.classList.remove('connection-active');
        }
    }

    startTimer() {
        this.connectionTime = 0;
        this.updateTimerDisplay();
        this.timerInterval = setInterval(() => {
            this.connectionTime++;
            this.updateTimerDisplay();
        }, 1000);
    }

    stopTimer() {
        if (this.timerInterval) {
            clearInterval(this.timerInterval);
            this.timerInterval = null;
        }
        this.connectionTime = 0;
        this.timeStat.updateValue('00:00:00');
    }

    updateTimerDisplay() {
        const hours = Math.floor(this.connectionTime / 3600);
        const minutes = Math.floor((this.connectionTime % 3600) / 60);
        const seconds = this.connectionTime % 60;
        const formatted = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
        this.timeStat.updateValue(formatted);
    }

    addLog(message, type = 'info') {
        const timestamp = new Date().toLocaleTimeString('en-US', { hour12: false });
        this.logConsole.addEntry(`[${timestamp}] ${message}`, type);
    }

    simulateTraffic() {
        if (!this.isConnected) return;
        
        const download = (Math.random() * 5 + 1).toFixed(1);
        const upload = (Math.random() * 2 + 0.5).toFixed(1);
        
        this.downloadStat.updateValue(`${download} MB/s`);
        this.uploadStat.updateValue(`${upload} MB/s`);
        
        if (this.isConnected) {
            setTimeout(() => this.simulateTraffic(), 2000);
        }
    }

    simulateMetrics() {
        // Random log entries when connected
        setInterval(() => {
            if (this.isConnected && Math.random() > 0.7) {
                const messages = [
                    'Heartbeat received',
                    'Network status: optimal',
                    'Rotating upstream proxy...',
                    'Ping: 45ms',
                    'Bandwidth throttling detected, switching protocol...'
                ];
                const randomMsg = messages[Math.floor(Math.random() * messages.length)];
                this.addLog(randomMsg, 'debug');
            }
        }, 5000);
    }
}

// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
    window.vpnController = new VPNController();
    
    // Initial logs
    setTimeout(() => {
        window.vpnController.addLog('Psiphon Pilot initialized', 'system');
        window.vpnController.addLog('Ready to establish secure connection', 'info');
    }, 100);
});