@@ -27,22 +27,37 @@ wsServer = new ws({
27
27
httpServer : server
28
28
} ) ;
29
29
30
+ function getShellFilePath ( moduleName ) {
31
+ return __dirname + '/modules/shell_files/' + moduleName + '.sh' ;
32
+ }
33
+
34
+ function shellPathAndModuleNameAreValid ( shellFilePath , moduleName ) {
35
+
36
+ var moduleInvalidName = moduleName . indexOf ( '.' ) > - 1 ;
37
+ var moduleNameEmpty = ! moduleName ;
38
+ var moduleNotFound = ! fs . existsSync ( shellFilePath ) ;
39
+ var isValid = true ;
40
+
41
+ if ( moduleInvalidName || moduleNameEmpty || moduleNotFound ) {
42
+ isValid = false ;
43
+ }
44
+
45
+ return isValid ;
46
+ }
47
+
30
48
wsServer . on ( 'request' , function ( request ) {
31
49
32
50
var wsClient = request . accept ( 'linux-dash' , request . origin ) ;
33
51
34
52
wsClient . on ( 'message' , function ( wsReq ) {
35
53
36
- var moduleName = wsReq . utf8Data ;
37
- var shellFile = __dirname + '/modules/shell_files/' + moduleName + '.sh' ;
38
- var moduleInvalidName = moduleName . indexOf ( '.' ) > - 1 ;
39
- var moduleNameEmpty = ! moduleName ;
40
- var moduleNotFound = ! fs . existsSync ( shellFile ) ;
54
+ var moduleName = wsReq . utf8Data ;
55
+ var shellFile = getShellFilePath ( moduleName ) ;
41
56
42
- if ( moduleInvalidName || moduleNameEmpty || moduleNotFound ) {
57
+ if ( ! shellPathAndModuleNameAreValid ( shellFile , moduleName ) ) {
43
58
res . sendStatus ( 406 ) ;
44
- return ;
45
- }
59
+ return ;
60
+ }
46
61
47
62
var command = spawn ( shellFile , [ wsReq . color || '' ] ) ;
48
63
var output = [ ] ;
@@ -71,15 +86,12 @@ wsServer.on('request', function(request) {
71
86
72
87
app . get ( '/server/' , function ( req , res ) {
73
88
74
- var shellFile = __dirname + '/modules/shell_files/' + req . query . module + '.sh' ;
89
+ var shellFile = getShellFilePath ( req . query . module ) ;
75
90
76
- if ( req . query . module . indexOf ( '.' ) > - 1
77
- || ! req . query . module
78
- || ! fs . existsSync ( shellFile ) )
79
- {
80
- res . sendStatus ( 406 ) ;
81
- return ;
82
- }
91
+ if ( ! shellPathAndModuleNameAreValid ( shellFile , req . query . module ) ) {
92
+ res . sendStatus ( 406 ) ;
93
+ return ;
94
+ }
83
95
84
96
var command = spawn ( shellFile , [ req . query . color || '' ] ) ;
85
97
var output = [ ] ;
0 commit comments