Skip to content

Commit 059448c

Browse files
committed
Validate IP address
1 parent 0ceeb19 commit 059448c

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

src/qimessaging.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ export class QiSession {
3838
) {
3939
this.connected = connected;
4040
this.disconnected = disconnected;
41-
console.log('REMOVE: isConnected ', this.isConnected());
41+
4242
console.log('DBG Emile qim about to connect w/17');
4343
this._socket = io.connect('nao:nao@' + ipAddress + ':' + port, {
4444
resource: 'libs/qimessaging/2/socket.io',
4545
'force new connection': true,
4646
});
47-
console.log('REMOVE: isConnected ', this.isConnected(), this._socket.socket);
4847
console.log('DBG Emile qim connecting..');
48+
4949
this._dfd = [];
5050
this._sigs = [];
5151
this._idm = 0;
@@ -62,7 +62,6 @@ export class QiSession {
6262

6363
this.service = this.createMetaCall('ServiceDirectory', 'service', 'data');
6464

65-
console.log('REMOVE: isConnected ', this.isConnected(), this._socket.socket);
6665
console.log('DBG Emile qim done with init');
6766
}
6867

@@ -152,11 +151,6 @@ export class QiSession {
152151
if (this.connected) {
153152
this.connected(this);
154153
}
155-
const connected : boolean =
156-
(this._socket !== undefined) ?
157-
this._socket.socket.connected : false;
158-
console.log('REMOVE: isConnected in on connect ', connected);
159-
160154
}
161155

162156
onDisconnect(_data: any) {
@@ -180,7 +174,6 @@ export class QiSession {
180174
});
181175
if (serviceArgs[0] === 'connect') {
182176
this.isConnected = this._socket.socket.connected;
183-
console.log('REMOVE: isConnected in MetaCall ', this.isConnected(), this._socket);
184177
this._dfd[this._idm].__cbi = data;
185178
}
186179
this._socket.emit('call', {

src/widget.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,27 @@ export class NaoRobotModel extends DOMWidgetModel {
4747
this.save_changes();
4848
}
4949

50+
private validateIPaddress(ipAddress: string) {
51+
// TODO: validate port also
52+
if (ipAddress == 'nao.local') {
53+
return true;
54+
} else {
55+
const regexp = new RegExp('^((25[0-5]|(2[0-4]|1[0-9]|[1-9]|)[0-9])(\.(?!$)|$)){4}$');
56+
return regexp.test(ipAddress);
57+
}
58+
}
59+
5060
async connect(ipAddress: string, port: string) {
5161
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
5262

5363
this.changeStatus('Establishing connection');
54-
// TODO: check ipAddress is valid format
64+
65+
if (!this.validateIPaddress(ipAddress)) {
66+
this.changeStatus('Invalid IP address');
67+
console.warn('IP Address ', ipAddress, ' is not valid');
68+
return;
69+
}
70+
5571
this.qiSession = new QiSession(ipAddress, port);
5672

5773
// Timeout after ~10 seconds

0 commit comments

Comments
 (0)