socksclient.d.ts 5.6 KB

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