mirror of
https://github.com/QingdaoU/JudgeServer.git
synced 2024-12-28 21:31:43 +00: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
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from utils import make_signature, check_signature
|
||||
from languages import c_lang_config, cpp_lang_config, java_lang_config
|
||||
|
||||
|
||||
token = hashlib.sha256("token").hexdigest()
|
||||
|
||||
c_src = r"""
|
||||
@ -50,46 +49,27 @@ public class Main{
|
||||
|
||||
|
||||
def compile_spj(src, spj_version, spj_config, test_case_id):
|
||||
data = make_signature(token=token,
|
||||
src=src,
|
||||
spj_version=spj_version,
|
||||
spj_compile_config=spj_config,
|
||||
test_case_id=test_case_id)
|
||||
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"]
|
||||
r = requests.post("http://123.57.151.42:11235/compile_spj",
|
||||
data=json.dumps({"src": src, "spj_version": spj_version,
|
||||
"spj_compile_config": spj_config, "test_case_id": test_case_id}),
|
||||
headers={"X-Judge-Server-Token": token})
|
||||
return r.json()
|
||||
|
||||
|
||||
def judge(src, language_config, submission_id):
|
||||
data = make_signature(token=token,
|
||||
language_config=language_config,
|
||||
submission_id=submission_id,
|
||||
src=src,
|
||||
max_cpu_time=1000, max_memory=1024 * 1024 * 1024,
|
||||
test_case_id="d8c460de943189a83bad166ec96d975d")
|
||||
r = requests.post("http://123.57.151.42:11235/judge", 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"]
|
||||
data = {"language_config": language_config,
|
||||
"submission_id": submission_id,
|
||||
"src": src,
|
||||
"max_cpu_time": 1000,
|
||||
"max_memory": 1024 * 1024 * 1024,
|
||||
"test_case_id": "d8c460de943189a83bad166ec96d975d"}
|
||||
r = requests.post("http://123.57.151.42:11235/judge", data=json.dumps(data), headers={"X-Judge-Server-Token": token})
|
||||
return r.json()
|
||||
|
||||
|
||||
def ping():
|
||||
data = make_signature(token=token)
|
||||
r = requests.post("http://123.57.151.42:11235/ping", 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"]
|
||||
r = requests.post("http://123.57.151.42:11235/ping", headers={"X-Judge-Server-Token": token})
|
||||
return r.json()
|
||||
|
||||
|
||||
print ping()
|
||||
|
@ -9,7 +9,7 @@ class SPJCompileError(CompileError):
|
||||
pass
|
||||
|
||||
|
||||
class SignatureVerificationFailed(Exception):
|
||||
class TokenVerificationFailed(Exception):
|
||||
pass
|
||||
|
||||
|
||||
|
38
server.py
38
server.py
@ -13,9 +13,8 @@ import web
|
||||
|
||||
from compiler import Compiler
|
||||
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 utils import make_signature, check_signature
|
||||
|
||||
DEBUG = os.environ.get("judger_debug") == "1"
|
||||
|
||||
@ -50,23 +49,9 @@ class JudgeServer(object):
|
||||
def _token(self):
|
||||
t = os.getenv("judger_token")
|
||||
if not t:
|
||||
raise SignatureVerificationFailed("token not set")
|
||||
raise TokenVerificationFailed("token not set")
|
||||
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):
|
||||
# init
|
||||
compile_config = language_config["compile"]
|
||||
@ -112,8 +97,15 @@ class JudgeServer(object):
|
||||
return "success"
|
||||
|
||||
def POST(self):
|
||||
token = web.ctx.env.get("HTTP_X_JUDGE_SERVER_TOKEN", None)
|
||||
try:
|
||||
data = json.loads(web.data())
|
||||
if token != self._token:
|
||||
raise TokenVerificationFailed("invalid token")
|
||||
if web.data():
|
||||
data = json.loads(web.data())
|
||||
else:
|
||||
data = {}
|
||||
|
||||
if web.ctx["path"] == "/judge":
|
||||
callback = self.judge
|
||||
elif web.ctx["path"] == "/ping":
|
||||
@ -121,12 +113,12 @@ class JudgeServer(object):
|
||||
elif web.ctx["path"] == "/compile_spj":
|
||||
callback = self.compile_spj
|
||||
else:
|
||||
return {"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": "invalid-method", "data": None})
|
||||
return json.dumps({"err": None, "data": callback(**data)})
|
||||
except Exception as e:
|
||||
ret = dict()
|
||||
ret["err"] = "ServerError"
|
||||
ret["data"] = ": ".join([e.__class__.__name__, e.message])
|
||||
ret["err"] = e.__class__.__name__
|
||||
ret["data"] = e.message
|
||||
return json.dumps(ret)
|
||||
|
||||
|
||||
@ -137,7 +129,7 @@ urls = (
|
||||
)
|
||||
|
||||
if not os.environ.get("judger_token"):
|
||||
raise SignatureVerificationFailed("token not set")
|
||||
raise TokenVerificationFailed("token not set")
|
||||
|
||||
app = web.application(urls, globals())
|
||||
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