|
| 1 | +package top.wherewego.vnt_app; |
| 2 | + |
| 3 | +import android.os.Build; |
| 4 | +import android.service.quicksettings.Tile; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import androidx.annotation.NonNull; |
| 8 | +import androidx.annotation.Nullable; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.function.Function; |
| 14 | + |
| 15 | +import io.flutter.embedding.engine.FlutterEngine; |
| 16 | +import io.flutter.plugin.common.MethodChannel; |
| 17 | +import top.wherewego.vnt_app.vpn.DeviceConfig; |
| 18 | +import top.wherewego.vnt_app.vpn.IpUtils; |
| 19 | + |
| 20 | +public class FlutterMethodChannel { |
| 21 | + private static final String CHANNEL = "top.wherewego.vnt/vpn"; |
| 22 | + |
| 23 | + private volatile static MethodChannel channel; |
| 24 | + private volatile static MethodChannel.Result pendingResult; |
| 25 | + private volatile static boolean tileStart = false; |
| 26 | + |
| 27 | + public static void setTileStart(boolean tileStart) { |
| 28 | + FlutterMethodChannel.tileStart = tileStart; |
| 29 | + } |
| 30 | + |
| 31 | + public static boolean initialized() { |
| 32 | + return channel != null; |
| 33 | + } |
| 34 | + |
| 35 | + public static void init(FlutterEngine flutterEngine, Callback callback) { |
| 36 | + channel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL); |
| 37 | + channel.setMethodCallHandler((call, result) -> { |
| 38 | + switch (call.method) { |
| 39 | + case "startVpn": |
| 40 | + DeviceConfig config; |
| 41 | + try { |
| 42 | + Map<String, Object> arguments = call.arguments(); |
| 43 | + config = parseDeviceConfig(arguments); |
| 44 | + } catch (Exception e) { |
| 45 | + result.error("VPN_ERROR", "Invalid DeviceConfig", e); |
| 46 | + return; |
| 47 | + } |
| 48 | + try { |
| 49 | + pendingResult = result; |
| 50 | + callback.startVpn(config); |
| 51 | + } catch (Exception e) { |
| 52 | + result.error("VPN_ERROR", e.getMessage(), e); |
| 53 | + } |
| 54 | + |
| 55 | + break; |
| 56 | + case "stopVpn": |
| 57 | + callback.stopVpn(); |
| 58 | + result.success(null); |
| 59 | + break; |
| 60 | + case "moveTaskToBack": |
| 61 | + callback.moveToBack(); |
| 62 | + result.success(null); |
| 63 | + break; |
| 64 | + case "isTileStart": |
| 65 | + result.success(tileStart); |
| 66 | + break; |
| 67 | + default: |
| 68 | + result.notImplemented(); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + public static void callSuccess(int fd) { |
| 76 | + if (pendingResult != null) { |
| 77 | + pendingResult.success(fd); |
| 78 | + } |
| 79 | + pendingResult = null; |
| 80 | + } |
| 81 | + |
| 82 | + public static void callError(String msg, Exception e) { |
| 83 | + if (pendingResult != null) { |
| 84 | + pendingResult.error("VPN_ERROR", msg, e); |
| 85 | + } |
| 86 | + pendingResult = null; |
| 87 | + } |
| 88 | + |
| 89 | + public static void startVnt(Function<Boolean, Void> function) { |
| 90 | + if (channel == null) { |
| 91 | + return; |
| 92 | + } |
| 93 | + channel.invokeMethod("startVnt", null, new MethodChannel.Result() { |
| 94 | + @Override |
| 95 | + public void success(@Nullable Object result) { |
| 96 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 97 | + function.apply(Boolean.TRUE.equals(result)); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void error(@NonNull String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) { |
| 103 | + Log.e("FlutterChannel", errorMessage); |
| 104 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 105 | + function.apply(false); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void notImplemented() { |
| 111 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 112 | + function.apply(false); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + public static void stopVnt() { |
| 119 | + if (channel == null) { |
| 120 | + return; |
| 121 | + } |
| 122 | + channel.invokeMethod("stopVnt", null); |
| 123 | + } |
| 124 | + |
| 125 | + public static void isRunning(Function<Boolean, Void> function) { |
| 126 | + if (channel == null) { |
| 127 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 128 | + function.apply(false); |
| 129 | + } |
| 130 | + return; |
| 131 | + } |
| 132 | + channel.invokeMethod("isRunning", null, new MethodChannel.Result() { |
| 133 | + @Override |
| 134 | + public void success(@Nullable Object result) { |
| 135 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 136 | + function.apply(Boolean.TRUE.equals(result)); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void error(@NonNull String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails) { |
| 142 | + Log.e("FlutterChannel", errorMessage); |
| 143 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 144 | + function.apply(false); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void notImplemented() { |
| 150 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 151 | + function.apply(false); |
| 152 | + } |
| 153 | + } |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + private static DeviceConfig parseDeviceConfig(Map<String, Object> arguments) { |
| 158 | + String virtualIp = (String) arguments.get("virtualIp"); |
| 159 | + String virtualNetmask = (String) arguments.get("virtualNetmask"); |
| 160 | + String virtualGateway = (String) arguments.get("virtualGateway"); |
| 161 | + Integer mtu = (Integer) arguments.get("mtu"); |
| 162 | + |
| 163 | + List<Map<String, String>> externalRouteList = (List<Map<String, String>>) arguments.get("externalRoute"); |
| 164 | + List<DeviceConfig.Route> externalRoute = new ArrayList<>(); |
| 165 | + if (externalRouteList != null) { |
| 166 | + for (Map<String, String> route : externalRouteList) { |
| 167 | + String destination = route.get("destination"); |
| 168 | + String netmask = route.get("netmask"); |
| 169 | + externalRoute.add(new DeviceConfig.Route(IpUtils.ipToInt(destination), IpUtils.ipToInt(netmask))); |
| 170 | + } |
| 171 | + } |
| 172 | + return new DeviceConfig(IpUtils.ipToInt(virtualIp), IpUtils.ipToInt(virtualNetmask), IpUtils.ipToInt(virtualGateway), mtu, externalRoute); |
| 173 | + } |
| 174 | + |
| 175 | + public interface Callback { |
| 176 | + int startVpn(DeviceConfig config); |
| 177 | + |
| 178 | + void stopVpn(); |
| 179 | + |
| 180 | + void moveToBack(); |
| 181 | + } |
| 182 | +} |
0 commit comments