This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.php
192 lines (143 loc) · 5.28 KB
/
app.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
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
181
182
183
184
185
186
187
188
189
190
191
192
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app['title'] = "Composer Proxy JP";
$app['base_url'] = "http://composer-proxy.jp/";
$app['repositories'] = array(
'packagist' => 'https://packagist.org'
);
$app['cache_dir'] = __DIR__.'/web/proxy';
# Read additional configs from a "config.ini" file.
# See config.ini.dist for an example.
$config_file = __DIR__ . '/config.ini';
if (is_file($config_file)) {
$config = parse_ini_file($config_file);
foreach ($config as $key => $value) {
if (is_array($value)) {
foreach ($value as $arr_key => $arr_value) {
$app[$key][$arr_key] = $arr_value;
}
} else {
$app[$key] = $value;
}
}
}
$app['browser'] = $app->share(function() {
$client = new Buzz\Client\Curl();
$client->setTimeout(20);
return new Buzz\Browser($client);
});
$app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
'http_cache.cache_dir' => __DIR__.'/cache/'
));
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views'
));
$app->get('/', function() use ($app) {
$body = $app['twig']->render('index.html.twig', array(
'app' => $app
));
return new Response($body, 200, array('Cache-Control' => 's-maxage=3600,public'));
});
$app->get('/proxy/{rep}/', function($rep) use ($app) {
return $app->redirect("/proxy/${rep}/packages.json");
});
$app->get('/proxy/{rep}/packages.json', function($rep) use ($app) {
if (!isset($app['repositories'][$rep])) {
$app->abort(404, "Not Found");
}
$url = $app['repositories'][$rep]."/packages.json";
$response = $app['browser']->get($url);
if (!$response->isOk()) {
$app->abort($response->getStatusCode(), "");
}
$responseJson = json_decode($response->getContent(), true);
// convert
if (isset($responseJson['notify']) && $responseJson['notify'][0] === '/') {
$responseJson['notify'] = $app['repositories'][$rep] . $responseJson['notify'];
}
if (isset($responseJson['notify-batch']) && $responseJson['notify-batch'][0] === '/') {
$responseJson['notify-batch'] = $app['repositories'][$rep] . $responseJson['notify-batch'];
}
if (isset($responseJson['search']) && $responseJson['search'][0] === '/') {
$responseJson['search'] = $app['repositories'][$rep] . $responseJson['search'];
}
$responseJson['providers-url'] = "/proxy/".$rep."/p/%package%$%hash%.json";
$dir = $app['cache_dir']."/".$rep;
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
file_put_contents($app['cache_dir']."/".$rep."/packages.json", json_encode($responseJson));
return $app->json($responseJson);
});
$app->get('/proxy/{rep}/p/{p}${hash}.json', function($rep, $p, $hash) use ($app) {
if (!isset($app['repositories'][$rep])) {
$app->abort(404, "Not Found");
}
$path = "/p/";
$file = $p."$".$hash.".json";
$url = $app['repositories'][$rep].$path.$file;
$response = $app['browser']->get($url);
if (!$response->isOk()) {
$app->abort($response->getStatusCode(), "");
}
$contents = $response->getContent();
$gethash = hash('sha256', $contents, false);
if ($gethash != $hash) {
$app->abort(500, "Cannot fetch file correctly.");
}
$responseJson = json_decode($contents, true);
$dir = $app['cache_dir']."/".$rep.$path;
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
file_put_contents($dir.$file, json_encode($responseJson));
return $app->json($responseJson);
});
$app->get('/proxy/{rep}/p/{namespace}/{package}${hash}.json', function($rep, $namespace, $package, $hash) use ($app) {
if (!isset($app['repositories'][$rep])) {
$app->abort(404, "Not Found");
}
$path = "/p/".$namespace."/";
$file = $package."$".$hash.".json";
$url = $app['repositories'][$rep].$path.$file;
$response = $app['browser']->get($url);
if (!$response->isOk()) {
$app->abort($response->getStatusCode(), "");
}
$contents = $response->getContent();
$gethash = hash('sha256', $contents, false);
// Check hash
if ($gethash != $hash) {
$app->abort(500, "Cannot fetch file correctly.");
}
$responseJson = json_decode($contents, true);
$dir = $app['cache_dir']."/".$rep.$path;
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
// Save cache
file_put_contents($dir.$file, json_encode($responseJson));
return $app->json($responseJson);
});
$app->get('/proxy/{rep}/p/{namespace}/{package}.json', function($rep, $namespace, $package) use ($app) {
if (!isset($app['repositories'][$rep])) {
$app->abort(404, "Not Found");
}
$path = "/p/".$namespace."/";
$file = $package.".json";
$url = $app['repositories'][$rep].$path.$file;
$response = $app['browser']->get($url);
if (!$response->isOk()) {
$app->abort($response->getStatusCode(), "");
}
$responseJson = json_decode($response->getContent(), true);
$dir = $app['cache_dir']."/".$rep.$path;
if (!is_dir($dir)) {
@mkdir($dir, 0777, true);
}
file_put_contents($dir.$file, json_encode($responseJson));
return $app->json($responseJson);
});
return $app;