2021-03-25 23:30:34 +00:00
|
|
|
import 'dart:async';
|
2021-01-21 21:01:42 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2021-01-21 07:35:38 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2021-03-25 23:30:34 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2021-08-29 13:54:28 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
2021-06-08 18:52:44 +00:00
|
|
|
import 'package:selfprivacy/logic/models/user.dart';
|
2021-01-21 07:35:38 +00:00
|
|
|
|
|
|
|
import 'api_map.dart';
|
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
class ServerApi extends ApiMap {
|
|
|
|
bool hasLoger;
|
|
|
|
bool isWithToken;
|
|
|
|
|
|
|
|
ServerApi({this.hasLoger = false, this.isWithToken = true});
|
|
|
|
|
|
|
|
BaseOptions get options {
|
|
|
|
var options = BaseOptions();
|
|
|
|
|
|
|
|
if (isWithToken) {
|
|
|
|
var cloudFlareDomain = getIt<ApiConfigModel>().cloudFlareDomain;
|
|
|
|
var domainName = cloudFlareDomain!.domainName;
|
|
|
|
|
|
|
|
options = BaseOptions(baseUrl: 'https://api.$domainName');
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
2021-01-21 21:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> isHttpServerWorking() async {
|
|
|
|
bool res;
|
|
|
|
Response response;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
|
|
|
var client = await getClient();
|
2021-01-21 21:01:42 +00:00
|
|
|
try {
|
2021-03-25 23:30:34 +00:00
|
|
|
response = await client.get('/serviceStatus');
|
2021-01-21 21:01:42 +00:00
|
|
|
res = response.statusCode == HttpStatus.ok;
|
|
|
|
} catch (e) {
|
|
|
|
res = false;
|
2021-01-21 07:35:38 +00:00
|
|
|
}
|
2021-03-26 13:38:39 +00:00
|
|
|
close(client);
|
2021-01-21 21:01:42 +00:00
|
|
|
return res;
|
2021-01-21 07:35:38 +00:00
|
|
|
}
|
2021-01-21 21:01:42 +00:00
|
|
|
|
2021-06-08 18:52:44 +00:00
|
|
|
Future<bool> createUser(User user) async {
|
|
|
|
bool res;
|
|
|
|
Response response;
|
|
|
|
|
|
|
|
var client = await getClient();
|
|
|
|
try {
|
2021-06-20 21:08:52 +00:00
|
|
|
response = await client.post(
|
|
|
|
'/createUser',
|
|
|
|
options: Options(
|
|
|
|
headers: {
|
2021-07-29 05:24:42 +00:00
|
|
|
"X-User": user.login,
|
|
|
|
"X-Password":
|
|
|
|
'\$6\$${user.hashPassword.salt}\$${user.hashPassword.hash}',
|
2021-06-20 21:08:52 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2021-06-08 18:52:44 +00:00
|
|
|
res = response.statusCode == HttpStatus.ok;
|
|
|
|
} catch (e) {
|
2021-06-20 21:08:52 +00:00
|
|
|
print(e);
|
2021-06-08 18:52:44 +00:00
|
|
|
res = false;
|
|
|
|
}
|
2021-06-20 21:08:52 +00:00
|
|
|
|
2021-06-08 18:52:44 +00:00
|
|
|
close(client);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
String get rootAddress =>
|
|
|
|
throw UnimplementedError('not used in with implementation');
|
2021-07-29 05:24:42 +00:00
|
|
|
|
|
|
|
Future<bool> apply() async {
|
|
|
|
bool res;
|
|
|
|
Response response;
|
|
|
|
|
|
|
|
var client = await getClient();
|
|
|
|
try {
|
|
|
|
response = await client.get(
|
|
|
|
'/apply',
|
|
|
|
);
|
|
|
|
|
|
|
|
res = response.statusCode == HttpStatus.ok;
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(client);
|
|
|
|
return res;
|
|
|
|
}
|
2021-01-21 21:01:42 +00:00
|
|
|
}
|
2021-08-29 13:54:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
extension UrlServerExt on ServiceTypes {
|
|
|
|
String get url {
|
|
|
|
switch (this) {
|
|
|
|
// case ServiceTypes.mail:
|
|
|
|
// return ''; // cannot be swithch off
|
|
|
|
// case ServiceTypes.messenger:
|
|
|
|
// return ''; // external service
|
|
|
|
// case ServiceTypes.video:
|
|
|
|
// return ''; // jeetsu meet not working
|
|
|
|
case ServiceTypes.passwordManager:
|
|
|
|
return 'bitwarden';
|
|
|
|
case ServiceTypes.cloud:
|
|
|
|
return 'nextcloud';
|
|
|
|
case ServiceTypes.socialNetwork:
|
|
|
|
return 'pleroma';
|
|
|
|
case ServiceTypes.git:
|
|
|
|
return 'gitea';
|
|
|
|
case ServiceTypes.vpn:
|
|
|
|
return 'ocserv';
|
|
|
|
default:
|
|
|
|
throw Exception('wrong state');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|