2022-11-17 21:43:16 +00:00
|
|
|
import 'dart:async';
|
2020-07-27 20:43:58 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
|
|
|
|
import 'package:mobile_nebula/components/SimplePage.dart';
|
|
|
|
import 'package:mobile_nebula/components/SiteItem.dart';
|
|
|
|
import 'package:mobile_nebula/models/Certificate.dart';
|
|
|
|
import 'package:mobile_nebula/models/IPAndPort.dart';
|
|
|
|
import 'package:mobile_nebula/models/Site.dart';
|
|
|
|
import 'package:mobile_nebula/models/StaticHosts.dart';
|
|
|
|
import 'package:mobile_nebula/models/UnsafeRoute.dart';
|
2022-11-17 21:43:16 +00:00
|
|
|
import 'package:mobile_nebula/screens/EnrollmentScreen.dart';
|
2020-07-27 20:43:58 +00:00
|
|
|
import 'package:mobile_nebula/screens/SettingsScreen.dart';
|
|
|
|
import 'package:mobile_nebula/screens/SiteDetailScreen.dart';
|
|
|
|
import 'package:mobile_nebula/screens/siteConfig/SiteConfigScreen.dart';
|
|
|
|
import 'package:mobile_nebula/services/utils.dart';
|
2022-11-17 21:43:16 +00:00
|
|
|
import 'package:pull_to_refresh/pull_to_refresh.dart';
|
2020-07-27 20:43:58 +00:00
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
/// Contains an expired CA and certificate
|
|
|
|
const badDebugSave = {
|
|
|
|
'name': 'Bad Site',
|
|
|
|
'cert': '''-----BEGIN NEBULA CERTIFICATE-----
|
|
|
|
CmIKBHRlc3QSCoKUoIUMgP7//w8ourrS+QUwjre3iAY6IDbmIX5cwd+UYVhLADLa
|
|
|
|
A5PwucZPVrNtP0P9NJE0boM2SiBSGzy8bcuFWWK5aVArJGA9VDtLg1HuujBu8lOp
|
|
|
|
VTgklxJAgbI1Xb1C9JC3a1Cnc6NPqWhnw+3VLoDXE9poBav09+zhw5DPDtgvQmxU
|
|
|
|
Sbw6cAF4gPS4e/tZ5Kjc8QEvjk3HDQ==
|
|
|
|
-----END NEBULA CERTIFICATE-----''',
|
|
|
|
'key': '''-----BEGIN NEBULA X25519 PRIVATE KEY-----
|
|
|
|
rmXnR1yvDZi1VPVmnNVY8NMsQpEpbbYlq7rul+ByQvg=
|
|
|
|
-----END NEBULA X25519 PRIVATE KEY-----''',
|
|
|
|
'ca': '''-----BEGIN NEBULA CERTIFICATE-----
|
|
|
|
CjkKB3Rlc3QgY2EopYyK9wUwpfOOhgY6IHj4yrtHbq+rt4hXTYGrxuQOS0412uKT
|
|
|
|
4wi5wL503+SAQAESQPhWXuVGjauHS1Qqd3aNA3DY+X8CnAweXNEoJKAN/kjH+BBv
|
|
|
|
mUOcsdFcCZiXrj7ryQIG1+WfqA46w71A/lV4nAc=
|
|
|
|
-----END NEBULA CERTIFICATE-----''',
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Contains an expired CA and certificate
|
|
|
|
const goodDebugSave = {
|
|
|
|
'name': 'Good Site',
|
|
|
|
'cert': '''-----BEGIN NEBULA CERTIFICATE-----
|
|
|
|
CmcKCmRlYnVnIGhvc3QSCYKAhFCA/v//DyiX0ZaaBjDjjPf5ETogyYzKdlRh7pW6
|
|
|
|
yOd8+aMQAFPha2wuYixuq53ru9+qXC9KIJd3ow6qIiaHInT1dgJvy+122WK7g86+
|
|
|
|
Z8qYtTZnox1cEkBYpC0SySrCp6jd/zeAFEJM6naPYgc6rmy/H/qveyQ6WAtbgLpK
|
|
|
|
tM3EXbbOE9+fV/Ma6Oilf1SixO3ZBo30nRYL
|
|
|
|
-----END NEBULA CERTIFICATE-----''',
|
|
|
|
'key': '''-----BEGIN NEBULA X25519 PRIVATE KEY-----
|
|
|
|
vu9t0mNy8cD5x3CMVpQ/cdKpjdz46NBlcRqvJAQpO44=
|
|
|
|
-----END NEBULA X25519 PRIVATE KEY-----''',
|
|
|
|
'ca': '''-----BEGIN NEBULA CERTIFICATE-----
|
|
|
|
CjcKBWRlYnVnKOTQlpoGMOSM9/kROiCWNJUs7c4ZRzUn2LbeAEQrz2PVswnu9dcL
|
|
|
|
Sn/2VNNu30ABEkCQtWxmCJqBr5Yd9vtDWCPo/T1JQmD3stBozcM6aUl1hP3zjURv
|
|
|
|
MAIH7gzreMGgrH/yR6rZpIHR3DxJ3E0aHtEI
|
|
|
|
-----END NEBULA CERTIFICATE-----''',
|
|
|
|
};
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
class MainScreen extends StatefulWidget {
|
2022-11-17 21:43:16 +00:00
|
|
|
const MainScreen(this.dnEnrollStream, {Key? key}) : super(key: key);
|
|
|
|
|
2022-11-22 16:12:38 +00:00
|
|
|
final StreamController dnEnrollStream;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_MainScreenState createState() => _MainScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MainScreenState extends State<MainScreen> {
|
2022-09-21 20:27:35 +00:00
|
|
|
List<Site>? sites;
|
2022-08-05 21:42:31 +00:00
|
|
|
// A set of widgets to display in a column that represents an error blocking us from moving forward entirely
|
2022-09-21 20:27:35 +00:00
|
|
|
List<Widget>? error;
|
2020-07-27 20:43:58 +00:00
|
|
|
|
2022-11-22 21:50:11 +00:00
|
|
|
bool supportsQRScanning = false;
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
static const platform = MethodChannel('net.defined.mobileNebula/NebulaVpnService');
|
2022-11-17 21:43:16 +00:00
|
|
|
RefreshController refreshController = RefreshController();
|
|
|
|
ScrollController scrollController = ScrollController();
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_loadSites();
|
|
|
|
|
2022-11-22 16:12:38 +00:00
|
|
|
widget.dnEnrollStream.stream.listen((_) {
|
2022-11-17 21:43:16 +00:00
|
|
|
_loadSites();
|
|
|
|
});
|
|
|
|
|
|
|
|
platform.setMethodCallHandler(handleMethodCall);
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
scrollController.dispose();
|
|
|
|
refreshController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> handleMethodCall(MethodCall call) async {
|
|
|
|
switch (call.method) {
|
|
|
|
case "refreshSites":
|
|
|
|
_loadSites();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print("ERR: Unexpected method call ${call.method}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-11-17 21:43:16 +00:00
|
|
|
Widget? debugSite;
|
|
|
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
debugSite = Row(
|
|
|
|
children: [
|
|
|
|
_debugSave(badDebugSave),
|
|
|
|
_debugSave(goodDebugSave),
|
2023-12-01 20:26:21 +00:00
|
|
|
_debugClearKeys(),
|
2022-11-17 21:43:16 +00:00
|
|
|
],
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-22 21:50:11 +00:00
|
|
|
// Determine whether the device supports QR scanning. For example, some
|
|
|
|
// Chromebooks do not have camera support.
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
platform.invokeMethod("android.deviceHasCamera").then(
|
|
|
|
(hasCamera) => setState(() => supportsQRScanning = hasCamera)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
supportsQRScanning = true;
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
return SimplePage(
|
2022-11-17 21:43:16 +00:00
|
|
|
title: Text('Nebula'),
|
|
|
|
scrollable: SimpleScrollable.vertical,
|
|
|
|
scrollController: scrollController,
|
2020-07-27 20:43:58 +00:00
|
|
|
leadingAction: PlatformIconButton(
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
icon: Icon(Icons.add, size: 28.0),
|
|
|
|
onPressed: () => Utils.openPage(context, (context) {
|
|
|
|
return SiteConfigScreen(onSave: (_) {
|
|
|
|
_loadSites();
|
2022-11-22 21:50:11 +00:00
|
|
|
}, supportsQRScanning: supportsQRScanning);
|
2020-07-27 20:43:58 +00:00
|
|
|
}),
|
|
|
|
),
|
2022-11-17 21:43:16 +00:00
|
|
|
refreshController: refreshController,
|
|
|
|
onRefresh: () {
|
|
|
|
_loadSites();
|
|
|
|
refreshController.refreshCompleted();
|
|
|
|
},
|
2020-07-27 20:43:58 +00:00
|
|
|
trailingActions: <Widget>[
|
|
|
|
PlatformIconButton(
|
|
|
|
padding: EdgeInsets.zero,
|
|
|
|
icon: Icon(Icons.menu, size: 28.0),
|
2022-11-22 16:12:38 +00:00
|
|
|
onPressed: () => Utils.openPage(context, (_) => SettingsScreen(widget.dnEnrollStream)),
|
2020-07-27 20:43:58 +00:00
|
|
|
),
|
|
|
|
],
|
2022-11-17 21:43:16 +00:00
|
|
|
bottomBar: debugSite,
|
2020-07-27 20:43:58 +00:00
|
|
|
child: _buildBody(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildBody() {
|
2022-08-05 21:42:31 +00:00
|
|
|
if (error != null) {
|
2022-09-21 20:27:35 +00:00
|
|
|
return Center(
|
|
|
|
child: Padding(
|
|
|
|
child: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
children: error!,
|
|
|
|
),
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10)));
|
2022-08-05 21:42:31 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
return _buildSites();
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildNoSites() {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
2020-08-07 16:38:16 +00:00
|
|
|
child: Center(
|
|
|
|
child: Column(
|
2020-07-27 20:43:58 +00:00
|
|
|
children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.fromLTRB(0, 8.0, 0, 8.0),
|
|
|
|
child: Text('Welcome to Nebula!', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20)),
|
2021-05-03 21:58:04 +00:00
|
|
|
),
|
2020-07-27 20:43:58 +00:00
|
|
|
Text('You don\'t have any site configurations installed yet. Hit the plus button above to get started.',
|
|
|
|
textAlign: TextAlign.center),
|
2021-05-03 21:58:04 +00:00
|
|
|
],
|
2020-07-27 20:43:58 +00:00
|
|
|
),
|
2020-08-07 16:38:16 +00:00
|
|
|
));
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildSites() {
|
2022-09-21 20:27:35 +00:00
|
|
|
if (sites == null || sites!.length == 0) {
|
|
|
|
return _buildNoSites();
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
List<Widget> items = [];
|
2022-09-21 20:27:35 +00:00
|
|
|
sites!.forEach((site) {
|
2020-07-27 20:43:58 +00:00
|
|
|
items.add(SiteItem(
|
|
|
|
key: Key(site.id),
|
|
|
|
site: site,
|
|
|
|
onPressed: () {
|
|
|
|
Utils.openPage(context, (context) {
|
2022-11-22 21:50:11 +00:00
|
|
|
return SiteDetailScreen(
|
|
|
|
site: site,
|
|
|
|
onChanged: () => _loadSites(),
|
|
|
|
supportsQRScanning: supportsQRScanning,
|
|
|
|
);
|
2020-07-27 20:43:58 +00:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
Widget child = ReorderableListView(
|
2022-11-17 21:43:16 +00:00
|
|
|
shrinkWrap: true,
|
|
|
|
scrollController: scrollController,
|
2020-07-27 20:43:58 +00:00
|
|
|
padding: EdgeInsets.symmetric(vertical: 5),
|
|
|
|
children: items,
|
|
|
|
onReorder: (oldI, newI) async {
|
|
|
|
if (oldI < newI) {
|
|
|
|
// removing the item at oldIndex will shorten the list by 1.
|
|
|
|
newI -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() {
|
2022-09-21 20:27:35 +00:00
|
|
|
final Site moved = sites!.removeAt(oldI);
|
|
|
|
sites!.insert(newI, moved);
|
2020-07-27 20:43:58 +00:00
|
|
|
});
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
for (var i = 0; i < sites!.length; i++) {
|
|
|
|
if (sites![i].sortKey == i) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
sites![i].sortKey = i;
|
2020-07-27 20:43:58 +00:00
|
|
|
try {
|
2022-09-21 20:27:35 +00:00
|
|
|
await sites![i].save();
|
2020-07-27 20:43:58 +00:00
|
|
|
} catch (err) {
|
|
|
|
//TODO: display error at the end
|
2022-09-21 20:27:35 +00:00
|
|
|
print('ERR ${sites![i].name} - $err');
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_loadSites();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Platform.isIOS) {
|
|
|
|
child = CupertinoTheme(child: child, data: CupertinoTheme.of(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The theme here is to remove the hardcoded canvas border reordering forces on us
|
|
|
|
return Theme(data: Theme.of(context).copyWith(canvasColor: Colors.transparent), child: child);
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
Widget _debugSave(Map<String, String> siteConfig) {
|
2020-07-27 20:43:58 +00:00
|
|
|
return CupertinoButton(
|
2022-11-17 21:43:16 +00:00
|
|
|
child: Text(siteConfig['name']!),
|
2020-07-27 20:43:58 +00:00
|
|
|
onPressed: () async {
|
|
|
|
var uuid = Uuid();
|
|
|
|
|
|
|
|
var s = Site(
|
2022-11-17 21:43:16 +00:00
|
|
|
name: siteConfig['name']!,
|
2020-07-27 20:43:58 +00:00
|
|
|
id: uuid.v4(),
|
|
|
|
staticHostmap: {
|
2021-04-23 21:23:06 +00:00
|
|
|
"10.1.0.1": StaticHost(
|
|
|
|
lighthouse: true,
|
|
|
|
destinations: [IPAndPort(ip: '10.1.1.53', port: 4242), IPAndPort(ip: '1::1', port: 4242)])
|
2020-07-27 20:43:58 +00:00
|
|
|
},
|
2022-11-17 21:43:16 +00:00
|
|
|
ca: [CertificateInfo.debug(rawCert: siteConfig['ca'])],
|
|
|
|
certInfo: CertificateInfo.debug(rawCert: siteConfig['cert']),
|
2020-07-27 20:43:58 +00:00
|
|
|
unsafeRoutes: [UnsafeRoute(route: '10.3.3.3/32', via: '10.1.0.1')]);
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
s.key = siteConfig['key'];
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
var err = await s.save();
|
|
|
|
if (err != null) {
|
|
|
|
Utils.popError(context, "Failed to save the site", err);
|
|
|
|
} else {
|
|
|
|
_loadSites();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-01 20:26:21 +00:00
|
|
|
Widget _debugClearKeys() {
|
2022-11-17 21:43:16 +00:00
|
|
|
return CupertinoButton(
|
2023-12-01 20:26:21 +00:00
|
|
|
child: Text("Clear Keys"),
|
|
|
|
onPressed: () async {
|
|
|
|
await platform.invokeMethod("debug.clearKeys", null);
|
|
|
|
},
|
2022-11-17 21:43:16 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-01 20:26:21 +00:00
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
_loadSites() async {
|
|
|
|
//TODO: This can throw, we need to show an error dialog
|
|
|
|
Map<String, dynamic> rawSites = jsonDecode(await platform.invokeMethod('listSites'));
|
|
|
|
bool hasErrors = false;
|
|
|
|
|
|
|
|
sites = [];
|
2021-04-23 21:23:06 +00:00
|
|
|
rawSites.forEach((id, rawSite) {
|
2020-07-27 20:43:58 +00:00
|
|
|
try {
|
|
|
|
var site = Site.fromJson(rawSite);
|
|
|
|
if (site.errors.length > 0) {
|
|
|
|
hasErrors = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: we need to cancel change listeners when we rebuild
|
|
|
|
site.onChange().listen((_) {
|
|
|
|
setState(() {});
|
|
|
|
}, onError: (err) {
|
|
|
|
setState(() {});
|
2022-09-21 20:27:35 +00:00
|
|
|
if (ModalRoute.of(context)!.isCurrent) {
|
2020-07-27 20:43:58 +00:00
|
|
|
Utils.popError(context, "${site.name} Error", err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
sites!.add(site);
|
2020-07-27 20:43:58 +00:00
|
|
|
} catch (err) {
|
|
|
|
//TODO: handle error
|
2021-04-23 21:23:06 +00:00
|
|
|
print("$err site config: $rawSite");
|
|
|
|
// Sometimes it is helpful to just nuke these is dev
|
|
|
|
// platform.invokeMethod('deleteSite', id);
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-29 15:17:43 +00:00
|
|
|
if (Platform.isAndroid) {
|
|
|
|
// Android suffers from a race to discover the active site and attach site specific event listeners
|
|
|
|
platform.invokeMethod("android.registerActiveSite");
|
|
|
|
}
|
|
|
|
|
2022-09-21 20:27:35 +00:00
|
|
|
sites!.sort((a, b) {
|
2022-11-17 21:43:16 +00:00
|
|
|
if (a.sortKey == b.sortKey) {
|
|
|
|
return a.name.compareTo(b.name);
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
return a.sortKey - b.sortKey;
|
|
|
|
});
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
setState(() {});
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|
|
|
|
}
|