11const dgram = require ( 'dgram' ) ;
22const delay = require ( 'delay' ) ;
33const debug = require ( 'debug' ) ( '@tuyapi/link:manual' ) ;
4+ const os = require ( 'os' ) ;
45
56/**
67 * A lower level option for linking
@@ -14,8 +15,10 @@ const debug = require('debug')('@tuyapi/link:manual');
1415class TuyaLink {
1516 constructor ( ) {
1617 this . abortBroadcasting = false ;
18+ this . bindAddr = undefined ;
1719 }
1820
21+
1922 /**
2023 * Thin wrapper for this.sendSmartLinkStart()
2124 * and this.sendSmartLinkData(). Unless you
@@ -66,6 +69,23 @@ class TuyaLink {
6669 throw new Error ( 'Invalid WiFi password' ) ;
6770 }
6871
72+ if ( options . bindAddr !== undefined )
73+ {
74+ if ( typeof options . bindAddr !== 'string' )
75+ {
76+ throw new Error ( 'Invalid binding address' ) ;
77+ }
78+
79+ const interfaces = os . networkInterfaces ( ) ;
80+ if ( ! Object . keys ( interfaces ) . some ( name =>
81+ ! interfaces [ name ] . some ( assigned => assigned . address . toLowerCase ( ) === options . bindAddr . toLowerCase ( ) )
82+ ) )
83+ {
84+ throw new Error ( 'Invalid binding address' ) ;
85+ }
86+ this . bindAddr = options . bindAddr ;
87+ }
88+
6989 debug ( 'Sending SmartLink initialization packets' ) ;
7090
7191 await this . sendSmartLinkStart ( ) ;
@@ -335,7 +355,7 @@ class TuyaLink {
335355 this . udpClient . on ( 'listening' , function ( ) {
336356 this . setBroadcast ( true ) ;
337357 } ) ;
338- this . udpClient . bind ( 0 ) ;
358+ this . udpClient . bind ( 0 , this . bindAddr ) ;
339359 }
340360
341361 // 0-filled buffer
0 commit comments