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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. const SocksClient = require('socks').SocksClient;
  11. const options = {
  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. } catch (err) {
  55. // Handle errors
  56. }
  57. }
  58. start();
  59. ```
  60. ### Using createConnection with Promises
  61. ```typescript
  62. const SocksClient = require('socks').SocksClient;
  63. const options = {
  64. proxy: {
  65. ipaddress: '104.131.124.203',
  66. port: 1081,
  67. type: 5
  68. },
  69. destination: {
  70. host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
  71. port: 80
  72. },
  73. command: 'connect'
  74. };
  75. SocksClient.createConnection(options)
  76. .then(info => {
  77. console.log(info.socket);
  78. // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
  79. info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
  80. info.socket.on('data', (data) => {
  81. 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).
  82. /*
  83. HTTP/1.1 200 OK
  84. Access-Control-Allow-Origin: *
  85. Content-Type: application/json; charset=utf-8
  86. Date: Sun, 24 Dec 2017 03:47:51 GMT
  87. Content-Length: 300
  88. {
  89. "as":"AS14061 Digital Ocean, Inc.",
  90. "city":"Clifton",
  91. "country":"United States",
  92. "countryCode":"US",
  93. "isp":"Digital Ocean",
  94. "lat":40.8326,
  95. "lon":-74.1307,
  96. "org":"Digital Ocean",
  97. "query":"104.131.124.203",
  98. "region":"NJ",
  99. "regionName":"New Jersey",
  100. "status":"success",
  101. "timezone":"America/New_York",
  102. "zip":"07014"
  103. }
  104. */
  105. })
  106. .catch(err => {
  107. // handle errors
  108. });
  109. ```
  110. ### Using createConnection with callbacks
  111. SocksClient.createConnection() optionally accepts a callback function as a second parameter.
  112. **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).
  113. ```typescript
  114. const SocksClient = require('socks').SocksClient;
  115. const options = {
  116. proxy: {
  117. ipaddress: '104.131.124.203',
  118. port: 1081,
  119. type: 5
  120. },
  121. destination: {
  122. host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
  123. port: 80
  124. },
  125. command: 'connect'
  126. };
  127. SocksClient.createConnection(options, (err, info) => {
  128. if (err) {
  129. // handle errors
  130. } else {
  131. console.log(info.socket);
  132. // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
  133. info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
  134. info.socket.on('data', (data) => {
  135. 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).
  136. /*
  137. HTTP/1.1 200 OK
  138. Access-Control-Allow-Origin: *
  139. Content-Type: application/json; charset=utf-8
  140. Date: Sun, 24 Dec 2017 03:47:51 GMT
  141. Content-Length: 300
  142. {
  143. "as":"AS14061 Digital Ocean, Inc.",
  144. "city":"Clifton",
  145. "country":"United States",
  146. "countryCode":"US",
  147. "isp":"Digital Ocean",
  148. "lat":40.8326,
  149. "lon":-74.1307,
  150. "org":"Digital Ocean",
  151. "query":"104.131.124.203",
  152. "region":"NJ",
  153. "regionName":"New Jersey",
  154. "status":"success",
  155. "timezone":"America/New_York",
  156. "zip":"07014"
  157. }
  158. */
  159. }
  160. })
  161. ```
  162. ### Using event handlers
  163. SocksClient also supports instance creation of a SocksClient. This allows for event based flow control.
  164. ```typescript
  165. const SocksClient = require('socks').SocksClient;
  166. const options = {
  167. proxy: {
  168. ipaddress: '104.131.124.203',
  169. port: 1081,
  170. type: 5
  171. },
  172. destination: {
  173. host: 'ip-api.com', // host names are supported with SOCKS v4a and SOCKS v5.
  174. port: 80
  175. },
  176. command: 'connect'
  177. };
  178. const client = new SocksClient(options);
  179. client.on('established', (info) => {
  180. console.log(info.socket);
  181. // <Socket ...> (this is a raw net.Socket that is established to the destination host through the given proxy servers)
  182. info.socket.write('GET /json HTTP/1.1\nHost: ip-api.com\n\n');
  183. info.socket.on('data', (data) => {
  184. 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).
  185. /*
  186. HTTP/1.1 200 OK
  187. Access-Control-Allow-Origin: *
  188. Content-Type: application/json; charset=utf-8
  189. Date: Sun, 24 Dec 2017 03:47:51 GMT
  190. Content-Length: 300
  191. {
  192. "as":"AS14061 Digital Ocean, Inc.",
  193. "city":"Clifton",
  194. "country":"United States",
  195. "countryCode":"US",
  196. "isp":"Digital Ocean",
  197. "lat":40.8326,
  198. "lon":-74.1307,
  199. "org":"Digital Ocean",
  200. "query":"104.131.124.203",
  201. "region":"NJ",
  202. "regionName":"New Jersey",
  203. "status":"success",
  204. "timezone":"America/New_York",
  205. "zip":"07014"
  206. }
  207. */
  208. });
  209. // Failed to establish proxy connection to destination.
  210. client.on('error', () => {
  211. // Handle errors
  212. });
  213. ```