Review of 742f376 (Fable). Three defects made the feature not work for the
case it exists for:
- armExpiryWarning() cancelled the alarm before its own early return. Dart
re-pushes the deadline every 60 s with no dedupe (rule 93's ">5 s" dedupe
does not exist), so the first sync after the warning came due deleted the
armed alarm and never re-armed it. Cancel now happens only on the branch
that re-arms.
- cancelSessionWarningAlarm() built its PendingIntent with
FLAG_UPDATE_CURRENT, rewriting the live alarm's RUNTIME_TOKEN to "" so a
delivery in flight was dropped as a stale generation. It now looks the
existing PendingIntent up with FLAG_NO_CREATE.
- SOCKS5 mode never requested POST_NOTIFICATIONS (requestPermission() is
TUN-only because it also raises the VPN consent dialog), so on Android 13+
both alerts were dropped. New plugin method requests it on its own.
Also:
- the native deadline wins the expiry race against the 1 s Dart tick, so the
commonest session end reached _onVpnConnectionChanged as a bare tunnel
drop with no message. It now reconciles and sets the expiry text, with
postAlert false since the native side already posted.
- a resume reconnect no longer resets the support-ad claim: it re-attaches
to the same server session, where the top-up has already been spent.
- RevoltAlerts sets setOnlyAlertOnce so a replacing post does not re-sound.
- AGENTS.md rule 93 described a setSessionExpiry/SESSION_EXPIRES_AT_MS path
that no longer exists, dedupe included. Corrected; that stale text is what
the warning bug was written against.
- CHANGELOG no longer claims the alert covers the tunnel stopping on its own.
flutter analyze clean; debug APK builds.
Co-Authored-By: Claude Opus 5 <[email protected]>
295 lines
10 KiB
Dart
295 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:revoltvpn/logic/app_colors.dart';
|
|
import 'package:revoltvpn/logic/connection_settings.dart';
|
|
import 'package:revoltvpn/logic/local_socks_tester.dart';
|
|
import 'package:revoltvpn/logic/session_timer.dart';
|
|
import 'package:revoltvpn/logic/vpn_connection.dart';
|
|
|
|
class ConnectionModeTile extends StatefulWidget {
|
|
final ValueChanged<ConnectionMode>? onChanged;
|
|
|
|
const ConnectionModeTile({super.key, this.onChanged});
|
|
|
|
@override
|
|
State<ConnectionModeTile> createState() => _ConnectionModeTileState();
|
|
}
|
|
|
|
class _ConnectionModeTileState extends State<ConnectionModeTile> {
|
|
ConnectionMode _mode = ConnectionSettings.mode;
|
|
bool _testingLocalSocks = false;
|
|
bool _reconnecting = false;
|
|
LocalSocksTestResult? _lastSocksTest;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_load();
|
|
}
|
|
|
|
Future<void> _load() async {
|
|
await ConnectionSettings.initialize();
|
|
if (mounted) setState(() => _mode = ConnectionSettings.mode);
|
|
}
|
|
|
|
bool _isTunnelBusy(VpnStatus status) {
|
|
return status == VpnStatus.connected ||
|
|
status == VpnStatus.connecting ||
|
|
status == VpnStatus.disconnecting;
|
|
}
|
|
|
|
Future<void> _changeMode(ConnectionMode? next) async {
|
|
if (next == null || next == _mode) return;
|
|
|
|
final vpn = context.read<VpnConnection>();
|
|
if (_isTunnelBusy(vpn.status)) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(
|
|
content: Text('Disconnect before changing connection mode.'),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
final saved = await ConnectionSettings.setMode(next);
|
|
if (!mounted) return;
|
|
if (!saved) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Could not save connection mode.')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_mode = next;
|
|
_lastSocksTest = null;
|
|
});
|
|
widget.onChanged?.call(next);
|
|
}
|
|
|
|
// Recovers from an orphaned proxy: tear the runtime down and re-attach to
|
|
// the session this device already holds (connect(resume: true)), so a
|
|
// readable set of credentials exists again. No rewarded ad and no new
|
|
// session — the user is repairing a connection they already paid for.
|
|
Future<void> _reconnect() async {
|
|
if (_reconnecting) return;
|
|
final vpn = context.read<VpnConnection>();
|
|
final timer = context.read<SessionTimer>();
|
|
|
|
setState(() => _reconnecting = true);
|
|
try {
|
|
await timer.disconnect();
|
|
if (!mounted) return;
|
|
if (await vpn.connect(resume: true)) {
|
|
if (!mounted) return;
|
|
await timer.start(resetSupportClaim: false);
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_reconnecting = false;
|
|
_lastSocksTest = null;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> _testLocalSocks() async {
|
|
if (_testingLocalSocks) return;
|
|
final vpn = context.read<VpnConnection>();
|
|
|
|
if (vpn.status != VpnStatus.connected || !vpn.canTestActiveLocalSocks) {
|
|
final orphaned =
|
|
vpn.status == VpnStatus.connected && vpn.adoptedRunningRuntime;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
orphaned
|
|
? 'Credentials were lost when the app restarted. Reconnect first.'
|
|
: 'Connect with SOCKS5 first, then run the local test.',
|
|
),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
setState(() => _testingLocalSocks = true);
|
|
final result = await vpn.testActiveLocalSocks();
|
|
if (!mounted) return;
|
|
|
|
setState(() {
|
|
_testingLocalSocks = false;
|
|
_lastSocksTest = result;
|
|
});
|
|
|
|
final latency = result.latencyMs == null ? '' : ' (${result.latencyMs} ms)';
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text('${result.message}$latency')),
|
|
);
|
|
}
|
|
|
|
String get _subtitle {
|
|
switch (_mode) {
|
|
case ConnectionMode.tun:
|
|
return 'TUN: Android VPN routing for normal device traffic.';
|
|
case ConnectionMode.proxy:
|
|
return 'SOCKS5: local authenticated proxy — point apps at 127.0.0.1:<port> with the credentials shown below.';
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
ListTile(
|
|
title: const Text(
|
|
'Connection mode',
|
|
style: TextStyle(color: AppColors.textWhite, fontSize: 15),
|
|
),
|
|
subtitle: Text(
|
|
_subtitle,
|
|
style: const TextStyle(color: AppColors.textDim, fontSize: 12),
|
|
),
|
|
trailing: DropdownButtonHideUnderline(
|
|
child: DropdownButton<ConnectionMode>(
|
|
value: _mode,
|
|
dropdownColor: AppColors.bgCard,
|
|
onChanged: _changeMode,
|
|
items: const [
|
|
DropdownMenuItem(
|
|
value: ConnectionMode.tun,
|
|
child: Text('TUN'),
|
|
),
|
|
DropdownMenuItem(
|
|
value: ConnectionMode.proxy,
|
|
child: Text('SOCKS5'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
if (_mode == ConnectionMode.proxy) ...[
|
|
Consumer<VpnConnection>(
|
|
builder: (context, vpn, _) {
|
|
final session = vpn.activeSocksSession;
|
|
if (session == null) {
|
|
// The proxy can be running while this session is unknown:
|
|
// Android killed the app process, the foreground service kept
|
|
// the listener alive, and its credentials existed only in the
|
|
// memory that died with it. Saying "Connect" there would be a
|
|
// lie — the user is already connected.
|
|
final orphaned = vpn.status == VpnStatus.connected &&
|
|
vpn.adoptedRunningRuntime;
|
|
if (orphaned) {
|
|
return Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 8),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'The proxy is still running, but its credentials were '
|
|
'lost when the app restarted. Reconnect to get a new '
|
|
'endpoint.',
|
|
style: TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
const SizedBox(height: 8),
|
|
OutlinedButton.icon(
|
|
onPressed: _reconnecting ? null : _reconnect,
|
|
icon: _reconnecting
|
|
? const SizedBox(
|
|
width: 16,
|
|
height: 16,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2,
|
|
),
|
|
)
|
|
: const Icon(Icons.refresh),
|
|
label: Text(
|
|
_reconnecting ? 'Reconnecting…' : 'Reconnect',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
return const Padding(
|
|
padding: EdgeInsets.fromLTRB(20, 0, 20, 8),
|
|
child: Text(
|
|
'Connect to start a local SOCKS5 proxy. Credentials change every session.',
|
|
style: TextStyle(color: AppColors.textMuted, fontSize: 12),
|
|
),
|
|
);
|
|
}
|
|
return Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 8),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Proxy: 127.0.0.1:${session.port}',
|
|
style: const TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 12,
|
|
fontFamily: 'monospace',
|
|
),
|
|
),
|
|
Text(
|
|
'Username: ${session.username}',
|
|
style: const TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 12,
|
|
fontFamily: 'monospace',
|
|
),
|
|
),
|
|
Text(
|
|
'Password: ${session.password}',
|
|
style: const TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 12,
|
|
fontFamily: 'monospace',
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 8),
|
|
child: OutlinedButton.icon(
|
|
onPressed: _testingLocalSocks ? null : _testLocalSocks,
|
|
icon: _testingLocalSocks
|
|
? const SizedBox(
|
|
width: 16,
|
|
height: 16,
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
)
|
|
: const Icon(Icons.network_check),
|
|
label: Text(
|
|
_testingLocalSocks ? 'Testing…' : 'Test Local SOCKS',
|
|
),
|
|
),
|
|
),
|
|
if (_lastSocksTest != null)
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 12),
|
|
child: Text(
|
|
_lastSocksTest!.ok
|
|
? 'Last test: OK${_lastSocksTest!.latencyMs == null ? '' : ' · ${_lastSocksTest!.latencyMs} ms'}'
|
|
: 'Last test: Failed · ${_lastSocksTest!.message}',
|
|
style: const TextStyle(
|
|
color: AppColors.textMuted,
|
|
fontSize: 12,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
);
|
|
}
|
|
}
|