initial commit
This commit is contained in:
parent
39c67e77ec
commit
6490b8fef6
16 changed files with 1910 additions and 0 deletions
87
bin/main.dart
Normal file
87
bin/main.dart
Normal file
|
@ -0,0 +1,87 @@
|
|||
import "dart:io";
|
||||
import "package:cli_tools/config.dart";
|
||||
import "package:http/http.dart" as http;
|
||||
import "package:lasuite_docs_proxy/models/settings.dart";
|
||||
import "package:shelf/shelf.dart";
|
||||
import "package:shelf/shelf_io.dart";
|
||||
import "package:shelf_router/shelf_router.dart";
|
||||
|
||||
void main(List<String> argsRaw) async {
|
||||
final parser = ConfigParser()
|
||||
..addOption("socket", abbr: "s")
|
||||
..addOption("authUri", abbr: "a")
|
||||
..addOption("minioUri", abbr: "m");
|
||||
|
||||
final args = parser.parse(argsRaw);
|
||||
final settings = Settings.fromJson(<String, dynamic>{
|
||||
for (final opt in args.options) opt: args.option(opt),
|
||||
});
|
||||
|
||||
final handler = const Pipeline()
|
||||
.addMiddleware(logRequests())
|
||||
.addHandler(
|
||||
(Router()..get("/media/<path|.*>", (
|
||||
Request request,
|
||||
String path,
|
||||
) async {
|
||||
final authResponse = await http.get(
|
||||
settings.authUri,
|
||||
headers: {
|
||||
if (request.headers['cookie'] != null)
|
||||
'cookie': request.headers['cookie']!,
|
||||
},
|
||||
);
|
||||
|
||||
if (authResponse.statusCode != 200) {
|
||||
return Response.internalServerError(
|
||||
body: {
|
||||
"error": "An error occurred: ${authResponse.statusCode}.",
|
||||
},
|
||||
headers: {HttpHeaders.contentTypeHeader: "application/json"},
|
||||
);
|
||||
}
|
||||
|
||||
final authHeaders = <String, String>{};
|
||||
for (var header in [
|
||||
'authorization',
|
||||
'x-amz-date',
|
||||
'x-amz-content-sha256',
|
||||
]) {
|
||||
final value = authResponse.headers[header];
|
||||
if (value != null) {
|
||||
authHeaders[header] = value;
|
||||
}
|
||||
}
|
||||
|
||||
final minioUrl = settings.minioUri.replace(
|
||||
path: "/lasuite-docs/$path",
|
||||
query: request.url.query,
|
||||
);
|
||||
|
||||
final minioResponse = await http.get(
|
||||
minioUrl,
|
||||
headers: {...authHeaders},
|
||||
);
|
||||
|
||||
return Response(
|
||||
minioResponse.statusCode,
|
||||
body: minioResponse.bodyBytes,
|
||||
headers: {
|
||||
...minioResponse.headers,
|
||||
'content-security-policy': "default-src 'none'",
|
||||
},
|
||||
);
|
||||
}))
|
||||
.call,
|
||||
);
|
||||
|
||||
final server = HttpServer.listenOn(
|
||||
await ServerSocket.bind(
|
||||
InternetAddress(settings.socket, type: InternetAddressType.unix),
|
||||
0,
|
||||
),
|
||||
);
|
||||
|
||||
serveRequests(server, handler);
|
||||
print("Proxy listening at ${server.address.address}");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue