* Create a SOCKS5 connection to the proxy
(targetHost, targetPort)
| 10953 | * Create a SOCKS5 connection to the proxy |
| 10954 | */ |
| 10955 | async createSocks5Connection(targetHost, targetPort) { |
| 10956 | const proxyHost = this[kProxyUrl].hostname; |
| 10957 | const proxyPort = parseInt(this[kProxyUrl].port) || 1080; |
| 10958 | debug("creating SOCKS5 connection to", proxyHost, proxyPort); |
| 10959 | const socketReady = Promise.withResolvers(); |
| 10960 | this[kConnector]({ |
| 10961 | hostname: proxyHost, |
| 10962 | host: proxyHost, |
| 10963 | port: proxyPort, |
| 10964 | protocol: this[kProxyProtocol] |
| 10965 | }, (err, socket2) => { |
| 10966 | if (err) { |
| 10967 | socketReady.reject(err); |
| 10968 | } else { |
| 10969 | socketReady.resolve(socket2); |
| 10970 | } |
| 10971 | }); |
| 10972 | const socket = await socketReady.promise; |
| 10973 | const socks5Client = new Socks5Client(socket, this[kProxyAuth]); |
| 10974 | socks5Client.on("error", (err) => { |
| 10975 | debug("SOCKS5 error:", err); |
| 10976 | socket.destroy(); |
| 10977 | }); |
| 10978 | await socks5Client.handshake(); |
| 10979 | const authenticationReady = Promise.withResolvers(); |
| 10980 | const authenticationTimeout = setTimeout(() => { |
| 10981 | authenticationReady.reject(new Error("SOCKS5 authentication timeout")); |
| 10982 | }, 5e3); |
| 10983 | const onAuthenticated = /* @__PURE__ */ __name(() => { |
| 10984 | clearTimeout(authenticationTimeout); |
| 10985 | socks5Client.removeListener("error", onAuthenticationError); |
| 10986 | authenticationReady.resolve(); |
| 10987 | }, "onAuthenticated"); |
| 10988 | const onAuthenticationError = /* @__PURE__ */ __name((err) => { |
| 10989 | clearTimeout(authenticationTimeout); |
| 10990 | socks5Client.removeListener("authenticated", onAuthenticated); |
| 10991 | authenticationReady.reject(err); |
| 10992 | }, "onAuthenticationError"); |
| 10993 | if (socks5Client.state === STATES.AUTHENTICATED) { |
| 10994 | clearTimeout(authenticationTimeout); |
| 10995 | authenticationReady.resolve(); |
| 10996 | } else { |
| 10997 | socks5Client.once("authenticated", onAuthenticated); |
| 10998 | socks5Client.once("error", onAuthenticationError); |
| 10999 | } |
| 11000 | await authenticationReady.promise; |
| 11001 | await socks5Client.connect(targetHost, targetPort); |
| 11002 | const connectionReady = Promise.withResolvers(); |
| 11003 | const connectionTimeout = setTimeout(() => { |
| 11004 | connectionReady.reject(new Error("SOCKS5 connection timeout")); |
| 11005 | }, 5e3); |
| 11006 | const onConnected = /* @__PURE__ */ __name((info) => { |
| 11007 | debug("SOCKS5 tunnel established to", targetHost, targetPort, "via", info); |
| 11008 | clearTimeout(connectionTimeout); |
| 11009 | socks5Client.removeListener("error", onConnectionError); |
| 11010 | connectionReady.resolve(); |
| 11011 | }, "onConnected"); |
| 11012 | const onConnectionError = /* @__PURE__ */ __name((err) => { |
no test coverage detected