You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

connectExample.md 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. # socks examples
  2. ## Example for SOCKS 'connect' command
  3. The connect command is the most common use-case for a SOCKS proxy. This establishes a direct connection to a destination host through a proxy server. The destination host only has knowledge of the proxy server connecting to it and does not know about the origin client (you).
  4. **Origin Client (you) <-> Proxy Server <-> Destination Server**
  5. In this example, we are connecting to a web server on port 80, and sending a very basic HTTP request to receive a response. It's worth noting that there are many socks-http-agents that can be used with the node http module (and libraries such as request.js) to make this easier. This HTTP request is used as a simple example.
  6. The 'connect' command can be used via the SocksClient.createConnection() factory function as well as by creating a SocksClient instance and using event handlers.
  7. ### Using createConnection with async/await
  8. Since SocksClient.createConnection returns a Promise, we can easily use async/await for flow control.
  9. ```typescript
  10. import { SocksClient, SocksClientOptions } from 'socks';
  11. const options: SocksClientOptions = {
  12. proxy: {
  13. host: '104.131.124.203',
  14. port: 1081,
  15. type: 5
  16. },
  17. destination: {
  18. host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
  19. port: 80
  20. },
  21. command: 'connect'
  22. };
  23. async function start() {
  24. try {
  25. const info = await SocksClient.createConnection(options);
  26. console.log(info.socket);
  27. // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
  28. info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
  29. info.socket.on('data', (data) => {
  30. console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
  31. /*
  32. HTTP/1.1 200 OK
  33. Access-Control-Allow-Origin: *
  34. Content-Type: application/json; charset=utf-8
  35. Date: Sun, 24 Dec 2017 03:47:51 GMT
  36. Content-Length: 300
  37. {
  38. "as":"AS14061 Digital Ocean, Inc.",
  39. "city":"Clifton",
  40. "country":"United States",
  41. "countryCode":"US",
  42. "isp":"Digital Ocean",
  43. "lat":40.8326,
  44. "lon":-74.1307,
  45. "org":"Digital Ocean",
  46. "query":"104.131.124.203",
  47. "region":"NJ",
  48. "regionName":"New Jersey",
  49. "status":"success",
  50. "timezone":"America/New_York",
  51. "zip":"07014"
  52. }
  53. */
  54. });
  55. } catch (err) {
  56. // Handle errors
  57. }
  58. }
  59. start();
  60. ```
  61. ### Using createConnection with Promises
  62. ```typescript
  63. import { SocksClient, SocksClientOptions } from 'socks';
  64. const options: SocksClientOptions = {
  65. proxy: {
  66. ipaddress: '104.131.124.203',
  67. port: 1081,
  68. type: 5
  69. },
  70. destination: {
  71. host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
  72. port: 80
  73. },
  74. command: 'connect'
  75. };
  76. SocksClient.createConnection(options)
  77. .then(info => {
  78. console.log(info.socket);
  79. // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
  80. info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
  81. info.socket.on('data', (data) => {
  82. console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
  83. /*
  84. HTTP/1.1 200 OK
  85. Access-Control-Allow-Origin: *
  86. Content-Type: application/json; charset=utf-8
  87. Date: Sun, 24 Dec 2017 03:47:51 GMT
  88. Content-Length: 300
  89. {
  90. "as":"AS14061 Digital Ocean, Inc.",
  91. "city":"Clifton",
  92. "country":"United States",
  93. "countryCode":"US",
  94. "isp":"Digital Ocean",
  95. "lat":40.8326,
  96. "lon":-74.1307,
  97. "org":"Digital Ocean",
  98. "query":"104.131.124.203",
  99. "region":"NJ",
  100. "regionName":"New Jersey",
  101. "status":"success",
  102. "timezone":"America/New_York",
  103. "zip":"07014"
  104. }
  105. */
  106. });
  107. })
  108. .catch(err => {
  109. // handle errors
  110. });
  111. ```
  112. ### Using createConnection with callbacks
  113. SocksClient.createConnection() optionally accepts a callback function as a second parameter.
  114. **Note:** If a callback function is provided, a Promise is still returned from the function, but the promise will always resolve regardless of if there was en error. (tldr: Do not mix callbacks and Promises).
  115. ```typescript
  116. import { SocksClient, SocksClientOptions } from 'socks';
  117. const options: SocksClientOptions = {
  118. proxy: {
  119. ipaddress: '104.131.124.203',
  120. port: 1081,
  121. type: 5
  122. },
  123. destination: {
  124. host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
  125. port: 80
  126. },
  127. command: 'connect'
  128. };
  129. SocksClient.createConnection(options, (err, info) => {
  130. if (err) {
  131. // handle errors
  132. } else {
  133. console.log(info.socket);
  134. // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
  135. info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
  136. info.socket.on('data', (data) => {
  137. console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
  138. /*
  139. HTTP/1.1 200 OK
  140. Access-Control-Allow-Origin: *
  141. Content-Type: application/json; charset=utf-8
  142. Date: Sun, 24 Dec 2017 03:47:51 GMT
  143. Content-Length: 300
  144. {
  145. "as":"AS14061 Digital Ocean, Inc.",
  146. "city":"Clifton",
  147. "country":"United States",
  148. "countryCode":"US",
  149. "isp":"Digital Ocean",
  150. "lat":40.8326,
  151. "lon":-74.1307,
  152. "org":"Digital Ocean",
  153. "query":"104.131.124.203",
  154. "region":"NJ",
  155. "regionName":"New Jersey",
  156. "status":"success",
  157. "timezone":"America/New_York",
  158. "zip":"07014"
  159. }
  160. */
  161. });
  162. }
  163. })
  164. ```
  165. ### Using event handlers
  166. SocksClient also supports instance creation of a SocksClient. This allows for event based flow control.
  167. ```typescript
  168. import { SocksClient, SocksClientOptions } from 'socks';
  169. const options: SocksClientOptions = {
  170. proxy: {
  171. ipaddress: '104.131.124.203',
  172. port: 1081,
  173. type: 5
  174. },
  175. destination: {
  176. host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
  177. port: 80
  178. },
  179. command: 'connect'
  180. };
  181. const client = new SocksClient(options);
  182. client.on('established', (info) => {
  183. console.log(info.socket);
  184. // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
  185. info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
  186. info.socket.on('data', (data) => {
  187. console.log(data.toString()); // ip-api.com sees that the last proxy (104.131.124.203) is connected to it and not the origin client (you).
  188. /*
  189. HTTP/1.1 200 OK
  190. Access-Control-Allow-Origin: *
  191. Content-Type: application/json; charset=utf-8
  192. Date: Sun, 24 Dec 2017 03:47:51 GMT
  193. Content-Length: 300
  194. {
  195. "as":"AS14061 Digital Ocean, Inc.",
  196. "city":"Clifton",
  197. "country":"United States",
  198. "countryCode":"US",
  199. "isp":"Digital Ocean",
  200. "lat":40.8326,
  201. "lon":-74.1307,
  202. "org":"Digital Ocean",
  203. "query":"104.131.124.203",
  204. "region":"NJ",
  205. "regionName":"New Jersey",
  206. "status":"success",
  207. "timezone":"America/New_York",
  208. "zip":"07014"
  209. }
  210. */
  211. });
  212. });
  213. // Failed to establish proxy connection to destination.
  214. client.on('error', () => {
  215. // Handle errors
  216. });
  217. // Start connection
  218. client.connect();
  219. ```