mobilecoder-mcp 2.1.1 → 2.1.3

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/dist/agent.js CHANGED
@@ -1,321 +1,351 @@
1
- import { io } from 'socket.io-client';
2
- import * as os from 'os';
3
- import * as pty from 'node-pty';
4
- import * as crypto from 'crypto';
5
- import chalk from 'chalk';
6
- import ora from 'ora';
7
- import * as fs from 'fs';
8
- import * as path from 'path';
9
- // 🚨 BURAYI KENDİ DOMAIN ADRESİNLE DEĞİŞTİR!
10
- const RELAY_SERVER_URL = 'https://api.mobilecoder.xyz';
11
- // Security Constants
12
- const PBKDF2_ITERATIONS = 100000;
13
- const KEY_LENGTH = 32; // 256 bits
14
- const SALT = 'mobilecoder-salt-v2';
15
- const KEY_ROTATION_INTERVAL = 600000; // 10 minutes
16
- const MESSAGE_MAX_AGE = 300000; // 5 minutes
17
- export class ZKRelayAgent {
18
- socket;
19
- ptyProcess;
20
- secretCode;
21
- roomId;
22
- // 🔐 Güçlendirilmiş Güvenlik Değişkenleri
23
- masterKey;
24
- sessionKey;
25
- sessionCounter = 0;
26
- seenNonces = new Set();
27
- nonceCleanupInterval = null;
28
- keyRotationInterval = null;
29
- spinner = ora('Initializing Secure Tunnel...');
30
- constructor() {
31
- this.setupSecurity();
32
- this.initPTY();
33
- this.connectToRelay();
34
- this.startSecurityMaintenance();
35
- }
36
- // 1. Güvenlik Katmanı: PBKDF2 ile Güçlendirilmiş Anahtar Türetme
37
- setupSecurity() {
38
- // 6 Haneli rastgele kod (Kullanıcının göreceği)
39
- const randomNum = Math.floor(100000 + Math.random() * 900000);
40
- this.secretCode = randomNum.toString();
41
- // Oda ID'si: Kodun Hash'i (Sunucu sadece bunu görür, kodu göremez)
42
- this.roomId = crypto.createHash('sha256').update(this.secretCode).digest('hex');
43
- // 🔐 PBKDF2 ile Master Key Türetme (Brute-force'a karşı savunma)
44
- this.masterKey = crypto.pbkdf2Sync(this.secretCode, SALT, PBKDF2_ITERATIONS, KEY_LENGTH, 'sha256');
45
- // Session Key (PFS için rotasyonlu)
46
- this.sessionKey = this.deriveSessionKey(0);
47
- }
48
- // 🔐 Session Key Türetme (Perfect Forward Secrecy)
49
- deriveSessionKey(counter) {
50
- return crypto.pbkdf2Sync(this.masterKey, `session-${counter}`, 10000, KEY_LENGTH, 'sha256');
51
- }
52
- // 🔄 Key Rotation (Her 10 dakikada bir)
53
- rotateKey() {
54
- this.sessionCounter++;
55
- this.sessionKey = this.deriveSessionKey(this.sessionCounter);
56
- console.log(chalk.cyan(`🔄 Session key rotated to #${this.sessionCounter}`));
57
- // Notify peer about key rotation
58
- this.sendSecurePayload('key_rotation', { counter: this.sessionCounter });
59
- }
60
- // 🧹 Güvenlik Bakımı
61
- startSecurityMaintenance() {
62
- // Nonce temizliği (5 dakikadan eski nonce'ları sil)
63
- this.nonceCleanupInterval = setInterval(() => {
64
- this.seenNonces.clear();
65
- }, MESSAGE_MAX_AGE);
66
- // Key rotation
67
- this.keyRotationInterval = setInterval(() => {
68
- this.rotateKey();
69
- }, KEY_ROTATION_INTERVAL);
70
- }
71
- // 2. Terminal (PTY) Başlatma
72
- initPTY() {
73
- const shell = os.platform() === 'win32' ? 'powershell.exe' : (process.env.SHELL || 'bash');
74
- this.ptyProcess = pty.spawn(shell, [], {
75
- name: 'xterm-256color',
76
- cols: 80,
77
- rows: 24,
78
- cwd: process.cwd(),
79
- env: process.env
80
- });
81
- // Terminalden çıkan veriyi şifrele ve yolla
82
- this.ptyProcess.onData((data) => {
83
- this.sendSecurePayload('term_data', data);
84
- });
85
- }
86
- // 3. Relay Sunucusuna Bağlanma
87
- connectToRelay() {
88
- this.spinner.start('Connecting to MobileCoder Relay Network...');
89
- this.socket = io(RELAY_SERVER_URL, {
90
- transports: ['websocket'], // Hız için sadece websocket
91
- reconnection: true,
92
- reconnectionAttempts: Infinity
93
- });
94
- this.socket.on('connect', () => {
95
- this.spinner.succeed(chalk.green('Connected to Relay Node!'));
96
- // Odaya Katıl (Sadece Hash ID gönderilir)
97
- this.socket.emit('join_room', { code: this.roomId, type: 'agent' });
98
- this.printBanner();
99
- });
100
- this.socket.on('disconnect', () => {
101
- this.spinner.warn(chalk.yellow('Connection lost. Reconnecting...'));
102
- });
103
- // Mobilden biri bağlandığında
104
- this.socket.on('peer_joined', () => {
105
- console.log(chalk.green('\n📱 Mobile Client Connected via Encrypted Tunnel!'));
106
- this.sendEnvInfo();
107
- });
108
- // Mobilden şifreli veri geldiğinde
109
- this.socket.on('relay_data', (message) => {
110
- this.handleIncomingMessage(message);
111
- });
112
- // Also listen for connect_error
113
- this.socket.on('connect_error', (err) => {
114
- // console.error(err);
115
- });
116
- }
117
- // 🔐 AES-256-CBC ile Şifreleme (Nonce tabanlı)
118
- encrypt(data) {
119
- const nonce = crypto.randomBytes(16);
120
- const cipher = crypto.createCipheriv('aes-256-cbc', this.sessionKey, nonce);
121
- const encrypted = Buffer.concat([
122
- cipher.update(data, 'utf8'),
123
- cipher.final()
124
- ]);
125
- return {
126
- nonce: nonce.toString('base64'),
127
- data: encrypted.toString('base64'),
128
- timestamp: Date.now()
129
- };
130
- }
131
- // 🔐 AES-256-CBC ile Şifre Çözme (Replay Attack Korumalı)
132
- decrypt(packet) {
133
- // Timestamp kontrolü (5 dakikadan eski mesajları reddet)
134
- if (Date.now() - packet.timestamp > MESSAGE_MAX_AGE) {
135
- throw new Error('Message too old - potential replay attack');
136
- }
137
- // Nonce uniqueness kontrolü (Replay attack önleme)
138
- if (this.seenNonces.has(packet.nonce)) {
139
- throw new Error('Replay attack detected - duplicate nonce');
140
- }
141
- this.seenNonces.add(packet.nonce);
142
- const decipher = crypto.createDecipheriv('aes-256-cbc', this.sessionKey, Buffer.from(packet.nonce, 'base64'));
143
- return Buffer.concat([
144
- decipher.update(Buffer.from(packet.data, 'base64')),
145
- decipher.final()
146
- ]).toString('utf8');
147
- }
148
- // Gelen veriyi çöz ve işle
149
- handleIncomingMessage(encryptedPayload) {
150
- try {
151
- let decryptedText;
152
- // Yeni format: { nonce, data, timestamp }
153
- if (typeof encryptedPayload === 'object' && encryptedPayload.nonce) {
154
- decryptedText = this.decrypt(encryptedPayload);
155
- }
156
- // Legacy format: { e: ciphertext } (CryptoJS uyumluluğu)
157
- else if (typeof encryptedPayload === 'object' && encryptedPayload.e) {
158
- // Legacy CryptoJS decrypt fallback
159
- const CryptoJS = require('crypto-js');
160
- const bytes = CryptoJS.AES.decrypt(encryptedPayload.e, this.secretCode);
161
- decryptedText = bytes.toString(CryptoJS.enc.Utf8);
162
- }
163
- else {
164
- return;
165
- }
166
- if (!decryptedText)
167
- return;
168
- // Veri JSON mu? (Resize komutu vs olabilir)
169
- try {
170
- const cmd = JSON.parse(decryptedText);
171
- // Handle key rotation from peer
172
- if (cmd.type === 'key_rotation') {
173
- this.sessionCounter = cmd.counter;
174
- this.sessionKey = this.deriveSessionKey(this.sessionCounter);
175
- console.log(chalk.cyan(`🔄 Key synced to session #${this.sessionCounter}`));
176
- return;
177
- }
178
- // 1. Terminal Input (Keystrokes)
179
- if (cmd.type === 'input') {
180
- this.ptyProcess.write(cmd.data);
181
- return;
182
- }
183
- // 2. Terminal Resize
184
- if (cmd.type === 'resize') {
185
- this.ptyProcess.resize(cmd.cols, cmd.rows);
186
- return;
187
- }
188
- // 3. Chat Commands (e.g. "ls -la")
189
- if (cmd.type === 'command') {
190
- this.ptyProcess.write(cmd.text + '\r');
191
- return;
192
- }
193
- // 4. CLI Tool/Button Commands (e.g. "git status")
194
- if (cmd.type === 'cli_command') {
195
- this.ptyProcess.write(cmd.command + '\r');
196
- return;
197
- }
198
- // 5. Tool Calls (File System Access)
199
- if (cmd.type === 'tool_call') {
200
- this.handleToolCall(cmd.tool, cmd.data, cmd.id);
201
- return;
202
- }
203
- }
204
- catch (e) {
205
- // JSON değilse saf terminal girdisidir
206
- this.ptyProcess.write(decryptedText);
207
- }
208
- }
209
- catch (error) {
210
- // Şifre çözülemezse (Yanlış anahtar vb.) sessizce yut
211
- console.error(chalk.red('Decryption failed:'), error.message);
212
- }
213
- }
214
- async handleToolCall(tool, args, id) {
215
- try {
216
- let result = null;
217
- if (tool === 'list_directory') {
218
- const dirPath = args.path ? path.resolve(args.path) : process.cwd();
219
- const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
220
- const files = entries.map(entry => ({
221
- name: entry.name,
222
- type: entry.isDirectory() ? 'directory' : 'file',
223
- size: 0 // Simplification for speed
224
- }));
225
- result = { files };
226
- }
227
- else if (tool === 'read_file') {
228
- const filePath = path.resolve(args.path);
229
- const content = await fs.promises.readFile(filePath, 'utf-8');
230
- result = { content };
231
- }
232
- else {
233
- throw new Error(`Unknown tool: ${tool}`);
234
- }
235
- this.sendSecurePayload('tool_result', {
236
- id,
237
- result
238
- });
239
- }
240
- catch (error) {
241
- this.sendSecurePayload('tool_result', {
242
- id,
243
- error: error.message,
244
- type: 'error'
245
- });
246
- }
247
- }
248
- // Veriyi şifrele ve gönder
249
- sendSecurePayload(type, data) {
250
- let payloadStr = '';
251
- if (type === 'meta' || type === 'tool_result' || type === 'key_rotation') {
252
- // Standardize format
253
- if (type === 'tool_result') {
254
- if (data.type === 'error') {
255
- payloadStr = JSON.stringify({ ...data });
256
- }
257
- else {
258
- payloadStr = JSON.stringify({ type: 'result', ...data });
259
- }
260
- }
261
- else if (type === 'key_rotation') {
262
- payloadStr = JSON.stringify({ type: 'key_rotation', ...data });
263
- }
264
- else {
265
- payloadStr = JSON.stringify(data);
266
- }
267
- }
268
- else if (type === 'term_data') {
269
- // Wrap output in { type: 'output', data: ... }
270
- if (typeof data === 'string') {
271
- payloadStr = JSON.stringify({ type: 'output', data: data });
272
- }
273
- else {
274
- payloadStr = JSON.stringify(data);
275
- }
276
- }
277
- // Fallback if empty (shouldn't happen with above logic)
278
- if (!payloadStr)
279
- payloadStr = JSON.stringify(data);
280
- // 🔐 Yeni şifreleme formatı ile gönder
281
- const encrypted = this.encrypt(payloadStr);
282
- this.socket.emit('relay_data', {
283
- room: this.roomId,
284
- payload: encrypted
285
- });
286
- }
287
- sendEnvInfo() {
288
- const info = {
289
- type: 'host_info',
290
- username: os.userInfo().username,
291
- hostname: os.hostname(),
292
- platform: os.platform(),
293
- cwd: process.cwd()
294
- };
295
- this.sendSecurePayload('meta', info);
296
- }
297
- printBanner() {
298
- console.log('\n' + chalk.bgBlue.bold(' MOBILECODER ZK-RELAY v2.1.0 '));
299
- console.log(chalk.gray('🔐 PBKDF2-SHA256 + AES-256-CBC + Nonce-Based Replay Protection'));
300
- console.log(chalk.gray('🔄 Perfect Forward Secrecy with 10-min Key Rotation\n'));
301
- console.log(chalk.yellow('┌──────────────────────────────────────┐'));
302
- console.log(chalk.yellow('│ 🔑 CONNECTION CODE (ENTER ON APP) │'));
303
- console.log(chalk.yellow('│ │'));
304
- console.log(chalk.yellow(`│ ${chalk.white.bold.bgBlack(` ${this.secretCode.slice(0, 3)} - ${this.secretCode.slice(3)} `)} │`));
305
- console.log(chalk.yellow('│ │'));
306
- console.log(chalk.yellow('└──────────────────────────────────────┘'));
307
- console.log(chalk.cyan('\nWaiting for mobile connection...'));
308
- }
309
- // Cleanup
310
- destroy() {
311
- if (this.nonceCleanupInterval)
312
- clearInterval(this.nonceCleanupInterval);
313
- if (this.keyRotationInterval)
314
- clearInterval(this.keyRotationInterval);
315
- if (this.socket)
316
- this.socket.disconnect();
317
- if (this.ptyProcess)
318
- this.ptyProcess.kill();
319
- }
320
- }
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.ZKRelayAgent = void 0;
30
+ const socket_io_client_1 = require("socket.io-client");
31
+ const os = __importStar(require("os"));
32
+ const pty = __importStar(require("node-pty"));
33
+ const crypto = __importStar(require("crypto"));
34
+ const chalk_1 = __importDefault(require("chalk"));
35
+ const ora_1 = __importDefault(require("ora"));
36
+ const fs = __importStar(require("fs"));
37
+ const path = __importStar(require("path"));
38
+ // 🚨 BURAYI KENDİ DOMAIN ADRESİNLE DEĞİŞTİR!
39
+ const RELAY_SERVER_URL = 'https://api.mobilecoder.xyz';
40
+ // Security Constants
41
+ const PBKDF2_ITERATIONS = 100000;
42
+ const KEY_LENGTH = 32; // 256 bits
43
+ const SALT = 'mobilecoder-salt-v2';
44
+ const KEY_ROTATION_INTERVAL = 600000; // 10 minutes
45
+ const MESSAGE_MAX_AGE = 300000; // 5 minutes
46
+ class ZKRelayAgent {
47
+ socket;
48
+ ptyProcess;
49
+ secretCode;
50
+ roomId;
51
+ // 🔐 Güçlendirilmiş Güvenlik Değişkenleri
52
+ masterKey;
53
+ sessionKey;
54
+ sessionCounter = 0;
55
+ seenNonces = new Set();
56
+ nonceCleanupInterval = null;
57
+ keyRotationInterval = null;
58
+ spinner = (0, ora_1.default)('Initializing Secure Tunnel...');
59
+ constructor() {
60
+ this.setupSecurity();
61
+ this.initPTY();
62
+ this.connectToRelay();
63
+ this.startSecurityMaintenance();
64
+ }
65
+ // 1. Güvenlik Katmanı: PBKDF2 ile Güçlendirilmiş Anahtar Türetme
66
+ setupSecurity() {
67
+ // 6 Haneli rastgele kod (Kullanıcının göreceği)
68
+ const randomNum = Math.floor(100000 + Math.random() * 900000);
69
+ this.secretCode = randomNum.toString();
70
+ // Oda ID'si: Kodun Hash'i (Sunucu sadece bunu görür, kodu göremez)
71
+ this.roomId = crypto.createHash('sha256').update(this.secretCode).digest('hex');
72
+ // 🔐 PBKDF2 ile Master Key Türetme (Brute-force'a karşı savunma)
73
+ this.masterKey = crypto.pbkdf2Sync(this.secretCode, SALT, PBKDF2_ITERATIONS, KEY_LENGTH, 'sha256');
74
+ // Session Key (PFS için rotasyonlu)
75
+ this.sessionKey = this.deriveSessionKey(0);
76
+ }
77
+ // 🔐 Session Key Türetme (Perfect Forward Secrecy)
78
+ deriveSessionKey(counter) {
79
+ return crypto.pbkdf2Sync(this.masterKey, `session-${counter}`, 10000, KEY_LENGTH, 'sha256');
80
+ }
81
+ // 🔄 Key Rotation (Her 10 dakikada bir)
82
+ rotateKey() {
83
+ this.sessionCounter++;
84
+ this.sessionKey = this.deriveSessionKey(this.sessionCounter);
85
+ console.log(chalk_1.default.cyan(`🔄 Session key rotated to #${this.sessionCounter}`));
86
+ // Notify peer about key rotation
87
+ this.sendSecurePayload('key_rotation', { counter: this.sessionCounter });
88
+ }
89
+ // 🧹 Güvenlik Bakımı
90
+ startSecurityMaintenance() {
91
+ // Nonce temizliği (5 dakikadan eski nonce'ları sil)
92
+ this.nonceCleanupInterval = setInterval(() => {
93
+ this.seenNonces.clear();
94
+ }, MESSAGE_MAX_AGE);
95
+ // Key rotation
96
+ this.keyRotationInterval = setInterval(() => {
97
+ this.rotateKey();
98
+ }, KEY_ROTATION_INTERVAL);
99
+ }
100
+ // 2. Terminal (PTY) Başlatma
101
+ initPTY() {
102
+ const shell = os.platform() === 'win32' ? 'powershell.exe' : (process.env.SHELL || 'bash');
103
+ this.ptyProcess = pty.spawn(shell, [], {
104
+ name: 'xterm-256color',
105
+ cols: 80,
106
+ rows: 24,
107
+ cwd: process.cwd(),
108
+ env: process.env
109
+ });
110
+ // Terminalden çıkan veriyi şifrele ve yolla
111
+ this.ptyProcess.onData((data) => {
112
+ this.sendSecurePayload('term_data', data);
113
+ });
114
+ }
115
+ // 3. Relay Sunucusuna Bağlanma
116
+ connectToRelay() {
117
+ this.spinner.start('Connecting to MobileCoder Relay Network...');
118
+ this.socket = (0, socket_io_client_1.io)(RELAY_SERVER_URL, {
119
+ transports: ['websocket'],
120
+ reconnection: true,
121
+ reconnectionAttempts: Infinity
122
+ });
123
+ this.socket.on('connect', () => {
124
+ this.spinner.succeed(chalk_1.default.green('Connected to Relay Node!'));
125
+ // Odaya Katıl (Sadece Hash ID gönderilir)
126
+ this.socket.emit('join_room', { code: this.roomId, type: 'agent' });
127
+ this.printBanner();
128
+ });
129
+ this.socket.on('disconnect', () => {
130
+ this.spinner.warn(chalk_1.default.yellow('Connection lost. Reconnecting...'));
131
+ });
132
+ // Mobilden biri bağlandığında
133
+ this.socket.on('peer_joined', () => {
134
+ console.log(chalk_1.default.green('\n📱 Mobile Client Connected via Encrypted Tunnel!'));
135
+ this.sendEnvInfo();
136
+ });
137
+ // Mobilden şifreli veri geldiğinde
138
+ this.socket.on('relay_data', (message) => {
139
+ this.handleIncomingMessage(message);
140
+ });
141
+ // Also listen for connect_error
142
+ this.socket.on('connect_error', (err) => {
143
+ // console.error(err);
144
+ });
145
+ }
146
+ // 🔐 AES-256-CBC ile Şifreleme (Nonce tabanlı)
147
+ encrypt(data) {
148
+ const nonce = crypto.randomBytes(16);
149
+ const cipher = crypto.createCipheriv('aes-256-cbc', this.sessionKey, nonce);
150
+ const encrypted = Buffer.concat([
151
+ cipher.update(data, 'utf8'),
152
+ cipher.final()
153
+ ]);
154
+ return {
155
+ nonce: nonce.toString('base64'),
156
+ data: encrypted.toString('base64'),
157
+ timestamp: Date.now()
158
+ };
159
+ }
160
+ // 🔐 AES-256-CBC ile Şifre Çözme (Replay Attack Korumalı)
161
+ decrypt(packet) {
162
+ // Timestamp kontrolü (5 dakikadan eski mesajları reddet)
163
+ if (Date.now() - packet.timestamp > MESSAGE_MAX_AGE) {
164
+ throw new Error('Message too old - potential replay attack');
165
+ }
166
+ // Nonce uniqueness kontrolü (Replay attack önleme)
167
+ if (this.seenNonces.has(packet.nonce)) {
168
+ throw new Error('Replay attack detected - duplicate nonce');
169
+ }
170
+ this.seenNonces.add(packet.nonce);
171
+ const decipher = crypto.createDecipheriv('aes-256-cbc', this.sessionKey, Buffer.from(packet.nonce, 'base64'));
172
+ return Buffer.concat([
173
+ decipher.update(Buffer.from(packet.data, 'base64')),
174
+ decipher.final()
175
+ ]).toString('utf8');
176
+ }
177
+ // Gelen veriyi çöz ve işle
178
+ handleIncomingMessage(encryptedPayload) {
179
+ try {
180
+ let decryptedText;
181
+ // Yeni format: { nonce, data, timestamp }
182
+ if (typeof encryptedPayload === 'object' && encryptedPayload.nonce) {
183
+ decryptedText = this.decrypt(encryptedPayload);
184
+ }
185
+ // Legacy format: { e: ciphertext } (CryptoJS uyumluluğu)
186
+ else if (typeof encryptedPayload === 'object' && encryptedPayload.e) {
187
+ // Legacy CryptoJS decrypt fallback
188
+ const CryptoJS = require('crypto-js');
189
+ const bytes = CryptoJS.AES.decrypt(encryptedPayload.e, this.secretCode);
190
+ decryptedText = bytes.toString(CryptoJS.enc.Utf8);
191
+ }
192
+ else {
193
+ return;
194
+ }
195
+ if (!decryptedText)
196
+ return;
197
+ // Veri JSON mu? (Resize komutu vs olabilir)
198
+ try {
199
+ const cmd = JSON.parse(decryptedText);
200
+ // Handle key rotation from peer
201
+ if (cmd.type === 'key_rotation') {
202
+ this.sessionCounter = cmd.counter;
203
+ this.sessionKey = this.deriveSessionKey(this.sessionCounter);
204
+ console.log(chalk_1.default.cyan(`🔄 Key synced to session #${this.sessionCounter}`));
205
+ return;
206
+ }
207
+ // 1. Terminal Input (Keystrokes)
208
+ if (cmd.type === 'input') {
209
+ this.ptyProcess.write(cmd.data);
210
+ return;
211
+ }
212
+ // 2. Terminal Resize
213
+ if (cmd.type === 'resize') {
214
+ this.ptyProcess.resize(cmd.cols, cmd.rows);
215
+ return;
216
+ }
217
+ // 3. Chat Commands (e.g. "ls -la")
218
+ if (cmd.type === 'command') {
219
+ this.ptyProcess.write(cmd.text + '\r');
220
+ return;
221
+ }
222
+ // 4. CLI Tool/Button Commands (e.g. "git status")
223
+ if (cmd.type === 'cli_command') {
224
+ this.ptyProcess.write(cmd.command + '\r');
225
+ return;
226
+ }
227
+ // 5. Tool Calls (File System Access)
228
+ if (cmd.type === 'tool_call') {
229
+ this.handleToolCall(cmd.tool, cmd.data, cmd.id);
230
+ return;
231
+ }
232
+ }
233
+ catch (e) {
234
+ // JSON değilse saf terminal girdisidir
235
+ this.ptyProcess.write(decryptedText);
236
+ }
237
+ }
238
+ catch (error) {
239
+ // Şifre çözülemezse (Yanlış anahtar vb.) sessizce yut
240
+ console.error(chalk_1.default.red('Decryption failed:'), error.message);
241
+ }
242
+ }
243
+ async handleToolCall(tool, args, id) {
244
+ try {
245
+ let result = null;
246
+ if (tool === 'list_directory') {
247
+ const dirPath = args.path ? path.resolve(args.path) : process.cwd();
248
+ const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
249
+ const files = entries.map(entry => ({
250
+ name: entry.name,
251
+ type: entry.isDirectory() ? 'directory' : 'file',
252
+ size: 0 // Simplification for speed
253
+ }));
254
+ result = { files };
255
+ }
256
+ else if (tool === 'read_file') {
257
+ const filePath = path.resolve(args.path);
258
+ const content = await fs.promises.readFile(filePath, 'utf-8');
259
+ result = { content };
260
+ }
261
+ else {
262
+ throw new Error(`Unknown tool: ${tool}`);
263
+ }
264
+ this.sendSecurePayload('tool_result', {
265
+ id,
266
+ result
267
+ });
268
+ }
269
+ catch (error) {
270
+ this.sendSecurePayload('tool_result', {
271
+ id,
272
+ error: error.message,
273
+ type: 'error'
274
+ });
275
+ }
276
+ }
277
+ // Veriyi şifrele ve gönder
278
+ sendSecurePayload(type, data) {
279
+ let payloadStr = '';
280
+ if (type === 'meta' || type === 'tool_result' || type === 'key_rotation') {
281
+ // Standardize format
282
+ if (type === 'tool_result') {
283
+ if (data.type === 'error') {
284
+ payloadStr = JSON.stringify({ ...data });
285
+ }
286
+ else {
287
+ payloadStr = JSON.stringify({ type: 'result', ...data });
288
+ }
289
+ }
290
+ else if (type === 'key_rotation') {
291
+ payloadStr = JSON.stringify({ type: 'key_rotation', ...data });
292
+ }
293
+ else {
294
+ payloadStr = JSON.stringify(data);
295
+ }
296
+ }
297
+ else if (type === 'term_data') {
298
+ // Wrap output in { type: 'output', data: ... }
299
+ if (typeof data === 'string') {
300
+ payloadStr = JSON.stringify({ type: 'output', data: data });
301
+ }
302
+ else {
303
+ payloadStr = JSON.stringify(data);
304
+ }
305
+ }
306
+ // Fallback if empty (shouldn't happen with above logic)
307
+ if (!payloadStr)
308
+ payloadStr = JSON.stringify(data);
309
+ // 🔐 Yeni şifreleme formatı ile gönder
310
+ const encrypted = this.encrypt(payloadStr);
311
+ this.socket.emit('relay_data', {
312
+ room: this.roomId,
313
+ payload: encrypted
314
+ });
315
+ }
316
+ sendEnvInfo() {
317
+ const info = {
318
+ type: 'host_info',
319
+ username: os.userInfo().username,
320
+ hostname: os.hostname(),
321
+ platform: os.platform(),
322
+ cwd: process.cwd()
323
+ };
324
+ this.sendSecurePayload('meta', info);
325
+ }
326
+ printBanner() {
327
+ console.log('\n' + chalk_1.default.bgBlue.bold(' MOBILECODER ZK-RELAY v2.1.0 '));
328
+ console.log(chalk_1.default.gray('🔐 PBKDF2-SHA256 + AES-256-CBC + Nonce-Based Replay Protection'));
329
+ console.log(chalk_1.default.gray('🔄 Perfect Forward Secrecy with 10-min Key Rotation\n'));
330
+ console.log(chalk_1.default.yellow('┌──────────────────────────────────────┐'));
331
+ console.log(chalk_1.default.yellow('│ 🔑 CONNECTION CODE (ENTER ON APP) │'));
332
+ console.log(chalk_1.default.yellow('│ │'));
333
+ console.log(chalk_1.default.yellow(`│ ${chalk_1.default.white.bold.bgBlack(` ${this.secretCode.slice(0, 3)} - ${this.secretCode.slice(3)} `)} │`));
334
+ console.log(chalk_1.default.yellow('│ │'));
335
+ console.log(chalk_1.default.yellow('└──────────────────────────────────────┘'));
336
+ console.log(chalk_1.default.cyan('\nWaiting for mobile connection...'));
337
+ }
338
+ // Cleanup
339
+ destroy() {
340
+ if (this.nonceCleanupInterval)
341
+ clearInterval(this.nonceCleanupInterval);
342
+ if (this.keyRotationInterval)
343
+ clearInterval(this.keyRotationInterval);
344
+ if (this.socket)
345
+ this.socket.disconnect();
346
+ if (this.ptyProcess)
347
+ this.ptyProcess.kill();
348
+ }
349
+ }
350
+ exports.ZKRelayAgent = ZKRelayAgent;
321
351
  //# sourceMappingURL=agent.js.map