Skip to content

Commit

Permalink
支持WG、支持全局路由
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Jul 31, 2024
1 parent 18d9536 commit 4e7356d
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 103 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ android {
ndk {
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
versionName "1.2.11"
versionName "1.2.13"
}

buildTypes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.VpnService;
import android.os.Build;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static void stopVpn() {
}
}

private int startVpn(DeviceConfig config) {
private int startVpn(DeviceConfig config) throws PackageManager.NameNotFoundException {
Builder builder = new Builder();
String ip = IpUtils.intToIpAddress(config.virtualIp);
int prefixLength = IpUtils.subnetMaskToPrefixLength(config.virtualNetmask);
Expand All @@ -74,6 +75,8 @@ private int startVpn(DeviceConfig config) {
.setBlocking(false)
.setMtu(config.mtu)
.addAddress(ip, prefixLength)
// 自己的流量不走网卡
.addDisallowedApplication("top.wherewego.vnt_app")
.addRoute(ipRoute, prefixLength);
if (config.externalRoute != null) {
for (DeviceConfig.Route routeItem : config.externalRoute) {
Expand Down
Binary file modified android/app/src/main/jniLibs/arm64-v8a/librust_lib_vnt_app.so
Binary file not shown.
Binary file modified android/app/src/main/jniLibs/armeabi-v7a/librust_lib_vnt_app.so
Binary file not shown.
Binary file modified android/app/src/main/jniLibs/x86/librust_lib_vnt_app.so
Binary file not shown.
Binary file modified android/app/src/main/jniLibs/x86_64/librust_lib_vnt_app.so
Binary file not shown.
12 changes: 11 additions & 1 deletion lib/network_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class NetworkConfig {
String punchModel;
String useChannelType;
String compressor;
bool allowWg;
String localIpv4;

NetworkConfig({
required this.itemKey,
Expand Down Expand Up @@ -55,6 +57,8 @@ class NetworkConfig {
required this.punchModel,
required this.useChannelType,
required this.compressor,
required this.allowWg,
required this.localIpv4,
});
Map<String, dynamic> toJson() {
return {
Expand Down Expand Up @@ -85,6 +89,8 @@ class NetworkConfig {
'punch_model': punchModel,
'use_channel': useChannelType,
'compressor': compressor,
'allow_wire_guard': allowWg,
'local_ipv4': localIpv4,
};
}

Expand Down Expand Up @@ -117,6 +123,8 @@ class NetworkConfig {
if (punchModel.isNotEmpty) 'punch_model': punchModel,
if (useChannelType.isNotEmpty) 'use_channel': useChannelType,
if (compressor.isNotEmpty) 'compressor': compressor,
if (allowWg) 'allow_wire_guard': allowWg,
if (localIpv4.isNotEmpty) 'local_ipv4': localIpv4,
};
}

Expand All @@ -134,7 +142,7 @@ class NetworkConfig {
portMappings: List<String>.from(json['mapping']),
groupPassword: json['password'],
isServerEncrypted: json['server_encrypt'],
protocol: json['protocol']??'UDP',
protocol: json['protocol'] ?? 'UDP',
dataFingerprintVerification: json['finger'],
encryptionAlgorithm: json['cipher_model'],
deviceID: json['device_id'],
Expand All @@ -149,6 +157,8 @@ class NetworkConfig {
punchModel: json['punch_model'],
useChannelType: json['use_channel'],
compressor: json['compressor'] ?? 'none',
allowWg: json['allow_wire_guard'] ?? false,
localIpv4: json['local_ipv4'] ?? '',
);
}
}
42 changes: 37 additions & 5 deletions lib/network_config_input_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ class _NetworkConfigInputPageState extends State<NetworkConfigInputPage> {
final _nameController = TextEditingController();
final _groupNumberController = TextEditingController();
final _deviceNameController = TextEditingController(
text: Platform.localHostname.length > 32
? Platform.localHostname.substring(0, 32)
: Platform.localHostname);
text: Platform.operatingSystemVersion.length > 16
? Platform.operatingSystemVersion.substring(0, 16)
: Platform.operatingSystemVersion);
final _virtualIPv4Controller = TextEditingController();
final _localIPv4Controller = TextEditingController();
final _serverAddressController = TextEditingController();
final _stunServers = <TextEditingController>[];
final _inIps = <TextEditingController>[];
Expand All @@ -49,6 +50,7 @@ class _NetworkConfigInputPageState extends State<NetworkConfigInputPage> {
bool _ipv6Selected = true;
bool _relaySelected = true;
bool _p2pSelected = true;
String _allowWg = 'FALSE';

String _compressionMethod = 'none'; // 默认不压缩
int _compressionLevel = 3; // 默认压缩级别
Expand Down Expand Up @@ -147,6 +149,8 @@ class _NetworkConfigInputPageState extends State<NetworkConfigInputPage> {
_compressionLevel = int.tryParse(arr[1]) ?? 3;
}
}
_allowWg = config.allowWg ? 'FALSE' : 'TRUE';
_localIPv4Controller.text = config.localIpv4;
setState(() {
_routingMode = config.firstLatency ? 'LOW_LATENCY' : 'P2P';
_builtInIpProxy = config.noInIpProxy ? 'CLOSE' : 'OPEN';
Expand Down Expand Up @@ -226,6 +230,8 @@ class _NetworkConfigInputPageState extends State<NetworkConfigInputPage> {
: (_p2pSelected ? 'p2p' : 'relay'),
compressor:
'$_compressionMethod${_compressionMethod == 'zstd' ? ',$_compressionLevel' : ''}',
allowWg: _allowWg == 'FALSE' ? false : true,
localIpv4: _localIPv4Controller.text,
);
Navigator.pop(context, config);
} else {
Expand Down Expand Up @@ -386,8 +392,7 @@ class _NetworkConfigInputPageState extends State<NetworkConfigInputPage> {
} else {
final match = addressPortRegex.firstMatch(value);
if (match != null) {
final domainRegex =
RegExp(r'^[a-zA-Z0-9.-]');
final domainRegex = RegExp(r'^[a-zA-Z0-9.-]');
if (!domainRegex.hasMatch(match.group(1)!)) {
return '地址格式错误';
}
Expand Down Expand Up @@ -451,6 +456,16 @@ class _NetworkConfigInputPageState extends State<NetworkConfigInputPage> {
});
},
),
_buildRadioGroup(
'允许WireGuard流量',
[('允许', 'TRUE'), ('不允许', 'FALSE')],
_allowWg,
(value) {
setState(() {
_allowWg = value!;
});
},
),
_buildSectionTitle('子网代理&端口映射'),
_buildDynamicTooltipFields(
'in-ip',
Expand Down Expand Up @@ -565,6 +580,23 @@ class _NetworkConfigInputPageState extends State<NetworkConfigInputPage> {
null,
false,
),
CustomTooltipTextField(
controller: _localIPv4Controller,
labelText: '本地IPv4',
tooltipMessage: '(不输入则自动获取)',
maxLength: 15,
validator: (value) {
final regex = RegExp(
r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$',
);
if (value != null &&
value.isNotEmpty &&
!regex.hasMatch(value)) {
return '请输入有效的 IPv4 地址';
}
return null;
},
),
_buildTextFormField(
_virtualNetworkCardNameController,
'虚拟网卡名称',
Expand Down
12 changes: 10 additions & 2 deletions lib/src/rust/api/vnt_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,8 @@ class VntConfig {
final int packetDelay;
final List<String> portMappingList;
final String compressor;
final bool allowWireGuard;
final String? localIpv4;

const VntConfig({
required this.tap,
Expand Down Expand Up @@ -422,6 +424,8 @@ class VntConfig {
required this.packetDelay,
required this.portMappingList,
required this.compressor,
required this.allowWireGuard,
this.localIpv4,
});

@override
Expand Down Expand Up @@ -450,7 +454,9 @@ class VntConfig {
packetLossRate.hashCode ^
packetDelay.hashCode ^
portMappingList.hashCode ^
compressor.hashCode;
compressor.hashCode ^
allowWireGuard.hashCode ^
localIpv4.hashCode;

@override
bool operator ==(Object other) =>
Expand Down Expand Up @@ -481,5 +487,7 @@ class VntConfig {
packetLossRate == other.packetLossRate &&
packetDelay == other.packetDelay &&
portMappingList == other.portMappingList &&
compressor == other.compressor;
compressor == other.compressor &&
allowWireGuard == other.allowWireGuard &&
localIpv4 == other.localIpv4;
}
14 changes: 11 additions & 3 deletions lib/src/rust/frb_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1536,8 +1536,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
VntConfig dco_decode_vnt_config(dynamic raw) {
// Codec=Dco (DartCObject based), see doc to use other codecs
final arr = raw as List<dynamic>;
if (arr.length != 25)
throw Exception('unexpected arr length: expect 25 but see ${arr.length}');
if (arr.length != 27)
throw Exception('unexpected arr length: expect 27 but see ${arr.length}');
return VntConfig(
tap: dco_decode_bool(arr[0]),
token: dco_decode_String(arr[1]),
Expand All @@ -1564,6 +1564,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
packetDelay: dco_decode_u_32(arr[22]),
portMappingList: dco_decode_list_String(arr[23]),
compressor: dco_decode_String(arr[24]),
allowWireGuard: dco_decode_bool(arr[25]),
localIpv4: dco_decode_opt_String(arr[26]),
);
}

Expand Down Expand Up @@ -2118,6 +2120,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
var var_packetDelay = sse_decode_u_32(deserializer);
var var_portMappingList = sse_decode_list_String(deserializer);
var var_compressor = sse_decode_String(deserializer);
var var_allowWireGuard = sse_decode_bool(deserializer);
var var_localIpv4 = sse_decode_opt_String(deserializer);
return VntConfig(
tap: var_tap,
token: var_token,
Expand All @@ -2143,7 +2147,9 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
packetLossRate: var_packetLossRate,
packetDelay: var_packetDelay,
portMappingList: var_portMappingList,
compressor: var_compressor);
compressor: var_compressor,
allowWireGuard: var_allowWireGuard,
localIpv4: var_localIpv4);
}

@protected
Expand Down Expand Up @@ -2723,6 +2729,8 @@ class RustLibApiImpl extends RustLibApiImplPlatform implements RustLibApi {
sse_encode_u_32(self.packetDelay, serializer);
sse_encode_list_String(self.portMappingList, serializer);
sse_encode_String(self.compressor, serializer);
sse_encode_bool(self.allowWireGuard, serializer);
sse_encode_opt_String(self.localIpv4, serializer);
}
}

Expand Down
61 changes: 31 additions & 30 deletions lib/vnt/vnt_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,37 @@ class VntBox {
});
static Future<VntBox> create(NetworkConfig config, SendPort uiCall) async {
var vntConfig = VntConfig(
tap: false,
token: config.token,
deviceId: config.deviceID,
name: config.deviceName,
serverAddressStr: config.serverAddress,
nameServers: config.dns,
stunServer: config.stunServers,
inIps: config.inIps.map((v) => IpUtils.parseInIpString(v)).toList(),
outIps: config.outIps.map((v) => IpUtils.parseOutIpString(v)).toList(),
password: config.groupPassword.isEmpty ? null : config.groupPassword,
mtu: config.mtu == 0 ? null : config.mtu,
ip: config.virtualIPv4.isEmpty ? null : config.virtualIPv4,
noProxy: config.noInIpProxy,
serverEncrypt: config.isServerEncrypted,
cipherModel: config.encryptionAlgorithm,
finger: config.dataFingerprintVerification,
punchModel: config.punchModel,
ports: config.ports.isEmpty ? null : Uint16List.fromList(config.ports),
firstLatency: config.firstLatency,
deviceName: config.virtualNetworkCardName.isEmpty
? null
: config.virtualNetworkCardName,
useChannelType: config.useChannelType,
packetLossRate: config.simulatedPacketLossRate == 0
? null
: config.simulatedPacketLossRate,
packetDelay: config.simulatedLatency,
portMappingList: config.portMappings,
compressor: config.compressor.isEmpty ? 'none' : config.compressor,
);
tap: false,
token: config.token,
deviceId: config.deviceID,
name: config.deviceName,
serverAddressStr: config.serverAddress,
nameServers: config.dns,
stunServer: config.stunServers,
inIps: config.inIps.map((v) => IpUtils.parseInIpString(v)).toList(),
outIps: config.outIps.map((v) => IpUtils.parseOutIpString(v)).toList(),
password: config.groupPassword.isEmpty ? null : config.groupPassword,
mtu: config.mtu == 0 ? null : config.mtu,
ip: config.virtualIPv4.isEmpty ? null : config.virtualIPv4,
noProxy: config.noInIpProxy,
serverEncrypt: config.isServerEncrypted,
cipherModel: config.encryptionAlgorithm,
finger: config.dataFingerprintVerification,
punchModel: config.punchModel,
ports: config.ports.isEmpty ? null : Uint16List.fromList(config.ports),
firstLatency: config.firstLatency,
deviceName: config.virtualNetworkCardName.isEmpty
? null
: config.virtualNetworkCardName,
useChannelType: config.useChannelType,
packetLossRate: config.simulatedPacketLossRate == 0
? null
: config.simulatedPacketLossRate,
packetDelay: config.simulatedLatency,
portMappingList: config.portMappings,
compressor: config.compressor.isEmpty ? 'none' : config.compressor,
allowWireGuard: config.allowWg,
localIpv4: config.localIpv4.isEmpty ? null : config.localIpv4);
var vntCall = VntApiCallback(successFn: () {
uiCall.send('success');
}, createTunFn: (info) {
Expand Down
Loading

0 comments on commit 4e7356d

Please sign in to comment.