tacticremote 1.6.0 → 1.6.5
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/README.md +9 -1
- package/bin/tactic-remote-server.js +636 -0
- package/lib/authChallengeManager.js +134 -0
- package/lib/bonjourPublisher.js +68 -0
- package/lib/errorCodes.js +127 -0
- package/lib/execSafe.js +218 -0
- package/lib/fileService.js +177 -0
- package/lib/hookInstaller.js +239 -0
- package/lib/outputParser.js +106 -0
- package/lib/rateLimiter.js +111 -0
- package/lib/tmuxManager.js +671 -0
- package/lib/uploadService.js +366 -0
- package/lib/validators.js +350 -0
- package/middleware/authMiddleware.js +152 -0
- package/package.json +32 -9
- package/routes/httpHandler.js +109 -0
- package/routes/wsHandler.js +1040 -0
- package/server.js +321 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auth Challenge Manager
|
|
3
|
+
* Manages challenge-response lifecycle for sensitive directory access.
|
|
4
|
+
* Server sends auth_challenge events to iOS, iOS responds with auth_response.
|
|
5
|
+
* Grants are cached per-client per-sensitiveDir with a TTL.
|
|
6
|
+
*
|
|
7
|
+
* ⚠️ SYNC WARNING: This file is duplicated in Windows/server/lib/authChallengeManager.js.
|
|
8
|
+
* Any changes here must be mirrored there to keep both servers in sync.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const crypto = require('crypto');
|
|
12
|
+
|
|
13
|
+
const CHALLENGE_TIMEOUT = 30_000; // 30 seconds
|
|
14
|
+
const GRANT_TTL = 5 * 60_000; // 5 minutes
|
|
15
|
+
|
|
16
|
+
// pendingChallenges: Map<challengeId, { ws, resolve, reject, timer, path, sensitiveDir, method }>
|
|
17
|
+
const pendingChallenges = new Map();
|
|
18
|
+
|
|
19
|
+
// grantCache: Map<ws, Map<sensitiveDir, expiresAt>>
|
|
20
|
+
const grantCache = new Map();
|
|
21
|
+
|
|
22
|
+
// dedupe: Map<ws, Map<sensitiveDir, Promise>> — avoid duplicate prompts for same dir
|
|
23
|
+
const pendingByDir = new Map();
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Check if a grant is still valid for this client + sensitiveDir
|
|
27
|
+
*/
|
|
28
|
+
function isGranted(ws, sensitiveDir) {
|
|
29
|
+
const clientGrants = grantCache.get(ws);
|
|
30
|
+
if (!clientGrants) return false;
|
|
31
|
+
const expiresAt = clientGrants.get(sensitiveDir);
|
|
32
|
+
if (!expiresAt) return false;
|
|
33
|
+
if (Date.now() >= expiresAt) {
|
|
34
|
+
clientGrants.delete(sensitiveDir);
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Add a grant for this client + sensitiveDir
|
|
42
|
+
*/
|
|
43
|
+
function addGrant(ws, sensitiveDir) {
|
|
44
|
+
if (!grantCache.has(ws)) grantCache.set(ws, new Map());
|
|
45
|
+
grantCache.get(ws).set(sensitiveDir, Date.now() + GRANT_TTL);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create an auth challenge and return a promise that resolves when the client responds.
|
|
50
|
+
* Deduplicates: if there's already a pending challenge for the same (ws, sensitiveDir),
|
|
51
|
+
* reuses the same promise.
|
|
52
|
+
*
|
|
53
|
+
* @returns {{ challengeId: string, expiresAt: number, promise: Promise<boolean> }}
|
|
54
|
+
*/
|
|
55
|
+
function createChallenge(ws, path, sensitiveDir, method) {
|
|
56
|
+
// Dedupe: reuse existing pending challenge for same (ws, sensitiveDir)
|
|
57
|
+
if (!pendingByDir.has(ws)) pendingByDir.set(ws, new Map());
|
|
58
|
+
const dirMap = pendingByDir.get(ws);
|
|
59
|
+
if (dirMap.has(sensitiveDir)) {
|
|
60
|
+
const existing = dirMap.get(sensitiveDir);
|
|
61
|
+
return existing;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const challengeId = crypto.randomUUID();
|
|
65
|
+
const expiresAt = Date.now() + CHALLENGE_TIMEOUT;
|
|
66
|
+
|
|
67
|
+
const promise = new Promise((resolve, reject) => {
|
|
68
|
+
const timer = setTimeout(() => {
|
|
69
|
+
pendingChallenges.delete(challengeId);
|
|
70
|
+
dirMap.delete(sensitiveDir);
|
|
71
|
+
resolve(false); // timeout = denied
|
|
72
|
+
}, CHALLENGE_TIMEOUT);
|
|
73
|
+
|
|
74
|
+
pendingChallenges.set(challengeId, {
|
|
75
|
+
ws, resolve, reject, timer, path, sensitiveDir, method
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const result = { challengeId, expiresAt, promise };
|
|
80
|
+
dirMap.set(sensitiveDir, result);
|
|
81
|
+
|
|
82
|
+
// Clean up dedupe entry when promise settles
|
|
83
|
+
promise.finally(() => {
|
|
84
|
+
dirMap.delete(sensitiveDir);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Resolve a pending challenge (called when iOS sends auth_response)
|
|
92
|
+
*/
|
|
93
|
+
function resolveChallenge(challengeId, granted) {
|
|
94
|
+
const challenge = pendingChallenges.get(challengeId);
|
|
95
|
+
if (!challenge) return false;
|
|
96
|
+
|
|
97
|
+
clearTimeout(challenge.timer);
|
|
98
|
+
pendingChallenges.delete(challengeId);
|
|
99
|
+
|
|
100
|
+
if (granted) {
|
|
101
|
+
addGrant(challenge.ws, challenge.sensitiveDir);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
challenge.resolve(granted);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Clean up all pending challenges and grants for a disconnected client
|
|
110
|
+
*/
|
|
111
|
+
function cleanupClient(ws) {
|
|
112
|
+
// Clean pending challenges
|
|
113
|
+
for (const [challengeId, challenge] of pendingChallenges) {
|
|
114
|
+
if (challenge.ws === ws) {
|
|
115
|
+
clearTimeout(challenge.timer);
|
|
116
|
+
challenge.resolve(false);
|
|
117
|
+
pendingChallenges.delete(challengeId);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Clean grants
|
|
121
|
+
grantCache.delete(ws);
|
|
122
|
+
// Clean dedupe
|
|
123
|
+
pendingByDir.delete(ws);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
module.exports = {
|
|
127
|
+
createChallenge,
|
|
128
|
+
resolveChallenge,
|
|
129
|
+
isGranted,
|
|
130
|
+
addGrant,
|
|
131
|
+
cleanupClient,
|
|
132
|
+
CHALLENGE_TIMEOUT,
|
|
133
|
+
GRANT_TTL
|
|
134
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bonjour/mDNS service publisher
|
|
3
|
+
* Broadcasts the server on the local network so iPhone can discover it
|
|
4
|
+
* Uses bonjour-service (pure JS, works on Windows without native deps)
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const os = require('os');
|
|
8
|
+
|
|
9
|
+
class BonjourPublisher {
|
|
10
|
+
constructor(port = 8765, apiKeyRequired = false, { silent = false } = {}) {
|
|
11
|
+
this.port = port;
|
|
12
|
+
this.apiKeyRequired = apiKeyRequired;
|
|
13
|
+
this.bonjour = null;
|
|
14
|
+
this.service = null;
|
|
15
|
+
this.silent = silent;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
start() {
|
|
19
|
+
try {
|
|
20
|
+
const { Bonjour } = require('bonjour-service');
|
|
21
|
+
this.bonjour = new Bonjour();
|
|
22
|
+
|
|
23
|
+
const hostname = os.hostname().replace(/[^a-zA-Z0-9-]/g, '') || 'Windows';
|
|
24
|
+
this.service = this.bonjour.publish({
|
|
25
|
+
name: `ClaudeCodeRemote-${hostname}`,
|
|
26
|
+
type: 'ws',
|
|
27
|
+
protocol: 'tcp',
|
|
28
|
+
port: this.port,
|
|
29
|
+
txt: {
|
|
30
|
+
version: '1.0',
|
|
31
|
+
apiKeyRequired: this.apiKeyRequired ? 'true' : 'false',
|
|
32
|
+
platform: process.platform
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (!this.silent) console.log(`📡 Bonjour: Publishing _ws._tcp on port ${this.port}`);
|
|
37
|
+
} catch (error) {
|
|
38
|
+
if (!this.silent) console.warn('Bonjour publishing failed:', error.message);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
stop() {
|
|
43
|
+
try {
|
|
44
|
+
if (this.service) {
|
|
45
|
+
this.service.stop();
|
|
46
|
+
this.service = null;
|
|
47
|
+
}
|
|
48
|
+
if (this.bonjour) {
|
|
49
|
+
this.bonjour.destroy();
|
|
50
|
+
this.bonjour = null;
|
|
51
|
+
}
|
|
52
|
+
if (!this.silent) console.log('📡 Bonjour: Stopped publishing');
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.warn('Bonjour stop error:', error.message);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
updateTXTRecord(apiKeyRequired) {
|
|
59
|
+
this.apiKeyRequired = apiKeyRequired;
|
|
60
|
+
// bonjour-service doesn't support live TXT updates; restart
|
|
61
|
+
if (this.service) {
|
|
62
|
+
this.stop();
|
|
63
|
+
this.start();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = { BonjourPublisher };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unified error codes for MacServer
|
|
3
|
+
* These codes are shared with iOS client for consistent error handling
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const ErrorCodes = {
|
|
7
|
+
// Authentication errors
|
|
8
|
+
AUTH_FAILED: 'AUTH_FAILED',
|
|
9
|
+
AUTH_RATE_LIMITED: 'AUTH_RATE_LIMITED',
|
|
10
|
+
|
|
11
|
+
// Session errors
|
|
12
|
+
SESSION_NOT_FOUND: 'SESSION_NOT_FOUND',
|
|
13
|
+
SESSION_INVALID_NAME: 'SESSION_INVALID_NAME',
|
|
14
|
+
SESSION_CREATE_FAILED: 'SESSION_CREATE_FAILED',
|
|
15
|
+
|
|
16
|
+
// Path errors
|
|
17
|
+
PATH_ACCESS_DENIED: 'PATH_ACCESS_DENIED',
|
|
18
|
+
PATH_NOT_FOUND: 'PATH_NOT_FOUND',
|
|
19
|
+
PATH_INVALID: 'PATH_INVALID',
|
|
20
|
+
|
|
21
|
+
// Command errors
|
|
22
|
+
COMMAND_INVALID: 'COMMAND_INVALID',
|
|
23
|
+
COMMAND_EXECUTION_FAILED: 'COMMAND_EXECUTION_FAILED',
|
|
24
|
+
|
|
25
|
+
// Connection errors
|
|
26
|
+
CONNECTION_TIMEOUT: 'CONNECTION_TIMEOUT',
|
|
27
|
+
CONNECTION_CLOSED: 'CONNECTION_CLOSED',
|
|
28
|
+
|
|
29
|
+
// Upload errors
|
|
30
|
+
UPLOAD_TOO_LARGE: 'UPLOAD_TOO_LARGE',
|
|
31
|
+
UPLOAD_INVALID_TYPE: 'UPLOAD_INVALID_TYPE',
|
|
32
|
+
UPLOAD_NOT_FOUND: 'UPLOAD_NOT_FOUND',
|
|
33
|
+
UPLOAD_CHUNK_ERROR: 'UPLOAD_CHUNK_ERROR',
|
|
34
|
+
UPLOAD_PATH_DENIED: 'UPLOAD_PATH_DENIED',
|
|
35
|
+
|
|
36
|
+
// General errors
|
|
37
|
+
INVALID_REQUEST: 'INVALID_REQUEST',
|
|
38
|
+
INTERNAL_ERROR: 'INTERNAL_ERROR',
|
|
39
|
+
RATE_LIMITED: 'RATE_LIMITED'
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Create a structured error response
|
|
44
|
+
* @param {string} code - Error code from ErrorCodes
|
|
45
|
+
* @param {string} message - Human-readable error message
|
|
46
|
+
* @param {Object} details - Additional error details
|
|
47
|
+
* @returns {Object} Structured error object
|
|
48
|
+
*/
|
|
49
|
+
function createError(code, message, details = {}) {
|
|
50
|
+
const error = {
|
|
51
|
+
type: 'error',
|
|
52
|
+
code,
|
|
53
|
+
error: message,
|
|
54
|
+
retryable: isRetryable(code),
|
|
55
|
+
...details
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Convert retryAfter (seconds) to retryAfterMs (milliseconds)
|
|
59
|
+
if (details.retryAfter !== undefined) {
|
|
60
|
+
error.retryAfterMs = details.retryAfter * 1000;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return error;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Create a rate limit error with retry information
|
|
68
|
+
* @param {number} retryAfter - Seconds until retry is allowed
|
|
69
|
+
* @returns {Object} Rate limit error object
|
|
70
|
+
*/
|
|
71
|
+
function createRateLimitError(retryAfter) {
|
|
72
|
+
return createError(
|
|
73
|
+
ErrorCodes.RATE_LIMITED,
|
|
74
|
+
'Too many requests, please try again later',
|
|
75
|
+
{ retryAfter }
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Create a session not found error
|
|
81
|
+
* @param {string} sessionName - Name of the session that was not found
|
|
82
|
+
* @returns {Object} Session error object
|
|
83
|
+
*/
|
|
84
|
+
function createSessionNotFoundError(sessionName) {
|
|
85
|
+
return createError(
|
|
86
|
+
ErrorCodes.SESSION_NOT_FOUND,
|
|
87
|
+
`Session not found: ${sessionName}`,
|
|
88
|
+
{ session: sessionName }
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Create a path access denied error
|
|
94
|
+
* @param {string} path - Path that was denied
|
|
95
|
+
* @returns {Object} Path error object
|
|
96
|
+
*/
|
|
97
|
+
function createPathAccessDeniedError(path) {
|
|
98
|
+
return createError(
|
|
99
|
+
ErrorCodes.PATH_ACCESS_DENIED,
|
|
100
|
+
`Access denied to path: ${path}`,
|
|
101
|
+
{ path }
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Check if an error code is retryable
|
|
107
|
+
* @param {string} code - Error code to check
|
|
108
|
+
* @returns {boolean} Whether the error is retryable
|
|
109
|
+
*/
|
|
110
|
+
function isRetryable(code) {
|
|
111
|
+
const retryableCodes = [
|
|
112
|
+
ErrorCodes.CONNECTION_TIMEOUT,
|
|
113
|
+
ErrorCodes.RATE_LIMITED,
|
|
114
|
+
ErrorCodes.AUTH_RATE_LIMITED,
|
|
115
|
+
ErrorCodes.INTERNAL_ERROR
|
|
116
|
+
];
|
|
117
|
+
return retryableCodes.includes(code);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = {
|
|
121
|
+
ErrorCodes,
|
|
122
|
+
createError,
|
|
123
|
+
createRateLimitError,
|
|
124
|
+
createSessionNotFoundError,
|
|
125
|
+
createPathAccessDeniedError,
|
|
126
|
+
isRetryable
|
|
127
|
+
};
|
package/lib/execSafe.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe command execution module
|
|
3
|
+
* Uses spawn instead of exec to prevent command injection
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { spawn } = require('child_process');
|
|
7
|
+
const os = require('os');
|
|
8
|
+
|
|
9
|
+
const TMUX_SOCKET_NAME = process.env.CLAUDE_REMOTE_TMUX_SOCKET || `tacticremote-${os.userInfo().username}`;
|
|
10
|
+
|
|
11
|
+
// Ensure Homebrew paths are in PATH for spawned processes
|
|
12
|
+
(function expandPath() {
|
|
13
|
+
const extra = process.platform === 'darwin'
|
|
14
|
+
? ['/opt/homebrew/bin', '/usr/local/bin', '/opt/homebrew/sbin', '/usr/local/sbin']
|
|
15
|
+
: ['/usr/local/bin'];
|
|
16
|
+
const current = (process.env.PATH || '').split(':');
|
|
17
|
+
const missing = extra.filter(p => !current.includes(p));
|
|
18
|
+
if (missing.length) process.env.PATH = [...missing, ...current].join(':');
|
|
19
|
+
})();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Execute a command safely using spawn (no shell interpolation)
|
|
23
|
+
* @param {string} cmd - The command to execute
|
|
24
|
+
* @param {string[]} args - Array of arguments
|
|
25
|
+
* @param {object} opts - Options for spawn
|
|
26
|
+
* @param {number} [opts.timeout] - Timeout in ms (default 30000, 0 = no timeout)
|
|
27
|
+
* @param {number} [opts.maxBuffer] - Maximum bytes per stream before abort (default 2MB)
|
|
28
|
+
* @returns {Promise<string>} - stdout output
|
|
29
|
+
*/
|
|
30
|
+
function execSafe(cmd, args = [], opts = {}) {
|
|
31
|
+
const { timeout = 30000, maxBuffer = 2 * 1024 * 1024, ...spawnOpts } = opts;
|
|
32
|
+
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
const child = spawn(cmd, args, {
|
|
35
|
+
shell: false,
|
|
36
|
+
...spawnOpts
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
let stdout = '';
|
|
40
|
+
let stderr = '';
|
|
41
|
+
let timedOut = false;
|
|
42
|
+
let bufferExceeded = false;
|
|
43
|
+
let exceededStream = '';
|
|
44
|
+
let timer = null;
|
|
45
|
+
|
|
46
|
+
let killTimer = null;
|
|
47
|
+
|
|
48
|
+
if (timeout > 0) {
|
|
49
|
+
timer = setTimeout(() => {
|
|
50
|
+
timedOut = true;
|
|
51
|
+
child.kill('SIGTERM');
|
|
52
|
+
// Force kill after 5s grace period
|
|
53
|
+
killTimer = setTimeout(() => { try { child.kill('SIGKILL'); } catch (_e) { /* ignore */ } }, 5000);
|
|
54
|
+
}, timeout);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
child.stdout.on('data', (data) => {
|
|
58
|
+
stdout += data.toString();
|
|
59
|
+
if (!bufferExceeded && stdout.length > maxBuffer) {
|
|
60
|
+
bufferExceeded = true;
|
|
61
|
+
exceededStream = 'stdout';
|
|
62
|
+
child.kill('SIGTERM');
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
child.stderr.on('data', (data) => {
|
|
67
|
+
stderr += data.toString();
|
|
68
|
+
if (!bufferExceeded && stderr.length > maxBuffer) {
|
|
69
|
+
bufferExceeded = true;
|
|
70
|
+
exceededStream = 'stderr';
|
|
71
|
+
child.kill('SIGTERM');
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
child.on('close', (code) => {
|
|
76
|
+
if (timer) clearTimeout(timer);
|
|
77
|
+
if (killTimer) clearTimeout(killTimer);
|
|
78
|
+
if (timedOut) {
|
|
79
|
+
const error = new Error(`Command timed out after ${timeout}ms: ${cmd} ${args.join(' ')}`);
|
|
80
|
+
error.code = 'ETIMEDOUT';
|
|
81
|
+
reject(error);
|
|
82
|
+
} else if (bufferExceeded) {
|
|
83
|
+
const error = new Error(`Command exceeded maxBuffer (${maxBuffer} bytes) on ${exceededStream}: ${cmd} ${args.join(' ')}`);
|
|
84
|
+
error.code = 'EMAXBUFFER';
|
|
85
|
+
error.stream = exceededStream;
|
|
86
|
+
reject(error);
|
|
87
|
+
} else if (code === 0) {
|
|
88
|
+
resolve(stdout.trim());
|
|
89
|
+
} else {
|
|
90
|
+
const error = new Error(stderr || `Command failed with code ${code}`);
|
|
91
|
+
error.code = code;
|
|
92
|
+
error.stderr = stderr;
|
|
93
|
+
reject(error);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
child.on('error', (err) => {
|
|
98
|
+
if (timer) clearTimeout(timer);
|
|
99
|
+
if (killTimer) clearTimeout(killTimer);
|
|
100
|
+
reject(err);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Execute tmux command safely
|
|
107
|
+
* @param {string} subCommand - tmux subcommand (e.g., 'send-keys', 'capture-pane')
|
|
108
|
+
* @param {string[]} args - Arguments for the subcommand
|
|
109
|
+
* @returns {Promise<string>}
|
|
110
|
+
*/
|
|
111
|
+
function execTmux(subCommand, args = []) {
|
|
112
|
+
const env = { ...process.env };
|
|
113
|
+
delete env.TMUX;
|
|
114
|
+
delete env.TMUX_PANE;
|
|
115
|
+
return execSafe('tmux', ['-L', TMUX_SOCKET_NAME, subCommand, ...args], { env });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Check if tmux session exists
|
|
120
|
+
* @param {string} sessionName - Session name (must be pre-validated)
|
|
121
|
+
* @returns {Promise<boolean>}
|
|
122
|
+
*/
|
|
123
|
+
async function tmuxSessionExists(sessionName) {
|
|
124
|
+
try {
|
|
125
|
+
await execTmux('has-session', ['-t', sessionName]);
|
|
126
|
+
return true;
|
|
127
|
+
} catch {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Capture tmux pane output
|
|
134
|
+
* @param {string} sessionName - Session name (must be pre-validated)
|
|
135
|
+
* @param {number} lines - Number of history lines to capture
|
|
136
|
+
* @returns {Promise<string>}
|
|
137
|
+
*/
|
|
138
|
+
function tmuxCapture(sessionName, lines = 500) {
|
|
139
|
+
return execTmux('capture-pane', ['-t', sessionName, '-p', '-S', `-${lines}`]);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Send keys to tmux session
|
|
144
|
+
* @param {string} sessionName - Session name (must be pre-validated)
|
|
145
|
+
* @param {string[]} keys - Keys to send
|
|
146
|
+
* @returns {Promise<string>}
|
|
147
|
+
*/
|
|
148
|
+
function tmuxSendKeys(sessionName, keys) {
|
|
149
|
+
return execTmux('send-keys', ['-t', sessionName, ...keys]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Create new tmux session with large history buffer
|
|
154
|
+
* @param {string} sessionName - Session name (must be pre-validated)
|
|
155
|
+
* @param {string} workingDir - Working directory (must be pre-validated)
|
|
156
|
+
* @param {number} [cols] - Optional initial column count
|
|
157
|
+
* @param {number} [rows] - Optional initial row count
|
|
158
|
+
* @returns {Promise<string>}
|
|
159
|
+
*/
|
|
160
|
+
async function tmuxNewSession(sessionName, workingDir, cols, rows) {
|
|
161
|
+
const args = ['-d', '-s', sessionName, '-c', workingDir];
|
|
162
|
+
if (cols && rows) {
|
|
163
|
+
args.push('-x', String(cols), '-y', String(rows));
|
|
164
|
+
}
|
|
165
|
+
await execTmux('new-session', args);
|
|
166
|
+
// Set large history-limit (50000 lines) for scrollback - essential for viewing Claude's long outputs
|
|
167
|
+
await execTmux('set-option', ['-t', sessionName, 'history-limit', '50000']);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Kill tmux session
|
|
172
|
+
* @param {string} sessionName - Session name (must be pre-validated)
|
|
173
|
+
* @returns {Promise<string>}
|
|
174
|
+
*/
|
|
175
|
+
function tmuxKillSession(sessionName) {
|
|
176
|
+
return execTmux('kill-session', ['-t', sessionName]);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* List tmux sessions
|
|
181
|
+
* @returns {Promise<string>}
|
|
182
|
+
*/
|
|
183
|
+
function tmuxListSessions() {
|
|
184
|
+
return execTmux('list-sessions', ['-F', '#{session_name}:#{session_attached}:#{pane_current_path}']);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Get tmux pane current path
|
|
189
|
+
* @param {string} sessionName - Session name (must be pre-validated)
|
|
190
|
+
* @returns {Promise<string>}
|
|
191
|
+
*/
|
|
192
|
+
function tmuxGetPanePath(sessionName) {
|
|
193
|
+
return execTmux('display-message', ['-t', sessionName, '-p', '#{pane_current_path}']);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Resize tmux window
|
|
198
|
+
* @param {string} sessionName - Session name (must be pre-validated)
|
|
199
|
+
* @param {number} cols - Number of columns
|
|
200
|
+
* @param {number} rows - Number of rows
|
|
201
|
+
* @returns {Promise<string>}
|
|
202
|
+
*/
|
|
203
|
+
function tmuxResizeWindow(sessionName, cols, rows) {
|
|
204
|
+
return execTmux('resize-window', ['-t', sessionName, '-x', String(cols), '-y', String(rows)]);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
module.exports = {
|
|
208
|
+
execSafe,
|
|
209
|
+
execTmux,
|
|
210
|
+
tmuxSessionExists,
|
|
211
|
+
tmuxCapture,
|
|
212
|
+
tmuxSendKeys,
|
|
213
|
+
tmuxNewSession,
|
|
214
|
+
tmuxKillSession,
|
|
215
|
+
tmuxListSessions,
|
|
216
|
+
tmuxGetPanePath,
|
|
217
|
+
tmuxResizeWindow
|
|
218
|
+
};
|