// Proxy Management Module
class ProxyManager {
constructor(API_BASE) {
this.API_BASE = API_BASE;
this.providers = [];
}
// Initialize proxy manager
init(providers) {
this.providers = providers;
}
// Load proxy forms
async loadProxyForms() {
const proxyContainer = document.getElementById('proxy-container');
if (!this.providers || this.providers.length === 0) {
proxyContainer.innerHTML = `
`;
} catch (error) {
console.error(`Error creating proxy card for ${provider.name}:`, error);
return ''; // Return empty string on error
}
}
// Create advanced proxy section
createAdvancedProxySection(providerName, existingProxy) {
const scope = existingProxy?.scope || {
api_calls: true,
authentication: true,
manifests: true,
license: true,
all: true
};
return `
`;
}
// Get proxy configuration from API
async getProxyConfig(providerName) {
try {
const response = await fetch(`${this.API_BASE}/api/providers/${providerName}/proxy`);
if (response.ok) {
const data = await response.json();
return data.proxy_config || null;
}
return null;
} catch (error) {
console.error(`Error getting proxy config for ${providerName}:`, error);
return null;
}
}
// Save proxy configuration
async saveProxy(providerName) {
const host = document.getElementById(`proxy-host-${providerName}`).value.trim();
const port = document.getElementById(`proxy-port-${providerName}`).value.trim();
const type = document.getElementById(`proxy-type-${providerName}`).value;
const user = document.getElementById(`proxy-user-${providerName}`).value.trim();
const pass = document.getElementById(`proxy-pass-${providerName}`).value;
// Advanced options
const timeout = document.getElementById(`proxy-timeout-${providerName}`)?.value || 30;
const verifySsl = document.getElementById(`proxy-verify-ssl-${providerName}`)?.checked !== false;
// Scope settings
const scope = {
api_calls: document.getElementById(`proxy-scope-api-${providerName}`)?.checked || false,
authentication: document.getElementById(`proxy-scope-auth-${providerName}`)?.checked || false,
manifests: document.getElementById(`proxy-scope-manifests-${providerName}`)?.checked || false,
license: document.getElementById(`proxy-scope-license-${providerName}`)?.checked || false,
all: document.getElementById(`proxy-scope-all-${providerName}`)?.checked || false
};
if (!host || !port) {
this.showProxyAlert('error', 'Please fill in at least Host and Port fields', providerName);
return;
}
const proxyConfig = {
host,
port: parseInt(port),
proxy_type: type,
timeout: parseInt(timeout),
verify_ssl: verifySsl,
scope: scope
};
if (user) {
proxyConfig.auth = { username: user };
if (pass) {
proxyConfig.auth.password = pass;
}
}
try {
const response = await fetch(`${this.API_BASE}/api/providers/${providerName}/proxy`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(proxyConfig)
});
if (response.ok) {
this.showProxyAlert('success', `Proxy configuration saved for ${providerName}`, providerName);
// Clear password field
if (document.getElementById(`proxy-pass-${providerName}`)) {
document.getElementById(`proxy-pass-${providerName}`).value = '';
}
// Reload proxy form to show updated status
await this.loadProxyForms();
} else {
const result = await response.json();
this.showProxyAlert('error', result.error || `Failed to save proxy for ${providerName}`, providerName);
}
} catch (error) {
this.showProxyAlert('error', `Network error: ${error.message}`, providerName);
console.error('Proxy save error:', error);
}
}
// Delete proxy configuration
async deleteProxy(providerName) {
if (!confirm(`Delete proxy configuration for ${providerName}?`)) return;
try {
const response = await fetch(`${this.API_BASE}/api/providers/${providerName}/proxy`, {
method: 'DELETE'
});
if (response.ok) {
this.showProxyAlert('success', `Proxy configuration deleted for ${providerName}`, providerName);
// Reload proxy form
await this.loadProxyForms();
} else {
const result = await response.json();
this.showProxyAlert('error', result.error || `Failed to delete proxy for ${providerName}`, providerName);
}
} catch (error) {
this.showProxyAlert('error', `Network error: ${error.message}`, providerName);
console.error('Proxy delete error:', error);
}
}
// Test proxy connection
async testProxy(providerName) {
const testResultEl = document.getElementById(`proxy-test-result-${providerName}`);
if (testResultEl) {
testResultEl.innerHTML = '