socksclient.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /// <reference types="node" />
  2. /// <reference types="node" />
  3. /// <reference types="node" />
  4. import { EventEmitter } from 'events';
  5. import { SocksClientOptions, SocksClientChainOptions, SocksRemoteHost, SocksProxy, SocksClientBoundEvent, SocksClientEstablishedEvent, SocksUDPFrameDetails } from '../common/constants';
  6. import { SocksClientError } from '../common/util';
  7. import { Duplex } from 'stream';
  8. declare interface SocksClient {
  9. on(event: 'error', listener: (err: SocksClientError) => void): this;
  10. on(event: 'bound', listener: (info: SocksClientBoundEvent) => void): this;
  11. on(event: 'established', listener: (info: SocksClientEstablishedEvent) => void): this;
  12. once(event: string, listener: (...args: unknown[]) => void): this;
  13. once(event: 'error', listener: (err: SocksClientError) => void): this;
  14. once(event: 'bound', listener: (info: SocksClientBoundEvent) => void): this;
  15. once(event: 'established', listener: (info: SocksClientEstablishedEvent) => void): this;
  16. emit(event: string | symbol, ...args: unknown[]): boolean;
  17. emit(event: 'error', err: SocksClientError): boolean;
  18. emit(event: 'bound', info: SocksClientBoundEvent): boolean;
  19. emit(event: 'established', info: SocksClientEstablishedEvent): boolean;
  20. }
  21. declare class SocksClient extends EventEmitter implements SocksClient {
  22. private options;
  23. private socket;
  24. private state;
  25. private receiveBuffer;
  26. private nextRequiredPacketBufferSize;
  27. private socks5ChosenAuthType;
  28. private onDataReceived;
  29. private onClose;
  30. private onError;
  31. private onConnect;
  32. constructor(options: SocksClientOptions);
  33. /**
  34. * Creates a new SOCKS connection.
  35. *
  36. * Note: Supports callbacks and promises. Only supports the connect command.
  37. * @param options { SocksClientOptions } Options.
  38. * @param callback { Function } An optional callback function.
  39. * @returns { Promise }
  40. */
  41. static createConnection(options: SocksClientOptions, callback?: (error: Error | null, info?: SocksClientEstablishedEvent) => void): Promise<SocksClientEstablishedEvent>;
  42. /**
  43. * Creates a new SOCKS connection chain to a destination host through 2 or more SOCKS proxies.
  44. *
  45. * Note: Supports callbacks and promises. Only supports the connect method.
  46. * Note: Implemented via createConnection() factory function.
  47. * @param options { SocksClientChainOptions } Options
  48. * @param callback { Function } An optional callback function.
  49. * @returns { Promise }
  50. */
  51. static createConnectionChain(options: SocksClientChainOptions, callback?: (error: Error | null, socket?: SocksClientEstablishedEvent) => void): Promise<SocksClientEstablishedEvent>;
  52. /**
  53. * Creates a SOCKS UDP Frame.
  54. * @param options
  55. */
  56. static createUDPFrame(options: SocksUDPFrameDetails): Buffer;
  57. /**
  58. * Parses a SOCKS UDP frame.
  59. * @param data
  60. */
  61. static parseUDPFrame(data: Buffer): SocksUDPFrameDetails;
  62. /**
  63. * Internal state setter. If the SocksClient is in an error state, it cannot be changed to a non error state.
  64. */
  65. private setState;
  66. /**
  67. * Starts the connection establishment to the proxy and destination.
  68. * @param existingSocket Connected socket to use instead of creating a new one (internal use).
  69. */
  70. connect(existingSocket?: Duplex): void;
  71. private getSocketOptions;
  72. /**
  73. * Handles internal Socks timeout callback.
  74. * Note: If the Socks client is not BoundWaitingForConnection or Established, the connection will be closed.
  75. */
  76. private onEstablishedTimeout;
  77. /**
  78. * Handles Socket connect event.
  79. */
  80. private onConnectHandler;
  81. /**
  82. * Handles Socket data event.
  83. * @param data
  84. */
  85. private onDataReceivedHandler;
  86. /**
  87. * Handles processing of the data we have received.
  88. */
  89. private processData;
  90. /**
  91. * Handles Socket close event.
  92. * @param had_error
  93. */
  94. private onCloseHandler;
  95. /**
  96. * Handles Socket error event.
  97. * @param err
  98. */
  99. private onErrorHandler;
  100. /**
  101. * Removes internal event listeners on the underlying Socket.
  102. */
  103. private removeInternalSocketHandlers;
  104. /**
  105. * Closes and destroys the underlying Socket. Emits an error event.
  106. * @param err { String } An error string to include in error event.
  107. */
  108. private closeSocket;
  109. /**
  110. * Sends initial Socks v4 handshake request.
  111. */
  112. private sendSocks4InitialHandshake;
  113. /**
  114. * Handles Socks v4 handshake response.
  115. * @param data
  116. */
  117. private handleSocks4FinalHandshakeResponse;
  118. /**
  119. * Handles Socks v4 incoming connection request (BIND)
  120. * @param data
  121. */
  122. private handleSocks4IncomingConnectionResponse;
  123. /**
  124. * Sends initial Socks v5 handshake request.
  125. */
  126. private sendSocks5InitialHandshake;
  127. /**
  128. * Handles initial Socks v5 handshake response.
  129. * @param data
  130. */
  131. private handleInitialSocks5HandshakeResponse;
  132. /**
  133. * Sends Socks v5 user & password auth handshake.
  134. *
  135. * Note: No auth and user/pass are currently supported.
  136. */
  137. private sendSocks5UserPassAuthentication;
  138. private sendSocks5CustomAuthentication;
  139. private handleSocks5CustomAuthHandshakeResponse;
  140. private handleSocks5AuthenticationNoAuthHandshakeResponse;
  141. private handleSocks5AuthenticationUserPassHandshakeResponse;
  142. /**
  143. * Handles Socks v5 auth handshake response.
  144. * @param data
  145. */
  146. private handleInitialSocks5AuthenticationHandshakeResponse;
  147. /**
  148. * Sends Socks v5 final handshake request.
  149. */
  150. private sendSocks5CommandRequest;
  151. /**
  152. * Handles Socks v5 final handshake response.
  153. * @param data
  154. */
  155. private handleSocks5FinalHandshakeResponse;
  156. /**
  157. * Handles Socks v5 incoming connection request (BIND).
  158. */
  159. private handleSocks5IncomingConnectionResponse;
  160. get socksClientOptions(): SocksClientOptions;
  161. }
  162. export { SocksClient, SocksClientOptions, SocksClientChainOptions, SocksClientError, SocksRemoteHost, SocksProxy, SocksUDPFrameDetails, };