11cb0ef41Sopenharmony_ci# socks 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci## Migrating from v1 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciFor the most part, migrating from v1 takes minimal effort as v2 still supports factory creation of proxy connections with callback support. 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci### Notable breaking changes 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci- In an options object, the proxy 'command' is now required and does not default to 'connect'. 101cb0ef41Sopenharmony_ci- **In an options object, 'target' is now known as 'destination'.** 111cb0ef41Sopenharmony_ci- 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). 121cb0ef41Sopenharmony_ci- 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). 131cb0ef41Sopenharmony_ci- In v2, the factory SocksClient.createConnection function callback is called with a single object rather than separate socket and info object. 141cb0ef41Sopenharmony_ci- A SOCKS http/https agent is no longer bundled into the library. 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ciFor informational purposes, here is the original getting started example from v1 converted to work with v2. 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci### Before (v1) 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci```javascript 211cb0ef41Sopenharmony_civar Socks = require('socks'); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_civar options = { 241cb0ef41Sopenharmony_ci proxy: { 251cb0ef41Sopenharmony_ci ipaddress: "202.101.228.108", 261cb0ef41Sopenharmony_ci port: 1080, 271cb0ef41Sopenharmony_ci type: 5 281cb0ef41Sopenharmony_ci }, 291cb0ef41Sopenharmony_ci target: { 301cb0ef41Sopenharmony_ci host: "google.com", 311cb0ef41Sopenharmony_ci port: 80 321cb0ef41Sopenharmony_ci }, 331cb0ef41Sopenharmony_ci command: 'connect' 341cb0ef41Sopenharmony_ci}; 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciSocks.createConnection(options, function(err, socket, info) { 371cb0ef41Sopenharmony_ci if (err) 381cb0ef41Sopenharmony_ci console.log(err); 391cb0ef41Sopenharmony_ci else { 401cb0ef41Sopenharmony_ci socket.write("GET / HTTP/1.1\nHost: google.com\n\n"); 411cb0ef41Sopenharmony_ci socket.on('data', function(data) { 421cb0ef41Sopenharmony_ci console.log(data.length); 431cb0ef41Sopenharmony_ci console.log(data); 441cb0ef41Sopenharmony_ci }); 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_ci // 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. 471cb0ef41Sopenharmony_ci socket.resume(); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci // 569 501cb0ef41Sopenharmony_ci // <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65... 511cb0ef41Sopenharmony_ci } 521cb0ef41Sopenharmony_ci}); 531cb0ef41Sopenharmony_ci``` 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci### After (v2) 561cb0ef41Sopenharmony_ci```javascript 571cb0ef41Sopenharmony_ciconst SocksClient = require('socks').SocksClient; 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_cilet options = { 601cb0ef41Sopenharmony_ci proxy: { 611cb0ef41Sopenharmony_ci ipaddress: "202.101.228.108", 621cb0ef41Sopenharmony_ci port: 1080, 631cb0ef41Sopenharmony_ci type: 5 641cb0ef41Sopenharmony_ci }, 651cb0ef41Sopenharmony_ci destination: { 661cb0ef41Sopenharmony_ci host: "google.com", 671cb0ef41Sopenharmony_ci port: 80 681cb0ef41Sopenharmony_ci }, 691cb0ef41Sopenharmony_ci command: 'connect' 701cb0ef41Sopenharmony_ci}; 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ciSocksClient.createConnection(options, function(err, result) { 731cb0ef41Sopenharmony_ci if (err) 741cb0ef41Sopenharmony_ci console.log(err); 751cb0ef41Sopenharmony_ci else { 761cb0ef41Sopenharmony_ci result.socket.write("GET / HTTP/1.1\nHost: google.com\n\n"); 771cb0ef41Sopenharmony_ci result.socket.on('data', function(data) { 781cb0ef41Sopenharmony_ci console.log(data.length); 791cb0ef41Sopenharmony_ci console.log(data); 801cb0ef41Sopenharmony_ci }); 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci // 569 831cb0ef41Sopenharmony_ci // <Buffer 48 54 54 50 2f 31 2e 31 20 33 30 31 20 4d 6f 76 65 64 20 50 65... 841cb0ef41Sopenharmony_ci } 851cb0ef41Sopenharmony_ci}); 861cb0ef41Sopenharmony_ci```