11import { EventEmitter } from 'events' ;
22import * as net from 'net' ;
3- import * as ip from 'ip' ;
43import { SmartBuffer } from 'smart-buffer' ;
54import {
65 DEFAULT_TIMEOUT ,
@@ -24,10 +23,14 @@ import {
2423import {
2524 validateSocksClientOptions ,
2625 validateSocksClientChainOptions ,
26+ ipv4ToInt32 ,
27+ ipToBuffer ,
28+ int32ToIpv4 ,
2729} from '../common/helpers' ;
2830import { ReceiveBuffer } from '../common/receivebuffer' ;
2931import { SocksClientError , shuffleArray } from '../common/util' ;
3032import { Duplex } from 'stream' ;
33+ import { Address6 } from 'ip-address' ;
3134
3235// Exposes SocksClient event types
3336declare interface SocksClient {
@@ -231,10 +234,10 @@ class SocksClient extends EventEmitter implements SocksClient {
231234 // IPv4/IPv6/Hostname
232235 if ( net . isIPv4 ( options . remoteHost . host ) ) {
233236 buff . writeUInt8 ( Socks5HostType . IPv4 ) ;
234- buff . writeUInt32BE ( ip . toLong ( options . remoteHost . host ) ) ;
237+ buff . writeUInt32BE ( ipv4ToInt32 ( options . remoteHost . host ) ) ;
235238 } else if ( net . isIPv6 ( options . remoteHost . host ) ) {
236239 buff . writeUInt8 ( Socks5HostType . IPv6 ) ;
237- buff . writeBuffer ( ip . toBuffer ( options . remoteHost . host ) ) ;
240+ buff . writeBuffer ( ipToBuffer ( options . remoteHost . host ) ) ;
238241 } else {
239242 buff . writeUInt8 ( Socks5HostType . Hostname ) ;
240243 buff . writeUInt8 ( Buffer . byteLength ( options . remoteHost . host ) ) ;
@@ -263,9 +266,11 @@ class SocksClient extends EventEmitter implements SocksClient {
263266 let remoteHost ;
264267
265268 if ( hostType === Socks5HostType . IPv4 ) {
266- remoteHost = ip . fromLong ( buff . readUInt32BE ( ) ) ;
269+ remoteHost = int32ToIpv4 ( buff . readUInt32BE ( ) ) ;
267270 } else if ( hostType === Socks5HostType . IPv6 ) {
268- remoteHost = ip . toString ( buff . readBuffer ( 16 ) ) ;
271+ remoteHost = Address6 . fromByteArray (
272+ Array . from ( buff . readBuffer ( 16 ) ) ,
273+ ) . canonicalForm ( ) ;
269274 } else {
270275 remoteHost = buff . readString ( buff . readUInt8 ( ) ) ;
271276 }
@@ -508,7 +513,7 @@ class SocksClient extends EventEmitter implements SocksClient {
508513
509514 // Socks 4 (IPv4)
510515 if ( net . isIPv4 ( this . options . destination . host ) ) {
511- buff . writeBuffer ( ip . toBuffer ( this . options . destination . host ) ) ;
516+ buff . writeBuffer ( ipToBuffer ( this . options . destination . host ) ) ;
512517 buff . writeStringNT ( userId ) ;
513518 // Socks 4a (hostname)
514519 } else {
@@ -546,7 +551,7 @@ class SocksClient extends EventEmitter implements SocksClient {
546551
547552 const remoteHost : SocksRemoteHost = {
548553 port : buff . readUInt16BE ( ) ,
549- host : ip . fromLong ( buff . readUInt32BE ( ) ) ,
554+ host : int32ToIpv4 ( buff . readUInt32BE ( ) ) ,
550555 } ;
551556
552557 // If host is 0.0.0.0, set to proxy host.
@@ -584,7 +589,7 @@ class SocksClient extends EventEmitter implements SocksClient {
584589
585590 const remoteHost : SocksRemoteHost = {
586591 port : buff . readUInt16BE ( ) ,
587- host : ip . fromLong ( buff . readUInt32BE ( ) ) ,
592+ host : int32ToIpv4 ( buff . readUInt32BE ( ) ) ,
588593 } ;
589594
590595 this . setState ( SocksClientState . Established ) ;
@@ -747,10 +752,10 @@ class SocksClient extends EventEmitter implements SocksClient {
747752 // ipv4, ipv6, domain?
748753 if ( net . isIPv4 ( this . options . destination . host ) ) {
749754 buff . writeUInt8 ( Socks5HostType . IPv4 ) ;
750- buff . writeBuffer ( ip . toBuffer ( this . options . destination . host ) ) ;
755+ buff . writeBuffer ( ipToBuffer ( this . options . destination . host ) ) ;
751756 } else if ( net . isIPv6 ( this . options . destination . host ) ) {
752757 buff . writeUInt8 ( Socks5HostType . IPv6 ) ;
753- buff . writeBuffer ( ip . toBuffer ( this . options . destination . host ) ) ;
758+ buff . writeBuffer ( ipToBuffer ( this . options . destination . host ) ) ;
754759 } else {
755760 buff . writeUInt8 ( Socks5HostType . Hostname ) ;
756761 buff . writeUInt8 ( this . options . destination . host . length ) ;
@@ -799,7 +804,7 @@ class SocksClient extends EventEmitter implements SocksClient {
799804 ) ;
800805
801806 remoteHost = {
802- host : ip . fromLong ( buff . readUInt32BE ( ) ) ,
807+ host : int32ToIpv4 ( buff . readUInt32BE ( ) ) ,
803808 port : buff . readUInt16BE ( ) ,
804809 } ;
805810
@@ -842,7 +847,9 @@ class SocksClient extends EventEmitter implements SocksClient {
842847 ) ;
843848
844849 remoteHost = {
845- host : ip . toString ( buff . readBuffer ( 16 ) ) ,
850+ host : Address6 . fromByteArray (
851+ Array . from ( buff . readBuffer ( 16 ) ) ,
852+ ) . canonicalForm ( ) ,
846853 port : buff . readUInt16BE ( ) ,
847854 } ;
848855 }
@@ -913,7 +920,7 @@ class SocksClient extends EventEmitter implements SocksClient {
913920 ) ;
914921
915922 remoteHost = {
916- host : ip . fromLong ( buff . readUInt32BE ( ) ) ,
923+ host : int32ToIpv4 ( buff . readUInt32BE ( ) ) ,
917924 port : buff . readUInt16BE ( ) ,
918925 } ;
919926
@@ -956,7 +963,9 @@ class SocksClient extends EventEmitter implements SocksClient {
956963 ) ;
957964
958965 remoteHost = {
959- host : ip . toString ( buff . readBuffer ( 16 ) ) ,
966+ host : Address6 . fromByteArray (
967+ Array . from ( buff . readBuffer ( 16 ) ) ,
968+ ) . canonicalForm ( ) ,
960969 port : buff . readUInt16BE ( ) ,
961970 } ;
962971 }
0 commit comments