Commit code. Update time: 2023-06-25
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import 'package:flutter/foundation.dart'
|
||||
show defaultTargetPlatform, kIsWeb, TargetPlatform;
|
||||
|
||||
enum Os {
|
||||
web,
|
||||
android,
|
||||
ios,
|
||||
macOS,
|
||||
linux,
|
||||
windows,
|
||||
fuchsia,
|
||||
}
|
||||
|
||||
class Platform {
|
||||
const Platform();
|
||||
|
||||
/// Platform is Web.
|
||||
static bool get isWeb => os == Os.web;
|
||||
|
||||
/// Platform is Android.
|
||||
static bool get isAndroid => os == Os.android;
|
||||
|
||||
/// Platform is IOS.
|
||||
static bool get isIOS => os == Os.ios;
|
||||
|
||||
/// Platform is Fuchsia.
|
||||
static bool get isFuchsia => os == Os.fuchsia;
|
||||
|
||||
/// Platform is Linux.
|
||||
static bool get isLinux => os == Os.linux;
|
||||
|
||||
/// Platform is MacOS.
|
||||
static bool get isMacOS => os == Os.macOS;
|
||||
|
||||
/// Platform is Windows.
|
||||
static bool get isWindows => os == Os.windows;
|
||||
|
||||
/// Platform is Android or IOS.
|
||||
static bool get isMobile => isAndroid || isIOS;
|
||||
|
||||
/// Platform is Android or IOS or Fuchsia.
|
||||
static bool get isFullMobile => isMobile || isFuchsia;
|
||||
|
||||
/// Platform is Linux or Windows or MacOS.
|
||||
static bool get isDesktop => isLinux || isWindows || isMacOS;
|
||||
|
||||
/// Getting the os name.
|
||||
static Os get os {
|
||||
if (kIsWeb) {
|
||||
return Os.web;
|
||||
}
|
||||
switch (defaultTargetPlatform) {
|
||||
case TargetPlatform.android:
|
||||
return Os.android;
|
||||
case TargetPlatform.iOS:
|
||||
return Os.ios;
|
||||
case TargetPlatform.macOS:
|
||||
return Os.macOS;
|
||||
case TargetPlatform.windows:
|
||||
return Os.windows;
|
||||
case TargetPlatform.fuchsia:
|
||||
return Os.fuchsia;
|
||||
case TargetPlatform.linux:
|
||||
return Os.linux;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:sail/channels/Platform.dart';
|
||||
import 'package:sail/utils/common_util.dart';
|
||||
|
||||
enum VpnStatus {
|
||||
connected(code: 2),
|
||||
connecting(code: 1),
|
||||
reasserting(code: 4),
|
||||
disconnecting(code: 5),
|
||||
disconnected(code: 0),
|
||||
invalid(code: 3);
|
||||
|
||||
const VpnStatus({required this.code});
|
||||
|
||||
final int code;
|
||||
}
|
||||
|
||||
class VpnManager {
|
||||
Future<VpnStatus> getStatus() async {
|
||||
// Native channel
|
||||
const platform = MethodChannel("com.sail_tunnel.sail/vpn_manager");
|
||||
if (Platform.isAndroid || Platform.isMacOS) {
|
||||
bool? result = await platform.invokeMethod("getStatus");
|
||||
// print("${result}");
|
||||
return (result ?? false) ? VpnStatus.connected : VpnStatus.disconnected;
|
||||
}
|
||||
|
||||
int result;
|
||||
try {
|
||||
// bool xxx = await platform.invokeMethod("getStatus");
|
||||
// result = xxx ? 1 : 0;
|
||||
result = await platform.invokeMethod("getStatus");
|
||||
} on PlatformException catch (e) {
|
||||
print(e.toString());
|
||||
|
||||
rethrow;
|
||||
}
|
||||
return VpnStatus.values.firstWhere((e) => e.code == result);
|
||||
}
|
||||
|
||||
Future<DateTime> getConnectedDate() async {
|
||||
// Native channel
|
||||
const platform = MethodChannel("com.sail_tunnel.sail/vpn_manager");
|
||||
double result;
|
||||
try {
|
||||
if (Platform.isAndroid || Platform.isMacOS) {
|
||||
// bool? result = await platform.invokeMethod("getConnectedDate");
|
||||
return DateTime.fromMillisecondsSinceEpoch((1 * 1000).toInt());
|
||||
}
|
||||
result = await platform.invokeMethod("getConnectedDate");
|
||||
} on PlatformException catch (e) {
|
||||
print(e.toString());
|
||||
|
||||
rethrow;
|
||||
}
|
||||
return DateTime.fromMillisecondsSinceEpoch((result * 1000).toInt());
|
||||
}
|
||||
|
||||
Future<bool> toggle() async {
|
||||
// Native channel
|
||||
const platform = MethodChannel("com.sail_tunnel.sail/vpn_manager");
|
||||
bool result = false;
|
||||
try {
|
||||
result = await platform.invokeMethod("toggle");
|
||||
} on PlatformException catch (e) {
|
||||
print(e.toString());
|
||||
|
||||
rethrow;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<String> getTunnelLog() async {
|
||||
// Native channel
|
||||
const platform = MethodChannel("com.sail_tunnel.sail/vpn_manager");
|
||||
String result;
|
||||
try {
|
||||
result = await platform.invokeMethod("getTunnelLog");
|
||||
} on PlatformException catch (e) {
|
||||
print(e.toString());
|
||||
|
||||
rethrow;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<String> getTunnelConfiguration() async {
|
||||
// Native channel
|
||||
const platform = MethodChannel("com.sail_tunnel.sail/vpn_manager");
|
||||
String result;
|
||||
try {
|
||||
result = await platform.invokeMethod("getTunnelConfiguration");
|
||||
} on PlatformException catch (e) {
|
||||
print(e.toString());
|
||||
|
||||
rethrow;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<String> setTunnelConfiguration(String conf) async {
|
||||
// Native channel
|
||||
const platform = MethodChannel("com.sail_tunnel.sail/vpn_manager");
|
||||
String result;
|
||||
try {
|
||||
result = await platform.invokeMethod("setTunnelConfiguration", conf);
|
||||
} on PlatformException catch (e) {
|
||||
print(e.toString());
|
||||
|
||||
rethrow;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user