refactor to use ServerConfig

This commit is contained in:
csf 2022-12-21 16:24:01 +09:00
parent f5cc55ab3d
commit cba6a3e0ee
4 changed files with 44 additions and 42 deletions

View File

@ -1525,7 +1525,9 @@ class ServerConfig {
this.key = key?.trim() ?? ''; this.key = key?.trim() ?? '';
} }
/// throw decoding failure /// decode from shared string (from user shared or rustdesk-server generated)
/// also see [encode]
/// throw when decoding failure
ServerConfig.decode(String msg) { ServerConfig.decode(String msg) {
final input = msg.split('').reversed.join(''); final input = msg.split('').reversed.join('');
final bytes = base64Decode(base64.normalize(input)); final bytes = base64Decode(base64.normalize(input));
@ -1537,6 +1539,8 @@ class ServerConfig {
key = json['key'] ?? ''; key = json['key'] ?? '';
} }
/// encode to shared string
/// also see [ServerConfig.decode]
String encode() { String encode() {
Map<String, String> config = {}; Map<String, String> config = {};
config['host'] = idServer.trim(); config['host'] = idServer.trim();
@ -1548,4 +1552,11 @@ class ServerConfig {
.reversed .reversed
.join(); .join();
} }
/// from local options
ServerConfig.fromOptions(Map<String, dynamic> options)
: idServer = options['custom-rendezvous-server'] ?? "",
relayServer = options['relay-server'] ?? "",
apiServer = options['api-server'] ?? "",
key = options['key'] ?? "";
} }

View File

