aimb-x/gitd
repositories / aimb-x / gitd

gitd public

git clone https://git.169.58.159.131.nip.io/aimb-x/gitd.git
gitd/httpd.py 17964 bytes 1 lines
1
"""gitd — smart-HTTP git server plus the AIMB-X web UI.
2
3
Protocol surface (exactly what git clients expect):
4
GET /<owner>/<repo>.git/info/refs?service=git-upload-pack|git-receive-pack
5
POST /<owner>/<repo>.git/git-upload-pack
6
POST /<owner>/<repo>.git/git-receive-pack
7
8
Auth is HTTP Basic: any username, password = an `ag_usr_…` token (Bearer works for the
9
web UI and API). Public repositories are readable anonymously; writes always need the
10
owner's token. A repository is created on first push.
11
"""
12
from __future__ import annotations
13
14
import argparse
15
import base64
16
import binascii
17
import json
18
import os
19
import re
20
import shutil
21
import subprocess
22
import sys
23
import threading
24
import time
25
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
26
from pathlib import Path
27
from urllib.parse import parse_qs, quote, unquote, urlparse
28
29
from . import web
30
from .store import Store, dumps
31
32
GIT = shutil.which("git") or "/usr/bin/git"
33
DATA = Path(os.environ.get("GITD_DATA", "/var/lib/gitd")).resolve()
34
REPOS = DATA / "repos"
35
DB = Path(os.environ.get("GITD_DB", str(DATA / "gitd.kilat")))
36
ADMIN_TOKEN = os.environ.get("GITD_ADMIN_TOKEN", "")
37
DEFAULT_VIS = os.environ.get("GITD_DEFAULT_VISIBILITY", "private")
38
MAX_BODY = int(os.environ.get("GITD_MAX_BODY", str(512 * 1024 * 1024)))
39
NAME_RE = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
40
41
_store: Store | None = None
42
_lock = threading.Lock()
43
44
45
def store() -> Store:
46
global _store
47
with _lock:
48
if _store is None:
49
DATA.mkdir(parents=True, exist_ok=True)
50
REPOS.mkdir(parents=True, exist_ok=True)
51
_store = Store(DB)
52
return _store
53
54
55
def repo_dir(owner: str, name: str) -> Path:
56
return REPOS / owner / f"{name}.git"
57
58
59
def git(repo: Path, *args: str, timeout: int = 120) -> str:
60
r = subprocess.run([GIT, "-C", str(repo), *args], capture_output=True, text=True, timeout=timeout)
61
if r.returncode != 0:
62
raise RuntimeError(r.stderr.strip()[:300] or "git failed")
63
return r.stdout
64
65
66
def pkt(s: bytes) -> bytes:
67
return b"%04x" % (len(s) + 4) + s
68
69
70
class Handler(BaseHTTPRequestHandler):
71
server_version = "gitd/0.1"
72
protocol_version = "HTTP/1.1"
73
74
# ---------- helpers ----------
75
def _send(self, code: int, body: bytes, ctype: str = "text/plain; charset=utf-8",
76
extra: dict | None = None) -> None:
77
self.send_response(code)
78
self.send_header("Content-Type", ctype)
79
self.send_header("Content-Length", str(len(body)))
80
self.send_header("Cache-Control", "no-cache, max-age=0, must-revalidate")
81
for k, v in (extra or {}).items():
82
self.send_header(k, v)
83
self.end_headers()
84
if self.command != "HEAD":
85
self.wfile.write(body)
86
87
def _html(self, code: int, page: str) -> None:
88
self._send(code, page.encode(), "text/html; charset=utf-8")
89
90
def _json(self, code: int, obj) -> None:
91
self._send(code, json.dumps(obj).encode(), "application/json; charset=utf-8")
92
93
def _auth(self) -> dict | None:
94
h = self.headers.get("Authorization", "")
95
token = ""
96
if h.startswith("Basic "):
97
try:
98
raw = base64.b64decode(h[6:]).decode("utf-8", "replace")
99
token = raw.split(":", 1)[1] if ":" in raw else raw
100
except (binascii.Error, ValueError):
101
return None
102
elif h.startswith("Bearer "):
103
token = h[7:].strip()
104
if not token:
105
q = parse_qs(urlparse(self.path).query)
106
token = (q.get("token") or [""])[0]
107
return store().user_by_token(token) if token else None
108
109
def _is_admin(self) -> bool:
110
h = self.headers.get("Authorization", "")
111
token = ""
112
if h.startswith("Bearer "):
113
token = h[7:].strip()
114
q = parse_qs(urlparse(self.path).query)
115
token = token or (q.get("token") or [""])[0]
116
return bool(ADMIN_TOKEN) and token == ADMIN_TOKEN
117
118
def _base_url(self) -> str:
119
host = self.headers.get("X-Forwarded-Host") or self.headers.get("Host") or "localhost"
120
proto = self.headers.get("X-Forwarded-Proto") or ("http" if host.startswith("127.") else "https")
121
return f"{proto}://{host}"
122
123
def _can_read(self, repo: dict | None, user: dict | None) -> bool:
124
if not repo:
125
return False
126
if repo["visibility"] == "public":
127
return True
128
if self._is_admin():
129
return True
130
return bool(user) and user["name"] == repo["owner"]
131
132
def _can_write(self, owner: str, user: dict | None) -> bool:
133
if self._is_admin():
134
return True
135
return bool(user) and user["name"] == owner
136
137
def _read_body(self) -> bytes:
138
te = (self.headers.get("Transfer-Encoding") or "").lower()
139
if "chunked" in te:
140
out = bytearray()
141
while True:
142
line = self.rfile.readline().strip()
143
if not line:
144
break
145
try:
146
size = int(line.split(b";")[0], 16)
147
except ValueError:
148
raise ValueError("bad chunk")
149
if size == 0:
150
self.rfile.readline()
151
break
152
out += self.rfile.read(size)
153
self.rfile.readline()
154
return bytes(out)
155
n = int(self.headers.get("Content-Length") or 0)
156
if n > MAX_BODY:
157
raise ValueError("body too large")
158
return self.rfile.read(n) if n else b""
159
160
def log_message(self, fmt: str, *args) -> None:
161
sys.stdout.write("%s %s\n" % (time.strftime("%Y-%m-%dT%H:%M:%S"), fmt % args))
162
163
# ---------- routes ----------
164
def do_HEAD(self) -> None: # noqa: N802
165
self.do_GET()
166
167
def do_GET(self) -> None: # noqa: N802
168
u = urlparse(self.path)
169
q = parse_qs(u.query)
170
path = u.path
171
user = self._auth()
172
173
if path == "/healthz":
174
st = store().stats()
175
return self._json(200, {"ok": True, "service": "gitd", "repos": st["repos"],
176
"users": st["users"], "engine": store().k.stats()})
177
if path == "/static/app.css":
178
return self._send(200, web.CSS.encode(), "text/css; charset=utf-8")
179
180
m = re.match(r"^/([^/]+)/([^/]+?)\.git/info/refs$", path)
181
if m:
182
return self._advertise(m.group(1), m.group(2), (q.get("service") or [""])[0], user)
183
184
if path in ("/", "/repos"):
185
repos = []
186
for r in store().repos():
187
if not self._can_read(r, user):
188
continue
189
d = repo_dir(r["owner"], r["name"])
190
if d.exists():
191
try:
192
r = dict(r)
193
r["branches"] = len(git(d, "for-each-ref", "--format=%(refname)", "refs/heads")
194
.splitlines())
195
r["last_commit"] = git(d, "log", "-1", "--format=%s", "--all").strip()[:60]
196
except Exception:
197
r["branches"] = 0
198
repos.append(r)
199
return self._html(200, web.page_repos(repos, store().stats(),
200
user["name"] if user else None))
201
202
if path == "/about":
203
return self._html(200, web.page_about())
204
205
m = re.match(r"^/u/([^/]+)$", path)
206
if m:
207
owner = unquote(m.group(1))
208
repos = [r for r in store().repos() if r["owner"] == owner and self._can_read(r, user)]
209
st = {"users": store().stats()["users"], "repos": len(repos)}
210
return self._html(200, web.page_repos(repos, st, owner))
211
212
m = re.match(r"^/r/([^/]+)/([^/]+)$", path)
213
if m:
214
return self._repo_page(unquote(m.group(1)), unquote(m.group(2)),
215
(q.get("ref") or [""])[0], (q.get("path") or [""])[0], user)
216
217
return self._html(404, web.page_404())
218
219
def do_POST(self) -> None: # noqa: N802
220
path = urlparse(self.path).path
221
if path == "/admin/repo":
222
return self._admin_repo()
223
m = re.match(r"^/([^/]+)/([^/]+?)\.git/(git-upload-pack|git-receive-pack)$", path)
224
if not m:
225
return self._json(404, {"error": "not found"})
226
return self._rpc(m.group(1), m.group(2), m.group(3))
227
228
def _admin_repo(self) -> None:
229
"""Metadata writes must happen inside this process: kilat is single-writer."""
230
if not self._is_admin():
231
return self._json(403, {"error": "admin token required"})
232
try:
233
body = json.loads(self._read_body() or b"{}")
234
owner, name = body["owner"], body["name"]
235
except Exception as e:
236
return self._json(400, {"error": f"bad request: {e}"})
237
row = store().add_repo(owner, name, body.get("visibility") or
238
(store().get_repo(owner, name) or {}).get("visibility", DEFAULT_VIS),
239
body.get("description", ""))
240
self._json(200, row)
241
242
# ---------- git smart HTTP ----------
243
def _advertise(self, owner: str, name: str, service: str, user: dict | None) -> None:
244
if not NAME_RE.match(owner) or not NAME_RE.match(name):
245
return self._send(400, b"bad repository name")
246
if service not in ("git-upload-pack", "git-receive-pack"):
247
return self._send(400, b"bad service")
248
repo = store().get_repo(owner, name)
249
d = repo_dir(owner, name)
250
251
if service == "git-receive-pack":
252
if not self._can_write(owner, user):
253
return self._send(401, b"authentication required\n",
254
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
255
if not d.exists():
256
# create-on-first-push, like a git host that has no web form
257
d.mkdir(parents=True, exist_ok=True)
258
subprocess.run([GIT, "init", "--bare", "--quiet", str(d)], check=True)
259
if not store().get_repo(owner, name): # jangan timpa visibility yang sudah diatur
260
store().add_repo(owner, name, DEFAULT_VIS)
261
else:
262
if not self._can_read(repo, user):
263
return self._send(401, b"authentication required\n",
264
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
265
if not d.exists():
266
return self._send(404, b"repository not found\n")
267
268
env = os.environ.copy()
269
proto = self.headers.get("Git-Protocol")
270
if proto:
271
env["GIT_PROTOCOL"] = proto
272
if user:
273
env["REMOTE_USER"] = user["name"]
274
sub = service[4:] if service.startswith("git-") else service # "git receive-pack"
275
out = subprocess.run([GIT, sub, "--stateless-rpc", "--advertise-refs", str(d)],
276
capture_output=True, env=env, timeout=120)
277
if out.returncode != 0:
278
return self._send(500, out.stderr[:400])
279
body = pkt(f"# service={service}\n".encode()) + b"0000" + out.stdout
280
self._send(200, body, f"application/x-{service}-advertisement")
281
282
def _rpc(self, owner: str, name: str, service: str) -> None:
283
user = self._auth()
284
d = repo_dir(owner, name)
285
if not d.exists():
286
return self._send(404, b"repository not found\n")
287
if service == "git-receive-pack" and not self._can_write(owner, user):
288
return self._send(401, b"authentication required\n",
289
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
290
if service == "git-upload-pack" and not self._can_read(store().get_repo(owner, name), user):
291
return self._send(401, b"authentication required\n",
292
extra={"WWW-Authenticate": 'Basic realm="gitd"'})
293
try:
294
body = self._read_body()
295
except ValueError as e:
296
return self._send(413, str(e).encode())
297
298
env = os.environ.copy()
299
proto = self.headers.get("Git-Protocol")
300
if proto:
301
env["GIT_PROTOCOL"] = proto
302
if user:
303
env["REMOTE_USER"] = user["name"]
304
sub = service[4:] if service.startswith("git-") else service
305
out = subprocess.run([GIT, sub, "--stateless-rpc", str(d)], input=body,
306
capture_output=True, env=env, timeout=600)
307
if out.returncode != 0:
308
self.log_message("git %s failed: %s", service, out.stderr.decode("utf-8", "replace")[:300])
309
return self._send(500, out.stderr[:400])
310
if service == "git-receive-pack":
311
_fix_head(d)
312
repo = store().get_repo(owner, name) or {}
313
repo["last_push"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
314
store().k.put("repos", f"{owner}/{name}", dumps(repo))
315
self._send(200, out.stdout, f"application/x-{service}-result")
316
317
# ---------- web repo page ----------
318
def _repo_page(self, owner: str, name: str, ref: str, path: str, user: dict | None) -> None:
319
repo = store().get_repo(owner, name)
320
d = repo_dir(owner, name)
321
if not repo or not d.exists() or not self._can_read(repo, user):
322
return self._html(404, web.page_404())
323
try:
324
branches = []
325
for line in git(d, "for-each-ref", "--format=%(refname:short)", "refs/heads").splitlines():
326
if not line:
327
continue
328
sha = git(d, "rev-parse", "--short", line).strip()
329
subj = git(d, "log", "-1", "--format=%s", line).strip()[:60]
330
branches.append({"name": line, "sha": sha, "subject": subj})
331
ref = ref or (branches[0]["name"] if branches else "")
332
commits = []
333
if ref:
334
for line in git(d, "log", "-12", "--format=%h%x09%an%x09%ad%x09%s",
335
"--date=iso-strict", ref).splitlines():
336
sha, author, date, subj = (line.split("\t") + ["", "", "", ""])[:4]
337
commits.append({"sha": sha, "author": author, "date": date, "subject": subj})
338
tree, blob = [], None
339
if ref:
340
target = f"{ref}:{path}" if path else ref
341
kind = subprocess.run([GIT, "-C", str(d), "cat-file", "-t", target],
342
capture_output=True, text=True).stdout.strip()
343
if path and kind == "blob":
344
raw = subprocess.run([GIT, "-C", str(d), "cat-file", "-p", target],
345
capture_output=True, timeout=30)
346
if raw.returncode == 0 and len(raw.stdout) < 400_000 and b"\x00" not in raw.stdout[:2048]:
347
blob = {"path": path, "bytes": len(raw.stdout),
348
"text": raw.stdout.decode("utf-8", "replace")}
349
parent = path.rsplit("/", 1)[0] if "/" in path else ""
350
lsref = f"{ref}:{parent}" if parent else ref
351
for line in git(d, "ls-tree", "-l", lsref).splitlines():
352
meta, fname = line.split("\t", 1)
353
mode, typ, obj, size = meta.split()[:4]
354
tree.append({"path": f"{parent}/{fname}" if parent else fname,
355
"type": "tree" if typ == "tree" else "blob",
356
"size_h": "" if typ == "tree" else _human(int(size or 0))})
357
elif kind == "tree" or not path:
358
for line in git(d, "ls-tree", "-l", target).splitlines():
359
meta, fname = line.split("\t", 1)
360
mode, typ, obj, size = meta.split()[:4]
361
tree.append({"path": f"{path}/{fname}" if path else fname,
362
"type": "tree" if typ == "tree" else "blob",
363
"size_h": "" if typ == "tree" else _human(int(size or 0))})
364
except RuntimeError:
365
return self._html(500, web.page_404())
366
clone = f"{self._base_url()}/{quote(owner)}/{quote(name)}.git"
367
self._html(200, web.page_repo(repo, branches, commits, tree, ref, path, blob, clone))
368
369
370
def _fix_head(d: Path) -> None:
371
"""Setelah push, HEAD harus menunjuk branch yang benar-benar ada; kalau tidak,
372
`git clone` berhasil tetapi checkout kosong (bare repo default ke master)."""
373
try:
374
branches = [b for b in git(d, "for-each-ref", "--format=%(refname)", "refs/heads").splitlines() if b]
375
if not branches:
376
return
377
cur = subprocess.run([GIT, "-C", str(d), "symbolic-ref", "HEAD"],
378
capture_output=True, text=True).stdout.strip()
379
if cur not in branches:
380
381
subprocess.run([GIT, "-C", str(d), "symbolic-ref", "HEAD", branches[0]], check=True)
382
except Exception:
383
pass
384
385
386
def _human(n: int) -> str:
387
for unit in ("B", "KB", "MB"):
388
if n < 1024:
389
return f"{n:.0f} {unit}" if unit == "B" else f"{n:.1f} {unit}"
390
n /= 1024
391
return f"{n:.1f} GB"
392
393
394
def serve(host: str, port: int) -> None:
395
store()
396
httpd = ThreadingHTTPServer((host, port), Handler)
397
httpd.daemon_threads = True
398
print(f"gitd listening on http://{host}:{port} · repos {REPOS}")
399
try:
400
httpd.serve_forever()
401
except KeyboardInterrupt:
402
pass
403
finally:
404
httpd.server_close()
405
406
407
def main(argv=None) -> int:
408
ap = argparse.ArgumentParser(prog="gitd-server")
409
ap.add_argument("--host", default=os.environ.get("GITD_HOST", "127.0.0.1"))
410
ap.add_argument("--port", type=int, default=int(os.environ.get("GITD_PORT", "8189")))
411
a = ap.parse_args(argv)
412
serve(a.host, a.port)
413
return 0
414
415
416
if __name__ == "__main__":
417
raise SystemExit(main())