forked from webinos/webinos-pzp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webinos_pzp.js
executable file
·188 lines (175 loc) · 6.32 KB
/
webinos_pzp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env node
/*******************************************************************************
* Code contributed to the webinos project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
var Pzp = require ("./lib/pzp");
__EnablePolicyEditor = false;
var argv = require ('optimist')
.usage ('Starts webinos PZP \nUsage: $0')
.options ({
"pzhHost" :{
describe:"set the ip-address of the pzh provider",
default :"0.0.0.0"
},
"pzhName" :{
describe:"sets The email id of the pzh you intend to connect",
default :""
},
"friendlyName" :{
describe:"sets the name assigned to the PZP such as PC/Mobile/TV",
default :""
},
"forcedDeviceName":{
describe:"Forced PZP device name that you assign instead of the default PZP name",
default :""
},
"widgetServer" :{
describe:"starts widget server",
default :false
},
"policyEditor" :{
describe:"starts policy editor server",
default :false
},
"signedWidgetOnly":{
describe:"only allow signed widgets",
default :false
},
"enforceWidgetCSP":{
describe:"enforce content security policy on the widgets",
default :false
},
"test":{
describe:"start the PZP and exit if it loaded successfully. Useful for testing the build",
default :false
},
"help" :{
describe:"to get this help menu"
}})
.argv;
if (argv.help) {
require ('optimist').showHelp ();
process.exit ();
}
if (argv.policyEditor) {
__EnablePolicyEditor = true;
}
require ("fs").readFile (require ("path").join (__dirname, "config-pzp.json"), function (err, data) {
var config = {};
if (!err) {
config = JSON.parse (data);
}
// overwrite config file options with cli options
config = require ('webinos-utilities').webinosHelpers.extend (config, argv);
if (config.pzhName !== "") {
config.sessionIdentity = config.pzhHost + '/' + config.pzhName;
} else {
config.sessionIdentity = config.pzhHost;
}
initializePzp (config);
});
function initializeWidgetServer () {
// Widget manager server
var wrt;
try {
wrt = require ("webinos-widgetServer");
} catch(err) {
console.log("Webinos widget is missing");
}
if (wrt) {
// Attempt to start the widget server.
wrt.start (argv.signedWidgetOnly, argv.enforceWidgetCSP, Pzp.session.getWebinosPorts().pzp_webSocket, "webinos",
function (msg, wrtPort) {
if (msg === "startedWRT") {
// Write the websocket and widget server ports to file so the renderer can pick them up.
var wrtConfig = {};
wrtConfig.runtimeWebServerPort = wrtPort;
wrtConfig.pzpWebSocketPort = Pzp.session.getWebinosPorts ().pzp_webSocket;
wrtConfig.pzpPath = Pzp.session.getWebinosPath ();
require ("fs").writeFile ((require ("path").join (Pzp.session.getWebinosPath (), '/wrt/webinos_runtime.json')),
JSON.stringify (wrtConfig, null, ' '), function (err) {
if (err) {
console.log ('error saving runtime configuration file: ' + err);
} else {
console.log ('saved configuration runtime file');
}
});
} else {
console.log ('error starting wrt server: ' + msg);
}
});
}
}
function startPzp() {
var pzpInstance = Pzp.session.getInstance();
pzpInstance.on("PZP_STARTED", function(){
testStart(true);
if (argv.widgetServer) initializeWidgetServer ();
});
pzpInstance.on("PZP_START_FAILED", function(errDetails){
console.log(errDetails);
testStart(false);
});
}
function ask(name) {
process.stdin.resume();
logger.log("change value - " + name);
process.stdin.once("data", function(data){
var response = data.toString().trim().toLowerCase();
if (isNumeric(response)){
}
})
}
function initializePzp (config) {
// Add a command line option to configure ports
/*if (!Pzp.session.getPzpConfigurationStatus()) { // PZP is not configured .. Reconfigure it
if (process.stdout.isTTY && process.stdin.isTTY) {
logger.log("Welcome to Webinos PZP","Current used ports by PZP are ", require("config.json").ports);
process.stdin.resume()
logger.log("Do you wish to change these ports? (Y/N): ");
process.stdin.once('data', function(data){
var response = data.toString().trim().toLowerCase();
if (response === "y") {
require("config.json").ports.forEach(function(name){
ask(name);
});
Pzp.session.setInputConfig(config);
} else {
Pzp.session.setInputConfig(config);
}
startPzp();
});
} else {
startPzp();
}
} */
Pzp.session.setInputConfig(config);
startPzp();
}
/* This function is only relevant when the --test switch is passed to
* the webinos_pzp.js script.
*/
function testStart(hasStarted) {
if (argv.test) {
if (hasStarted) {
console.log("Started successfully. This is a test, so the process is now exiting");
process.exit(0);
} else {
console.log("The PZP did not start successfully. Ending.");
process.exit(-1);
}
}
}