add wrapper for min modifications

Signed-off-by: dignow <linlong1265@gmail.com>
This commit is contained in:
dignow 2023-07-23 18:01:00 +08:00
parent b80051bb35
commit dfd5ea8a7f

View File

@ -525,75 +525,80 @@ class _SafetyState extends State<_Safety> with AutomaticKeepAliveClientMixin {
Widget permissions(context) { Widget permissions(context) {
bool enabled = !locked; bool enabled = !locked;
String accessMode = bind.mainGetOptionSync(key: 'access-mode'); // Simple temp wrapper for PR check
_AccessMode mode; tmpWrapper() {
if (accessMode == 'full') { String accessMode = bind.mainGetOptionSync(key: 'access-mode');
mode = _AccessMode.full; _AccessMode mode;
} else if (accessMode == 'view') { if (accessMode == 'full') {
mode = _AccessMode.view; mode = _AccessMode.full;
} else { } else if (accessMode == 'view') {
mode = _AccessMode.custom; mode = _AccessMode.view;
} } else {
String initialKey; mode = _AccessMode.custom;
bool? fakeValue; }
switch (mode) { String initialKey;
case _AccessMode.custom: bool? fakeValue;
initialKey = ''; switch (mode) {
fakeValue = null; case _AccessMode.custom:
break; initialKey = '';
case _AccessMode.full: fakeValue = null;
initialKey = 'full'; break;
fakeValue = true; case _AccessMode.full:
break; initialKey = 'full';
case _AccessMode.view: fakeValue = true;
initialKey = 'view'; break;
fakeValue = false; case _AccessMode.view:
break; initialKey = 'view';
fakeValue = false;
break;
}
return _Card(title: 'Permissions', children: [
_ComboBox(
keys: [
'',
'full',
'view',
],
values: [
translate('Custom'),
translate('Full Access'),
translate('Screen Share'),
],
enabled: enabled,
initialKey: initialKey,
onChanged: (mode) async {
await bind.mainSetOption(key: 'access-mode', value: mode);
setState(() {});
}).marginOnly(left: _kContentHMargin),
Column(
children: [
_OptionCheckBox(context, 'Enable Keyboard/Mouse', 'enable-keyboard',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable Clipboard', 'enable-clipboard',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(
context, 'Enable File Transfer', 'enable-file-transfer',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable Audio', 'enable-audio',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable TCP Tunneling', 'enable-tunnel',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(
context, 'Enable Remote Restart', 'enable-remote-restart',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(
context, 'Enable Recording Session', 'enable-record-session',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable remote configuration modification',
'allow-remote-config-modification',
enabled: enabled, fakeValue: fakeValue),
],
),
]);
} }
return _Card(title: 'Permissions', children: [ return tmpWrapper();
_ComboBox(
keys: [
'',
'full',
'view',
],
values: [
translate('Custom'),
translate('Full Access'),
translate('Screen Share'),
],
enabled: enabled,
initialKey: initialKey,
onChanged: (mode) async {
await bind.mainSetOption(key: 'access-mode', value: mode);
setState(() {});
}).marginOnly(left: _kContentHMargin),
Column(
children: [
_OptionCheckBox(context, 'Enable Keyboard/Mouse', 'enable-keyboard',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable Clipboard', 'enable-clipboard',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(
context, 'Enable File Transfer', 'enable-file-transfer',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable Audio', 'enable-audio',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable TCP Tunneling', 'enable-tunnel',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(
context, 'Enable Remote Restart', 'enable-remote-restart',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(
context, 'Enable Recording Session', 'enable-record-session',
enabled: enabled, fakeValue: fakeValue),
_OptionCheckBox(context, 'Enable remote configuration modification',
'allow-remote-config-modification',
enabled: enabled, fakeValue: fakeValue),
],
),
]);
} }
Widget password(BuildContext context) { Widget password(BuildContext context) {
@ -752,93 +757,105 @@ class _SafetyState extends State<_Safety> with AutomaticKeepAliveClientMixin {
_OptionCheckBox(context, 'Enable Direct IP Access', 'direct-server', _OptionCheckBox(context, 'Enable Direct IP Access', 'direct-server',
update: update, enabled: !locked), update: update, enabled: !locked),
() { () {
bool enabled = option2bool( // Simple temp wrapper for PR check
'direct-server', bind.mainGetOptionSync(key: 'direct-server')); tmpWrapper() {
if (!enabled) applyEnabled.value = false; bool enabled = option2bool(
controller.text = bind.mainGetOptionSync(key: 'direct-access-port'); 'direct-server', bind.mainGetOptionSync(key: 'direct-server'));
return Offstage( if (!enabled) applyEnabled.value = false;
offstage: !enabled, controller.text = bind.mainGetOptionSync(key: 'direct-access-port');
child: _SubLabeledWidget( return Offstage(
context, offstage: !enabled,
'Port', child: _SubLabeledWidget(
Row(children: [ context,
SizedBox( 'Port',
width: 95, Row(children: [
child: TextField( SizedBox(
controller: controller, width: 95,
enabled: enabled && !locked, child: TextField(
onChanged: (_) => applyEnabled.value = true, controller: controller,
inputFormatters: [ enabled: enabled && !locked,
FilteringTextInputFormatter.allow(RegExp( onChanged: (_) => applyEnabled.value = true,
r'^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$')), inputFormatters: [
], FilteringTextInputFormatter.allow(RegExp(
decoration: const InputDecoration( r'^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$')),
hintText: '21118', ],
contentPadding: decoration: const InputDecoration(
EdgeInsets.symmetric(vertical: 12, horizontal: 12), hintText: '21118',
), contentPadding:
).marginOnly(right: 15), EdgeInsets.symmetric(vertical: 12, horizontal: 12),
),
Obx(() => ElevatedButton(
onPressed: applyEnabled.value && enabled && !locked
? () async {
applyEnabled.value = false;
await bind.mainSetOption(
key: 'direct-access-port',
value: controller.text);
}
: null,
child: Text(
translate('Apply'),
), ),
)) ).marginOnly(right: 15),
]), ),
enabled: enabled && !locked, Obx(() => ElevatedButton(
), onPressed: applyEnabled.value && enabled && !locked
); ? () async {
applyEnabled.value = false;
await bind.mainSetOption(
key: 'direct-access-port',
value: controller.text);
}
: null,
child: Text(
translate('Apply'),
),
))
]),
enabled: enabled && !locked,
),
);
}
return tmpWrapper();
}(), }(),
]; ];
} }
Widget whitelist() { Widget whitelist() {
bool enabled = !locked; bool enabled = !locked;
RxBool hasWhitelist = // Simple temp wrapper for PR check
bind.mainGetOptionSync(key: 'whitelist').isNotEmpty.obs; tmpWrapper() {
update() async { RxBool hasWhitelist =
hasWhitelist.value = bind.mainGetOptionSync(key: 'whitelist').isNotEmpty; bind.mainGetOptionSync(key: 'whitelist').isNotEmpty.obs;
} update() async {
hasWhitelist.value =
bind.mainGetOptionSync(key: 'whitelist').isNotEmpty;
}
onChanged(bool? checked) async { onChanged(bool? checked) async {
changeWhiteList(callback: update); changeWhiteList(callback: update);
} }
return GestureDetector( return GestureDetector(
child: Tooltip( child: Tooltip(
message: translate('whitelist_tip'), message: translate('whitelist_tip'),
child: Obx(() => Row( child: Obx(() => Row(
children: [ children: [
Checkbox( Checkbox(
value: hasWhitelist.value, value: hasWhitelist.value,
onChanged: enabled ? onChanged : null) onChanged: enabled ? onChanged : null)
.marginOnly(right: 5),
Offstage(
offstage: !hasWhitelist.value,
child: const Icon(Icons.warning_amber_rounded,
color: Color.fromARGB(255, 255, 204, 0))
.marginOnly(right: 5), .marginOnly(right: 5),
), Offstage(
Expanded( offstage: !hasWhitelist.value,
child: Text( child: const Icon(Icons.warning_amber_rounded,
translate('Use IP Whitelisting'), color: Color.fromARGB(255, 255, 204, 0))
style: TextStyle(color: _disabledTextColor(context, enabled)), .marginOnly(right: 5),
)) ),
], Expanded(
)), child: Text(
), translate('Use IP Whitelisting'),
onTap: () { style:
onChanged(!hasWhitelist.value); TextStyle(color: _disabledTextColor(context, enabled)),
}, ))
).marginOnly(left: _kCheckBoxLeftMargin); ],
)),
),
onTap: () {
onChanged(!hasWhitelist.value);
},
).marginOnly(left: _kCheckBoxLeftMargin);
}
return tmpWrapper();
} }
Widget hide_cm(bool enabled) { Widget hide_cm(bool enabled) {
@ -923,150 +940,156 @@ class _NetworkState extends State<_Network> with AutomaticKeepAliveClientMixin {
} }
server(bool enabled) { server(bool enabled) {
// Setting page is not modal, oldOptions should only be used when getting options, never when setting. // Simple temp wrapper for PR check
Map<String, dynamic> oldOptions = tmpWrapper() {
jsonDecode(bind.mainGetOptionsSync() as String); // Setting page is not modal, oldOptions should only be used when getting options, never when setting.
old(String key) { Map<String, dynamic> oldOptions =
return (oldOptions[key] ?? '').trim(); jsonDecode(bind.mainGetOptionsSync() as String);
} old(String key) {
return (oldOptions[key] ?? '').trim();
}
RxString idErrMsg = ''.obs; RxString idErrMsg = ''.obs;
RxString relayErrMsg = ''.obs; RxString relayErrMsg = ''.obs;
RxString apiErrMsg = ''.obs; RxString apiErrMsg = ''.obs;
var idController = var idController =
TextEditingController(text: old('custom-rendezvous-server')); TextEditingController(text: old('custom-rendezvous-server'));
var relayController = TextEditingController(text: old('relay-server')); var relayController = TextEditingController(text: old('relay-server'));
var apiController = TextEditingController(text: old('api-server')); var apiController = TextEditingController(text: old('api-server'));
var keyController = TextEditingController(text: old('key')); var keyController = TextEditingController(text: old('key'));
set(String idServer, String relayServer, String apiServer, set(String idServer, String relayServer, String apiServer,
String key) async { String key) async {
idServer = idServer.trim(); idServer = idServer.trim();
relayServer = relayServer.trim(); relayServer = relayServer.trim();
apiServer = apiServer.trim(); apiServer = apiServer.trim();
key = key.trim(); key = key.trim();
if (idServer.isNotEmpty) { if (idServer.isNotEmpty) {
idErrMsg.value = idErrMsg.value =
translate(await bind.mainTestIfValidServer(server: idServer)); translate(await bind.mainTestIfValidServer(server: idServer));
if (idErrMsg.isNotEmpty) { if (idErrMsg.isNotEmpty) {
return false; return false;
}
}
if (relayServer.isNotEmpty) {
relayErrMsg.value =
translate(await bind.mainTestIfValidServer(server: relayServer));
if (relayErrMsg.isNotEmpty) {
return false;
}
}
if (apiServer.isNotEmpty) {
if (!apiServer.startsWith('http://') &&
!apiServer.startsWith('https://')) {
apiErrMsg.value =
'${translate("API Server")}: ${translate("invalid_http")}';
return false;
}
}
final old = await bind.mainGetOption(key: 'custom-rendezvous-server');
if (old.isNotEmpty && old != idServer) {
await gFFI.userModel.logOut();
}
// should set one by one
await bind.mainSetOption(
key: 'custom-rendezvous-server', value: idServer);
await bind.mainSetOption(key: 'relay-server', value: relayServer);
await bind.mainSetOption(key: 'api-server', value: apiServer);
await bind.mainSetOption(key: 'key', value: key);
return true;
}
submit() async {
bool result = await set(idController.text, relayController.text,
apiController.text, keyController.text);
if (result) {
setState(() {});
showToast(translate('Successful'));
} else {
showToast(translate('Failed'));
} }
} }
if (relayServer.isNotEmpty) {
relayErrMsg.value =
translate(await bind.mainTestIfValidServer(server: relayServer));
if (relayErrMsg.isNotEmpty) {
return false;
}
}
if (apiServer.isNotEmpty) {
if (!apiServer.startsWith('http://') &&
!apiServer.startsWith('https://')) {
apiErrMsg.value =
'${translate("API Server")}: ${translate("invalid_http")}';
return false;
}
}
final old = await bind.mainGetOption(key: 'custom-rendezvous-server');
if (old.isNotEmpty && old != idServer) {
await gFFI.userModel.logOut();
}
// should set one by one
await bind.mainSetOption(
key: 'custom-rendezvous-server', value: idServer);
await bind.mainSetOption(key: 'relay-server', value: relayServer);
await bind.mainSetOption(key: 'api-server', value: apiServer);
await bind.mainSetOption(key: 'key', value: key);
return true;
}
submit() async { import() {
bool result = await set(idController.text, relayController.text, Clipboard.getData(Clipboard.kTextPlain).then((value) {
apiController.text, keyController.text); final text = value?.text;
if (result) { if (text != null && text.isNotEmpty) {
setState(() {}); try {
showToast(translate('Successful')); final sc = ServerConfig.decode(text);
} else { if (sc.idServer.isNotEmpty) {
showToast(translate('Failed')); idController.text = sc.idServer;
} relayController.text = sc.relayServer;
} apiController.text = sc.apiServer;
keyController.text = sc.key;
import() { Future<bool> success =
Clipboard.getData(Clipboard.kTextPlain).then((value) { set(sc.idServer, sc.relayServer, sc.apiServer, sc.key);
final text = value?.text; success.then((value) {
if (text != null && text.isNotEmpty) { if (value) {
try { showToast(
final sc = ServerConfig.decode(text); translate('Import server configuration successfully'));
if (sc.idServer.isNotEmpty) { } else {
idController.text = sc.idServer; showToast(translate('Invalid server configuration'));
relayController.text = sc.relayServer; }
apiController.text = sc.apiServer; });
keyController.text = sc.key; } else {
Future<bool> success = showToast(translate('Invalid server configuration'));
set(sc.idServer, sc.relayServer, sc.apiServer, sc.key); }
success.then((value) { } catch (e) {
if (value) {
showToast(
translate('Import server configuration successfully'));
} else {
showToast(translate('Invalid server configuration'));
}
});
} else {
showToast(translate('Invalid server configuration')); showToast(translate('Invalid server configuration'));
} }
} catch (e) { } else {
showToast(translate('Invalid server configuration')); showToast(translate('Clipboard is empty'));
} }
} else { });
showToast(translate('Clipboard is empty')); }
}
});
}
export() { export() {
final text = ServerConfig( final text = ServerConfig(
idServer: idController.text, idServer: idController.text,
relayServer: relayController.text, relayServer: relayController.text,
apiServer: apiController.text, apiServer: apiController.text,
key: keyController.text) key: keyController.text)
.encode(); .encode();
debugPrint("ServerConfig export: $text"); debugPrint("ServerConfig export: $text");
Clipboard.setData(ClipboardData(text: text)); Clipboard.setData(ClipboardData(text: text));
showToast(translate('Export server configuration successfully')); showToast(translate('Export server configuration successfully'));
} }
bool secure = !enabled; bool secure = !enabled;
return _Card(title: 'ID/Relay Server', title_suffix: [ return _Card(title: 'ID/Relay Server', title_suffix: [
Tooltip( Tooltip(
message: translate('Import Server Config'), message: translate('Import Server Config'),
child: IconButton(
icon: Icon(Icons.paste, color: Colors.grey),
onPressed: enabled ? import : null),
),
Tooltip(
message: translate('Export Server Config'),
child: IconButton( child: IconButton(
icon: Icon(Icons.copy, color: Colors.grey), icon: Icon(Icons.paste, color: Colors.grey),
onPressed: enabled ? export : null)), onPressed: enabled ? import : null),
], children: [ ),
Column( Tooltip(
children: [ message: translate('Export Server Config'),
Obx(() => _LabeledTextField(context, 'ID Server', idController, child: IconButton(
idErrMsg.value, enabled, secure)), icon: Icon(Icons.copy, color: Colors.grey),
Obx(() => _LabeledTextField(context, 'Relay Server', relayController, onPressed: enabled ? export : null)),
relayErrMsg.value, enabled, secure)), ], children: [
Obx(() => _LabeledTextField(context, 'API Server', apiController, Column(
apiErrMsg.value, enabled, secure)), children: [
_LabeledTextField(context, 'Key', keyController, '', enabled, secure), Obx(() => _LabeledTextField(context, 'ID Server', idController,
Row( idErrMsg.value, enabled, secure)),
mainAxisAlignment: MainAxisAlignment.end, Obx(() => _LabeledTextField(context, 'Relay Server',
children: [_Button('Apply', submit, enabled: enabled)], relayController, relayErrMsg.value, enabled, secure)),
).marginOnly(top: 10), Obx(() => _LabeledTextField(context, 'API Server', apiController,
], apiErrMsg.value, enabled, secure)),
) _LabeledTextField(
]); context, 'Key', keyController, '', enabled, secure),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [_Button('Apply', submit, enabled: enabled)],
).marginOnly(top: 10),
],
)
]);
}
return tmpWrapper();
} }
} }