BotZ_app/lib/utils/verify_server.dart
2019-08-18 18:06:25 -03:00

43 lines
1.1 KiB
Dart

import 'package:http/http.dart';
import 'package:logger/logger.dart';
import 'dart:convert';
import 'package:dbcrypt/dbcrypt.dart';
import 'connection.dart';
final log = Logger();
Future<bool> verifyServer(Uri serverAddr, Uri zTargetAddr, {Map<String, String> routes=defaultRoutes}) async {
String botZTargetUri = serverAddr.toString() + routes['ping'];
log.d("botZ server uri: $botZTargetUri");
log.d("z server: ${zTargetAddr.toString()}");
var resp = await get(
botZTargetUri,
);
log.d("Status code: ${resp.statusCode}");
if (resp.statusCode >= 300 || resp.statusCode < 200) {
log.d("Status code is not 2xx: $resp.statusCode");
return false;
}
Map<String, dynamic> respData = jsonDecode(resp.body);
log.d("Response: ${respData.toString()}");
if (!respData.containsKey('hash')) {
log.d("Response is malformed: ${respData.toString()}");
return false;
}
if (!DBCrypt().checkpw(zTargetAddr.toString(), respData['hash'])) {
log.d("The server answer do not match the expected response: ${respData.toString()}");
return false;
}
log.d("Response is ok!");
return true;
}