def compile(submission_id, language_id): secure.low_level() '''将程序编译成可执行文件''' language = language.get_lang(language_id).lower() dir_work = os.path.join(config.work_dir, str(submission_id)) build_cmd = { "gcc": "gcc main.c -o main -Wall -lm -O2 -std=c99 --static -DONLINE_JUDGE", "g++": "g++ main.cpp -O2 -Wall -lm --static -DONLINE_JUDGE -o main", "java": "javac Main.java", "ruby": "reek main.rb", "perl": "perl -c main.pl", "pascal": 'fpc main.pas -O2 -Co -Ct -Ci', "go": '/opt/golang/bin/go build -ldflags "-s -w" main.go', "lua": 'luac -o main main.lua', "python2": 'python2 -m py_compile main.py', "python3": 'python3 -m py_compile main.py', "haskell": "ghc -o main main.hs", } if language not in build_cmd.keys(): return False p = subprocess.Popen( build_cmd[language], shell=True, cwd=dir_work, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) out, err = p.communicate() # 获取编译错误信息 if p.returncode == 0: # 返回值为0,编译成功 return True init.dblock.acquire() update.update_compile_info(submission_id, err + out) # 编译失败,更新题目的编译错误信息 init.dblock.release() return False
def handle_send_message_event(data): app.logger.info("{} has sent message to the room {}: {} in {}".format( data['username'], data['room'], data['message'], data['lang'])) for sid in clients: translator = Translator() data['message'] = str( translator.translate(data['message'], dest=get_lang(userdata[sid])).text) socketio.emit('receive_message', data, room=sid)
def check_dangerous_code(submission_id, language_id): language = language.get_lang(language_id).lower() if language in ['python2', 'python3']: code = file('/work/%s/main.py' % submission_id).readlines() support_modules = [ 're', # 正则表达式 'sys', # sys.stdin 'string', # 字符串处理 'scanf', # 格式化输入 'math', # 数学库 'cmath', # 复数数学库 'decimal', # 数学库,浮点数 'numbers', # 抽象基类 'fractions', # 有理数 'random', # 随机数 'itertools', # 迭代函数 'functools', #Higher order functions and operations on callable objects 'operator', # 函数操作 'readline', # 读文件 'json', # 解析json 'array', # 数组 'sets', # 集合 'queue', # 队列 'types', # 判断类型 ] for line in code: if line.find('import') >= 0: words = line.split() tag = 0 for w in words: if w in support_modules: tag = 1 break if tag == 0: return False return True if language in ['c', 'c++']: try: code = file('/work/%s/main.c' % submission_id).read() except: code = file('/work/%s/main.cpp' % submission_id).read() if code.find('system') >= 0: return False return True if language == 'go': code = file('/work/%s/main.go' % submission_id).read() danger_package = [ 'os', 'path', 'net', 'sql', 'syslog', 'http', 'mail', 'rpc', 'smtp', 'exec', 'user', ] for item in danger_package: if code.find('"%s"' % item) >= 0: return False return True