prem / lib /services /vpn_service.dart
Nitishkumar-ai's picture
Deploy source code to Hugging Face without binaries
c25dcd7
import 'package:flutter/services.dart';
import 'package:premithus/services/storage_service.dart';
class VpnStats {
final int blockedToday;
final String? lastBlockedDomain;
final String? lastBlockedCategory;
final List<BlockedDomain> topBlockedDomains;
VpnStats({
required this.blockedToday,
this.lastBlockedDomain,
this.lastBlockedCategory,
required this.topBlockedDomains,
});
}
class BlockedDomain {
final String domain;
final int hitCount;
BlockedDomain({required this.domain, required this.hitCount});
}
class KavachaVpn {
static const _channel = MethodChannel('in.inmodel.premithius/vpn');
static bool _isRunning = false;
static bool get isRunning => _isRunning;
static Future<void> start() async {
final result = await _channel.invokeMethod('startVpn');
_isRunning = result == 'started';
}
static Future<void> stop() async {
await _channel.invokeMethod('stopVpn');
_isRunning = false;
}
static Future<VpnStats> getStats() async {
final db = await StorageService().database;
final today = DateTime.now().millisecondsSinceEpoch - 86400000;
final blocked = await db.rawQuery(
'SELECT COUNT(*) as c FROM vpn_events WHERE event_type="blocked" AND created_at > ?',
[today]
);
final lastBlocked = await db.rawQuery(
'SELECT domain, category, created_at FROM vpn_events WHERE event_type="blocked" ORDER BY created_at DESC LIMIT 1'
);
final topDomains = await db.rawQuery(
'SELECT domain, hit_count FROM scam_domains ORDER BY hit_count DESC LIMIT 5'
);
int count = 0;
if (blocked.isNotEmpty && blocked.first['c'] != null) {
count = blocked.first['c'] as int;
}
return VpnStats(
blockedToday: count,
lastBlockedDomain: lastBlocked.isNotEmpty ? lastBlocked.first['domain'] as String : null,
lastBlockedCategory: lastBlocked.isNotEmpty ? lastBlocked.first['category'] as String : null,
topBlockedDomains: topDomains.map((r) => BlockedDomain(
domain: r['domain'] as String,
hitCount: r['hit_count'] as int,
)).toList(),
);
}
static Future<String?> getLastEvent() async {
return await _channel.invokeMethod('getLastEvent');
}
static Future<void> reloadBlocklist() async {
await _channel.invokeMethod('reloadBlocklist');
}
}