2021-01-19 08:38:02 +00:00
|
|
|
import 'dart:convert';
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
2021-03-25 23:30:34 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/api_map.dart';
|
2021-03-26 13:38:39 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hetzner_server_info.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:selfprivacy/logic/models/server_details.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/user.dart';
|
2021-02-17 14:30:02 +00:00
|
|
|
import 'package:selfprivacy/utils/password_generator2.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
class HetznerApi extends ApiMap {
|
|
|
|
bool hasLoger;
|
|
|
|
bool isWithToken;
|
|
|
|
|
|
|
|
HetznerApi({this.hasLoger = false, this.isWithToken = true});
|
|
|
|
|
|
|
|
BaseOptions get options {
|
|
|
|
var options = BaseOptions(baseUrl: rootAddress);
|
|
|
|
if (isWithToken) {
|
|
|
|
var token = getIt<ApiConfigModel>().hetznerKey;
|
|
|
|
assert(token != null);
|
|
|
|
options.headers = {'Authorization': 'Bearer $token'};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validateStatus != null) {
|
|
|
|
options.validateStatus = validateStatus!;
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
2021-03-25 23:30:34 +00:00
|
|
|
|
|
|
|
return options;
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-25 23:30:34 +00:00
|
|
|
String rootAddress = 'https://api.hetzner.cloud/v1';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
Future<bool> isValid(String token) async {
|
2021-03-25 23:30:34 +00:00
|
|
|
validateStatus = (status) {
|
|
|
|
return status == HttpStatus.ok || status == HttpStatus.unauthorized;
|
|
|
|
};
|
|
|
|
var client = await getClient();
|
|
|
|
Response response = await client.get(
|
|
|
|
'/servers',
|
|
|
|
options: Options(
|
|
|
|
headers: {'Authorization': 'Bearer $token'},
|
|
|
|
),
|
2021-01-06 17:35:57 +00:00
|
|
|
);
|
2021-03-26 13:38:39 +00:00
|
|
|
close(client);
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
if (response.statusCode == HttpStatus.ok) {
|
|
|
|
return true;
|
|
|
|
} else if (response.statusCode == HttpStatus.unauthorized) {
|
|
|
|
return false;
|
|
|
|
} else {
|
2021-01-13 16:45:46 +00:00
|
|
|
throw Exception('code: ${response.statusCode}');
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<HetznerServerDetails> createServer({
|
2021-03-25 23:30:34 +00:00
|
|
|
required String cloudFlareKey,
|
2021-03-15 15:39:44 +00:00
|
|
|
required User rootUser,
|
2021-03-25 23:30:34 +00:00
|
|
|
required String domainName,
|
2021-01-06 17:35:57 +00:00
|
|
|
}) async {
|
2021-02-17 14:30:02 +00:00
|
|
|
var dbPassword = getRandomString(40);
|
|
|
|
|
2021-01-19 08:38:02 +00:00
|
|
|
var data = jsonDecode(
|
2021-02-17 11:36:20 +00:00
|
|
|
'''{"name":"selfprivacy-server","server_type":"cx11","start_after_create":false,"image":"ubuntu-20.04", "volumes":[],"networks":[],"user_data":"#cloud-config\\nruncmd:\\n- curl https://git.selfprivacy.org/ilchub/selfprivacy-nixos-infect/raw/branch/master/nixos-infect | PROVIDER=hetzner NIX_CHANNEL=nixos-20.09 DOMAIN=$domainName LUSER=${rootUser.login} PASSWORD=${rootUser.password} HASHED_PASSWORD=${rootUser.hashPassword} CF_TOKEN=$cloudFlareKey DB_PASSWORD=$dbPassword bash 2>&1 | tee /tmp/infect.log","labels":{},"automount":false}''',
|
2021-02-02 18:24:56 +00:00
|
|
|
);
|
2021-01-19 08:38:02 +00:00
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
var client = await getClient();
|
|
|
|
Response response = await client.post(
|
|
|
|
'/servers',
|
2021-01-06 17:35:57 +00:00
|
|
|
data: data,
|
|
|
|
);
|
2021-03-25 23:30:34 +00:00
|
|
|
client.close();
|
2021-01-06 17:35:57 +00:00
|
|
|
return HetznerServerDetails(
|
|
|
|
id: response.data['server']['id'],
|
|
|
|
ip4: response.data['server']['public_net']['ipv4']['ip'],
|
2021-01-19 12:05:40 +00:00
|
|
|
createTime: DateTime.now(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
Future<void> deleteSelfprivacyServer() async {
|
|
|
|
var client = await getClient();
|
|
|
|
Response response = await client.get('/servers');
|
2021-02-15 18:58:29 +00:00
|
|
|
|
|
|
|
List list = response.data['servers'];
|
|
|
|
var server = list.firstWhere((el) => el['name'] == 'selfprivacy-server');
|
2021-03-25 23:30:34 +00:00
|
|
|
await client.delete('/servers/${server['id']}');
|
2021-03-26 13:38:39 +00:00
|
|
|
close(client);
|
2021-02-15 18:58:29 +00:00
|
|
|
}
|
|
|
|
|
2021-01-19 12:05:40 +00:00
|
|
|
Future<HetznerServerDetails> startServer({
|
2021-03-15 15:39:44 +00:00
|
|
|
required HetznerServerDetails server,
|
2021-01-19 12:05:40 +00:00
|
|
|
}) async {
|
2021-03-25 23:30:34 +00:00
|
|
|
var client = await getClient();
|
|
|
|
|
|
|
|
await client.post('/servers/${server.id}/actions/poweron');
|
2021-03-26 13:38:39 +00:00
|
|
|
close(client);
|
2021-01-19 12:05:40 +00:00
|
|
|
|
|
|
|
return server.copyWith(
|
2021-01-06 19:25:53 +00:00
|
|
|
startTime: DateTime.now(),
|
2021-01-06 17:35:57 +00:00
|
|
|
);
|
|
|
|
}
|
2021-02-03 19:51:07 +00:00
|
|
|
|
2021-02-03 20:26:38 +00:00
|
|
|
Future<HetznerServerDetails> restart({
|
2021-03-15 15:39:44 +00:00
|
|
|
required HetznerServerDetails server,
|
2021-02-03 19:51:07 +00:00
|
|
|
}) async {
|
2021-03-25 23:30:34 +00:00
|
|
|
var client = await getClient();
|
|
|
|
await client.post('/servers/${server.id}/actions/poweron');
|
2021-03-26 13:38:39 +00:00
|
|
|
close(client);
|
2021-02-03 19:51:07 +00:00
|
|
|
return server.copyWith(
|
|
|
|
startTime: DateTime.now(),
|
|
|
|
);
|
|
|
|
}
|
2021-03-25 23:30:34 +00:00
|
|
|
|
|
|
|
metrics() async {
|
|
|
|
var hetznerServer = getIt<ApiConfigModel>().hetznerServer;
|
|
|
|
var client = await getClient();
|
|
|
|
await client.post('/servers/${hetznerServer!.id}/metrics');
|
2021-03-26 13:38:39 +00:00
|
|
|
close(client);
|
2021-03-25 23:30:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 13:38:39 +00:00
|
|
|
Future<HetznerServerInfo> getInfo() async {
|
2021-03-25 23:30:34 +00:00
|
|
|
var hetznerServer = getIt<ApiConfigModel>().hetznerServer;
|
|
|
|
var client = await getClient();
|
2021-03-26 13:38:39 +00:00
|
|
|
Response response = await client.get('/servers/${hetznerServer!.id}');
|
|
|
|
close(client);
|
|
|
|
|
|
|
|
return HetznerServerInfo.fromJson(response.data!['server']);
|
2021-03-25 23:30:34 +00:00
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|