2022-09-26 18:49:41 +02:00
|
|
|
// ignore_for_file: depend_on_referenced_packages
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
2022-09-30 23:33:57 +02:00
|
|
|
import 'package:adguard_home_manager/models/logs.dart';
|
2022-10-01 03:13:50 +02:00
|
|
|
import 'package:adguard_home_manager/models/filtering_status.dart';
|
2022-09-30 01:23:28 +02:00
|
|
|
import 'package:adguard_home_manager/models/app_log.dart';
|
2022-09-27 12:43:25 +02:00
|
|
|
import 'package:adguard_home_manager/models/server_status.dart';
|
2022-09-28 15:47:43 +02:00
|
|
|
import 'package:adguard_home_manager/models/clients.dart';
|
2022-09-29 00:13:54 +02:00
|
|
|
import 'package:adguard_home_manager/models/clients_allowed_blocked.dart';
|
2022-09-26 18:49:41 +02:00
|
|
|
import 'package:adguard_home_manager/models/server.dart';
|
|
|
|
|
2022-09-27 22:49:58 +02:00
|
|
|
|
2022-10-03 16:22:17 +02:00
|
|
|
Future<Map<String, dynamic>> apiRequest({
|
|
|
|
required Server server,
|
|
|
|
required String method,
|
|
|
|
required String urlPath,
|
2022-10-03 22:25:20 +02:00
|
|
|
Map<String, dynamic>? body
|
2022-10-03 16:22:17 +02:00
|
|
|
}) async {
|
2022-09-26 18:49:41 +02:00
|
|
|
try {
|
2022-10-03 16:22:17 +02:00
|
|
|
HttpClient httpClient = HttpClient();
|
|
|
|
if (method == 'get') {
|
|
|
|
HttpClientRequest request = await httpClient.getUrl(Uri.parse("${server.connectionMethod}://${server.domain}${server.path ?? ""}${server.port != null ? ':${server.port}' : ""}/control$urlPath"));
|
2022-10-03 22:25:20 +02:00
|
|
|
request.headers.set('authorization', 'Basic ${server.authToken}');
|
2022-10-03 16:22:17 +02:00
|
|
|
HttpClientResponse response = await request.close();
|
2022-10-03 22:25:20 +02:00
|
|
|
response.timeout(const Duration(seconds: 10));
|
2022-10-03 16:22:17 +02:00
|
|
|
String reply = await response.transform(utf8.decoder).join();
|
|
|
|
httpClient.close();
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
return {
|
|
|
|
'hasResponse': true,
|
|
|
|
'error': false,
|
|
|
|
'statusCode': response.statusCode,
|
|
|
|
'body': reply
|
|
|
|
};
|
2022-09-30 02:24:25 +02:00
|
|
|
}
|
2022-10-03 16:22:17 +02:00
|
|
|
else {
|
|
|
|
return {
|
|
|
|
'hasResponse': true,
|
|
|
|
'error': true,
|
|
|
|
'statusCode': response.statusCode,
|
|
|
|
'body': reply
|
|
|
|
};
|
|
|
|
}
|
2022-09-26 18:49:41 +02:00
|
|
|
}
|
2022-10-03 16:22:17 +02:00
|
|
|
else if (method == 'post') {
|
|
|
|
HttpClientRequest request = await httpClient.postUrl(Uri.parse("${server.connectionMethod}://${server.domain}${server.path ?? ""}${server.port != null ? ':${server.port}' : ""}/control$urlPath"));
|
2022-10-03 22:25:20 +02:00
|
|
|
request.headers.set('authorization', 'Basic ${server.authToken}');
|
2022-10-03 17:11:22 +02:00
|
|
|
request.headers.set('content-type', 'application/json');
|
2022-10-03 17:56:01 +02:00
|
|
|
request.add(utf8.encode(json.encode(body)));
|
2022-10-03 16:22:17 +02:00
|
|
|
HttpClientResponse response = await request.close();
|
2022-10-03 22:25:20 +02:00
|
|
|
response.timeout(const Duration(seconds: 10));
|
2022-10-03 16:22:17 +02:00
|
|
|
String reply = await response.transform(utf8.decoder).join();
|
|
|
|
httpClient.close();
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
return {
|
|
|
|
'hasResponse': true,
|
|
|
|
'error': false,
|
|
|
|
'statusCode': response.statusCode,
|
|
|
|
'body': reply
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
'hasResponse': true,
|
|
|
|
'error': true,
|
|
|
|
'statusCode': response.statusCode,
|
|
|
|
'body': reply
|
|
|
|
};
|
|
|
|
}
|
2022-09-26 18:49:41 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-10-03 16:22:17 +02:00
|
|
|
throw Exception('Method is required');
|
2022-09-26 18:49:41 +02:00
|
|
|
}
|
|
|
|
} on SocketException {
|
2022-09-30 01:23:28 +02:00
|
|
|
return {
|
|
|
|
'result': 'no_connection',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'SocketException'
|
|
|
|
)
|
|
|
|
};
|
2022-09-26 18:49:41 +02:00
|
|
|
} on TimeoutException {
|
2022-09-30 01:23:28 +02:00
|
|
|
return {
|
|
|
|
'result': 'no_connection',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'TimeoutException'
|
|
|
|
)
|
|
|
|
};
|
2022-09-26 18:49:41 +02:00
|
|
|
} on HandshakeException {
|
2022-09-30 01:23:28 +02:00
|
|
|
return {
|
|
|
|
'result': 'ssl_error',
|
|
|
|
'message': 'HandshakeException',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'TimeoutException'
|
|
|
|
)
|
|
|
|
};
|
2022-09-26 18:49:41 +02:00
|
|
|
} catch (e) {
|
2022-09-30 01:23:28 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: e.toString()
|
|
|
|
)
|
|
|
|
};
|
2022-09-26 18:49:41 +02:00
|
|
|
}
|
2022-09-27 02:38:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:22:17 +02:00
|
|
|
Future login(Server server) async {
|
|
|
|
final result = await apiRequest(
|
|
|
|
server: server,
|
|
|
|
method: 'post',
|
|
|
|
urlPath: '/login',
|
2022-10-03 17:56:01 +02:00
|
|
|
body: {
|
2022-10-03 16:22:17 +02:00
|
|
|
"name": server.user,
|
|
|
|
"password": server.password
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
|
|
|
else if (result['statusCode'] == 400) {
|
|
|
|
return {
|
|
|
|
'result': 'invalid_username_password',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'invalid_username_password',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (result['statusCode'] == 429) {
|
|
|
|
return {
|
|
|
|
'result': 'many_attempts',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'many_attempts',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 12:43:25 +02:00
|
|
|
Future getServerStatus(Server server) async {
|
2022-10-03 22:25:20 +02:00
|
|
|
final result = await Future.wait([
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/stats'),
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/status'),
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/filtering/status'),
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/safesearch/status'),
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/safebrowsing/status'),
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/parental/status'),
|
|
|
|
]);
|
2022-09-27 12:43:25 +02:00
|
|
|
|
2022-10-03 22:25:20 +02:00
|
|
|
if (
|
|
|
|
result[0]['hasResponse'] == true &&
|
|
|
|
result[1]['hasResponse'] == true &&
|
|
|
|
result[2]['hasResponse'] == true &&
|
|
|
|
result[3]['hasResponse'] == true &&
|
|
|
|
result[4]['hasResponse'] == true &&
|
|
|
|
result[5]['hasResponse'] == true
|
|
|
|
) {
|
2022-09-27 12:43:25 +02:00
|
|
|
if (
|
2022-10-03 22:25:20 +02:00
|
|
|
result[0]['statusCode'] == 200 &&
|
|
|
|
result[1]['statusCode'] == 200 &&
|
|
|
|
result[2]['statusCode'] == 200 &&
|
|
|
|
result[3]['statusCode'] == 200 &&
|
|
|
|
result[4]['statusCode'] == 200 &&
|
|
|
|
result[5]['statusCode'] == 200
|
2022-09-27 12:43:25 +02:00
|
|
|
) {
|
|
|
|
final Map<String, dynamic> mappedData = {
|
2022-10-03 22:25:20 +02:00
|
|
|
'stats': jsonDecode(result[0]['body']),
|
|
|
|
'generalEnabled': jsonDecode(result[1]['body']),
|
|
|
|
'filteringEnabled': jsonDecode(result[2]['body']),
|
|
|
|
'safeSearchEnabled': jsonDecode(result[3]['body']),
|
|
|
|
'safeBrowsingEnabled': jsonDecode(result[4]['body']),
|
|
|
|
'parentalControlEnabled': jsonDecode(result[5]['body']),
|
2022-09-27 12:43:25 +02:00
|
|
|
};
|
2022-09-27 02:38:59 +02:00
|
|
|
return {
|
|
|
|
'result': 'success',
|
2022-09-27 14:29:36 +02:00
|
|
|
'data': ServerStatusData.fromJson(mappedData)
|
2022-09-27 02:38:59 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 22:25:20 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'get_server_status',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result.map((res) => res['statusCode']).toString(),
|
|
|
|
resBody: result.map((res) => res['body']).toString()
|
|
|
|
)
|
|
|
|
};
|
2022-09-27 02:38:59 +02:00
|
|
|
}
|
2022-10-03 22:25:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'get_server_status',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'no_response',
|
|
|
|
statusCode: result.map((res) => res['statusCode'] ?? 'null').toString(),
|
|
|
|
resBody: result.map((res) => res['body'] ?? 'null').toString()
|
|
|
|
)
|
|
|
|
};
|
2022-09-27 02:38:59 +02:00
|
|
|
}
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future updateFiltering(Server server, bool enable) async {
|
2022-10-03 16:22:17 +02:00
|
|
|
final result = await apiRequest(
|
|
|
|
urlPath: '/filtering/config',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
2022-10-03 17:56:01 +02:00
|
|
|
body: {
|
2022-10-03 16:22:17 +02:00
|
|
|
'enabled': enable
|
2022-10-03 22:25:20 +02:00
|
|
|
}
|
2022-10-03 16:22:17 +02:00
|
|
|
);
|
2022-09-27 22:49:58 +02:00
|
|
|
|
2022-10-03 16:22:17 +02:00
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-09-27 22:49:58 +02:00
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 16:22:17 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
2022-10-03 16:22:17 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future updateSafeSearch(Server server, bool enable) async {
|
2022-10-03 16:22:17 +02:00
|
|
|
final result = enable == true
|
|
|
|
? await apiRequest(
|
|
|
|
urlPath: '/safesearch/enable',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
|
|
|
)
|
|
|
|
: await apiRequest(
|
|
|
|
urlPath: '/safesearch/disable',
|
|
|
|
method: 'post',
|
2022-10-03 22:25:20 +02:00
|
|
|
server: server,
|
2022-10-03 16:22:17 +02:00
|
|
|
);
|
2022-09-27 22:49:58 +02:00
|
|
|
|
2022-10-03 17:11:22 +02:00
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-09-27 22:49:58 +02:00
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 16:22:17 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
2022-10-03 16:22:17 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future updateSafeBrowsing(Server server, bool enable) async {
|
2022-10-03 16:22:17 +02:00
|
|
|
final result = enable == true
|
|
|
|
? await apiRequest(
|
|
|
|
urlPath: '/safebrowsing/enable',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
|
|
|
)
|
|
|
|
: await apiRequest(
|
|
|
|
urlPath: '/safebrowsing/disable',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
|
|
|
);
|
2022-10-03 17:56:01 +02:00
|
|
|
|
2022-10-03 17:11:22 +02:00
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-09-27 22:49:58 +02:00
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 16:22:17 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
2022-10-03 16:22:17 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future updateParentalControl(Server server, bool enable) async {
|
2022-10-03 16:22:17 +02:00
|
|
|
final result = enable == true
|
|
|
|
? await apiRequest(
|
|
|
|
urlPath: '/parental/enable',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
|
|
|
)
|
|
|
|
: await apiRequest(
|
|
|
|
urlPath: '/parental/disable',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
|
|
|
);
|
2022-10-03 17:56:01 +02:00
|
|
|
|
2022-10-03 17:11:22 +02:00
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-09-27 22:49:58 +02:00
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 16:22:17 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
2022-10-03 16:22:17 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future updateGeneralProtection(Server server, bool enable) async {
|
2022-10-03 17:11:22 +02:00
|
|
|
final result = await apiRequest(
|
|
|
|
urlPath: '/dns_config',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
2022-10-03 17:56:01 +02:00
|
|
|
body: {
|
2022-10-03 17:11:22 +02:00
|
|
|
'protection_enabled': enable
|
|
|
|
},
|
|
|
|
);
|
2022-09-27 22:49:58 +02:00
|
|
|
|
2022-10-03 17:11:22 +02:00
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-09-27 22:49:58 +02:00
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 17:11:22 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
2022-10-03 17:11:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-09-27 22:49:58 +02:00
|
|
|
}
|
2022-09-28 15:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future getClients(Server server) async {
|
2022-10-03 22:25:20 +02:00
|
|
|
final result = await Future.wait([
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/clients'),
|
|
|
|
apiRequest(server: server, method: 'get', urlPath: '/access/list'),
|
|
|
|
]);
|
2022-09-28 15:47:43 +02:00
|
|
|
|
2022-10-03 22:25:20 +02:00
|
|
|
if (result[0]['hasResponse'] == true && result[1]['hasResponse'] == true) {
|
|
|
|
if (result[0]['statusCode'] == 200 && result[1]['statusCode'] == 200) {
|
|
|
|
final clients = ClientsData.fromJson(jsonDecode(result[0]['body']));
|
|
|
|
clients.clientsAllowedBlocked = ClientsAllowedBlocked.fromJson(jsonDecode(result[1]['body']));
|
2022-09-28 15:47:43 +02:00
|
|
|
return {
|
|
|
|
'result': 'success',
|
2022-09-29 00:13:54 +02:00
|
|
|
'data': clients
|
2022-09-28 15:47:43 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
2022-10-03 22:25:20 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'get_clients',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result.map((res) => res['statusCode'] ?? 'null').toString(),
|
|
|
|
resBody: result.map((res) => res['body'] ?? 'null').toString(),
|
|
|
|
)
|
|
|
|
};
|
2022-09-28 15:47:43 +02:00
|
|
|
}
|
2022-10-03 22:25:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'get_clients',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'no_response',
|
|
|
|
statusCode: result.map((res) => res['statusCode'] ?? 'null').toString(),
|
|
|
|
resBody: result.map((res) => res['body'] ?? 'null').toString(),
|
|
|
|
)
|
|
|
|
};
|
2022-09-28 15:47:43 +02:00
|
|
|
}
|
2022-09-29 21:26:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future requestAllowedBlockedClientsHosts(Server server, Map<String, List<String>?> body) async {
|
2022-10-03 17:11:22 +02:00
|
|
|
final result = await apiRequest(
|
|
|
|
urlPath: '/access/set',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
2022-10-03 17:56:01 +02:00
|
|
|
body: body,
|
2022-10-03 17:11:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-09-29 21:26:18 +02:00
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
2022-10-03 17:11:22 +02:00
|
|
|
if (result['statusCode'] == 400) {
|
2022-09-29 23:27:29 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'message': 'client_another_list'
|
|
|
|
};
|
|
|
|
}
|
2022-09-29 21:26:18 +02:00
|
|
|
else {
|
2022-10-03 17:11:22 +02:00
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'login',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
|
|
|
)
|
|
|
|
};
|
2022-09-29 21:26:18 +02:00
|
|
|
}
|
2022-10-03 17:11:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-09-29 21:26:18 +02:00
|
|
|
}
|
2022-09-30 23:33:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future getLogs({
|
|
|
|
required Server server,
|
|
|
|
required int count,
|
2022-10-02 03:58:02 +02:00
|
|
|
int? offset,
|
|
|
|
DateTime? olderThan,
|
2022-10-02 05:32:24 +02:00
|
|
|
String? responseStatus,
|
2022-10-02 13:55:41 +02:00
|
|
|
String? search
|
2022-09-30 23:33:57 +02:00
|
|
|
}) async {
|
2022-10-03 22:25:20 +02:00
|
|
|
final result = await apiRequest(
|
|
|
|
server: server,
|
|
|
|
method: 'get',
|
|
|
|
urlPath: '/querylog?limit=$count${offset != null ? '&offset=$offset' : ''}${olderThan != null ? '&older_than=${olderThan.toIso8601String()}' : ''}${responseStatus != null ? '&response_status=$responseStatus' : ''}${search != null ? '&search=$search' : ''}'
|
|
|
|
);
|
2022-09-30 23:33:57 +02:00
|
|
|
|
2022-10-03 22:25:20 +02:00
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-09-30 23:33:57 +02:00
|
|
|
return {
|
|
|
|
'result': 'success',
|
2022-10-03 22:25:20 +02:00
|
|
|
'data': LogsData.fromJson(jsonDecode(result['body']))
|
2022-09-30 23:33:57 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
|
|
|
type: 'logs',
|
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
2022-10-03 22:25:20 +02:00
|
|
|
statusCode: result['statusCode'].toString(),
|
|
|
|
resBody: result['body']
|
2022-09-30 23:33:57 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2022-10-03 22:25:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-09-30 23:33:57 +02:00
|
|
|
}
|
2022-10-01 03:13:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future getFilteringRules({
|
|
|
|
required Server server,
|
|
|
|
}) async {
|
2022-10-03 22:25:20 +02:00
|
|
|
final result = await apiRequest(
|
|
|
|
server: server,
|
|
|
|
method: 'get',
|
|
|
|
urlPath: '/filtering/status'
|
|
|
|
);
|
2022-10-01 03:13:50 +02:00
|
|
|
|
2022-10-03 22:25:20 +02:00
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-10-01 03:13:50 +02:00
|
|
|
return {
|
|
|
|
'result': 'success',
|
2022-10-03 22:25:20 +02:00
|
|
|
'data': FilteringStatus.fromJson(jsonDecode(result['body']))
|
2022-10-01 03:13:50 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
|
|
|
'result': 'error',
|
|
|
|
'log': AppLog(
|
2022-10-03 22:25:20 +02:00
|
|
|
type: 'logs',
|
2022-10-01 03:13:50 +02:00
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
2022-10-03 22:25:20 +02:00
|
|
|
statusCode: result['statusCode'].toString(),
|
|
|
|
resBody: result['body']
|
2022-10-01 03:13:50 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2022-10-03 22:25:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-10-01 03:13:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future postFilteringRules({
|
|
|
|
required Server server,
|
2022-10-03 17:56:01 +02:00
|
|
|
required Map<String, List<String>> data,
|
2022-10-01 03:13:50 +02:00
|
|
|
}) async {
|
2022-10-03 17:11:22 +02:00
|
|
|
final result = await apiRequest(
|
|
|
|
urlPath: '/filtering/set_rules',
|
|
|
|
method: 'post',
|
|
|
|
server: server,
|
2022-10-03 17:56:01 +02:00
|
|
|
body: data,
|
2022-10-03 17:11:22 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (result['hasResponse'] == true) {
|
|
|
|
if (result['statusCode'] == 200) {
|
2022-10-01 03:13:50 +02:00
|
|
|
return {'result': 'success'};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {
|
2022-10-03 17:11:22 +02:00
|
|
|
'result': 'error',
|
2022-10-01 03:13:50 +02:00
|
|
|
'log': AppLog(
|
2022-10-03 17:11:22 +02:00
|
|
|
type: 'login',
|
2022-10-01 03:13:50 +02:00
|
|
|
dateTime: DateTime.now(),
|
|
|
|
message: 'error_code_not_expected',
|
2022-10-03 17:11:22 +02:00
|
|
|
statusCode: result['statusCode'],
|
|
|
|
resBody: result['body']
|
2022-10-01 03:13:50 +02:00
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
2022-10-03 17:11:22 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return result;
|
2022-10-01 03:13:50 +02:00
|
|
|
}
|
2022-09-26 18:49:41 +02:00
|
|
|
}
|