@@ -45,18 +45,6 @@ async function disableIPV6 () { // eslint-disable-line
4545 return sudoExec ( cmd )
4646}
4747
48- async function togglePhysicalAdapterConnection ( index , action = 'off' ) {
49- // Pratice: no need to config dns
50- const fakeIP = '168.254.254.254'
51- const fakeMast = '255.0.0.0'
52- const infIndex = index || await getActiveAdapterIndex ( )
53- if ( action === 'on' ) {
54- return enableDHCP ( infIndex )
55- } else {
56- return enableStatic ( infIndex , fakeIP , fakeMast )
57- }
58- }
59-
6048async function getActiveAdapterIndexAndName ( ) {
6149 const cmd = 'WMIC nic where "PhysicalAdapter = TRUE and NetConnectionStatus = 2" get InterfaceIndex,Name'
6250
@@ -112,31 +100,13 @@ async function getRouterIP () { // eslint-disable-line
112100 return DHCPServer
113101}
114102
115- async function configWindowsHostOnlyInf ( index , gateway ) {
116- await changeGateway ( index , gateway )
117- await changeDns ( index , gateway )
118- }
119-
120103async function infNameToIndex ( infName ) {
121104 const subCmd = `WMIC nic where "Name = '${ infName } '" get InterfaceIndex`
122105 const indexOuput = await execute ( subCmd )
123106 const index = indexOuput . split ( '\n' ) [ 1 ] . trim ( )
124107 return index
125108}
126109
127- async function enableDHCP ( index ) {
128- const cmd = `WMIC nicconfig where "InterfaceIndex = ${ index } " call EnableDHCP`
129- return sudoExec ( cmd )
130- }
131- async function enableStatic ( index , ip , mask ) {
132- const cmd = `WMIC nicconfig where "InterfaceIndex = ${ index } " call EnableStatic ("${ ip } "),("${ mask } ")`
133- return sudoExec ( cmd )
134- }
135- async function emptyDNS ( index ) {
136- const cmd = `WMIC nicconfig where "InterfaceIndex = ${ index } " call SetDNSServerSearchOrder`
137- return sudoExec ( cmd )
138- }
139-
140110class Win {
141111 static async getActiveAdapter ( ) {
142112 const indexAndName = await getActiveAdapterIndexAndName ( )
@@ -209,50 +179,41 @@ class Win {
209179 }
210180
211181 static async trafficToVirtualRouter ( infName , gateway ) {
212- const p1 = infNameToIndex ( infName )
213- . then ( index => {
214- logger . debug ( `about to configWindowsHostOnlyInf, infName: ${ infName } ` )
215- return configWindowsHostOnlyInf ( index , gateway )
216- } )
182+ const hostonlyInfIndex = await infNameToIndex ( infName )
183+ const activeAdapterIndex = await getActiveAdapterIndex ( )
184+ const cmds = [ ]
217185
218- const p2 = getActiveAdapterIndex ( )
219- . then ( index => {
220- logger . debug ( `about to togglePhysicalAdapterConnection off, index: ${ index } ` )
221- return togglePhysicalAdapterConnection ( index , 'off' )
222- } )
223- return Promise . all ( [ p1 , p2 ] )
186+ // change dns
187+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call SetDNSServerSearchOrder ("${ gateway } ")` )
188+ // changeGateway
189+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call SetGateways ("${ gateway } ")` )
190+
191+ // set fake ip/mask
192+ const fakeIP = '168.254.254.254'
193+ const fakeMask = '255.0.0.0'
194+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ activeAdapterIndex } " call EnableStatic ("${ fakeIP } "),("${ fakeMask } ")` )
195+
196+ return sudoExec ( cmds . join ( ' & ' ) )
224197 }
225198
226199 static async trafficToPhysicalRouter ( infName , ip , mask ) {
227- const p1 = Promise . resolve ( )
228- . then ( ( ) => {
229- logger . debug ( `about to get infName's InterfaceIndex` )
230- return infNameToIndex ( infName )
231- } )
232- . then ( index => {
233- logger . debug ( `about to enableDHCP of ${ infName } ` )
234- return enableDHCP ( index )
235- . then ( ( ) => index )
236- } )
237- . then ( index => {
238- logger . debug ( `about to configWindowsHostOnlyInf of ${ infName } ` )
239- return Promise . all ( [
240- enableStatic ( index , ip , mask ) ,
241- emptyDNS ( index )
242- ] )
243- } )
200+ const hostonlyInfIndex = await infNameToIndex ( infName )
201+ const activeAdapterIndex = await getActiveAdapterIndex ( )
202+ const cmds = [ ]
244203
245- const p2 = Promise . resolve ( )
246- . then ( ( ) => {
247- logger . debug ( `about to getActiveAdapterIndex` )
248- return getActiveAdapterIndex ( )
249- } )
250- . then ( index => {
251- logger . debug ( `about to togglePhysicalAdapterConnection on. index: ${ index } ` )
252- return togglePhysicalAdapterConnection ( index , 'on' )
253- } )
204+ // enableDHCP to get rid of gateway
205+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call EnableDHCP` )
206+
207+ // config ip/mask
208+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call EnableStatic ("${ ip } "),("${ mask } ")` )
209+
210+ // emptyDNS
211+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call SetDNSServerSearchOrder` )
212+
213+ // enable physicalIfs
214+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ activeAdapterIndex } " call EnableDHCP` )
254215
255- return Promise . all ( [ p1 , p2 ] )
216+ return sudoExec ( cmds . join ( ' & ' ) )
256217 }
257218}
258219
0 commit comments