"use strict";function createProxyAgent(e){try{const t=new URL(e).protocol.toLowerCase();return"socks4:"===t||"socks5:"===t||"socks:"===t?new socks_proxy_agent_1.SocksProxyAgent(e):new https_proxy_agent_1.HttpsProxyAgent(e)}catch(e){return null}}var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,o,r){void 0===r&&(r=o);var n=Object.getOwnPropertyDescriptor(t,o);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[o]}});Object.defineProperty(e,r,n)}:function(e,t,o,r){void 0===r&&(r=o);e[r]=t[o]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(){var e=function(t){e=Object.getOwnPropertyNames||function(e){var t=[];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[t.length]=o);return t};return e(t)};return function(t){if(t&&t.__esModule)return t;var o={};if(null!=t)for(var r=e(t),n=0;n<r.length;n++)"default"!==r[n]&&__createBinding(o,t,r[n]);__setModuleDefault(o,t);return o}}();Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const db_1=require("@b/db"),ccxt=__importStar(require("ccxt")),https_proxy_agent_1=require("https-proxy-agent"),socks_proxy_agent_1=require("socks-proxy-agent"),console_1=require("@b/utils/console");exports.metadata={summary:"Test proxy connection for an exchange",operationId:"testExchangeProxy",tags:["Admin","Exchanges"],parameters:[{index:0,name:"productId",in:"path",description:"ID of the exchange to test proxy for",required:!0,schema:{type:"string"}}],requestBody:{description:"Proxy URL to test",content:{"application/json":{schema:{type:"object",properties:{proxyUrl:{type:"string",description:"Proxy URL to test (if not provided, uses saved proxy)"}}}}}},responses:{200:{description:"Proxy test result",content:{"application/json":{schema:{type:"object",properties:{status:{type:"boolean"},message:{type:"string"}}}}}}},requiresAuth:!0,permission:"edit.exchange",logModule:"ADMIN_FIN",logTitle:"Test Exchange Proxy"};exports.default=async e=>{var t,o,r,n;const{params:s,body:a,ctx:i}=e,{productId:c}=s,l=await db_1.models.exchange.findOne({where:{id:c}});if(!l)throw new Error("Exchange not found");const u=(null==a?void 0:a.proxyUrl)||l.proxyUrl,p=l.name;if(!u)return{status:!1,message:"No proxy URL provided. Please enter a proxy URL to test."};null===(t=null==i?void 0:i.step)||void 0===t||t.call(i,`Testing proxy connection for ${p}`);const d=createProxyAgent(u);if(!d)return{status:!1,message:"Invalid proxy URL format. Please use http://, https://, socks4://, or socks5:// protocol."};let y=null;try{null===(o=null==i?void 0:i.step)||void 0===o||o.call(i,"Creating test exchange instance with proxy");y=new ccxt.pro[p]({agent:d,timeout:15e3,enableRateLimit:!0});null===(r=null==i?void 0:i.step)||void 0===r||r.call(i,"Fetching server time through proxy");const e=await y.fetchTime();await y.close();if(e){null===(n=null==i?void 0:i.success)||void 0===n||n.call(i,"Proxy connection successful");return{status:!0,message:`Proxy connection successful! Server time: ${new Date(e).toISOString()}`}}return{status:!1,message:"Proxy connected but failed to get server response."}}catch(e){if(y)try{await y.close()}catch(e){}console_1.logger.error("EXCHANGE",`Proxy test failed for ${p}`,e);const t=e.message||"";return t.includes("451")||t.includes("restricted location")||t.includes("Eligibility")?{status:!1,message:"Proxy connected but the proxy server's location is also blocked by the exchange. Please use a proxy from an allowed region."}:"ECONNREFUSED"===e.code||"ETIMEDOUT"===e.code||"ENOTFOUND"===e.code?{status:!1,message:"Failed to connect to proxy server. Please check the proxy URL and ensure the proxy server is running."}:t.includes("407")||t.includes("Proxy Authentication")?{status:!1,message:"Proxy authentication failed. Please check your proxy credentials."}:{status:!1,message:`Proxy test failed: ${t||"Unknown error"}`}}};