blob: 59473fb63548dcfaf8bda93ee2913a3e310739e3 [file] [log] [blame]
Matteo Scandoloaca86652017-08-08 13:05:27 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
AyumuUeha76a01bc2017-05-18 13:34:13 +090019/**
20 * startClient - start pppoe client
21 */
22var fs=require("fs");
23var Promise=require('bluebird');
24var execAsync=Promise.promisify(require('child_process').exec);
25
26String.prototype.trim = function() {
27 return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
28}
29
30module.exports={
31 verifyFun:function(userName,passWord){
32 return execAsync("ifconfig").then(function (result) {
33 if(result.indexOf("ppp0") > 0)
34 {
35 console.log("PPPoE session up.");
36 return true;
37 }
38 else
39 {
40 var data =
41 "noipdefault\n" +
42 "usepeerdns\n" +
43 "defaultroute\n" +
44 "replacedefaultroute\n" +
45 "hide-password\n" +
46 "lcp-echo-interval 20\n" +
47 "lcp-echo-failure 3\n" +
48 "noauth\n" +
49 "persist\n" +
50 "mtu 1442\n" +
51 "noaccomp\n" +
52 "default-asyncmap\n" +
53 "pty \"pppoe -I eth2 -T 80\"\n" +
54 "user \"" + userName + "\"\n";
55
56 var secretData = "\"" + userName + "\"" + " * " + "\"" + passWord + "\"";
57 console.log("data:"+data+",secretData:"+secretData);
58
59 fs.writeFile('/etc/ppp/peers/provider',data, function(err){
60 if(err) throw err;
61 console.log("write provider.");
62 });
63
64 fs.writeFile('/etc/ppp/chap-secrets',secretData,function(err){
65 if(err) throw err;
66 console.log("write chap-secrets.");
67 });
68
69 return execAsync('ps -ef|grep pppd').then(function (result) {
70 if (result.indexOf("call provider") > 0)
71 {
72 execAsync("poff -a").then(function (result) {
73 execAsync('pon');
74 console.log("restart");
75 }, function (err) {
76 console.log(err);
77 });
78 }
79 else
80 {
81 execAsync('pon');
82 console.log("start");
83 }
84 return true;
85 }, function (err) {
86 console.log(err);
87 return false;
88 });
89 }
90 });
91 }
92}