Skip to content

Commit

Permalink
Fixed remaining requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JGeek00 committed Oct 3, 2022
1 parent 76c16df commit 0ad2cbe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 58 deletions.
70 changes: 36 additions & 34 deletions lib/screens/logs/log_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,48 @@ class LogTile extends StatelessWidget {
}

void blockUnblock(Log log, String newStatus) async {
FilteringStatus oldStatus = serversProvider.filteringStatus!;
final ProcessModal processModal = ProcessModal(context: context);
processModal.open(AppLocalizations.of(context)!.savingUserFilters);

List<String> newRules = serversProvider.filteringStatus!.userRules.where((domain) => !domain.contains(log.question.name)).toList();
if (newStatus == 'block') {
newRules.add("||${log.question.name}^");
}
else if (newStatus == 'unblock') {
newRules.add("@@||${log.question.name}^");
}
FilteringStatus newObj = serversProvider.filteringStatus!;
newObj.userRules = newRules;
serversProvider.setFilteringStatus(newObj);

String formattedFilters = "";
for (var rule in newObj.userRules) {
if (formattedFilters == "") {
formattedFilters = "$formattedFilters$rule";
final rules = await getFilteringRules(server: serversProvider.selectedServer!);

if (rules['result'] == 'success') {
FilteringStatus oldStatus = serversProvider.filteringStatus!;

List<String> newRules = rules['data'].userRules.where((domain) => !domain.contains(log.question.name)).toList();
if (newStatus == 'block') {
newRules.add("||${log.question.name}^");
}
else {
formattedFilters = "$formattedFilters\n$rule";
else if (newStatus == 'unblock') {
newRules.add("@@||${log.question.name}^");
}
}

final ProcessModal processModal = ProcessModal(context: context);
processModal.open(AppLocalizations.of(context)!.savingUserFilters);
FilteringStatus newObj = serversProvider.filteringStatus!;
newObj.userRules = newRules;
serversProvider.setFilteringStatus(newObj);

final result = await postFilteringRules(server: serversProvider.selectedServer!, data: formattedFilters);

processModal.close();

if (result['result'] == 'success') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.userFilteringRulesUpdated),
backgroundColor: Colors.green,
)
);
final result = await postFilteringRules(server: serversProvider.selectedServer!, data: {'rules': newRules});

processModal.close();

if (result['result'] == 'success') {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.userFilteringRulesUpdated),
backgroundColor: Colors.green,
)
);
}
else {
serversProvider.setFilteringStatus(oldStatus);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.userFilteringRulesNotUpdated),
backgroundColor: Colors.red,
)
);
}
}
else {
serversProvider.setFilteringStatus(oldStatus);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.userFilteringRulesNotUpdated),
Expand Down
34 changes: 10 additions & 24 deletions lib/services/http_requests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ Future<Map<String, dynamic>> apiRequest({
required Server server,
required String method,
required String urlPath,
Map<String, dynamic>? jsonBody,
String? stringBody,
Map<String, dynamic>? body,
bool? withAuth,
}) async {
try {
Expand Down Expand Up @@ -76,27 +75,14 @@ Future<Map<String, dynamic>> apiRequest({
}
else if (method == 'post') {
HttpClientRequest request = await httpClient.postUrl(Uri.parse("${server.connectionMethod}://${server.domain}${server.path ?? ""}${server.port != null ? ':${server.port}' : ""}/control$urlPath"));

if (withAuth != null && withAuth == true) {
request.headers.set('authorization', 'Basic ${server.authToken}');
}

request.headers.set('content-type', 'application/json');
if (jsonBody != null && stringBody == null) {
request.add(utf8.encode(json.encode(jsonBody)));
}
else if (jsonBody == null && stringBody != null) {
request.write(stringBody);
}
else if (jsonBody != null && stringBody != null) {
throw Exception("Don't add a jsonBody and a stringBody");
}

request.add(utf8.encode(json.encode(body)));
HttpClientResponse response = await request.close();
String reply = await response.transform(utf8.decoder).join();

httpClient.close();

if (response.statusCode == 200) {
return {
'hasResponse': true,
Expand Down Expand Up @@ -162,7 +148,7 @@ Future login(Server server) async {
server: server,
method: 'post',
urlPath: '/login',
jsonBody: {
body: {
"name": server.user,
"password": server.password
}
Expand Down Expand Up @@ -266,7 +252,7 @@ Future updateFiltering(Server server, bool enable) async {
urlPath: '/filtering/config',
method: 'post',
server: server,
jsonBody: {
body: {
'enabled': enable
},
withAuth: true
Expand Down Expand Up @@ -345,7 +331,7 @@ Future updateSafeBrowsing(Server server, bool enable) async {
server: server,
withAuth: true
);
print(result);

if (result['hasResponse'] == true) {
if (result['statusCode'] == 200) {
return {'result': 'success'};
Expand Down Expand Up @@ -382,7 +368,7 @@ Future updateParentalControl(Server server, bool enable) async {
server: server,
withAuth: true
);
print(result);

if (result['hasResponse'] == true) {
if (result['statusCode'] == 200) {
return {'result': 'success'};
Expand Down Expand Up @@ -410,7 +396,7 @@ Future updateGeneralProtection(Server server, bool enable) async {
urlPath: '/dns_config',
method: 'post',
server: server,
jsonBody: {
body: {
'protection_enabled': enable
},
withAuth: true
Expand Down Expand Up @@ -472,7 +458,7 @@ Future requestAllowedBlockedClientsHosts(Server server, Map<String, List<String>
urlPath: '/access/set',
method: 'post',
server: server,
jsonBody: body,
body: body,
withAuth: true
);

Expand Down Expand Up @@ -645,13 +631,13 @@ Future getFilteringRules({

Future postFilteringRules({
required Server server,
required String data,
required Map<String, List<String>> data,
}) async {
final result = await apiRequest(
urlPath: '/filtering/set_rules',
method: 'post',
server: server,
stringBody: data,
body: data,
withAuth: true
);

Expand Down

0 comments on commit 0ad2cbe

Please sign in to comment.