fix: Windows port discovery via Get-NetTCPConnection and ignore Gemini 4 Argon bucket overwrite
Build and Push Docker Image / build-and-push (push) Canceled after 0s
Build and Push Docker Image / build-and-push (push) Canceled after 0s
This commit is contained in:
1 parent
e98edaf675
commit
dff1f37c46
2 files changed
+191
-83
No files matched your search
+43
-2
@@ -94,11 +94,52 @@ async function discoverProcListeningPorts(pid) {
|
||||
return ports;
|
||||
}
|
||||
|
||||
async function discoverWindowsListeningPorts(pid) {
|
||||
const command = [
|
||||
'powershell -NoProfile -Command',
|
||||
`"Get-NetTCPConnection -OwningProcess ${pid} -ErrorAction SilentlyContinue`,
|
||||
'| Select-Object LocalAddress,LocalPort,RemotePort,State',
|
||||
'| ConvertTo-Json"',
|
||||
].join(' ');
|
||||
const result = await runCommand(command);
|
||||
const ports = [];
|
||||
if (result.stdout.trim()) {
|
||||
try {
|
||||
const parsed = JSON.parse(result.stdout);
|
||||
const rows = Array.isArray(parsed) ? parsed : [parsed];
|
||||
for (const row of rows) {
|
||||
if (!row) continue;
|
||||
const isLoopback = row.LocalAddress === '127.0.0.1' || row.LocalAddress === '::1';
|
||||
const isListeningState = row.State === null || row.State === 2 || row.State === 'Listen';
|
||||
const port = Number(row.LocalPort);
|
||||
if (isLoopback && Number(row.RemotePort) === 0 && isListeningState && port > 0 && port < 65536) {
|
||||
ports.push(port);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fall back to netstat below if PowerShell JSON parsing fails.
|
||||
}
|
||||
}
|
||||
if (ports.length > 0) return ports;
|
||||
|
||||
const netstatResult = await runCommand('netstat -ano');
|
||||
const netstatPorts = extractListeningPorts(netstatResult.stdout, pid);
|
||||
if (netstatPorts.length > 0) return netstatPorts;
|
||||
|
||||
for (const line of netstatResult.stdout.split(/\r?\n/)) {
|
||||
if (!belongsToProcess(line, pid) || !/(HERGESTELLT|ESTABLISHED)/i.test(line)) continue;
|
||||
const match = line.match(/^\s*TCP\s+(?:127\.0\.0\.1|\[?::1\]?):(\d{2,5})\s/i);
|
||||
if (!match) continue;
|
||||
const port = Number(match[1]);
|
||||
if (port > 0 && port < 65536) ports.push(port);
|
||||
}
|
||||
return ports;
|
||||
}
|
||||
|
||||
async function discoverListeningPorts(pid) {
|
||||
let ports = [];
|
||||
if (os.platform() === 'win32') {
|
||||
const result = await runCommand('netstat -ano');
|
||||
ports = extractListeningPorts(result.stdout, pid);
|
||||
ports = await discoverWindowsListeningPorts(pid);
|
||||
} else if (os.platform() === 'darwin') {
|
||||
const result = await runCommand('lsof -nP -iTCP -sTCP:LISTEN');
|
||||
ports = extractListeningPorts(result.stdout, pid);
|
||||
|
||||
+148
-81
@@ -89,6 +89,46 @@ function parseResponse(body) {
|
||||
return null;
|
||||
}
|
||||
|
||||
async function discoverWindowsListeningPorts(pid) {
|
||||
const command = [
|
||||
'powershell -NoProfile -Command',
|
||||
`"Get-NetTCPConnection -OwningProcess ${pid} -ErrorAction SilentlyContinue`,
|
||||
'| Select-Object LocalAddress,LocalPort,RemotePort,State',
|
||||
'| ConvertTo-Json"',
|
||||
].join(' ');
|
||||
const result = await runCommand(command);
|
||||
const ports = [];
|
||||
if (result.stdout.trim()) {
|
||||
try {
|
||||
const parsed = JSON.parse(result.stdout);
|
||||
const rows = Array.isArray(parsed) ? parsed : [parsed];
|
||||
for (const row of rows) {
|
||||
if (!row) continue;
|
||||
const isLoopback = row.LocalAddress === '127.0.0.1' || row.LocalAddress === '::1';
|
||||
const isListeningState = row.State === null || row.State === 2 || row.State === 'Listen';
|
||||
const port = Number(row.LocalPort);
|
||||
if (isLoopback && Number(row.RemotePort) === 0 && isListeningState && port > 0 && port < 65536) {
|
||||
ports.push(port);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fall back to netstat below if PowerShell JSON parsing fails.
|
||||
}
|
||||
}
|
||||
if (ports.length > 0) return ports;
|
||||
|
||||
const netstatResult = await runCommand('netstat -ano');
|
||||
for (const line of netstatResult.stdout.split(/\r?\n/)) {
|
||||
if (!new RegExp(`\\s${pid}\\s*$`).test(line)) continue;
|
||||
if (!/(LISTEN|ABH|HERGESTELLT|ESTABLISHED)/i.test(line)) continue;
|
||||
const match = line.match(/^\s*TCP\s+(?:127\.0\.0\.1|\[?::1\]?):(\d{2,5})\s/i);
|
||||
if (!match) continue;
|
||||
const port = Number(match[1]);
|
||||
if (port > 0 && port < 65536) ports.push(port);
|
||||
}
|
||||
return ports;
|
||||
}
|
||||
|
||||
async function syncToTokenWatch(quotaData) {
|
||||
const postData = JSON.stringify(quotaData);
|
||||
// Ersetze diese URL durch die echte von TokenWatch auf der Jannik-Cloud, sobald sie läuft.
|
||||
@@ -99,10 +139,24 @@ async function syncToTokenWatch(quotaData) {
|
||||
return new Promise((resolve) => {
|
||||
const req = client.request(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) }
|
||||
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) },
|
||||
timeout: 10000,
|
||||
}, (res) => {
|
||||
res.on('data', () => {});
|
||||
res.on('end', () => resolve(true));
|
||||
let body = '';
|
||||
res.on('data', chunk => body += chunk);
|
||||
res.on('end', () => {
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
resolve(true);
|
||||
} else {
|
||||
console.error(`Webhook HTTP ${res.statusCode}:`, body);
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
req.on('timeout', () => {
|
||||
console.error('Webhook error: Request timed out');
|
||||
req.destroy();
|
||||
resolve(false);
|
||||
});
|
||||
req.on('error', (err) => {
|
||||
console.error('Webhook error:', err.message);
|
||||
@@ -113,92 +167,105 @@ async function syncToTokenWatch(quotaData) {
|
||||
});
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log('Suche Antigravity Language Server...');
|
||||
const processes = await listLanguageServerProcesses();
|
||||
const processInfo = processes.find(item => extractArgument(item.commandLine, 'csrf_token'));
|
||||
if (!processInfo) { console.error('Fehler: Kein Antigravity language_server Prozess gefunden.'); return; }
|
||||
|
||||
const csrfToken = extractArgument(processInfo.commandLine, 'csrf_token');
|
||||
const extensionPort = Number(extractArgument(processInfo.commandLine, 'extension_server_port')) || null;
|
||||
|
||||
function extractListeningPorts(stdout, pid) {
|
||||
const ports = [];
|
||||
for (const line of stdout.split(/\r?\n/)) {
|
||||
// (LISTEN|ABH) matches German 'ABHÖREN' from netstat
|
||||
if (!/(LISTEN|ABH)/i.test(line) || !new RegExp(`\\s${pid}\\s*$`).test(line)) continue;
|
||||
for (const match of line.matchAll(/(?:127\.0\.0\.1|\[?::1\]?):(\d{2,5})/g)) {
|
||||
const port = Number(match[1]);
|
||||
if (port > 0 && port < 65536) ports.push(port);
|
||||
}
|
||||
}
|
||||
return ports;
|
||||
}
|
||||
|
||||
const netstatResult = await runCommand('netstat -ano');
|
||||
const listeningPorts = extractListeningPorts(netstatResult.stdout, processInfo.pid);
|
||||
const candidates = [...new Set(listeningPorts)].filter(port => port !== extensionPort).sort((a,b)=>a-b);
|
||||
|
||||
let rawData = null;
|
||||
for (const port of candidates) {
|
||||
for (const protocol of ['https', 'http']) {
|
||||
const result = await tryPort(port, csrfToken, protocol);
|
||||
if (result && result.statusCode === 200) {
|
||||
rawData = parseResponse(result.body);
|
||||
if (rawData) break;
|
||||
}
|
||||
}
|
||||
if (rawData) break;
|
||||
}
|
||||
|
||||
const actualData = Array.isArray(rawData) ? rawData[0] : rawData;
|
||||
const groups = actualData?.response?.groups;
|
||||
|
||||
if (!groups) {
|
||||
console.error('Fehler: Konnte keine JSON-Antwort mit Quota auslesen.', rawData);
|
||||
return;
|
||||
}
|
||||
|
||||
let gemini5h = 1, geminiWeekly = 1, gemini5hReset = null, geminiWeeklyReset = null;
|
||||
let claude5h = 1, claudeWeekly = 1, claude5hReset = null, claudeWeeklyReset = null;
|
||||
|
||||
function extractQuotaBuckets(groups) {
|
||||
let gemini5h = null, geminiWeekly = null, gemini5hReset = null, geminiWeeklyReset = null;
|
||||
let claude5h = null, claudeWeekly = null, claude5hReset = null, claudeWeeklyReset = null;
|
||||
|
||||
for (const group of groups) {
|
||||
const isGemini = group.displayName && group.displayName.toLowerCase().includes('gemini');
|
||||
const displayName = (group.displayName || '').toLowerCase();
|
||||
const isMainGeminiGroup = displayName === 'gemini models' || (displayName.includes('gemini') && !displayName.includes('argon'));
|
||||
const isClaudeGroup = displayName.includes('claude') || displayName.includes('3p');
|
||||
|
||||
for (const bucket of group.buckets || []) {
|
||||
if (isGemini) {
|
||||
if (bucket.window === '5h') {
|
||||
gemini5h = bucket.remainingFraction ?? 1;
|
||||
gemini5hReset = bucket.resetTime || null;
|
||||
} else if (bucket.window === 'weekly') {
|
||||
geminiWeekly = bucket.remainingFraction ?? 1;
|
||||
geminiWeeklyReset = bucket.resetTime || null;
|
||||
}
|
||||
} else {
|
||||
if (bucket.window === '5h') {
|
||||
claude5h = bucket.remainingFraction ?? 1;
|
||||
claude5hReset = bucket.resetTime || null;
|
||||
} else if (bucket.window === 'weekly') {
|
||||
claudeWeekly = bucket.remainingFraction ?? 1;
|
||||
claudeWeeklyReset = bucket.resetTime || null;
|
||||
}
|
||||
const id = bucket.bucketId;
|
||||
if (id === 'gemini-5h' || (gemini5h === null && isMainGeminiGroup && bucket.window === '5h')) {
|
||||
gemini5h = bucket.remainingFraction ?? 1;
|
||||
gemini5hReset = bucket.resetTime || null;
|
||||
} else if (id === 'gemini-weekly' || (geminiWeekly === null && isMainGeminiGroup && bucket.window === 'weekly')) {
|
||||
geminiWeekly = bucket.remainingFraction ?? 1;
|
||||
geminiWeeklyReset = bucket.resetTime || null;
|
||||
} else if (id === '3p-5h' || (claude5h === null && isClaudeGroup && bucket.window === '5h')) {
|
||||
claude5h = bucket.remainingFraction ?? 1;
|
||||
claude5hReset = bucket.resetTime || null;
|
||||
} else if (id === '3p-weekly' || (claudeWeekly === null && isClaudeGroup && bucket.window === 'weekly')) {
|
||||
claudeWeekly = bucket.remainingFraction ?? 1;
|
||||
claudeWeeklyReset = bucket.resetTime || null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const payload = {
|
||||
gemini_5h_remaining: Math.round(gemini5h * 100),
|
||||
gemini_weekly_remaining: Math.round(geminiWeekly * 100),
|
||||
claude_5h_remaining: Math.round(claude5h * 100),
|
||||
claude_weekly_remaining: Math.round(claudeWeekly * 100),
|
||||
|
||||
return {
|
||||
gemini_5h_remaining: Math.round((gemini5h ?? 1) * 100),
|
||||
gemini_weekly_remaining: Math.round((geminiWeekly ?? 1) * 100),
|
||||
claude_5h_remaining: Math.round((claude5h ?? 1) * 100),
|
||||
claude_weekly_remaining: Math.round((claudeWeekly ?? 1) * 100),
|
||||
gemini_5h_reset: gemini5hReset,
|
||||
gemini_weekly_reset: geminiWeeklyReset,
|
||||
claude_5h_reset: claude5hReset,
|
||||
claude_weekly_reset: claudeWeeklyReset
|
||||
claude_weekly_reset: claudeWeeklyReset,
|
||||
};
|
||||
|
||||
console.log('Gefundene Live-Daten:', payload);
|
||||
await syncToTokenWatch(payload);
|
||||
console.log('Erfolgreich an TokenWatch gesendet!');
|
||||
}
|
||||
|
||||
main().catch(console.error);
|
||||
async function main() {
|
||||
console.log('Suche Antigravity Language Server...');
|
||||
const processes = await listLanguageServerProcesses();
|
||||
const candidatesProcs = processes
|
||||
.filter(item => extractArgument(item.commandLine, 'csrf_token'))
|
||||
.sort((a, b) => {
|
||||
const aHub = extractArgument(a.commandLine, 'subclient_type') === 'hub' ? 0 : 1;
|
||||
const bHub = extractArgument(b.commandLine, 'subclient_type') === 'hub' ? 0 : 1;
|
||||
return aHub - bHub;
|
||||
});
|
||||
|
||||
if (candidatesProcs.length === 0) {
|
||||
console.error('Fehler: Kein Antigravity language_server Prozess gefunden.');
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
let actualData = null;
|
||||
for (const processInfo of candidatesProcs) {
|
||||
const csrfToken = extractArgument(processInfo.commandLine, 'csrf_token');
|
||||
const extensionPort = Number(extractArgument(processInfo.commandLine, 'extension_server_port')) || null;
|
||||
const listeningPorts = await discoverWindowsListeningPorts(processInfo.pid);
|
||||
const candidates = [...new Set(listeningPorts)].filter(port => port !== extensionPort).sort((a, b) => a - b);
|
||||
|
||||
for (const port of candidates) {
|
||||
for (const protocol of ['https', 'http']) {
|
||||
const result = await tryPort(port, csrfToken, protocol);
|
||||
if (result && result.statusCode === 200) {
|
||||
const parsed = parseResponse(result.body);
|
||||
const candidateData = Array.isArray(parsed) ? parsed[0] : parsed;
|
||||
if (candidateData?.response?.groups) {
|
||||
actualData = candidateData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (actualData) break;
|
||||
}
|
||||
if (actualData) break;
|
||||
}
|
||||
|
||||
const groups = actualData?.response?.groups;
|
||||
if (!groups) {
|
||||
console.error('Fehler: Konnte keine JSON-Antwort mit Quota auslesen.', actualData);
|
||||
process.exitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = extractQuotaBuckets(groups);
|
||||
console.log('Gefundene Live-Daten:', payload);
|
||||
const ok = await syncToTokenWatch(payload);
|
||||
if (ok) {
|
||||
console.log('Erfolgreich an TokenWatch gesendet!');
|
||||
} else {
|
||||
console.error('Fehler beim Senden an TokenWatch.');
|
||||
process.exitCode = 1;
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error(err);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in new issue
Block a user