Skip to content

Commit 1fc4d78

Browse files
committed
Integrate kerio control to IRNet
1 parent b2a502a commit 1fc4d78

6 files changed

Lines changed: 184 additions & 55 deletions

File tree

assets/kerio.png

12.4 KB
Loading

lib/data/leak_item.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ class LeakItem {
66

77
static List<String> prePopulatedUrls() {
88
return [
9-
'https://console.firebase.google.com',
109
'https://developer.android.com',
11-
'https://storage.googleapis.com/dartlang-pub-public-packages/packages/live_event-0.0.1.tar.gz',
1210
'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/4.1.3/gradle-4.1.3.pom',
11+
'https://storage.googleapis.com/dartlang-pub-public-packages/packages/live_event-0.0.1.tar.gz'
1312
];
1413
}
1514
}

lib/data/shared_preferences.dart

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
import 'package:shared_preferences/shared_preferences.dart';
22

33
class AppSharedPreferences {
4-
static SharedPreferences? __instance;
4+
static Future<String?> get kerioIP async {
5+
return (await _preference).getString(_keyKerioIP);
6+
}
57

6-
static Future<SharedPreferences> get _preference async {
7-
__instance ??= await SharedPreferences.getInstance();
8-
return __instance!;
8+
static Future<void> setKerioIP(String value) async {
9+
(await _preference).setString(_keyKerioIP, value);
10+
}
11+
12+
static Future<String?> get kerioUsername async {
13+
return (await _preference).getString(_keyKerioUsername);
14+
}
15+
16+
static Future<void> setKerioUsername(String value) async {
17+
(await _preference).setString(_keyKerioUsername, value);
18+
}
19+
20+
static Future<String?> get kerioPassword async {
21+
return (await _preference).getString(_keyKerioPassword);
22+
}
23+
24+
static Future<void> setKerioPassword(String value) async {
25+
(await _preference).setString(_keyKerioPassword, value);
26+
}
27+
28+
static Future<bool> get kerioAutoLogin async {
29+
return (await _preference).getBool(_keyKerioAutoLogin) ?? true;
30+
}
31+
32+
static Future<void> setKerioAutoLogin(bool value) async {
33+
(await _preference).setBool(_keyKerioAutoLogin, value);
934
}
1035

1136
static Future<bool> get showLeakInSysTray async {
12-
return (await _preference).getBool(_keyShowLeakInSysTray) ?? false;
37+
return (await _preference).getBool(_keyShowLeakInSysTray) ?? true;
1338
}
1439

1540
static Future<void> setShowLeakInSysTray(bool value) async {
@@ -44,7 +69,17 @@ class AppSharedPreferences {
4469
(await _preference).setString(_keyLeakCheckList, previousChecklist.join(';'));
4570
}
4671

72+
static SharedPreferences? __instance;
73+
static Future<SharedPreferences> get _preference async {
74+
__instance ??= await SharedPreferences.getInstance();
75+
return __instance!;
76+
}
77+
4778
static const _keyIsLeakPrePopulated = 'isLeakPrePopulated';
4879
static const _keyShowLeakInSysTray = 'showLeakInSysTray';
4980
static const _keyLeakCheckList = 'leakChecklist';
81+
static const _keyKerioIP = 'kerioIP';
82+
static const _keyKerioUsername = 'kerioUsername';
83+
static const _keyKerioPassword = 'kerioPassword';
84+
static const _keyKerioAutoLogin = 'kerioAutoLogin';
5085
}

lib/main.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Future<void> initSharedPreferences() async {
5454
}
5555
await AppSharedPreferences.setIsLeakPrePopulated(true);
5656
}
57+
if ((await AppSharedPreferences.kerioIP) == null) {
58+
await AppSharedPreferences.setKerioIP('172.18.18.1:4080');
59+
}
5760
}
5861

