Skip to content

Commit 79f5986

Browse files
committed
Add socket.io for challenge notifications
- using static localhost:3001 (see juice-shop#206)
1 parent a4f1c6b commit 79f5986

File tree

11 files changed

+56
-7
lines changed

11 files changed

+56
-7
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ RUN cd /juice-shop; npm install; ./node_modules/.bin/bower install; ./node_modul
77

88
WORKDIR /juice-shop
99

10-
EXPOSE 3000
10+
EXPOSE 3000 3001
1111
CMD npm start

app/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script>
4646
<script src="bower_components/ng-file-upload/ng-file-upload-shim.min.js"></script> <!-- for no html5 browsers support -->
4747
<script src="bower_components/ng-file-upload/ng-file-upload.min.js"></script>
48+
<script src="bower_components/socket.io-client/socket.io.js"></script>
4849

4950
<!-- application specific source files -->
5051
<script src="dist/juice-shop.min.js"></script>
@@ -117,6 +118,14 @@
117118
</div>
118119
</nav>
119120

121+
<script>
122+
var socket = io('localhost:3001') // TODO Exchange with URL retrieved through SocketController
123+
socket.on('solved', function (data) {
124+
console.log(data)
125+
})
126+
</script>
127+
128+
120129
<h1 class="hidden">OWASP Juice Shop</h1><!-- for SEO -->
121130

122131
<!-- CONTENT -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
angular.module('juiceShop').controller('SocketController', [
2+
'$scope',
3+
'AdministrationService',
4+
function ($scope, administrationService) {
5+
'use strict'
6+
7+
$scope.url = ''
8+
9+
administrationService.getSocketUrl().success(function (data) {
10+
if (data && data.url) {
11+
$scope.url = data.url
12+
}
13+
}).error(function (err) {
14+
console.log(err)
15+
})
16+
}])

app/js/services/AdministrationService.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ angular.module('juiceShop').factory('AdministrationService', ['$http', function
77
return $http.get(host + '/application-version')
88
}
99

10+
function getSocketUrl () {
11+
return $http.get(host + '/socket.io')
12+
}
13+
1014
return {
11-
getApplicationVersion: getApplicationVersion
15+
getApplicationVersion: getApplicationVersion,
16+
getSocketUrl: getSocketUrl
1217
}
1318
}])

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"fontawesome": "~4.6",
2424
"angular-translate": "^2.12.1",
2525
"angular-translate-loader-static-files": "^2.12.1",
26-
"flag-icon-css": "^2.5.0"
26+
"flag-icon-css": "^2.5.0",
27+
"socket.io-client": "^1.4.8"
2728
},
2829
"resolutions": {
2930
"angular": "~1.5"

karma.conf-ci.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module.exports = function (config) {
5757
'app/bower_components/underscore/underscore.js',
5858
'app/bower_components/jquery/dist/jquery.js',
5959
'app/bower_components/ng-file-upload/ng-file-upload.js',
60+
'app/bower_components/socket.io-client/socket.io.js',
6061
'app/js/app.js',
6162
'app/js/**/*.js',
6263
'test/client/**/*.js'

karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = function (config) {
1818
'app/bower_components/underscore/underscore.js',
1919
'app/bower_components/jquery/dist/jquery.js',
2020
'app/bower_components/ng-file-upload/ng-file-upload.js',
21+
'app/bower_components/socket.io-client/socket.io.js',
2122
'app/js/**/*.js',
2223
'test/client/**/*.js'
2324
],

lib/utils.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
var colors = require('colors/safe')
55
var packageJson = require('../package.json')
66

7-
var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
7+
exports.ioPort = process.env.IO_PORT || 3001
8+
var io = require('socket.io')(this.ioPort, { origins: '*:*' })
9+
10+
var months = [ 'JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC' ]
811

912
exports.queryResultToJson = function (data, status) {
1013
var wrappedData = {}
@@ -14,7 +17,7 @@ exports.queryResultToJson = function (data, status) {
1417
} else if (data.length > 0) {
1518
wrappedData = []
1619
for (var i = 0; i < data.length; i++) {
17-
wrappedData.push(data[i].dataValues ? data[i].dataValues : data[i])
20+
wrappedData.push(data[ i ].dataValues ? data[ i ].dataValues : data[ i ])
1821
}
1922
} else {
2023
wrappedData = data
@@ -48,7 +51,7 @@ exports.unquote = function (str) {
4851

4952
exports.version = function (module) {
5053
if (module) {
51-
return packageJson.dependencies[module]
54+
return packageJson.dependencies[ module ]
5255
} else {
5356
return packageJson.version
5457
}
@@ -58,6 +61,7 @@ exports.solve = function (challenge) {
5861
challenge.solved = true
5962
challenge.save().success(function () {
6063
console.log(colors.green('Solved') + ' challenge ' + colors.cyan(challenge.name) + ' (' + challenge.description + ')')
64+
io.emit('solved', { challenge: challenge })
6165
})
6266
}
6367

@@ -68,5 +72,5 @@ exports.notSolved = function (challenge) {
6872
exports.toMMMYY = function (date) {
6973
var month = date.getMonth()
7074
var year = date.getFullYear()
71-
return months[month] + year.toString().substring(2, 4)
75+
return months[ month ] + year.toString().substring(2, 4)
7276
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"sequelize-restful": "~0.4",
4242
"serve-favicon": "~2.3",
4343
"serve-index": "~1.8",
44+
"socket.io": "^1.4.8",
4445
"sqlite3": "3.1",
4546
"z85": "~0.0"
4647
},

routes/socketUrl.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
var utils = require('../lib/utils')
4+
5+
exports = module.exports = function socketUrl () {
6+
return function (req, res) {
7+
res.json({url: req.protocol + '://' + req.hostname + ':' + utils.ioPort})
8+
}
9+
}

0 commit comments

Comments
 (0)