mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2025-11-04 14:50:01 +08:00
t # This is a combination of 2 commits.
去掉了签名机制
This commit is contained in:
parent
177655b4c8
commit
a93a22357c
52
client.py
52
client.py
@ -1,15 +1,14 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from utils import make_signature, check_signature
|
|
||||||
from languages import c_lang_config, cpp_lang_config, java_lang_config
|
from languages import c_lang_config, cpp_lang_config, java_lang_config
|
||||||
|
|
||||||
|
|
||||||
token = hashlib.sha256("token").hexdigest()
|
token = hashlib.sha256("token").hexdigest()
|
||||||
|
|
||||||
c_src = r"""
|
c_src = r"""
|
||||||
@ -50,46 +49,27 @@ public class Main{
|
|||||||
|
|
||||||
|
|
||||||
def compile_spj(src, spj_version, spj_config, test_case_id):
|
def compile_spj(src, spj_version, spj_config, test_case_id):
|
||||||
data = make_signature(token=token,
|
r = requests.post("http://123.57.151.42:11235/compile_spj",
|
||||||
src=src,
|
data=json.dumps({"src": src, "spj_version": spj_version,
|
||||||
spj_version=spj_version,
|
"spj_compile_config": spj_config, "test_case_id": test_case_id}),
|
||||||
spj_compile_config=spj_config,
|
headers={"X-Judge-Server-Token": token})
|
||||||
test_case_id=test_case_id)
|
return r.json()
|
||||||
r = requests.post("http://123.57.151.42:11235/compile_spj", data=json.dumps(data))
|
|
||||||
data = r.json()
|
|
||||||
check_signature(token=token, **data)
|
|
||||||
result = json.loads(data["data"])
|
|
||||||
if result["err"]:
|
|
||||||
return result["data"]
|
|
||||||
return json.loads(data["data"])["data"]
|
|
||||||
|
|
||||||
|
|
||||||
def judge(src, language_config, submission_id):
|
def judge(src, language_config, submission_id):
|
||||||
data = make_signature(token=token,
|
data = {"language_config": language_config,
|
||||||
language_config=language_config,
|
"submission_id": submission_id,
|
||||||
submission_id=submission_id,
|
"src": src,
|
||||||
src=src,
|
"max_cpu_time": 1000,
|
||||||
max_cpu_time=1000, max_memory=1024 * 1024 * 1024,
|
"max_memory": 1024 * 1024 * 1024,
|
||||||
test_case_id="d8c460de943189a83bad166ec96d975d")
|
"test_case_id": "d8c460de943189a83bad166ec96d975d"}
|
||||||
r = requests.post("http://123.57.151.42:11235/judge", data=json.dumps(data))
|
r = requests.post("http://123.57.151.42:11235/judge", data=json.dumps(data), headers={"X-Judge-Server-Token": token})
|
||||||
data = r.json()
|
return r.json()
|
||||||
check_signature(token=token, **data)
|
|
||||||
result = json.loads(data["data"])
|
|
||||||
if result["err"]:
|
|
||||||
return result["data"]
|
|
||||||
return json.loads(data["data"])["data"]
|
|
||||||
|
|
||||||
|
|
||||||
def ping():
|
def ping():
|
||||||
data = make_signature(token=token)
|
r = requests.post("http://123.57.151.42:11235/ping", headers={"X-Judge-Server-Token": token})
|
||||||
r = requests.post("http://123.57.151.42:11235/ping", data=json.dumps(data))
|
return r.json()
|
||||||
|
|
||||||
data = r.json()
|
|
||||||
check_signature(token=token, **data)
|
|
||||||
result = json.loads(data["data"])
|
|
||||||
if result["err"]:
|
|
||||||
return result["data"]
|
|
||||||
return json.loads(data["data"])["data"]
|
|
||||||
|
|
||||||
|
|
||||||
print ping()
|
print ping()
|
||||||
|
|||||||
@ -9,7 +9,7 @@ class SPJCompileError(CompileError):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class SignatureVerificationFailed(Exception):
|
class TokenVerificationFailed(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
36
server.py
36
server.py
@ -13,9 +13,8 @@ import web
|
|||||||
|
|
||||||
from compiler import Compiler
|
from compiler import Compiler
|
||||||
from config import JUDGER_WORKSPACE_BASE, TEST_CASE_DIR
|
from config import JUDGER_WORKSPACE_BASE, TEST_CASE_DIR
|
||||||
from exception import SignatureVerificationFailed, CompileError, SPJCompileError
|
from exception import TokenVerificationFailed, CompileError, SPJCompileError
|
||||||
from judge_client import JudgeClient
|
from judge_client import JudgeClient
|
||||||
from utils import make_signature, check_signature
|
|
||||||
|
|
||||||
DEBUG = os.environ.get("judger_debug") == "1"
|
DEBUG = os.environ.get("judger_debug") == "1"
|
||||||
|
|
||||||
@ -50,23 +49,9 @@ class JudgeServer(object):
|
|||||||
def _token(self):
|
def _token(self):
|
||||||
t = os.getenv("judger_token")
|
t = os.getenv("judger_token")
|
||||||
if not t:
|
if not t:
|
||||||
raise SignatureVerificationFailed("token not set")
|
raise TokenVerificationFailed("token not set")
|
||||||
return hashlib.sha256(t).hexdigest()
|
return hashlib.sha256(t).hexdigest()
|
||||||
|
|
||||||
def entry(self, data, signature, timestamp, callback):
|
|
||||||
if not DEBUG:
|
|
||||||
check_signature(token=self._token, data=data, signature=signature, timestamp=timestamp)
|
|
||||||
ret = {"err": None, "data": None}
|
|
||||||
try:
|
|
||||||
ret["data"] = callback(**json.loads(data))
|
|
||||||
except (CompileError, SPJCompileError, SignatureVerificationFailed) as e:
|
|
||||||
ret["err"] = e.__class__.__name__
|
|
||||||
ret["data"] = e.message
|
|
||||||
except Exception as e:
|
|
||||||
ret["err"] = "ServerError"
|
|
||||||
ret["data"] = ": ".join([e.__class__.__name__, e.message])
|
|
||||||
return make_signature(token=self._token, **ret)
|
|
||||||
|
|
||||||
def judge(self, language_config, submission_id, src, max_cpu_time, max_memory, test_case_id):
|
def judge(self, language_config, submission_id, src, max_cpu_time, max_memory, test_case_id):
|
||||||
# init
|
# init
|
||||||
compile_config = language_config["compile"]
|
compile_config = language_config["compile"]
|
||||||
@ -112,8 +97,15 @@ class JudgeServer(object):
|
|||||||
return "success"
|
return "success"
|
||||||
|
|
||||||
def POST(self):
|
def POST(self):
|
||||||
|
token = web.ctx.env.get("HTTP_X_JUDGE_SERVER_TOKEN", None)
|
||||||
try:
|
try:
|
||||||
|
if token != self._token:
|
||||||
|
raise TokenVerificationFailed("invalid token")
|
||||||
|
if web.data():
|
||||||
data = json.loads(web.data())
|
data = json.loads(web.data())
|
||||||
|
else:
|
||||||
|
data = {}
|
||||||
|
|
||||||
if web.ctx["path"] == "/judge":
|
if web.ctx["path"] == "/judge":
|
||||||
callback = self.judge
|
callback = self.judge
|
||||||
elif web.ctx["path"] == "/ping":
|
elif web.ctx["path"] == "/ping":
|
||||||
@ -121,12 +113,12 @@ class JudgeServer(object):
|
|||||||
elif web.ctx["path"] == "/compile_spj":
|
elif web.ctx["path"] == "/compile_spj":
|
||||||
callback = self.compile_spj
|
callback = self.compile_spj
|
||||||
else:
|
else:
|
||||||
return {"err": "invalid-method", "data": None}
|
return json.dumps({"err": "invalid-method", "data": None})
|
||||||
return json.dumps(self.entry(data=data["data"], signature=data["signature"], timestamp=data["timestamp"], callback=callback))
|
return json.dumps({"err": None, "data": callback(**data)})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
ret = dict()
|
ret = dict()
|
||||||
ret["err"] = "ServerError"
|
ret["err"] = e.__class__.__name__
|
||||||
ret["data"] = ": ".join([e.__class__.__name__, e.message])
|
ret["data"] = e.message
|
||||||
return json.dumps(ret)
|
return json.dumps(ret)
|
||||||
|
|
||||||
|
|
||||||
@ -137,7 +129,7 @@ urls = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not os.environ.get("judger_token"):
|
if not os.environ.get("judger_token"):
|
||||||
raise SignatureVerificationFailed("token not set")
|
raise TokenVerificationFailed("token not set")
|
||||||
|
|
||||||
app = web.application(urls, globals())
|
app = web.application(urls, globals())
|
||||||
wsgiapp = app.wsgifunc()
|
wsgiapp = app.wsgifunc()
|
||||||
|
|||||||
24
utils.py
24
utils.py
@ -1,24 +0,0 @@
|
|||||||
# coding=utf-8
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
import json
|
|
||||||
import time
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
from exception import SignatureVerificationFailed
|
|
||||||
|
|
||||||
|
|
||||||
def make_signature(**kwargs):
|
|
||||||
token = kwargs.pop("token")
|
|
||||||
data = json.dumps(kwargs)
|
|
||||||
timestamp = int(time.time())
|
|
||||||
return {"data": data, "signature": hashlib.sha256(data + str(timestamp) + token).hexdigest(), "timestamp": timestamp}
|
|
||||||
|
|
||||||
|
|
||||||
def check_signature(token, data, signature, timestamp):
|
|
||||||
ts = int(time.time())
|
|
||||||
if abs(timestamp - ts) > 10:
|
|
||||||
raise SignatureVerificationFailed("Timestamp interval is too long")
|
|
||||||
|
|
||||||
if hashlib.sha256(data + str(timestamp) + token).hexdigest() != signature:
|
|
||||||
raise SignatureVerificationFailed("Wrong signature")
|
|
||||||
Loading…
Reference in New Issue
Block a user