-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (154 loc) · 5.5 KB
/
index.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
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
/*=========================================================
* Remote WakeUp PC
*
* 2016/6/22 @toshirot
* http://cht.pw/chat.htm
*---------------------------------------------------------
*/
var wol = require('wake_on_lan');
var WsServer = require('ws').Server;
/*=========================================================
* rww
*
* @param {Object} option Options
* @property {String} [op.url='localhost'] WebSocketServer
* @property {Int} [op.port=8503] WebSocketServerPort
* @property {Object} op.lists WakeUpLists
* @property {Array} [op.allowIP=['*']] allowIPLists
* @property {String} [op.keyword='*'] allowKeyword
* @property {Int} [op.timeout=1000*60] timeout
* @return {Object} WebSocket Object
* 引数はwebに公開される場所には置かないように注意しましょう
* Argument is let's be careful not place such as in a location that can be published to the web
//Argument samples for rww. rwwの引数設定サンプル
var op = {
//WebSocketサーバー
url: '192.168.1.22'
,port: 8503
//起動したいマシンのMacアドレスを調べて下記を書き換えてください
//名前はリモートから呼び出すための仮称でIPである必要はありません
,lists: {
'192.168.1.4': 'xx:xx:xx:xx:xx:xx'
,'subPC' : 'xx:xx:xx:xx:xx:xx'
,'WiFi2' : 'xx:xx:xx:xx:xx:xx'
}
//※必要ならIPやキーワードやSSL接続などで安全を確保すること
//接続を許可するクライアントIPアドレス
,allowIP: ['*'] //'*'は全部許可
//命令実行を許可するキーワード
,keyword: '*'//'*'は全部許可
//WakeUpしたら最大1分で接続を切る
,timeout: 1000*60
};
*/
var conn = function( option ){
/*=========================================================
* WebSocket
*/
var WebSocketServer = option.url||'localhost';
var WebSocketServerPort = option.port||8503;
var WakeUpLists = option.lists;
var allowIPLists = option.allowIPs|| ['*'];
var allowKeyword = option.keyword || '*';
var timeout = option.timeout || 60000;//default 60sec
if(!WakeUpLists)return;
//WebSocket Server
var ws = new WsServer({
host: WebSocketServer,
port: WebSocketServerPort
});
//on connection
ws.on('connection', function(socket) {
//IPチェック接続拒否
if(!allowIPChk(socket, ws)){ return; }
//受信時
socket.on('message', function(msg) {
var _msg ={};
//msg is JSON e.g. JSON.stringify({"name":"192.168.1.4", "keyword": "myhoge"})
var dataErr = false;
try{
_msg = JSON.parse( msg );
var name = _msg.name;
var keyword = _msg.keyword;
console.log(_msg, typeof _msg, "name: ",_msg.name, "keyword: ",_msg.keyword)
if(socket.readyState===1){
//ここでIP正規チェックもした方が良いかも
//キーワードチェック
if(!allowKeywordChk(keyword)){ return }
//WakeUp
if(WakeUpLists[name]!==undefined){
wakeUp (socket, name);//e.g.'192.168.1.4'
//送信データフォーマット e.g. {name: name, msg: name+ 'を起動しています。'}
socket.send(JSON.stringify({name: name, msg: name+ 'を起動しています。'}));
console.log(name, 'を起動しています');
}
}
} catch(e){
dataErr = true;
console.log(JSON.stringify(e))
}
if(dataErr)return;
});
});
ws.on('close', function() {
console.log('closed')
});
/*=========================================================
* Wake on LAN
*/
//マジックパケット送信
function wakeUp (socket, name){
var mac = WakeUpLists[name];
if(!mac){
console.log(name + ' はリストにありません');
return;
}
console.log('処理中: ' + name );
wol.wake(mac, function(error) {
if (error) {
console.log(error);
} else {
//送信データフォーマット e.g. {name: name, msg: name+ 'へ起動命令を送りました。'}
socket.send(JSON.stringify({name:name, msg: name + 'へ起動命令を送りました。'}));
// socket.close();//接続終了
//console.log('処理終了: ' + name + ': ' + mac);
setTimeout(function(){ socket.close();},timeout)
}
});
}
/*=========================================================
* allow
*/
function allowIPChk(socket, ws){
if(!allowIPLists)return true;
//接続してきたクライアントのIPアドレス
var clientIP = socket.upgradeReq.socket.remoteAddress;
console.log('connected: ', (new Date),'clientLen: ' + ws.clients.length, clientIP);
//接続許可
for(var i=0; i<allowIPLists.length;i++){
if(allowIPLists[i] === "*"){return true}
if(allowIPLists[i] === clientIP){
return true;
} else {
continue;
}
}
return false;
}
function allowKeywordChk(keyword){
if(!allowKeyword)return true;
//キーワードチェックのロジック追加してください
if(allowKeyword === "*")return true;
if(allowKeyword === keyword)return true;
return false;
}
return ws;
}
/*=========================================================
* モジュール化
*/
var rww = new function () {
this.conn = conn;
}
module.exports = rww;