181 lines
4.7 KiB
Dart
181 lines
4.7 KiB
Dart
import 'dart:async';
|
||
import 'dart:convert';
|
||
|
||
import 'package:cryptography/cryptography.dart';
|
||
import 'package:flutter/foundation.dart';
|
||
|
||
import 'api.dart';
|
||
import 'models.dart';
|
||
import 'secure_store.dart';
|
||
import 'tunnel.dart';
|
||
|
||
class AppState extends ChangeNotifier {
|
||
final SecureStore _store = SecureStore();
|
||
final EnrollmentApi _api = EnrollmentApi();
|
||
final TunnelPlatform _tunnel = TunnelPlatform();
|
||
|
||
VpnConfig? config;
|
||
TunnelStatus status = TunnelStatus.disconnected;
|
||
bool initialized = false;
|
||
bool busy = false;
|
||
String? error;
|
||
String? publicKey;
|
||
int? pingMs;
|
||
Timer? _statusTimer;
|
||
|
||
bool get hasConfig => config != null;
|
||
bool get connected => status.connected;
|
||
bool get connecting => status.busy || busy;
|
||
String get address => status.address ?? config?.address ?? '—';
|
||
String get endpoint => status.endpoint ?? config?.endpoint ?? '—';
|
||
|
||
Future<void> initialize() async {
|
||
try {
|
||
config = await _store.readConfig();
|
||
publicKey = await _readPublicKey();
|
||
if (config != null) {
|
||
status = await _tunnel.status();
|
||
}
|
||
} catch (e) {
|
||
error = 'Yerel kimlik okunamadı: $e';
|
||
} finally {
|
||
initialized = true;
|
||
notifyListeners();
|
||
_startPolling();
|
||
}
|
||
}
|
||
|
||
Future<String?> _readPublicKey() async {
|
||
final raw = await _store.readPrivateKey();
|
||
if (raw == null) return null;
|
||
try {
|
||
final algorithm = X25519();
|
||
final keyPair = await algorithm.newKeyPairFromSeed(base64Decode(raw));
|
||
final pub = await keyPair.extractPublicKey();
|
||
return base64Encode(pub.bytes);
|
||
} catch (_) {
|
||
return null;
|
||
}
|
||
}
|
||
|
||
Future<void> enroll(String token) async {
|
||
final clean = token.trim();
|
||
if (clean.isEmpty) {
|
||
error = 'Etkinleştirme kodu boş olamaz.';
|
||
notifyListeners();
|
||
return;
|
||
}
|
||
busy = true;
|
||
error = null;
|
||
notifyListeners();
|
||
try {
|
||
var privateKey = await _store.readPrivateKey();
|
||
if (privateKey == null) {
|
||
final algorithm = X25519();
|
||
final keyPair = await algorithm.newKeyPair();
|
||
final privateBytes = await keyPair.extractPrivateKeyBytes();
|
||
privateKey = base64Encode(privateBytes);
|
||
await _store.writePrivateKey(privateKey);
|
||
}
|
||
final algorithm = X25519();
|
||
final keyPair = await algorithm.newKeyPairFromSeed(base64Decode(privateKey));
|
||
final public = await keyPair.extractPublicKey();
|
||
publicKey = base64Encode(public.bytes);
|
||
final deviceId = await _store.deviceId();
|
||
final enrolled = await _api.enroll(
|
||
token: clean,
|
||
publicKey: publicKey!,
|
||
deviceId: deviceId,
|
||
privateKey: privateKey,
|
||
);
|
||
await _store.writeConfig(enrolled);
|
||
config = enrolled;
|
||
status = await _tunnel.status();
|
||
await refreshPing();
|
||
} on EnrollmentException catch (e) {
|
||
error = e.message;
|
||
} catch (e) {
|
||
error = 'Kayıt tamamlanamadı: $e';
|
||
} finally {
|
||
busy = false;
|
||
notifyListeners();
|
||
}
|
||
}
|
||
|
||
Future<void> connect() async {
|
||
final cfg = config;
|
||
if (cfg == null) {
|
||
error = 'Önce etkinleştirme kodunu gir.';
|
||
notifyListeners();
|
||
return;
|
||
}
|
||
busy = true;
|
||
error = null;
|
||
notifyListeners();
|
||
try {
|
||
if (!await _tunnel.prepare()) {
|
||
error = 'Android VPN izni verilmedi. Tekrar dene ve sistem onayını kabul et.';
|
||
return;
|
||
}
|
||
await _tunnel.start(cfg);
|
||
status = await _tunnel.status();
|
||
await refreshPing();
|
||
} catch (e) {
|
||
error = 'Tünel başlatılamadı: $e';
|
||
status = TunnelStatus(state: TunnelState.error, error: error);
|
||
} finally {
|
||
busy = false;
|
||
notifyListeners();
|
||
}
|
||
}
|
||
|
||
Future<void> disconnect() async {
|
||
busy = true;
|
||
error = null;
|
||
notifyListeners();
|
||
try {
|
||
await _tunnel.stop();
|
||
status = await _tunnel.status();
|
||
} catch (e) {
|
||
error = 'Tünel kapatılamadı: $e';
|
||
} finally {
|
||
busy = false;
|
||
notifyListeners();
|
||
}
|
||
}
|
||
|
||
Future<void> refreshStatus() async {
|
||
try {
|
||
status = await _tunnel.status();
|
||
notifyListeners();
|
||
} catch (_) {}
|
||
}
|
||
|
||
Future<void> refreshPing() async {
|
||
pingMs = await _api.ping();
|
||
notifyListeners();
|
||
}
|
||
|
||
Future<void> openVpnSettings() => _tunnel.openVpnSettings();
|
||
|
||
void _startPolling() {
|
||
_statusTimer?.cancel();
|
||
_statusTimer = Timer.periodic(const Duration(seconds: 2), (_) async {
|
||
if (!initialized) return;
|
||
try {
|
||
final next = await _tunnel.status();
|
||
if (next.state != status.state || next.address != status.address) {
|
||
status = next;
|
||
notifyListeners();
|
||
}
|
||
} catch (_) {}
|
||
});
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_statusTimer?.cancel();
|
||
_api.close();
|
||
super.dispose();
|
||
}
|
||
}
|