@@ -26,7 +26,10 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2626
2727*/
2828
29- // This is a nodejs server based on express that serves the v4-cokapi/ app
29+ // This is a nodejs server based on express that serves the v4-cokapi/ app,
30+ // which implements OPT language backends for languages such as Java,
31+ // Ruby, JavaScript, TypeScript, C, C++, ...
32+
3033// To test locally, run 'make' and load http://localhost:3000/
3134
3235// Run with an 'https' command-line flag to use https (must have
@@ -38,10 +41,6 @@ var PRODUCTION_PORT = 3000;
3841var PRODUCTION_HTTPS_PORT = 8001 ;
3942var DEBUG_PORT = 5001 ;
4043
41- // VERY IMPORTANT - turn on the sandbox when deploying online, or else
42- // you'll be executing untrusted code on your server!
43- var USE_DOCKER_SANDBOX = true ;
44-
4544var assert = require ( 'assert' ) ;
4645var child_process = require ( 'child_process' ) ;
4746var express = require ( 'express' ) ;
@@ -63,7 +62,7 @@ var CPP_TIMEOUT_SECS = 15; // the C/C++ backend is also SUPER SLOW :/
6362
6463var MAX_BUFFER_SIZE = 10 * 1024 * 1024 ;
6564
66- var MEM_LIMIT = "256M " ;
65+ var MEM_LIMIT = "512M " ;
6766
6867
6968// bind() res and useJSONP before using
@@ -119,14 +118,6 @@ var app = express();
119118// http://ilee.co.uk/jsonp-in-express-nodejs/
120119app . set ( "jsonp callback" , true ) ;
121120
122- app . get ( '/exec_py2' , function ( req , res ) {
123- executePython ( 'py2' , req , res ) ;
124- } ) ;
125-
126- app . get ( '/exec_py3' , function ( req , res ) {
127- executePython ( 'py3' , req , res ) ;
128- } ) ;
129-
130121app . get ( '/exec_js' , exec_js_handler . bind ( null , false , false ) ) ;
131122app . get ( '/exec_js_jsonp' , exec_js_handler . bind ( null , true , false ) ) ;
132123
@@ -139,20 +130,13 @@ function exec_js_handler(useJSONP /* use bind first */, isTypescript /* use bind
139130 var exeFile ;
140131 var args = [ ] ;
141132
142- if ( USE_DOCKER_SANDBOX ) {
143- // this needs to match the docker setup in Dockerfile
144- exeFile = '/usr/bin/docker' ; // absolute path to docker executable
145- args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/cokapi-js:v1' ,
146- //'node', // built-in Node.js version
147- '/tmp/javascript/node-v6.0.0-linux-x64/bin/node' , // custom Node.js version
148- '--expose-debug-as=Debug' ,
149- '/tmp/javascript/jslogger.js' ) ;
150- } else {
151- assert ( false ) ;
152- //exeFile = NODE_BIN;
153- //args.push('--expose-debug-as=Debug',
154- // 'backends/javascript/jslogger.js');
155- }
133+ // must match the docker setup in backends/javascript/Dockerfile
134+ exeFile = '/usr/bin/docker' ; // absolute path to docker executable
135+ args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/cokapi-js:v1' ,
136+ '/tmp/javascript/node-v6.0.0-linux-x64/bin/node' , // custom Node.js version
137+ '--expose-debug-as=Debug' ,
138+ '/tmp/javascript/jslogger.js' ) ;
139+
156140 if ( isTypescript ) {
157141 args . push ( '--typescript=true' ) ;
158142 }
@@ -170,6 +154,7 @@ function exec_js_handler(useJSONP /* use bind first */, isTypescript /* use bind
170154 postExecHandler . bind ( null , res , useJSONP ) ) ;
171155}
172156
157+
173158app . get ( '/exec_java' , exec_java_handler . bind ( null , false ) ) ;
174159app . get ( '/exec_java_jsonp' , exec_java_handler . bind ( null , true ) ) ;
175160
@@ -199,15 +184,11 @@ function exec_java_handler(useJSONP /* use bind first */, req, res) {
199184
200185 var inputObjJSON = JSON . stringify ( inputObj ) ;
201186
202- if ( USE_DOCKER_SANDBOX ) {
203- // this needs to match the docker setup in Dockerfile
204- exeFile = '/usr/bin/docker' ; // absolute path to docker executable
205- args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/cokapi:v1' ,
206- '/tmp/run-java-backend.sh' ,
207- inputObjJSON ) ;
208- } else {
209- assert ( false ) ;
210- }
187+ // must match the docker setup in backends/java/Dockerfile
188+ exeFile = '/usr/bin/docker' ; // absolute path to docker executable
189+ args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/cokapi-java:v1' ,
190+ '/tmp/run-java-backend.sh' ,
191+ inputObjJSON ) ;
211192
212193 child_process . execFile ( exeFile , args ,
213194 { timeout : JAVA_TIMEOUT_SECS * 1000 /* milliseconds */ ,
@@ -220,6 +201,7 @@ function exec_java_handler(useJSONP /* use bind first */, req, res) {
220201 postExecHandler . bind ( null , res , useJSONP ) ) ;
221202}
222203
204+
223205app . get ( '/exec_ruby' , exec_ruby_handler . bind ( null , false ) ) ;
224206app . get ( '/exec_ruby_jsonp' , exec_ruby_handler . bind ( null , true ) ) ;
225207
@@ -229,18 +211,13 @@ function exec_ruby_handler(useJSONP /* use bind first */, req, res) {
229211 var exeFile ;
230212 var args = [ ] ;
231213
232- if ( USE_DOCKER_SANDBOX ) {
233- // this needs to match the docker setup in Dockerfile
234- exeFile = '/usr/bin/docker' ; // absolute path to docker executable
235- args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/cokapi:v1' ,
236- '/tmp/ruby/ruby' ,
237- '/tmp/ruby/pg_logger.rb' ,
238- '-c' ,
239- usrCod ) ;
240- } else {
241- assert ( false ) ;
242- // must use Docker
243- }
214+ // must match the docker setup in backends/ruby/Dockerfile
215+ exeFile = '/usr/bin/docker' ; // absolute path to docker executable
216+ args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/cokapi-ruby:v1' ,
217+ '/tmp/ruby/ruby' ,
218+ '/tmp/ruby/pg_logger.rb' ,
219+ '-c' ,
220+ usrCod ) ;
244221
245222 child_process . execFile ( exeFile , args ,
246223 { timeout : TIMEOUT_SECS * 1000 /* milliseconds */ ,
@@ -253,6 +230,7 @@ function exec_ruby_handler(useJSONP /* use bind first */, req, res) {
253230 postExecHandler . bind ( null , res , useJSONP ) ) ;
254231}
255232
233+
256234app . get ( '/exec_c' , exec_cpp_handler . bind ( null , false , false ) ) ;
257235app . get ( '/exec_c_jsonp' , exec_cpp_handler . bind ( null , false , true ) ) ;
258236app . get ( '/exec_cpp' , exec_cpp_handler . bind ( null , true , false ) ) ;
@@ -264,18 +242,13 @@ function exec_cpp_handler(useCPP /* use bind first */, useJSONP /* use bind firs
264242 var exeFile ;
265243 var args = [ ] ;
266244
267- if ( USE_DOCKER_SANDBOX ) {
268- // this needs to match the docker setup in Dockerfile
269- exeFile = '/usr/bin/docker' ; // absolute path to docker executable
270- args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/opt-cpp-backend:v1' ,
271- 'python' ,
272- '/tmp/opt-cpp-backend/run_cpp_backend.py' ,
273- usrCod ,
274- useCPP ? 'cpp' : 'c' ) ;
275- } else {
276- assert ( false ) ;
277- // must use Docker
278- }
245+ // this needs to match the docker setup in opt-cpp-backend/Dockerfile (in the https://github.com/pgbovine/opt-cpp-backend repo)
246+ exeFile = '/usr/bin/docker' ; // absolute path to docker executable
247+ args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/opt-cpp-backend:v1' ,
248+ 'python' ,
249+ '/tmp/opt-cpp-backend/run_cpp_backend.py' ,
250+ usrCod ,
251+ useCPP ? 'cpp' : 'c' ) ;
279252
280253 child_process . execFile ( exeFile , args ,
281254 { timeout : CPP_TIMEOUT_SECS * 1000 /* milliseconds */ ,
@@ -289,52 +262,6 @@ function exec_cpp_handler(useCPP /* use bind first */, useJSONP /* use bind firs
289262}
290263
291264
292-
293- function executePython ( pyVer , req , res ) {
294- var parsedOptions = JSON . parse ( req . query . options_json ) ;
295-
296- var usrCod = req . query . user_script ;
297- var usrInputJson = req . query . raw_input_json ;
298- var cumulativeMode = parsedOptions . cumulative_mode ;
299- var heapPrimitives = parsedOptions . heap_primitives ;
300-
301- var exeFile ;
302- var args = [ ] ;
303-
304- if ( USE_DOCKER_SANDBOX ) {
305- // this needs to match the docker setup in Dockerfile
306- exeFile = '/usr/bin/docker' ; // absolute path to docker executable
307- args . push ( 'run' , '-m' , MEM_LIMIT , '--rm' , '--user=netuser' , '--net=none' , '--cap-drop' , 'all' , 'pgbovine/cokapi:v1' ,
308- ( pyVer == 'py2' ? 'python' : 'python3' ) ,
309- '/tmp/python/generate_json_trace.py' ) ;
310- } else {
311- assert ( false ) ;
312- //exeFile = (pyVer == 'py2' ? PYTHON2_BIN : PYTHON3_BIN);
313- //args.push('backends/python/generate_json_trace.py');
314- }
315-
316- if ( cumulativeMode ) {
317- args . push ( '-c' ) ;
318- }
319- if ( heapPrimitives ) {
320- args . push ( '-p' ) ;
321- }
322- if ( usrInputJson ) {
323- args . push ( '--input=' + usrInputJson ) ;
324- }
325- args . push ( '--code=' + usrCod ) ;
326-
327- child_process . execFile ( exeFile , args ,
328- { timeout : TIMEOUT_SECS * 1000 /* milliseconds */ ,
329- maxBuffer : MAX_BUFFER_SIZE ,
330- // make SURE docker gets the kill signal;
331- // this signal seems to allow docker to clean
332- // up after itself to --rm the container, but
333- // double-check with 'docker ps -a'
334- killSignal : 'SIGINT' } ,
335- postExecHandler . bind ( null , res , false ) ) ;
336- }
337-
338265// https support
339266var https = require ( 'https' ) ;
340267var fs = require ( 'fs' ) ;
0 commit comments