fix: correct proxy agent configuration and enhance testing
This commit is contained in:
parent
aa099bad37
commit
72038d9ad1
3 changed files with 225 additions and 73 deletions
|
|
@ -1,24 +1,33 @@
|
|||
import { ProxyAgent } from "undici";
|
||||
import { httpFetchClient } from "../lib/http-client.js";
|
||||
|
||||
const proxyAgent = new ProxyAgent("http://wireguard:8888");
|
||||
import { createSmsGatewayClient } from "../lib/sms-gateway";
|
||||
|
||||
async function testEndpoint(url, useProxy = true, options = {}) {
|
||||
const startTime = Date.now();
|
||||
try {
|
||||
const fetchOptions = {
|
||||
method: options.method || "GET",
|
||||
headers: options.headers || {},
|
||||
...options,
|
||||
};
|
||||
let response;
|
||||
|
||||
if (useProxy) {
|
||||
fetchOptions.agent = proxyAgent;
|
||||
if (options.method === "POST") {
|
||||
response = await httpFetchClient.post(
|
||||
url,
|
||||
options.body,
|
||||
options.headers,
|
||||
);
|
||||
} else {
|
||||
response = await httpFetchClient.get(url, options.headers);
|
||||
}
|
||||
} else {
|
||||
const fetchOptions = {
|
||||
method: options.method || "GET",
|
||||
headers: options.headers || {},
|
||||
timeout: 15000,
|
||||
...options,
|
||||
};
|
||||
response = await $fetch(url, fetchOptions);
|
||||
}
|
||||
|
||||
const response = await $fetch(url, fetchOptions);
|
||||
const endTime = Date.now();
|
||||
|
||||
|
||||
return {
|
||||
success: true,
|
||||
url,
|
||||
|
|
@ -26,7 +35,7 @@ async function testEndpoint(url, useProxy = true, options = {}) {
|
|||
responseTime: endTime - startTime,
|
||||
status: response.status || 200,
|
||||
data: response,
|
||||
error: null
|
||||
error: null,
|
||||
};
|
||||
} catch (error) {
|
||||
const endTime = Date.now();
|
||||
|
|
@ -35,26 +44,27 @@ async function testEndpoint(url, useProxy = true, options = {}) {
|
|||
url,
|
||||
useProxy,
|
||||
responseTime: endTime - startTime,
|
||||
status: error.status || null,
|
||||
status: error.status || error.statusCode || null,
|
||||
data: null,
|
||||
error: {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
stack: error.stack
|
||||
}
|
||||
name: error.name,
|
||||
stack: process.env.NODE_ENV === "development" ? error.stack : undefined,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function testDNSResolution(hostname) {
|
||||
try {
|
||||
const dns = await import('dns').then(m => m.promises);
|
||||
const dns = await import("dns").then((m) => m.promises);
|
||||
const addresses = await dns.lookup(hostname);
|
||||
return {
|
||||
success: true,
|
||||
hostname,
|
||||
addresses,
|
||||
error: null
|
||||
error: null,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
|
|
@ -63,8 +73,8 @@ async function testDNSResolution(hostname) {
|
|||
addresses: null,
|
||||
error: {
|
||||
message: error.message,
|
||||
code: error.code
|
||||
}
|
||||
code: error.code,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -72,67 +82,123 @@ async function testDNSResolution(hostname) {
|
|||
export default defineEventHandler(async (event) => {
|
||||
const config = useRuntimeConfig();
|
||||
const query = getQuery(event);
|
||||
const testType = query.test || 'all';
|
||||
|
||||
const testType = query.test || "all";
|
||||
|
||||
const results = {
|
||||
timestamp: new Date().toISOString(),
|
||||
environment: {
|
||||
nodeEnv: process.env.NODE_ENV,
|
||||
proxyUrl: "http://wireguard:8888",
|
||||
smsGatewayUrl: config.androidSmsGatewayUrl,
|
||||
smsGatewayBypass: config.androidSmsGatewayBypass
|
||||
smsGatewayBypass: config.androidSmsGatewayBypass,
|
||||
},
|
||||
tests: {}
|
||||
tests: {},
|
||||
};
|
||||
|
||||
// DNS Resolution Tests
|
||||
if (testType === 'all' || testType === 'dns') {
|
||||
if (testType === "all" || testType === "dns") {
|
||||
results.tests.dns = {
|
||||
wireguard: await testDNSResolution('wireguard'),
|
||||
google: await testDNSResolution('google.com'),
|
||||
httpbin: await testDNSResolution('httpbin.org')
|
||||
wireguard: await testDNSResolution("wireguard"),
|
||||
google: await testDNSResolution("google.com"),
|
||||
httpbin: await testDNSResolution("httpbin.org"),
|
||||
};
|
||||
}
|
||||
|
||||
// Basic Connectivity Tests
|
||||
if (testType === 'all' || testType === 'connectivity') {
|
||||
if (testType === "all" || testType === "connectivity") {
|
||||
results.tests.connectivity = {};
|
||||
|
||||
|
||||
// Test known good endpoints
|
||||
const testEndpoints = [
|
||||
'https://httpbin.org/get',
|
||||
'https://google.com',
|
||||
'https://api.github.com'
|
||||
"https://httpbin.org/get",
|
||||
"https://google.com",
|
||||
"https://api.github.com",
|
||||
];
|
||||
|
||||
for (const endpoint of testEndpoints) {
|
||||
const endpointKey = endpoint.replace(/[^a-zA-Z0-9]/g, '_');
|
||||
results.tests.connectivity[`${endpointKey}_direct`] = await testEndpoint(endpoint, false);
|
||||
results.tests.connectivity[`${endpointKey}_proxy`] = await testEndpoint(endpoint, true);
|
||||
const endpointKey = endpoint.replace(/[^a-zA-Z0-9]/g, "_");
|
||||
results.tests.connectivity[`${endpointKey}_direct`] = await testEndpoint(
|
||||
endpoint,
|
||||
false,
|
||||
);
|
||||
results.tests.connectivity[`${endpointKey}_proxy`] = await testEndpoint(
|
||||
endpoint,
|
||||
true,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// SMS Gateway Specific Tests
|
||||
if (testType === 'all' || testType === 'sms') {
|
||||
if (testType === "all" || testType === "sms") {
|
||||
results.tests.smsGateway = {};
|
||||
|
||||
|
||||
if (config.androidSmsGatewayUrl) {
|
||||
// Test basic connectivity to SMS gateway
|
||||
const smsUrl = config.androidSmsGatewayUrl;
|
||||
results.tests.smsGateway.proxy = await testEndpoint(smsUrl, true);
|
||||
|
||||
// Test SMS gateway health endpoint if available
|
||||
|
||||
// Test SMS gateway health endpoint
|
||||
const healthUrl = `${smsUrl}/health`;
|
||||
results.tests.smsGateway.health_proxy = await testEndpoint(healthUrl, true);
|
||||
|
||||
results.tests.smsGateway.health_proxy = await testEndpoint(
|
||||
healthUrl,
|
||||
true,
|
||||
);
|
||||
|
||||
// Test with authentication headers
|
||||
if (config.androidSmsGatewayLogin && config.androidSmsGatewayPassword) {
|
||||
const authHeaders = {
|
||||
'Authorization': `Basic ${Buffer.from(`${config.androidSmsGatewayLogin}:${config.androidSmsGatewayPassword}`).toString('base64')}`
|
||||
Authorization: `Basic ${Buffer.from(`${config.androidSmsGatewayLogin}:${config.androidSmsGatewayPassword}`).toString("base64")}`,
|
||||
};
|
||||
|
||||
results.tests.smsGateway.auth_direct = await testEndpoint(smsUrl, false, { headers: authHeaders });
|
||||
results.tests.smsGateway.auth_proxy = await testEndpoint(smsUrl, true, { headers: authHeaders });
|
||||
|
||||
// Test authenticated status endpoint (this simulates real SMS gateway usage)
|
||||
results.tests.smsGateway.auth_status_proxy = await testEndpoint(
|
||||
smsUrl + "/device",
|
||||
true,
|
||||
{ headers: authHeaders },
|
||||
);
|
||||
|
||||
const api = createSmsGatewayClient(config);
|
||||
const message = {
|
||||
phoneNumbers: ["2067452154"],
|
||||
message: `Testing 1 2 3...`,
|
||||
};
|
||||
const startTime = Date.now();
|
||||
try {
|
||||
const msg_id = (await api.send(message)).id;
|
||||
await new Promise((r) => setTimeout(r, 3000));
|
||||
const state = await api.getState(msg_id);
|
||||
const endTime = Date.now();
|
||||
|
||||
results.tests.smsGateway.auth_api_send_msg = {
|
||||
success: true,
|
||||
url: null,
|
||||
useProxy: true,
|
||||
responseTime: endTime - startTime,
|
||||
status: state.state,
|
||||
data: "msg_id: " + state.id,
|
||||
error: null,
|
||||
};
|
||||
} catch (error) {
|
||||
const endTime = Date.now();
|
||||
results.tests.smsGateway.auth_api_send_msg = {
|
||||
success: false,
|
||||
url: null,
|
||||
useProxy: true,
|
||||
responseTime: endTime - startTime,
|
||||
status: error.status || error.statusCode || null,
|
||||
data: null,
|
||||
error: {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
name: error.name,
|
||||
stack:
|
||||
process.env.NODE_ENV === "development"
|
||||
? error.stack
|
||||
: undefined,
|
||||
},
|
||||
};
|
||||
}
|
||||
} else {
|
||||
results.tests.smsGateway.auth_error =
|
||||
"SMS Gateway credentials not configured";
|
||||
}
|
||||
} else {
|
||||
results.tests.smsGateway.error = "SMS Gateway URL not configured";
|
||||
|
|
@ -140,15 +206,13 @@ export default defineEventHandler(async (event) => {
|
|||
}
|
||||
|
||||
// HTTP Client Library Test
|
||||
if (testType === 'all' || testType === 'httpclient') {
|
||||
if (testType === "all" || testType === "httpclient") {
|
||||
results.tests.httpClient = {};
|
||||
|
||||
|
||||
try {
|
||||
// Test using your existing http client
|
||||
const testResult = await httpFetchClient.get('https://httpbin.org/get', {
|
||||
'User-Agent': 'Portfolio-Proxy-Test/1.0'
|
||||
});
|
||||
|
||||
const testResult = await httpFetchClient.get("https://httpbin.org/get");
|
||||
|
||||
results.tests.httpClient.success = true;
|
||||
results.tests.httpClient.data = testResult;
|
||||
results.tests.httpClient.error = null;
|
||||
|
|
@ -163,19 +227,14 @@ export default defineEventHandler(async (event) => {
|
|||
}
|
||||
}
|
||||
|
||||
// Proxy Agent Direct Test
|
||||
if (testType === 'all' || testType === 'proxyagent') {
|
||||
// Proxy Agent Direct Test (using httpFetchClient)
|
||||
if (testType === "all" || testType === "proxyagent") {
|
||||
results.tests.proxyAgent = {};
|
||||
|
||||
|
||||
try {
|
||||
// Test proxy agent directly
|
||||
const response = await $fetch('https://httpbin.org/ip', {
|
||||
agent: proxyAgent,
|
||||
headers: {
|
||||
'User-Agent': 'Portfolio-Proxy-Test/1.0'
|
||||
}
|
||||
});
|
||||
|
||||
// Test proxy agent using httpFetchClient (which uses ProxyAgent internally)
|
||||
const response = await httpFetchClient.get("https://httpbin.org/ip");
|
||||
|
||||
results.tests.proxyAgent.success = true;
|
||||
results.tests.proxyAgent.data = response;
|
||||
results.tests.proxyAgent.error = null;
|
||||
|
|
@ -185,7 +244,8 @@ export default defineEventHandler(async (event) => {
|
|||
results.tests.proxyAgent.error = {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
stack: error.stack
|
||||
name: error.name,
|
||||
stack: process.env.NODE_ENV === "development" ? error.stack : undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import { ProxyAgent } from "undici";
|
||||
|
||||
const proxyAgent = new ProxyAgent("http://wireguard:8888");
|
||||
const REQUEST_TIMEOUT = 5000;
|
||||
const REQUEST_TIMEOUT = 15000;
|
||||
|
||||
export const httpFetchClient = {
|
||||
get: async (url, headers) => {
|
||||
const response = await $fetch(url, {
|
||||
method: "GET",
|
||||
headers,
|
||||
agent: proxyAgent,
|
||||
dispatcher: proxyAgent,
|
||||
timeout: REQUEST_TIMEOUT,
|
||||
});
|
||||
return response;
|
||||
|
|
@ -18,7 +18,7 @@ export const httpFetchClient = {
|
|||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", ...headers },
|
||||
body: JSON.stringify(body),
|
||||
agent: proxyAgent,
|
||||
dispatcher: proxyAgent,
|
||||
timeout: REQUEST_TIMEOUT,
|
||||
});
|
||||
return response;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue