Skip to content

Commit

Permalink
github webhook update 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Light-Beacon committed Feb 21, 2024
1 parent a386174 commit 148ab03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from flask import Flask
from flask import Flask, jsonify, request
from Core.Project import Project
from Core.FileIO import readYaml
from Core.Debug import LogFatal
from Server.project_updater import request_update
import os
import subprocess
app = Flask(__name__)
Expand All @@ -10,6 +11,10 @@
def getpage(name:str):
if name.endswith('/version'):
return server.getPage('version')
if name.endswith('/pull'):
status,data = request_update(request,server.project_path,
server.config['github_secret'])
return jsonify({'status':status,'data':data})
while name.endswith('/'):
name = name[:-1]
return server.getPage(name)
Expand Down
23 changes: 18 additions & 5 deletions Server/project_updater.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import hmac

GIT_PULL = 'git pull -f'

class GithubAuthError(Exception):
pass

def __update(request,project_path):
if not vaild_from_github(request):
def encrypt(secret,data):
key = secret.encode('UTF-8')
obj = hmac.new(key,msg=data,digestmod='sha1')
return obj.hexdigest()

def vaild_from_github(request,secret):
post_data = request.data
token = encrypt(secret,post_data)
sig = request.headers.get('X-Hub-Signature','').split('=')[-1]
return sig == token

def __update(request,project_path,secret):
if not vaild_from_github(request,secret):
raise GithubAuthError('Auth Failed')
result = subprocess.check_output(GIT_PULL,cwd = project_path, shell=True)
return result

def request_update(request,project_path):
def request_update(request,project_path,secret):
try:
return (True,__update(request,project_path))
return (200,__update(request,project_path))
except Exception as e:
return (False,e.args)
return (401,e.args)

0 comments on commit 148ab03

Please sign in to comment.