-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
180 lines (149 loc) · 4.81 KB
/
Copy pathmain.ts
File metadata and controls
180 lines (149 loc) · 4.81 KB
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
import { serveLandingPage } from "./landing.ts";
const GITHUB_HOSTS: Record<string, string> = {
"": "github.com",
"raw": "raw.githubusercontent.com",
"gist": "gist.github.com",
"api": "api.github.com",
"codeload": "codeload.github.com",
"releases": "github.com",
"objects": "objects.githubusercontent.com",
"avatars": "avatars.githubusercontent.com",
};
const HOP_BY_HOP_HEADERS = [
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
];
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers": "*",
"Access-Control-Max-Age": "86400",
};
function buildTargetUrl(pathname: string, search: string): string | null {
const path = pathname.startsWith("/") ? pathname.slice(1) : pathname;
if (!path) return null;
const segments = path.split("/");
const firstSegment = segments[0].toLowerCase();
let targetHost: string;
let targetPath: string;
if (firstSegment in GITHUB_HOSTS && firstSegment !== "") {
targetHost = GITHUB_HOSTS[firstSegment];
targetPath = segments.slice(1).join("/");
} else {
targetHost = GITHUB_HOSTS[""];
targetPath = path;
}
if (!targetPath) return null;
return `https://${targetHost}/${targetPath}${search}`;
}
async function proxyToGitHub(
request: Request,
targetUrl: string
): Promise<Response> {
const headers = new Headers();
for (const [key, value] of request.headers.entries()) {
const lowerKey = key.toLowerCase();
if (!HOP_BY_HOP_HEADERS.includes(lowerKey) && lowerKey !== "host") {
headers.set(key, value);
}
}
headers.set("Host", new URL(targetUrl).host);
headers.set("User-Agent", request.headers.get("User-Agent") || "GitPro/1.0");
headers.delete("Referer");
try {
const response = await fetch(targetUrl, {
method: request.method,
headers,
body: request.method !== "GET" && request.method !== "HEAD"
? request.body
: undefined,
redirect: "manual",
});
const responseHeaders = new Headers();
for (const [key, value] of response.headers.entries()) {
const lowerKey = key.toLowerCase();
if (!HOP_BY_HOP_HEADERS.includes(lowerKey)) {
responseHeaders.set(key, value);
}
}
for (const [key, value] of Object.entries(CORS_HEADERS)) {
responseHeaders.set(key, value);
}
if (response.status >= 300 && response.status < 400) {
const location = response.headers.get("location");
if (location) {
const rewrittenLocation = rewriteLocation(location);
responseHeaders.set("Location", rewrittenLocation);
}
}
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: responseHeaders,
});
} catch (error) {
console.error("Proxy error:", error);
return new Response(
JSON.stringify({ error: "Failed to fetch from GitHub", details: String(error) }),
{
status: 502,
headers: { "Content-Type": "application/json", ...CORS_HEADERS },
}
);
}
}
function rewriteLocation(location: string): string {
try {
const url = new URL(location);
const host = url.host;
const hostToPrefix: Record<string, string> = {
"github.com": "",
"raw.githubusercontent.com": "/raw",
"api.github.com": "/api",
"gist.github.com": "/gist",
"codeload.github.com": "/codeload",
"objects.githubusercontent.com": "/objects",
"avatars.githubusercontent.com": "/avatars",
};
if (host in hostToPrefix) {
const prefix = hostToPrefix[host];
return `${prefix}${url.pathname}${url.search}`;
}
return location;
} catch {
return location;
}
}
async function handler(request: Request): Promise<Response> {
const url = new URL(request.url);
const pathname = url.pathname;
if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: CORS_HEADERS });
}
if (pathname === "/" || pathname === "") {
return serveLandingPage();
}
if (pathname === "/favicon.ico") {
return new Response(null, { status: 204 });
}
if (pathname === "/health") {
return new Response(JSON.stringify({ status: "ok", service: "gitpro" }), {
headers: { "Content-Type": "application/json", ...CORS_HEADERS },
});
}
const targetUrl = buildTargetUrl(pathname, url.search);
if (!targetUrl) {
return new Response(
JSON.stringify({ error: "Invalid path. Use format: /user/repo or /raw/user/repo/file" }),
{ status: 400, headers: { "Content-Type": "application/json", ...CORS_HEADERS } }
);
}
return proxyToGitHub(request, targetUrl);
}
Deno.serve(handler);