-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproxy.php
137 lines (108 loc) · 4.11 KB
/
proxy.php
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
<?php
$start_time = microtime(true);
$action = $_GET['action'] ?? null;
$repo = $_GET['repo'] ?? null;
$branch = $_GET['branch'] ?? null;
$directory = $_GET['directory'] ?? null;
$debug = $_GET['debug'] ?? false;
$debug_log = '';
$checkouts_dir = __DIR__ . '/checkouts';
$logs_dir = __DIR__ . '/logs';
if (!file_exists($logs_dir)) {
mkdir($logs_dir, 0755, true);
}
if (!$repo) {
http_response_code(400);
die('Repo required');
}
$repo_name = explode('/', $repo);
$repo_name = end($repo_name);
if (!$branch) {
$branch = getDefaultBranch($repo);
if (!$branch) {
http_response_code(400);
die('Invalid default branch');
}
}
if ($debug) { $debug_log .= "Branch: $branch\n";}
$repo_filename = "$repo_name-$branch";
if ($debug) { $debug_log .= "Repo name: $repo_name\n";}
if ($debug) { $debug_log .= "Repo filename: $repo_filename\n";}
$unique_id = uniqid($action . '_' . str_replace('/', '-', $repo) . '_' . str_replace('/', '-', $branch) . '_' . str_replace('/', '-', $directory) . '_');
if ($debug) { $debug_log .= "Unique id: $unique_id\n";}
switch ($action) {
case 'archive':
$url = "https://github.com/$repo/archive/$branch.zip";
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $repo_filename . '.zip"');
header('Pragma: no-cache');
readfile($url);
break;
case 'partial':
if (!$directory) {
http_response_code(400);
die('Directory required for partial action');
}
if (!file_exists($checkouts_dir)) {
mkdir($checkouts_dir, 0755, true);
}
$temp_checkout_dir = $checkouts_dir . '/' . $unique_id;
if ($debug) { $debug_log .= "Temp dir: $temp_checkout_dir\n";}
mkdir($temp_checkout_dir);
chdir($temp_checkout_dir);
$github_repo_url = "https://github.com/$repo.git";
if ($debug) { $debug_log .= "Github repo url: $github_repo_url\n";}
exec("git clone --depth 1 --filter=blob:none --sparse --no-checkout --branch $branch $github_repo_url");
if ($debug) {
$debug_log .= "git clone --depth 1 --filter=blob:none --sparse --no-checkout --branch $branch $github_repo_url\n";
}
chdir($repo_name);
if ($debug) { $debug_log .= "cd $repo_name\n";}
exec("git config core.sparseCheckout true");
if ($debug) { $debug_log .= "git config core.sparseCheckout true\n";}
// Set only the directory we need
exec("git sparse-checkout set $directory --no-cone");
if ($debug) { $debug_log .= "git sparse-checkout set $directory --no-cone\n";}
exec("git read-tree -mu HEAD");
if ($debug) { $debug_log .= "git read-tree -mu HEAD\n";}
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $repo_filename . '.zip"');
header('Pragma: no-cache');
$command = "zip -r - $directory";
if ($debug) { $debug_log .= "Zipping file: $command\n";}
passthru($command);
chdir($checkouts_dir);
if (!$debug) {
exec("rm -rf $temp_checkout_dir");
}
break;
default:
header('Location: /');
exit;
}
$elapsed_time = (microtime(true) - $start_time) * 1000;
if ($debug) {
$debug_log .= "Total time: $elapsed_time ms\n";
file_put_contents("$logs_dir/$unique_id.log", $debug_log);
}
$usage = date('Y/m/d g:i:s a') . " [$elapsed_time ms] " . print_r($_GET, true) . "\n";
file_put_contents("$logs_dir/usage.log", $usage, FILE_APPEND);
function getDefaultBranch($repo) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.github.com/repos/$repo");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
$response = curl_exec($ch);
curl_close($ch);
if ($response) {
$data = json_decode($response, true);
$default_branch = $data['default_branch'] ?? 'unknown';
return $default_branch;
} else {
return false;
}
}