@ -140,8 +140,7 @@ class _ScanPageState extends State<ScanPage> {
try { try {
final sc = ServerConfig.decode(data.substring(7)); final sc = ServerConfig.decode(data.substring(7));
Timer(Duration(milliseconds: 60), () { Timer(Duration(milliseconds: 60), () {
showServerSettingsWithValue(sc.idServer, sc.relayServer, sc.key, showServerSettingsWithValue(sc, gFFI.dialogManager);
sc.apiServer, gFFI.dialogManager);
}); });
} catch (e) { } catch (e) {
showToast('Invalid QR code'); showToast('Invalid QR code');

View File

@ -391,11 +391,7 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
void showServerSettings(OverlayDialogManager dialogManager) async { void showServerSettings(OverlayDialogManager dialogManager) async {
Map<String, dynamic> options = jsonDecode(await bind.mainGetOptions()); Map<String, dynamic> options = jsonDecode(await bind.mainGetOptions());
String id = options['custom-rendezvous-server'] ?? ""; showServerSettingsWithValue(ServerConfig.fromOptions(options), dialogManager);
String relay = options['relay-server'] ?? "";
String api = options['api-server'] ?? "";
String key = options['key'] ?? "";
showServerSettingsWithValue(id, relay, key, api, dialogManager);
} }
void showLanguageSettings(OverlayDialogManager dialogManager) async { void showLanguageSettings(OverlayDialogManager dialogManager) async {

View File

@ -237,17 +237,16 @@ void wrongPasswordDialog(String id, OverlayDialogManager dialogManager) {
])); ]));
} }
void showServerSettingsWithValue(String id, String relay, String key, void showServerSettingsWithValue(
String api, OverlayDialogManager dialogManager) async { ServerConfig serverConfig, OverlayDialogManager dialogManager) async {
Map<String, dynamic> oldOptions = jsonDecode(await bind.mainGetOptions()); Map<String, dynamic> oldOptions = jsonDecode(await bind.mainGetOptions());
String id0 = oldOptions['custom-rendezvous-server'] ?? ""; final oldCfg = ServerConfig.fromOptions(oldOptions);
String relay0 = oldOptions['relay-server'] ?? "";
String api0 = oldOptions['api-server'] ?? "";
String key0 = oldOptions['key'] ?? "";
var isInProgress = false; var isInProgress = false;
final idController = TextEditingController(text: id); final idCtrl = TextEditingController(text: serverConfig.idServer);
final relayController = TextEditingController(text: relay); final relayCtrl = TextEditingController(text: serverConfig.relayServer);
final apiController = TextEditingController(text: api); final apiCtrl = TextEditingController(text: serverConfig.apiServer);
final keyCtrl = TextEditingController(text: serverConfig.key);
String? idServerMsg; String? idServerMsg;
String? relayServerMsg; String? relayServerMsg;
@ -255,21 +254,18 @@ void showServerSettingsWithValue(String id, String relay, String key,
dialogManager.show((setState, close) { dialogManager.show((setState, close) {
Future<bool> validate() async { Future<bool> validate() async {
if (idController.text != id) { if (idCtrl.text != oldCfg.idServer) {
final res = await validateAsync(idController.text); final res = await validateAsync(idCtrl.text);
setState(() => idServerMsg = res); setState(() => idServerMsg = res);
if (idServerMsg != null) return false; if (idServerMsg != null) return false;
id = idController.text;
} }
if (relayController.text != relay) { if (relayCtrl.text != oldCfg.relayServer) {
relayServerMsg = await validateAsync(relayController.text); relayServerMsg = await validateAsync(relayCtrl.text);
if (relayServerMsg != null) return false; if (relayServerMsg != null) return false;
relay = relayController.text;
} }
if (apiController.text != relay) { if (apiCtrl.text != oldCfg.apiServer) {
apiServerMsg = await validateAsync(apiController.text); apiServerMsg = await validateAsync(apiCtrl.text);
if (apiServerMsg != null) return false; if (apiServerMsg != null) return false;
api = apiController.text;
} }
return true; return true;
} }
@ -281,7 +277,7 @@ void showServerSettingsWithValue(String id, String relay, String key,
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: <Widget>[ children: <Widget>[
TextFormField( TextFormField(
controller: idController, controller: idCtrl,
decoration: InputDecoration( decoration: InputDecoration(
labelText: translate('ID Server'), labelText: translate('ID Server'),
errorText: idServerMsg), errorText: idServerMsg),
@ -290,7 +286,7 @@ void showServerSettingsWithValue(String id, String relay, String key,
(isAndroid (isAndroid
? [ ? [
TextFormField( TextFormField(
controller: relayController, controller: relayCtrl,
decoration: InputDecoration( decoration: InputDecoration(
labelText: translate('Relay Server'), labelText: translate('Relay Server'),
errorText: relayServerMsg), errorText: relayServerMsg),
@ -299,7 +295,7 @@ void showServerSettingsWithValue(String id, String relay, String key,
: []) + : []) +
[ [
TextFormField( TextFormField(
controller: apiController, controller: apiCtrl,
decoration: InputDecoration( decoration: InputDecoration(
labelText: translate('API Server'), labelText: translate('API Server'),
), ),
@ -315,13 +311,10 @@ void showServerSettingsWithValue(String id, String relay, String key,
}, },
), ),
TextFormField( TextFormField(
initialValue: key, controller: keyCtrl,
decoration: InputDecoration( decoration: InputDecoration(
labelText: 'Key', labelText: 'Key',
), ),
onChanged: (String? value) {
if (value != null) key = value.trim();
},
), ),
Offstage( Offstage(
offstage: !isInProgress, offstage: !isInProgress,
@ -345,18 +338,21 @@ void showServerSettingsWithValue(String id, String relay, String key,
isInProgress = true; isInProgress = true;
}); });
if (await validate()) { if (await validate()) {
if (id != id0) { if (idCtrl.text != oldCfg.idServer) {
if (id0.isNotEmpty) { if (oldCfg.idServer.isNotEmpty) {
await gFFI.userModel.logOut(); await gFFI.userModel.logOut();
} }
bind.mainSetOption(key: "custom-rendezvous-server", value: id); bind.mainSetOption(
key: "custom-rendezvous-server", value: idCtrl.text);
} }
if (relay != relay0) { if (relayCtrl.text != oldCfg.relayServer) {
bind.mainSetOption(key: "relay-server", value: relay); bind.mainSetOption(key: "relay-server", value: relayCtrl.text);
} }
if (key != key0) bind.mainSetOption(key: "key", value: key); if (keyCtrl.text != oldCfg.key) {
if (api != api0) { bind.mainSetOption(key: "key", value: keyCtrl.text);
bind.mainSetOption(key: "api-server", value: api); }
if (apiCtrl.text != oldCfg.apiServer) {
bind.mainSetOption(key: "api-server", value: apiCtrl.text);
} }
close(); close();
} }
@ -429,7 +425,7 @@ class _PasswordWidgetState extends State<PasswordWidget> {
color: Theme.of(context).primaryColorDark, color: Theme.of(context).primaryColorDark,
), ),
onPressed: () { onPressed: () {
// Update the state i.e. toogle the state of passwordVisible variable // Update the state i.e. toggle the state of passwordVisible variable
setState(() { setState(() {
_passwordVisible = !_passwordVisible; _passwordVisible = !_passwordVisible;
}); });