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.

migratingFromV1.md 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # socks
  2. ## Migrating from v1
  3. For the most part, migrating from v1 takes minimal effort as v2 still supports factory creation of proxy connections with callback support.
  4. ### Notable breaking changes
  5. - In an options object, the proxy 'command' is now required and does not default to 'connect'.
  6. - **In an options object, 'target' is now known as 'destination'.**
  7. - Sockets are no longer paused after a SOCKS connection is made, so socket.resume() is no longer required. (Please be sure to attach data handlers immediately to the Socket to avoid losing data).
  8. - In v2, only the 'connect' command is supported via the factory SocksClient.createConnection function. (BIND and ASSOCIATE must be used with a SocksClient instance via event handlers).
  9. - In v2, the factory SocksClient.createConnection function callback is called with a single object rather than separate socket and info object.
  10. - A SOCKS http/https agent is no longer bundled into the library.
  11. For informational purposes, here is the original getting started example from v1 converted to work with v2.
  12. ### Before (v1)
  13. ```javascript
  14. var Socks = require('socks');
  15. var options = {
  16. proxy: {
  17. ipaddress: "202.101.228.108",
  18. port: 1080,
  19. type: 5
  20. },
  21. target: {
  22. host: "google.com",
  23. port: 80
  24. },
  25. command: 'connect'
  26. };
  27. Socks.createConnection(options, function(err, socket, info) {
  28. if (err)
  29. console.log(err);
  30. else {
  31. socket.write("GET / HTTP/1.1\nHost: google.com\n\n");
  32. socket.on('data', function(data) {
  33. console.log(data.length);
  34. console.log(data);
  35. });
  36. // PLEASE NOTE: sockets need to be resumed before any data will come in or out as they are paused right before this callback is fired.
  37. socket.resume();
  38. // 569
  39. // <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65...
  40. }
  41. });
  42. ```
  43. ### After (v2)
  44. ```javascript
  45. const SocksClient = require('socks').SocksClient;
  46. let options = {
  47. proxy: {
  48. ipaddress: "202.101.228.108",
  49. port: 1080,
  50. type: 5
  51. },
  52. destination: {
  53. host: "google.com",
  54. port: 80
  55. },
  56. command: 'connect'
  57. };
  58. SocksClient.createConnection(options, function(err, result) {
  59. if (err)
  60. console.log(err);
  61. else {
  62. result.socket.write("GET / HTTP/1.1\nHost: google.com\n\n");
  63. result.socket.on('data', function(data) {
  64. console.log(data.length);
  65. console.log(data);
  66. });
  67. // 569
  68. // <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65...
  69. }
  70. });
  71. ```