@@ -80,8 +80,8 @@ async function changeGateway (index, ip) { // eslint-disable-line
8080 return sudoExec ( cmd )
8181}
8282
83- async function getRouterIP ( ) { // eslint-disable-line
84- const infIndex = await getActiveAdapterIndex ( )
83+ async function getRouterIP ( index ) { // eslint-disable-line
84+ const infIndex = index || await getActiveAdapterIndex ( )
8585 const cmd = `WMIC nicconfig where "InterfaceIndex = ${ infIndex } " get DHCPServer`
8686
8787 // DHCPServer
@@ -171,39 +171,46 @@ class Win {
171171 }
172172
173173 static async trafficToVirtualRouter ( infName , gateway ) {
174- const hostonlyInfIndex = await infNameToIndex ( infName )
175174 const activeAdapterIndex = await getActiveAdapterIndex ( )
175+ const routerIP = await getRouterIP ( activeAdapterIndex )
176+ const hostonlyInfIndex = await infNameToIndex ( infName )
176177 const cmds = [ ]
177178
179+ // hostonlyInf
178180 // change dns
179181 cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call SetDNSServerSearchOrder ("${ gateway } ")` )
180182 // changeGateway
181183 cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call SetGateways ("${ gateway } ")` )
182184
183- // set fake ip/mask
184- const fakeIP = '168.254.254.254'
185- const fakeMask = '255.0.0.0'
186- cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ activeAdapterIndex } " call EnableStatic ("${ fakeIP } "),("${ fakeMask } ")` )
185+ // physicalIfs dns
186+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ activeAdapterIndex } " call SetDNSServerSearchOrder ("${ gateway } ")` )
187+
188+ // change route table
189+ cmds . push ( `route change 0.0.0.0 mask 0.0.0.0 ${ routerIP } METRIC 300` )
190+ cmds . push ( `route add 0.0.0.0 mask 0.0.0.0 ${ gateway } METRIC 1` )
187191
188192 return sudoExec ( cmds . join ( ' & ' ) )
189193 }
190194
191195 static async trafficToPhysicalRouter ( infName , ip , mask ) {
192- const hostonlyInfIndex = await infNameToIndex ( infName )
193196 const activeAdapterIndex = await getActiveAdapterIndex ( )
197+ const hostonlyInfIndex = await infNameToIndex ( infName )
198+ const routerIP = await getRouterIP ( activeAdapterIndex )
194199 const cmds = [ ]
195200
196201 // enableDHCP to get rid of gateway
197- cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call EnableDHCP` )
202+ // cmds.push(`WMIC nicconfig where "InterfaceIndex = ${hostonlyInfIndex}" call EnableDHCP`)
198203
199204 // config ip/mask
200- cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call EnableStatic ("${ ip } "),("${ mask } ")` )
205+ // cmds.push(`WMIC nicconfig where "InterfaceIndex = ${hostonlyInfIndex}" call EnableStatic ("${ip}"),("${mask}")`)
201206
202207 // emptyDNS
203208 cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ hostonlyInfIndex } " call SetDNSServerSearchOrder` )
204209
205210 // enable physicalIfs
206- cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ activeAdapterIndex } " call EnableDHCP` )
211+ // cmds.push(`WMIC nicconfig where "InterfaceIndex = ${activeAdapterIndex}" call EnableDHCP`)
212+ cmds . push ( `WMIC nicconfig where "InterfaceIndex = ${ activeAdapterIndex } " call SetDNSServerSearchOrder` )
213+ cmds . push ( `route change 0.0.0.0 mask 0.0.0.0 ${ routerIP } METRIC 1` )
207214
208215 return sudoExec ( cmds . join ( ' & ' ) )
209216 }
0 commit comments