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