AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame^] | 1 | /** |
| 2 | * checkPNI - starts with the system manager input CLI command in ONOS console that enable/disable the PPPoE service. |
| 3 | */ |
| 4 | var Promise=require('bluebird'); |
| 5 | var execAsync=Promise.promisify(require('child_process').exec); |
| 6 | var fs=require('fs'); |
| 7 | var os=require('os'); |
| 8 | var ifaces = os.networkInterfaces(); |
| 9 | var oriGw = '10.6.1.129'; |
| 10 | var dnsFwdr = '8.8.8.8'; |
| 11 | |
| 12 | |
| 13 | //ip6tables -I FORWARD 1 -i eth1 -j DROP |
| 14 | function disconnDev(iface) { |
| 15 | |
| 16 | var cmd = 'ip6tables -w -I FORWARD 1 -i ' + iface + ' -j DROP'; |
| 17 | var checkCmd = 'ip6tables -w -v -L FORWARD 1'; |
| 18 | var check = 'DROP all ' + iface; |
| 19 | |
| 20 | console.log(checkCmd + ' to check ' + check); |
| 21 | execAsync(checkCmd).then(function (result) { |
| 22 | console.log("indexOf:" + result.indexOf(check)); |
| 23 | if (result.indexOf(check) > 0) { |
| 24 | console.log("Forward rule exists."); |
| 25 | } |
| 26 | else { |
| 27 | execAsync(cmd); |
| 28 | console.log(cmd); |
| 29 | } |
| 30 | }, function (err) {console.error(err);}); |
| 31 | |
| 32 | } |
| 33 | |
| 34 | //ip6tables -D FORWARD -i eth1 -j DROP |
| 35 | function connDev(iface) { |
| 36 | var cmd = 'ip6tables -w -D FORWARD -i ' + iface + ' -j DROP'; |
| 37 | var checkCmd = 'ip6tables -w -v -L FORWARD 1'; |
| 38 | var check = 'DROP all ' + iface; |
| 39 | |
| 40 | console.log(checkCmd + ' to check ' + check); |
| 41 | execAsync(checkCmd).then(function (result) { |
| 42 | console.log("indexOf:" + result.indexOf(check)); |
| 43 | if (result.indexOf(check) > 0) { |
| 44 | execAsync(cmd); |
| 45 | console.log(cmd); |
| 46 | } |
| 47 | else { |
| 48 | console.log("Forward rule not exist."); |
| 49 | } |
| 50 | }, function (err) {console.error(err);}); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | function natRedirectPkt(iface, ipAddr, port) { |
| 55 | |
| 56 | var cmd = 'ip6tables -w -t nat -A PREROUTING -i ' + iface + |
| 57 | ' -p tcp --dport ' + port + ' -j DNAT --to-destination [' |
| 58 | + ipAddr + ']:' + port; |
| 59 | var checkCmd = 'ip6tables -w -t nat -v -L PREROUTING'; |
| 60 | var check = 'to:[' + ipAddr + ']:' + port; |
| 61 | |
| 62 | console.log(checkCmd + ' to check ' + check); |
| 63 | execAsync(checkCmd).then(function (result) { |
| 64 | console.log("indexOf:" + result.indexOf(check)); |
| 65 | if (result.indexOf(check) > 0) { |
| 66 | console.log("nat rule exists."); |
| 67 | } |
| 68 | else { |
| 69 | execAsync(cmd); |
| 70 | console.log(cmd); |
| 71 | } |
| 72 | }, function (err) {console.error(err);}); |
| 73 | |
| 74 | } |
| 75 | |
| 76 | function natRecoverPkt(iface, ipAddr, port) { |
| 77 | |
| 78 | var cmd = 'ip6tables -w -t nat -D PREROUTING -i ' + iface + |
| 79 | ' -p tcp --dport ' + port + ' -j DNAT --to-destination [' |
| 80 | + ipAddr + ']:' + port; |
| 81 | var checkCmd = 'ip6tables -w -t nat -v -L PREROUTING'; |
| 82 | var check = 'to:[' + ipAddr + ']:' + port; |
| 83 | |
| 84 | console.log(checkCmd + ' to check ' + check); |
| 85 | execAsync(checkCmd).then(function (result) { |
| 86 | console.log("indexOf:" + result.indexOf(check)); |
| 87 | if (result.indexOf(check) > 0) { |
| 88 | execAsync(cmd); |
| 89 | console.log(cmd); |
| 90 | } |
| 91 | else { |
| 92 | console.log("nat rule not exist."); |
| 93 | } |
| 94 | }, function (err) {console.error(err);}); |
| 95 | |
| 96 | } |
| 97 | |
| 98 | function natMasquerade(iface) { |
| 99 | |
| 100 | var cmd = 'iptables -w -t nat -A POSTROUTING --out-interface ' |
| 101 | + iface + ' -j MASQUERADE'; |
| 102 | var checkCmd = 'iptables -w -t nat -v -L POSTROUTING'; |
| 103 | var check = 'MASQUERADE all -- any ' + iface; |
| 104 | |
| 105 | console.log(checkCmd + ' to check ' + check); |
| 106 | execAsync(checkCmd).then(function (result) { |
| 107 | console.log("indexOf:" + result.indexOf(check)); |
| 108 | if (result.indexOf(check) > 0) { |
| 109 | console.log("nat rule exists."); |
| 110 | } |
| 111 | else { |
| 112 | execAsync(cmd); |
| 113 | console.log(cmd); |
| 114 | } |
| 115 | }, function (err) {console.error(err);}); |
| 116 | |
| 117 | } |
| 118 | |
| 119 | function natRmMasquerade(iface) { |
| 120 | |
| 121 | var cmd = 'iptables -w -t nat -D POSTROUTING --out-interface ' |
| 122 | + iface + ' -j MASQUERADE'; |
| 123 | var checkCmd = 'iptables -w -t nat -v -L POSTROUTING'; |
| 124 | var check = 'MASQUERADE all -- any ' + iface; |
| 125 | |
| 126 | console.log(checkCmd + ' to check ' + check); |
| 127 | execAsync(checkCmd).then(function (result) { |
| 128 | console.log("indexOf:" + result.indexOf(check)); |
| 129 | if (result.indexOf(check) > 0) { |
| 130 | execAsync(cmd); |
| 131 | console.log(cmd); |
| 132 | } |
| 133 | else { |
| 134 | console.log("nat rule not exist."); |
| 135 | } |
| 136 | }, function (err) {console.error(err);}); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | function natRmDfltGw(ipAddr) { |
| 141 | |
| 142 | var cmd = 'route del default gw ' + ipAddr; |
| 143 | var checkCmd = 'ip route'; |
| 144 | var check = 'default via ' + ipAddr; |
| 145 | |
| 146 | console.log(checkCmd + ' to check ' + check); |
| 147 | |
| 148 | execAsync(checkCmd).then(function (result) { |
| 149 | console.log("indexOf:" + result.indexOf(check)); |
| 150 | if (result.indexOf(check) >= 0) { |
| 151 | execAsync(cmd); |
| 152 | console.log(cmd); |
| 153 | } |
| 154 | else { |
| 155 | console.log("default gw " + ipAddr + " not exists."); |
| 156 | } |
| 157 | }, function (err) {console.error(err);}); |
| 158 | |
| 159 | } |
| 160 | |
| 161 | function natAddDfltGw(ipAddr, dev) { |
| 162 | |
| 163 | var cmd = 'route add default gw ' + ipAddr + ' dev ' + dev; |
| 164 | var checkCmd = 'ip route'; |
| 165 | var check = 'default via ' + ipAddr; |
| 166 | |
| 167 | console.log(checkCmd + ' to check ' + check); |
| 168 | |
| 169 | execAsync(checkCmd).then(function (result) { |
| 170 | console.log("indexOf:" + result.indexOf(check)); |
| 171 | if (result.indexOf(check) >= 0) { |
| 172 | console.log("default gw " + ipAddr + " exists."); |
| 173 | } |
| 174 | else { |
| 175 | execAsync(cmd); |
| 176 | console.log(cmd); |
| 177 | } |
| 178 | }, function (err) {console.error(err);}); |
| 179 | |
| 180 | } |
| 181 | |
| 182 | function setDnsRoute(ipAddr, gw, dev) { |
| 183 | |
| 184 | var cmd = 'route add ' + ipAddr + ' gw ' + gw + ' dev ' + dev; |
| 185 | var checkCmd = 'ip route'; |
| 186 | var check = ipAddr + ' via ' + gw; |
| 187 | |
| 188 | console.log(checkCmd + ' to check ' + check); |
| 189 | |
| 190 | execAsync(checkCmd).then(function (result) { |
| 191 | console.log("indexOf:" + result.indexOf(check)); |
| 192 | if (result.indexOf(check) >= 0) { |
| 193 | console.log("route " + ipAddr + " exists."); |
| 194 | } |
| 195 | else { |
| 196 | execAsync(cmd); |
| 197 | console.log(cmd); |
| 198 | } |
| 199 | }, function (err) {console.error(err);}); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | function rmDnsRoute(ipAddr) { |
| 204 | |
| 205 | var cmd = 'route del ' + ipAddr; |
| 206 | var checkCmd = 'ip route'; |
| 207 | var check = ipAddr + ' via'; |
| 208 | |
| 209 | console.log(checkCmd + ' to check ' + check); |
| 210 | |
| 211 | execAsync(checkCmd).then(function (result) { |
| 212 | console.log("indexOf:" + result.indexOf(check)); |
| 213 | if (result.indexOf(check) >= 0) { |
| 214 | execAsync(cmd); |
| 215 | console.log(cmd); |
| 216 | } |
| 217 | else { |
| 218 | console.log("route " + ipAddr + " not exists."); |
| 219 | } |
| 220 | }, function (err) {console.error(err);}); |
| 221 | |
| 222 | } |
| 223 | |
| 224 | function pppoeMasquerade() { |
| 225 | natMasquerade('ppp0'); |
| 226 | } |
| 227 | |
| 228 | function pppoeRmMasquerade() { |
| 229 | natRmMasquerade('ppp0'); |
| 230 | } |
| 231 | |
| 232 | function pppoeRedirectPkt(ipAddr) { |
| 233 | natRedirectPkt('eth1', ipAddr, '80'); |
| 234 | natRedirectPkt('eth1', ipAddr, '443'); |
| 235 | disconnDev('eth1'); |
| 236 | } |
| 237 | |
| 238 | function pppoeRmRedirectPkt(ipAddr) { |
| 239 | natRecoverPkt('eth1', ipAddr, '80'); |
| 240 | natRecoverPkt('eth1', ipAddr, '443'); |
| 241 | connDev('eth1'); |
| 242 | } |
| 243 | |
| 244 | function pppoeSetRoute() { |
| 245 | natRmDfltGw(oriGw); |
| 246 | setDnsRoute(dnsFwdr, oriGw, 'eth0'); |
| 247 | } |
| 248 | |
| 249 | function pppoeRmRoute() { |
| 250 | natAddDfltGw(oriGw, 'eth0'); |
| 251 | rmDnsRoute(dnsFwdr); |
| 252 | } |
| 253 | |
| 254 | module.exports = { |
| 255 | check:function(adminState,endSession){ |
| 256 | var adminEnable = "enable"; |
| 257 | var adminDisable = "disable"; |
| 258 | return execAsync("cat adminState.txt ").then(function (result) { |
| 259 | var i = false; |
| 260 | ifaces['eth1'].forEach(function(details){ |
| 261 | if (details.family=='IPv6' && i == false) |
| 262 | { |
| 263 | i = true; |
| 264 | eh1ip = details.address; |
| 265 | console.log('eh1ip:'+eh1ip); |
| 266 | } |
| 267 | |
| 268 | }); |
| 269 | console.log("result:"+ result); |
| 270 | if(adminState == adminEnable) |
| 271 | { |
| 272 | if(result.indexOf(adminDisable) > 0 && endSession == false) |
| 273 | { |
| 274 | pppoeRedirectPkt(eh1ip); |
| 275 | pppoeMasquerade(); |
| 276 | pppoeSetRoute(); |
| 277 | var writeData = '"admin-state": "enable","end-session": "false"'; |
| 278 | console.log("writeData :"+writeData); |
| 279 | fs.writeFile('adminState.txt',writeData, function(err){ |
| 280 | if(err) throw err; |
| 281 | console.log("write success."); |
| 282 | }); |
| 283 | return true; |
| 284 | } |
| 285 | else if (result.indexOf(adminEnable) > 0 && endSession == true) |
| 286 | { |
| 287 | return execAsync('ps -ef|grep pppd').then(function (result) { |
| 288 | console.log("indexOf:"+result.indexOf("call provider")); |
| 289 | if (result.indexOf("call provider") > 0) |
| 290 | { |
| 291 | execAsync("poff -a"); |
| 292 | pppoeRedirectPkt(eh1ip); |
| 293 | pppoeSetRoute(); |
| 294 | console.log("poff ok."); |
| 295 | var writeData = '"admin-state": "enable","end-session": "true"'; |
| 296 | console.log("writeData :"+writeData); |
| 297 | fs.writeFile('adminState.txt',writeData, function(err){ |
| 298 | if(err) throw err; |
| 299 | console.log("write success."); |
| 300 | |
| 301 | }); |
| 302 | return true; |
| 303 | } |
| 304 | }, function (err) { |
| 305 | console.error(err); |
| 306 | return false; |
| 307 | }); |
| 308 | |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | console.log("NO enable case"); |
| 313 | return false; |
| 314 | } |
| 315 | } |
| 316 | else if(adminState == adminDisable) |
| 317 | { |
| 318 | if (result.indexOf(adminEnable) > 0 && endSession == false) |
| 319 | { |
| 320 | return execAsync('ps -ef|grep pppd').then(function (result) { |
| 321 | console.log("indexOf:"+result.indexOf("call provider")); |
| 322 | if (result.indexOf("call provider") > 0) |
| 323 | { |
| 324 | execAsync("poff -a"); |
| 325 | console.log("poff ok."); |
| 326 | } |
| 327 | |
| 328 | pppoeRmRedirectPkt(eh1ip); |
| 329 | pppoeRmMasquerade(); |
| 330 | pppoeRmRoute(); |
| 331 | |
| 332 | var writeData = '"admin-state": "disable","end-session": "false"'; |
| 333 | console.log("writeData :"+writeData); |
| 334 | fs.writeFile('adminState.txt',writeData, function(err){ |
| 335 | if(err) throw err; |
| 336 | console.log("write success."); |
| 337 | }); |
| 338 | return true; |
| 339 | }, function (err) { |
| 340 | console.error(err); |
| 341 | return false; |
| 342 | }); |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | console.log("NO disable case"); |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | }, function (err) { |
| 351 | console.log("cat adminState.txt fail"); |
| 352 | return false; |
| 353 | }); |
| 354 | } |
| 355 | } |