-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.py
215 lines (184 loc) · 7.14 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import json
from flask import Flask, request, abort, send_file, render_template_string, session, redirect
from flask_httpauth import HTTPBasicAuth
import md5
import logging
import logging.handlers
from multiprocessing.pool import ThreadPool
import time
import subprocess
pool = ThreadPool(processes=5)
app = Flask(__name__)
app.config['SECRET_KEY'] = '上线之后需要修改,网管ヽ( ̄▽ ̄)'
app.debug = True
auth = HTTPBasicAuth()
logs = {}
def getlog(filename):
log = logging.getLogger(filename)
log.setLevel(logging.INFO)
log_handler = logging.handlers.RotatingFileHandler(filename + ".log",
maxBytes=4086,
backupCount=1024)
f = logging.Formatter("%(asctime)s %(levelname)s %(message)s",
"%Y-%m-%d %H:%M:%S")
log_handler.setFormatter(f)
log.addHandler(log_handler)
return log
webhooklog = getlog("/var/webhook/webhook")
@auth.verify_password
def verify_pw(repo, password):
repos = json.loads(open('repos.json', 'rb').read())
if repos.get(repo):
if repos.get(repo).get('pass') == md5.md5(password + app.config['SECRET_KEY']).hexdigest():
session['repo'] = repo
if not logs.get(repo):
logs[repo] = getlog(repo)
return True
return False
def build(name, url, branch, log):
log.info('building %s:%s' % (name, branch))
# check if need git init
basedir = os.path.join(app.root_path, name)
if not os.path.isdir(basedir):
r = os.system("git clone %s" % url)
if r != 0:
log.critical('%s clone error' % name)
return
else:
log.info('%s clone ok' % name)
env = 'GIT_DIR="%s/.git" GIT_WORK_TREE="%s/" ' % (basedir, basedir)
# change branch
r = os.system(env + "git checkout master && " + env +
"git pull && " + env + " git checkout %s" % branch)
if r != 0:
log.critical('%s:%s checkout error' % (name, branch))
else:
log.info('%s:%s checkout ok' % (name, branch))
r = os.system(env + "git pull origin %s:%s" % (branch, branch))
if r != 0:
log.critical('%s:%s pull error' % (name, branch))
else:
log.info('%s:%s pull ok' % (name, branch))
outpath = os.path.join(
app.root_path, "outfile/%s/%s/" % (
name,
branch
)
)
if not os.path.isdir(outpath):
os.makedirs(outpath, 0755)
args = ['zip', '-r',
os.path.join(outpath, str(int(time.time())) + '.zip')]
if os.path.isfile(os.path.join(basedir, 'build.json')):
b = json.loads(
open(os.path.join(basedir, 'build.json')).read())
for x in b.get('include', [basedir]):
args.append(x)
for x in b.get('exclude', []):
args.append("-x")
args.append(x)
p = subprocess.Popen(args, cwd=basedir)
timeout = 2
while timeout > 0:
timeout -= 1
if p.poll():
break
time.sleep(1)
if not p.poll():
try:
p.kill()
except:
pass
r = p.returncode
if r != 0:
log.critical('%s:%s build error' % (name, branch))
else:
log.info('%s:%s build ok' % (name, branch))
else:
log.critical('%s:%s build error' % (name, branch))
@app.route("/push", methods=['GET', 'POST'])
def push():
if request.method == 'GET':
return 'OK'
elif request.method == 'POST' and request.host.split(":")[0] == "webhook.ssctf.seclover.com":
post = json.loads(request.data)
if 'repository' not in post:
abort(403)
repos = json.loads(open('repos.json').read())
name = post['repository']["name"]
match = re.match(r"refs/heads/(?P<branch>.*)", post['ref'])
if match:
branch = match.groupdict()['branch']
branch = 'master'
before = post.get("before") or ""
msg = "recived push repo:{name} with before \n"
msg += json.dumps(before, indent=4)
webhooklog.info(msg.format(**locals()))
else:
abort(403)
if repos.get(name):
if not logs.get(name):
logs[name] = getlog(name)
url = repos[name].get("url", "")
log = logs[name]
pool.apply_async(build, (name, url, branch, log))
else:
abort(403)
return 'OK'
@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
@auth.login_required
def dir_listing(req_path):
BASE_DIR = os.path.join(app.root_path, 'outfile', session['repo'])
# Joining the base and the requested path
abs_path = os.path.realpath(os.path.join(BASE_DIR, req_path))
# Return 404 if path doesn't exist
if not os.path.exists(abs_path) or not abs_path.startswith(BASE_DIR):
return abort(404)
# Check if path is a file and serve
if os.path.isfile(abs_path):
return send_file(abs_path)
# Show directory contents
files = sorted(os.listdir(abs_path), reverse=True)
return render_template_string('''<ul>
{% for file in files %}
<li><a href="{%if req_path %}/{{req_path}}{% endif%}/{{ file }}">{{ file }}</a></li>
{% endfor %}
</ul>''', files=files, req_path=req_path)
@app.route("/log", methods=['GET'])
@auth.login_required
def showlog():
return open(session['repo'] + '.log', 'r').read(), 200, {'Content-Type': 'text/plain; charset=utf-8'}
@app.route("/webhooklog", methods=['GET'])
def showwebhooklog():
return open('/var/webhook/webhook.log', 'r').read(), 200, {'Content-Type': 'text/plain; charset=utf-8'}
@app.route("/addrepo", methods=['GET'])
def addrepo():
repos = json.loads(open('repos.json', 'rb').read())
repo = request.args.get('repo', "")
key = request.args.get('key', "")
url = request.args.get('url', "")
password = request.args.get('pass', "")
if key != md5.md5(repo + app.config['SECRET_KEY'] * 20 + repo).hexdigest():
abort(403)
if repo in repos:
abort(403)
m = re.search(
r'^https://(github\.com|git\.coding\.net)/\w+/(\w+)\.git$', url)
if not m:
abort(403)
if m.group(2) != repo:
abort(403)
repos[repo] = {
"url": url,
"pass": md5.md5(password + app.config['SECRET_KEY']).hexdigest()
}
open('repos.json', 'wb').write(json.dumps(repos))
return "OK"
if __name__ == "__main__":
webhooklog.info('started web server...')
app.run(host='0.0.0.0', port=8000, threaded=True)