-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGitServer.js
70 lines (61 loc) · 2.32 KB
/
GitServer.js
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
import { GitProxy } from "./GitProxy.js";
import { existsDirSync, fetchJSON } from "./WebUtil.js";
import { serve } from "https://deno.land/[email protected]/http/server.ts";
const settings = await fetchJSON("settings.json");
const REPO_PATH = settings?.repoPath || "repo/";
const IP_WHITELIST = settings?.ipWhiteList || { "::1": { "name": "me" }};
const PORT = settings?.port || 7005;
const createResponse = (text, ctype = "text/plain; charset=utf-8") => {
return new Response(text, {
status: 200,
headers: new Headers({
"Content-Type": ctype,
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type, Accept",
// "Access-Control-Allow-Methods": "PUT, DELETE, PATCH",
"Connection": "close",
}),
});
};
const createResponseGitAdvertise = (text, service, type) => {
text = new TextDecoder().decode(text);
const code = service == "git-upload-pack" ? "001e" : "001f";
return createResponse(
code + "# service=" + service + "\n0000" + text,
"application/x-" + service + "-" + type,
);
}
const createResponseGit = (text, service, type) => {
return createResponse(text, "application/x-" + service + "-" + type);
};
const hostname = "::";
serve(async (req, conn) => {
const url = new URL(req.url);
req.path = url.pathname;
req.param = url.searchParams;
req.remoteAddr = conn.remoteAddr.hostname;
const name = IP_WHITELIST[req.remoteAddr];
console.log("access from " + JSON.stringify(name) + " - req.remoteAddr " + req.remoteAddr);
if (!name) {
return null;
}
//console.log(req);
const n = req.path.indexOf("/", 1);
const repo = req.path.substring(1, n);
if (repo.indexOf("..") >= 0) {
return null;
}
const service = req.path.substring(n + 1);
if (service == "info/refs") {
const pservice = req.param.get("service");
console.log(REPO_PATH + repo)
if (pservice == "git-receive-pack" && !existsDirSync(REPO_PATH + repo)) {
await GitProxy.init(REPO_PATH + repo);
}
const res = await GitProxy.service(pservice, true, REPO_PATH + repo);
return createResponseGitAdvertise(res, pservice, "advertisement");
}
const text = new Uint8Array(await req.arrayBuffer());
const res = await GitProxy.service(service, false, REPO_PATH + repo, text);
return createResponseGit(res, service, "result");
}, { port: PORT, hostname });