-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
97 lines (69 loc) · 3.51 KB
/
server.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
# "(.*?)/git-upload-pack$": Service{"POST", serviceRpc, "upload-pack"},
# "(.*?)/git-receive-pack$": Service{"POST", serviceRpc, "receive-pack"},
# "(.*?)/info/refs$": Service{"GET", getInfoRefs, ""},
# "(.*?)/HEAD$": Service{"GET", getTextFile, ""},
# "(.*?)/objects/info/alternates$": Service{"GET", getTextFile, ""},
# "(.*?)/objects/info/http-alternates$": Service{"GET", getTextFile, ""},
# "(.*?)/objects/info/packs$": Service{"GET", getInfoPacks, ""},
# "(.*?)/objects/info/[^/]*$": Service{"GET", getTextFile, ""},
# "(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$": Service{"GET", getLooseObject, ""},
# "(.*?)/objects/pack/pack-[0-9a-f]{40}\\.pack$": Service{"GET", getPackFile, ""},
# "(.*?)/objects/pack/pack-[0-9a-f]{40}\\.idx$": Service{"GET", getIdxFile, ""},
import os
from gevent import monkey
from gevent.pywsgi import WSGIServer
from git import git_command_with_input, git_command
monkey.patch_all()
from flask import request, make_response, current_app, abort, json, jsonify, Response, Flask
from flask_httpauth import HTTPBasicAuth
from config import PROJECTS_PATH, GIT_REPOS_PATH, GIT_PATH, SERVER_HOST, SERVER_PORT, USER_NAME, PASSWORD
auth = HTTPBasicAuth()
@auth.verify_password
def verify_pw(username, password):
return username == USER_NAME and password == PASSWORD
app = Flask(__name__, template_folder='../templates/')
@app.before_request
def git_before():
# print(request.path)
pass
@app.route('/<string:repo_name>/git-upload-pack', methods=['POST'])
@app.route('/<string:repo_name>.git/git-upload-pack', methods=['POST'])
def git_upload_pack(repo_name):
# print(request.headers.get('Git-Protocol'))
repo_name = repo_name + '.git'
args = ['upload-pack', "--stateless-rpc", '.']
res = git_command_with_input(repo_name, '', request.data, *args)
return Response(res)
@app.route('/<string:repo_name>/git-receive-pack', methods=['POST'])
@app.route('/<string:repo_name>.git/git-receive-pack', methods=['POST'])
@auth.login_required
def git_receive_pack(repo_name):
# push 操作需要验证
repo = repo_name + '.git'
old_version = request.headers.get('Git-Protocol')
args = ['receive-pack', "--stateless-rpc", '.']
res = git_command_with_input(repo, '', request.data, *args)
return Response(res)
@app.route('/<string:repo_name>/info/refs', methods=['GET'])
@app.route('/<string:repo_name>.git/info/refs', methods=['GET'])
def git_info_refs(repo_name):
repo_name = repo_name + '.git'
repo_path = os.path.join(GIT_REPOS_PATH, repo_name)
old_version = request.headers.get('Git-Protocol')
version = request.headers.get('git/2.17.1')
service = request.args.get('service')
if service and 'git-' in service:
service_name = service[4:]
else:
service_name = 'upload-pack'
if service_name == 'receive-pack' and not auth.username():
# push 操作需要验证
return auth.login_required(git_info_refs)(repo_name)
args = [service_name, "--stateless-rpc", "--advertise-refs", "."]
res = git_command(repo_name, version, *args)
first_line = '# service=git-%s\n0000' % service_name
first_line = ('%.4x' % len(first_line)) + first_line
resp = make_response(first_line + res.decode())
resp.headers['Content-Type'] = 'application/x-git-%s-advertisement' % service_name
return resp
WSGIServer((SERVER_HOST, SERVER_PORT), app).serve_forever()