5962
Future<void> initLaunchAtStartup() async {

lib/views/kerio_login.dart

Lines changed: 137 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import 'package:flutter/material.dart';
22
import 'package:http/http.dart' as http;
3-
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
3+
import 'package:ir_net/data/shared_preferences.dart';
4+
5+
class KerioLoginView extends StatefulWidget {
6+
const KerioLoginView({super.key});
47

5-
class KerioLoginPage extends StatefulWidget {
68
@override
7-
_KerioLoginPageState createState() => _KerioLoginPageState();
9+
State<KerioLoginView> createState() => _KerioLoginViewState();
810
}
911

10-
class _KerioLoginPageState extends State<KerioLoginPage> {
12+
class _KerioLoginViewState extends State<KerioLoginView> {
1113
final TextEditingController _ipController = TextEditingController();
1214
final TextEditingController _usernameController = TextEditingController();
1315
final TextEditingController _passwordController = TextEditingController();
14-
final _storage = FlutterSecureStorage();
1516

1617
@override
1718
void initState() {
@@ -20,19 +21,21 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
2021
}
2122

2223
Future<void> _attemptAutoLogin() async {
23-
final ip = await _storage.read(key: 'kerio_ip');
24-
final username = await _storage.read(key: 'kerio_username');
25-
final password = await _storage.read(key: 'kerio_password');
24+
final ip = await AppSharedPreferences.kerioIP;
25+
final username = await AppSharedPreferences.kerioUsername;
26+
final password = await AppSharedPreferences.kerioPassword;
27+
final enabled = await AppSharedPreferences.kerioAutoLogin;
2628

27-
if (ip != null && username != null && password != null) {
29+
_ipController.text = ip ?? '';
30+
if (ip != null && username != null && password != null && enabled == true) {
2831
_ipController.text = ip;
2932
_usernameController.text = username;
3033
_passwordController.text = password;
31-
_login();
34+
_login(true);
3235
}
3336
}
3437

35-
void _login() async {
38+
void _login(bool auto) async {
3639
final ip = _ipController.text;
3740
final username = _usernameController.text;
3841
final password = _passwordController.text;
@@ -43,9 +46,9 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
4346
}
4447

4548
// Save credentials for auto-login
46-
await _storage.write(key: 'kerio_ip', value: ip);
47-
await _storage.write(key: 'kerio_username', value: username);
48-
await _storage.write(key: 'kerio_password', value: password);
49+
await AppSharedPreferences.setKerioIP(ip);
50+
await AppSharedPreferences.setKerioUsername(username);
51+
await AppSharedPreferences.setKerioPassword(password);
4952

5053
final url = 'http://$ip/internal/dologin.php';
5154
final response = await http.post(
@@ -56,11 +59,11 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
5659
},
5760
);
5861

59-
if (response.statusCode == 200) {
60-
_showMessage('Login successful');
61-
} else {
62-
_showMessage('Login failed');
62+
if (auto) {
63+
return;
6364
}
65+
66+
_showMessage('Login request sent'); // todo: handle failure case
6467
}
6568

6669
void _showMessage(String message) {
@@ -71,7 +74,7 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
7174
actions: [
7275
TextButton(
7376
onPressed: () => Navigator.of(context).pop(),
74-
child: Text('OK'),
77+
child: const Text('OK'),
7578
),
7679
],
7780
),
@@ -80,37 +83,124 @@ class _KerioLoginPageState extends State<KerioLoginPage> {
8083

8184
@override
8285
Widget build(BuildContext context) {
83-
return Scaffold(
84-
appBar: AppBar(
85-
title: Text('Kerio Login'),
86+
return SizedBox(
87+
width: 400,
88+
child: Column(
89+
crossAxisAlignment: CrossAxisAlignment.center,
90+
children: [
91+
ipInput(),
92+
const SizedBox(height: 4),
93+
Row(
94+
children: [
95+
Expanded(
96+
child: username(),
97+
),
98+
const SizedBox(width: 4),
99+
Expanded(
100+
child: password(),
101+
),
102+
],
103+
),
104+
const SizedBox(height: 8),
105+
loginRow()
106+
],
86107
),
87-
body: Padding(
88-
padding: const EdgeInsets.all(16.0),
89-
child: Column(
90-
crossAxisAlignment: CrossAxisAlignment.stretch,
91-
children: [
92-
TextField(
93-
controller: _ipController,
94-
decoration: InputDecoration(labelText: 'Kerio Login Page IP'),
95-
keyboardType: TextInputType.url,
96-
),
97-
TextField(
98-
controller: _usernameController,
99-
decoration: InputDecoration(labelText: 'Username'),
100-
),
101-
TextField(
102-
controller: _passwordController,
103-
decoration: InputDecoration(labelText: 'Password'),
104-
obscureText: true,
105-
),
106-
SizedBox(height: 20),
107-
ElevatedButton(
108-
onPressed: _login,
109-
child: Text('Login'),
110-
),
111-
],
108+
);
109+
}
110+
111+
Widget ipInput() {
112+
return TextField(
113+
controller: _ipController,
114+
decoration: InputDecoration(
115+
focusedBorder: const OutlineInputBorder(
116+
borderSide: BorderSide(color: Colors.green),
117+
),
118+
enabledBorder: const OutlineInputBorder(
119+
borderSide: BorderSide(color: Colors.blue),
120+
),
121+
hintText: 'Kerio login page IP',
122+
hintStyle: const TextStyle(color: Colors.black38),
123+
suffixIcon: IconButton(
124+
onPressed: null,
125+
icon: Image.asset('assets/kerio.png', width: 24, height: 24),
126+
),
127+
),
128+
keyboardType: TextInputType.url,
129+
);
130+
}
131+
132+
Widget username() {
133+
return TextField(
134+
controller: _usernameController,
135+
decoration: const InputDecoration(
136+
focusedBorder: OutlineInputBorder(
137+
borderSide: BorderSide(color: Colors.green),
138+
),
139+
enabledBorder: OutlineInputBorder(
140+
borderSide: BorderSide(color: Colors.blue),
112141
),
142+
hintText: 'Username',
143+
hintStyle: TextStyle(color: Colors.black38),
113144
),
114145
);
115146
}
147+
148+
Widget password() {
149+
return TextField(
150+
controller: _passwordController,
151+
decoration: const InputDecoration(
152+
focusedBorder: OutlineInputBorder(
153+
borderSide: BorderSide(color: Colors.green),
154+
),
155+
enabledBorder: OutlineInputBorder(
156+
borderSide: BorderSide(color: Colors.blue),
157+
),
158+
hintText: 'Password',
159+
hintStyle: TextStyle(color: Colors.black38),
160+
),
161+
obscureText: true,
162+
);
163+
}
164+
165+
Widget loginRow() {
166+
return Row(
167+
children: [
168+
Expanded(
169+
flex: 2,
170+
child: ElevatedButton(
171+
onPressed: () => _login(false),
172+
style: ElevatedButton.styleFrom(
173+
shape: const RoundedRectangleBorder(
174+
borderRadius: BorderRadius.all(Radius.circular(8)),
175+
),
176+
padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
177+
backgroundColor: Colors.blue,
178+
),
179+
child: const Text('Login', style: TextStyle(color: Colors.white)),
180+
),
181+
),
182+
const SizedBox(width: 10),
183+
Expanded(
184+
child: autoLoginOption(),
185+
)
186+
],
187+
);
188+
}
189+
190+
Widget autoLoginOption() {
191+
return FutureBuilder<bool>(
192+
future: AppSharedPreferences.kerioAutoLogin,
193+
builder: (context, snapshot) {
194+
final value = snapshot.data ?? false;
195+
return CheckboxListTile(
196+
title: const Text('Auto?'),
197+
value: value,
198+
onChanged: (enabled) async {
199+
await AppSharedPreferences.setKerioAutoLogin(enabled ?? false);
200+
setState(() {});
201+
},
202+
);
203+
},
204+
);
205+
}
116206
}

lib/views/leak.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:ir_net/data/leak_item.dart';
33
import 'package:ir_net/main.dart';
4+
import 'package:ir_net/views/kerio_login.dart';
45
import 'package:touch_mouse_behavior/touch_mouse_behavior.dart';
56
import 'package:url_launcher/url_launcher.dart';
67

@@ -41,6 +42,7 @@ class _LeakViewState extends State<LeakView> {
4142
),
4243
const SizedBox(height: 16),
4344
items(),
45+
const KerioLoginView()
4446
],
4547
);
4648
}
@@ -55,7 +57,7 @@ class _LeakViewState extends State<LeakView> {
5557
}
5658
return SizedBox(
5759
width: 400,
58-
height: 400,
60+
height: 250,
5961
child: TouchMouseScrollable(
6062
child: ListView.builder(
6163
itemCount: data.length,

0 commit comments

Comments
 (0)