3
0
Fork 0
trifid_mobile/lib/screens/siteConfig/SiteConfigScreen.dart

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

318 lines
10 KiB
Dart
Raw Permalink Normal View History

2020-07-27 20:43:58 +00:00
import 'dart:convert';
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' as fpw;
import 'package:intl/intl.dart';
2020-07-27 20:43:58 +00:00
import 'package:mobile_nebula/components/FormPage.dart';
import 'package:mobile_nebula/components/PlatformTextFormField.dart';
import 'package:mobile_nebula/components/config/ConfigPageItem.dart';
import 'package:mobile_nebula/components/config/ConfigItem.dart';
import 'package:mobile_nebula/components/config/ConfigSection.dart';
import 'package:mobile_nebula/models/Site.dart';
import 'package:mobile_nebula/screens/siteConfig/AdvancedScreen.dart';
import 'package:mobile_nebula/screens/siteConfig/CAListScreen.dart';
2021-05-03 20:16:00 +00:00
import 'package:mobile_nebula/screens/siteConfig/AddCertificateScreen.dart';
import 'package:mobile_nebula/screens/siteConfig/CertificateDetailsScreen.dart';
2020-07-27 20:43:58 +00:00
import 'package:mobile_nebula/screens/siteConfig/StaticHostsScreen.dart';
import 'package:mobile_nebula/services/utils.dart';
//TODO: Add a config test mechanism
2021-05-03 20:16:00 +00:00
//TODO: Enforce a name
2020-07-27 20:43:58 +00:00
class SiteConfigScreen extends StatefulWidget {
const SiteConfigScreen({
Key? key,
this.site,
required this.onSave,
required this.supportsQRScanning,
}) : super(key: key);
2020-07-27 20:43:58 +00:00
final Site? site;
2020-07-27 20:43:58 +00:00
// This is called after the target OS has saved the configuration
final ValueChanged<Site> onSave;
final bool supportsQRScanning;
2020-07-27 20:43:58 +00:00
@override
_SiteConfigScreenState createState() => _SiteConfigScreenState();
}
class _SiteConfigScreenState extends State<SiteConfigScreen> {
bool changed = false;
bool newSite = false;
bool debug = false;
late Site site;
String? pubKey;
String? privKey;
2020-07-27 20:43:58 +00:00
static const platform = MethodChannel('net.defined.mobileNebula/NebulaVpnService');
2020-07-27 20:43:58 +00:00
final nameController = TextEditingController();
@override
void initState() {
//NOTE: this is slightly wasteful since a keypair will be generated every time this page is opened
_generateKeys();
2020-07-27 20:43:58 +00:00
if (widget.site == null) {
newSite = true;
site = Site();
} else {
site = widget.site!;
2020-07-27 20:43:58 +00:00
nameController.text = site.name;
}
super.initState();
}
@override
Widget build(BuildContext context) {
if (pubKey == null || privKey == null) {
return Center(
child: fpw.PlatformCircularProgressIndicator(cupertino: (_, __) {
return fpw.CupertinoProgressIndicatorData(radius: 50);
}),
);
}
2020-07-27 20:43:58 +00:00
return FormPage(
title: newSite ? 'New Site' : 'Edit Site',
changed: changed,
onSave: () async {
site.name = nameController.text;
try {
await site.save();
} catch (error) {
return Utils.popError(context, 'Failed to save the site configuration', error.toString());
}
Navigator.pop(context);
widget.onSave(site);
2020-07-27 20:43:58 +00:00
},
child: Column(
children: <Widget>[
_main(),
_keys(),
_hosts(),
_advanced(),
_managed(),
2020-07-27 20:43:58 +00:00
kDebugMode ? _debugConfig() : Container(height: 0),
],
));
}
Widget _debugConfig() {
var data = "";
try {
final encoder = new JsonEncoder.withIndent(' ');
data = encoder.convert(site);
} catch (err) {
data = err.toString();
}
return ConfigSection(label: 'DEBUG', children: [ConfigItem(labelWidth: 0, content: SelectableText(data))]);
2020-07-27 20:43:58 +00:00
}
Widget _main() {
return ConfigSection(children: <Widget>[
ConfigItem(
label: Text("Name"),
content: PlatformTextFormField(
placeholder: 'Required',
controller: nameController,
2021-05-03 20:16:00 +00:00
validator: (name) {
if (name == null || name == "") {
return "A name is required";
}
return null;
},
2020-07-27 20:43:58 +00:00
))
]);
}
Widget _managed() {
final formatter = DateFormat.yMMMMd('en_US').add_jm();
var lastUpdate = "Unknown";
if (site.lastManagedUpdate != null) {
lastUpdate = formatter.format(site.lastManagedUpdate!.toLocal());
}
return site.managed ? ConfigSection(
label: "MANAGED CONFIG",
children: <Widget>[
ConfigItem(
label: Text("Last Update"),
content: Wrap(alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, children: <Widget>[
Text(lastUpdate),
]),
)
]
) : Container();
}
2020-07-27 20:43:58 +00:00
Widget _keys() {
final certError = site.certInfo == null || site.certInfo!.validity == null || !site.certInfo!.validity!.valid;
var caError = false;
if (!site.managed) {
var caError = site.ca.length == 0;
if (!caError) {
site.ca.forEach((ca) {
if (ca.validity == null || !ca.validity!.valid) {
caError = true;
}
});
}
2020-07-27 20:43:58 +00:00
}
return ConfigSection(
2020-07-27 20:43:58 +00:00
label: "IDENTITY",
children: [
ConfigPageItem(
label: Text('Certificate'),
content: Wrap(alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, children: <Widget>[
certError
? Padding(
child: Icon(Icons.error, color: CupertinoColors.systemRed.resolveFrom(context), size: 20),
padding: EdgeInsets.only(right: 5))
: Container(),
certError ? Text('Needs attention') : Text(site.certInfo?.cert.details.name ?? 'Unknown certificate')
2020-07-27 20:43:58 +00:00
]),
onPressed: () {
Utils.openPage(context, (context) {
2021-05-03 20:16:00 +00:00
if (site.certInfo != null) {
return CertificateDetailsScreen(
certInfo: site.certInfo!,
pubKey: pubKey,
privKey: privKey,
onReplace: site.managed ? null : (result) {
2021-05-03 20:16:00 +00:00
setState(() {
changed = true;
site.certInfo = result.certInfo;
site.key = result.key;
});
},
supportsQRScanning: widget.supportsQRScanning,
);
2021-05-03 20:16:00 +00:00
}
return AddCertificateScreen(
pubKey: pubKey!,
privKey: privKey!,
onSave: (result) {
setState(() {
changed = true;
site.certInfo = result.certInfo;
site.key = result.key;
});
},
supportsQRScanning: widget.supportsQRScanning,
);
2020-07-27 20:43:58 +00:00
});
},
),
ConfigPageItem(
label: Text("CA"),
content:
Wrap(alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, children: <Widget>[
caError
? Padding(
child: Icon(Icons.error, color: CupertinoColors.systemRed.resolveFrom(context), size: 20),
padding: EdgeInsets.only(right: 5))
: Container(),
caError ? Text('Needs attention') : Text(Utils.itemCountFormat(site.ca.length))
]),
onPressed: () {
Utils.openPage(context, (context) {
return CAListScreen(
cas: site.ca,
onSave: site.managed ? null : (ca) {
2020-07-27 20:43:58 +00:00
setState(() {
changed = true;
site.ca = ca;
});
},
supportsQRScanning: widget.supportsQRScanning,
);
2020-07-27 20:43:58 +00:00
});
})
],
);
}
Widget _hosts() {
return ConfigSection(
label: "LIGHTHOUSES / STATIC HOSTS",
2020-07-27 20:43:58 +00:00
children: <Widget>[
ConfigPageItem(
label: Text('Hosts'),
content: Wrap(alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, children: <Widget>[
site.staticHostmap.length == 0
? Padding(
child: Icon(Icons.error, color: CupertinoColors.systemRed.resolveFrom(context), size: 20),
padding: EdgeInsets.only(right: 5))
: Container(),
site.staticHostmap.length == 0
? Text('Needs attention')
: Text(Utils.itemCountFormat(site.staticHostmap.length))
]),
onPressed: () {
Utils.openPage(context, (context) {
return StaticHostsScreen(
hostmap: site.staticHostmap,
onSave: site.managed ? null : (map) {
2020-07-27 20:43:58 +00:00
setState(() {
changed = true;
site.staticHostmap = map;
});
});
});
},
),
],
);
}
Widget _advanced() {
return ConfigSection(
label: "ADVANCED",
2020-07-27 20:43:58 +00:00
children: <Widget>[
ConfigPageItem(
label: Text('Advanced'),
onPressed: () {
Utils.openPage(context, (context) {
return AdvancedScreen(
site: site,
onSave: (settings) {
setState(() {
changed = true;
site.cipher = settings.cipher;
site.lhDuration = settings.lhDuration;
site.port = settings.port;
site.logVerbosity = settings.verbosity;
site.unsafeRoutes = settings.unsafeRoutes;
site.mtu = settings.mtu;
});
});
});
})
],
);
}
_generateKeys() async {
try {
var kp = await platform.invokeMethod("nebula.generateKeyPair");
Map<String, dynamic> keyPair = jsonDecode(kp);
setState(() {
pubKey = keyPair['PublicKey'];
privKey = keyPair['PrivateKey'];
});
} on PlatformException catch (err) {
Utils.popError(context, 'Failed to generate key pair', err.details ?? err.message);
}
}
2020-07-27 20:43:58 +00:00
}