feat(port_forward): Supports local, remote, and dynamic port forwarding types (#1096)
* feat(port_forward): Supports local, remote, and dynamic port forwarding types Added the PortForwardType enumeration to extend port forwarding functionality, supporting three modes: 1. Local forwarding (Local) 2. Remote forwarding (Remote) 3. Dynamic forwarding (SOCKS5) Refactored the PortForwardConfig model and related adapters, and updated the UI configuration interface to support type selection * fix(port_forward): Fixed display and validation issues with port forwarding configurations Fixed the display logic for the local host; when the type is set to “Dynamic Forwarding,” 127.0.0.1 is used by default Added validation for required fields in remote forwarding configurations to ensure that the remote host and port are not empty Optimized remote forwarding log messages by removing redundant local address displays * fix(port_forward): Fixed issues with remote port forwarding configuration and connections - Fixed the handling of default values when the remote port forwarding type field is empty - Corrected the labels for local/remote host and port displayed on the remote port forwarding interface - Fixed the local port validation logic to disallow 0 or negative numbers - Implemented connection management and error handling for remote port forwarding * feat (Port Forwarding): Add localization labels for types and optimize code Add localization labels for local and remote types in the port forwarding feature Simplify the logic for retrieving prompt text on the port forwarding page Change the default binding host from ‘0.0.0.0’ to 'localhost' * fix(port_forward): Fixed an issue with the display format of remote port forwarding addresses Added special handling for remote port forwarding types in the `displayAddr` method of `PortForwardConfig` to correctly display the remote bound address and port. Also optimized the code formatting to improve readability. * refactor(port_forward): Remove automatically generated JSON serialization code and implement it manually Modify the JSON parsing logic in PortForwardConfig and remove the automatically generated .g.dart files Simplify the handling of localhost addresses in displayAddr
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'port_forward.freezed.dart';
|
||||
part 'port_forward.g.dart';
|
||||
|
||||
enum PortForwardType {
|
||||
@JsonValue('local')
|
||||
local,
|
||||
@JsonValue('remote')
|
||||
remote,
|
||||
@JsonValue('dynamic')
|
||||
dynamic,
|
||||
}
|
||||
|
||||
@freezed
|
||||
abstract class PortForwardConfig with _$PortForwardConfig {
|
||||
@@ -9,18 +17,50 @@ abstract class PortForwardConfig with _$PortForwardConfig {
|
||||
required String id,
|
||||
required String serverId,
|
||||
required String name,
|
||||
@Default('localhost') String localHost,
|
||||
required int localPort,
|
||||
required String remoteHost,
|
||||
required int remotePort,
|
||||
String? description,
|
||||
required PortForwardType type,
|
||||
String? localHost,
|
||||
@Default(0) int localPort,
|
||||
String? remoteHost,
|
||||
int? remotePort,
|
||||
}) = _PortForwardConfig;
|
||||
|
||||
factory PortForwardConfig.fromJson(Map<String, dynamic> json) => _$PortForwardConfigFromJson(json);
|
||||
factory PortForwardConfig.fromJson(Map<String, dynamic> json) {
|
||||
PortForwardType type;
|
||||
if (json['type'] == null) {
|
||||
type = PortForwardType.local;
|
||||
} else {
|
||||
final typeStr = json['type'] as String;
|
||||
type = PortForwardType.values.firstWhere(
|
||||
(e) => e.name == typeStr,
|
||||
orElse: () => PortForwardType.local,
|
||||
);
|
||||
}
|
||||
return PortForwardConfig(
|
||||
id: json['id'] as String,
|
||||
serverId: json['serverId'] as String,
|
||||
name: json['name'] as String,
|
||||
type: type,
|
||||
localHost: json['localHost'] as String?,
|
||||
localPort: (json['localPort'] as num?)?.toInt() ?? 0,
|
||||
remoteHost: json['remoteHost'] as String?,
|
||||
remotePort: (json['remotePort'] as num?)?.toInt(),
|
||||
);
|
||||
}
|
||||
|
||||
const PortForwardConfig._();
|
||||
|
||||
String get displayAddr => '$localHost:$localPort → $remoteHost:$remotePort';
|
||||
String get displayAddr {
|
||||
final localBindHost =
|
||||
localHost ?? 'localhost';
|
||||
if (type == PortForwardType.dynamic) {
|
||||
return '$localBindHost:$localPort (SOCKS5)';
|
||||
}
|
||||
if (type == PortForwardType.remote) {
|
||||
final remoteBindHost = remoteHost ?? '?';
|
||||
return '$remoteBindHost:${remotePort ?? "?"} → $localBindHost:$localPort';
|
||||
}
|
||||
return '$localBindHost:$localPort → ${remoteHost ?? "?"}:${remotePort ?? "?"}';
|
||||
}
|
||||
}
|
||||
|
||||
@freezed
|
||||
|
||||
@@ -11,33 +11,30 @@ part of 'port_forward.dart';
|
||||
|
||||
// dart format off
|
||||
T _$identity<T>(T value) => value;
|
||||
|
||||
/// @nodoc
|
||||
mixin _$PortForwardConfig {
|
||||
|
||||
String get id; String get serverId; String get name; String get localHost; int get localPort; String get remoteHost; int get remotePort; String? get description;
|
||||
String get id; String get serverId; String get name; PortForwardType get type; String? get localHost; int get localPort; String? get remoteHost; int? get remotePort;
|
||||
/// Create a copy of PortForwardConfig
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@pragma('vm:prefer-inline')
|
||||
$PortForwardConfigCopyWith<PortForwardConfig> get copyWith => _$PortForwardConfigCopyWithImpl<PortForwardConfig>(this as PortForwardConfig, _$identity);
|
||||
|
||||
/// Serializes this PortForwardConfig to a JSON map.
|
||||
Map<String, dynamic> toJson();
|
||||
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is PortForwardConfig&&(identical(other.id, id) || other.id == id)&&(identical(other.serverId, serverId) || other.serverId == serverId)&&(identical(other.name, name) || other.name == name)&&(identical(other.localHost, localHost) || other.localHost == localHost)&&(identical(other.localPort, localPort) || other.localPort == localPort)&&(identical(other.remoteHost, remoteHost) || other.remoteHost == remoteHost)&&(identical(other.remotePort, remotePort) || other.remotePort == remotePort)&&(identical(other.description, description) || other.description == description));
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is PortForwardConfig&&(identical(other.id, id) || other.id == id)&&(identical(other.serverId, serverId) || other.serverId == serverId)&&(identical(other.name, name) || other.name == name)&&(identical(other.type, type) || other.type == type)&&(identical(other.localHost, localHost) || other.localHost == localHost)&&(identical(other.localPort, localPort) || other.localPort == localPort)&&(identical(other.remoteHost, remoteHost) || other.remoteHost == remoteHost)&&(identical(other.remotePort, remotePort) || other.remotePort == remotePort));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,id,serverId,name,localHost,localPort,remoteHost,remotePort,description);
|
||||
int get hashCode => Object.hash(runtimeType,id,serverId,name,type,localHost,localPort,remoteHost,remotePort);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'PortForwardConfig(id: $id, serverId: $serverId, name: $name, localHost: $localHost, localPort: $localPort, remoteHost: $remoteHost, remotePort: $remotePort, description: $description)';
|
||||
return 'PortForwardConfig(id: $id, serverId: $serverId, name: $name, type: $type, localHost: $localHost, localPort: $localPort, remoteHost: $remoteHost, remotePort: $remotePort)';
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +45,7 @@ abstract mixin class $PortForwardConfigCopyWith<$Res> {
|
||||
factory $PortForwardConfigCopyWith(PortForwardConfig value, $Res Function(PortForwardConfig) _then) = _$PortForwardConfigCopyWithImpl;
|
||||
@useResult
|
||||
$Res call({
|
||||
String id, String serverId, String name, String localHost, int localPort, String remoteHost, int remotePort, String? description
|
||||
String id, String serverId, String name, PortForwardType type, String? localHost, int localPort, String? remoteHost, int? remotePort
|
||||
});
|
||||
|
||||
|
||||
@@ -65,17 +62,17 @@ class _$PortForwardConfigCopyWithImpl<$Res>
|
||||
|
||||
/// Create a copy of PortForwardConfig
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? id = null,Object? serverId = null,Object? name = null,Object? localHost = null,Object? localPort = null,Object? remoteHost = null,Object? remotePort = null,Object? description = freezed,}) {
|
||||
@pragma('vm:prefer-inline') @override $Res call({Object? id = null,Object? serverId = null,Object? name = null,Object? type = null,Object? localHost = freezed,Object? localPort = null,Object? remoteHost = freezed,Object? remotePort = freezed,}) {
|
||||
return _then(_self.copyWith(
|
||||
id: null == id ? _self.id : id // ignore: cast_nullable_to_non_nullable
|
||||
as String,serverId: null == serverId ? _self.serverId : serverId // ignore: cast_nullable_to_non_nullable
|
||||
as String,name: null == name ? _self.name : name // ignore: cast_nullable_to_non_nullable
|
||||
as String,localHost: null == localHost ? _self.localHost : localHost // ignore: cast_nullable_to_non_nullable
|
||||
as String,localPort: null == localPort ? _self.localPort : localPort // ignore: cast_nullable_to_non_nullable
|
||||
as int,remoteHost: null == remoteHost ? _self.remoteHost : remoteHost // ignore: cast_nullable_to_non_nullable
|
||||
as String,remotePort: null == remotePort ? _self.remotePort : remotePort // ignore: cast_nullable_to_non_nullable
|
||||
as int,description: freezed == description ? _self.description : description // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
as String,type: null == type ? _self.type : type // ignore: cast_nullable_to_non_nullable
|
||||
as PortForwardType,localHost: freezed == localHost ? _self.localHost : localHost // ignore: cast_nullable_to_non_nullable
|
||||
as String?,localPort: null == localPort ? _self.localPort : localPort // ignore: cast_nullable_to_non_nullable
|
||||
as int,remoteHost: freezed == remoteHost ? _self.remoteHost : remoteHost // ignore: cast_nullable_to_non_nullable
|
||||
as String?,remotePort: freezed == remotePort ? _self.remotePort : remotePort // ignore: cast_nullable_to_non_nullable
|
||||
as int?,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -160,10 +157,10 @@ return $default(_that);case _:
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String id, String serverId, String name, String localHost, int localPort, String remoteHost, int remotePort, String? description)? $default,{required TResult orElse(),}) {final _that = this;
|
||||
@optionalTypeArgs TResult maybeWhen<TResult extends Object?>(TResult Function( String id, String serverId, String name, PortForwardType type, String? localHost, int localPort, String? remoteHost, int? remotePort)? $default,{required TResult orElse(),}) {final _that = this;
|
||||
switch (_that) {
|
||||
case _PortForwardConfig() when $default != null:
|
||||
return $default(_that.id,_that.serverId,_that.name,_that.localHost,_that.localPort,_that.remoteHost,_that.remotePort,_that.description);case _:
|
||||
return $default(_that.id,_that.serverId,_that.name,_that.type,_that.localHost,_that.localPort,_that.remoteHost,_that.remotePort);case _:
|
||||
return orElse();
|
||||
|
||||
}
|
||||
@@ -181,10 +178,10 @@ return $default(_that.id,_that.serverId,_that.name,_that.localHost,_that.localPo
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String id, String serverId, String name, String localHost, int localPort, String remoteHost, int remotePort, String? description) $default,) {final _that = this;
|
||||
@optionalTypeArgs TResult when<TResult extends Object?>(TResult Function( String id, String serverId, String name, PortForwardType type, String? localHost, int localPort, String? remoteHost, int? remotePort) $default,) {final _that = this;
|
||||
switch (_that) {
|
||||
case _PortForwardConfig():
|
||||
return $default(_that.id,_that.serverId,_that.name,_that.localHost,_that.localPort,_that.remoteHost,_that.remotePort,_that.description);case _:
|
||||
return $default(_that.id,_that.serverId,_that.name,_that.type,_that.localHost,_that.localPort,_that.remoteHost,_that.remotePort);case _:
|
||||
throw StateError('Unexpected subclass');
|
||||
|
||||
}
|
||||
@@ -201,10 +198,10 @@ return $default(_that.id,_that.serverId,_that.name,_that.localHost,_that.localPo
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String id, String serverId, String name, String localHost, int localPort, String remoteHost, int remotePort, String? description)? $default,) {final _that = this;
|
||||
@optionalTypeArgs TResult? whenOrNull<TResult extends Object?>(TResult? Function( String id, String serverId, String name, PortForwardType type, String? localHost, int localPort, String? remoteHost, int? remotePort)? $default,) {final _that = this;
|
||||
switch (_that) {
|
||||
case _PortForwardConfig() when $default != null:
|
||||
return $default(_that.id,_that.serverId,_that.name,_that.localHost,_that.localPort,_that.remoteHost,_that.remotePort,_that.description);case _:
|
||||
return $default(_that.id,_that.serverId,_that.name,_that.type,_that.localHost,_that.localPort,_that.remoteHost,_that.remotePort);case _:
|
||||
return null;
|
||||
|
||||
}
|
||||
@@ -213,20 +210,20 @@ return $default(_that.id,_that.serverId,_that.name,_that.localHost,_that.localPo
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@JsonSerializable()
|
||||
|
||||
|
||||
class _PortForwardConfig extends PortForwardConfig {
|
||||
const _PortForwardConfig({required this.id, required this.serverId, required this.name, this.localHost = 'localhost', required this.localPort, required this.remoteHost, required this.remotePort, this.description}): super._();
|
||||
factory _PortForwardConfig.fromJson(Map<String, dynamic> json) => _$PortForwardConfigFromJson(json);
|
||||
const _PortForwardConfig({required this.id, required this.serverId, required this.name, required this.type, this.localHost, this.localPort = 0, this.remoteHost, this.remotePort}): super._();
|
||||
|
||||
|
||||
@override final String id;
|
||||
@override final String serverId;
|
||||
@override final String name;
|
||||
@override@JsonKey() final String localHost;
|
||||
@override final int localPort;
|
||||
@override final String remoteHost;
|
||||
@override final int remotePort;
|
||||
@override final String? description;
|
||||
@override final PortForwardType type;
|
||||
@override final String? localHost;
|
||||
@override@JsonKey() final int localPort;
|
||||
@override final String? remoteHost;
|
||||
@override final int? remotePort;
|
||||
|
||||
/// Create a copy of PortForwardConfig
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@@ -234,23 +231,20 @@ class _PortForwardConfig extends PortForwardConfig {
|
||||
@pragma('vm:prefer-inline')
|
||||
_$PortForwardConfigCopyWith<_PortForwardConfig> get copyWith => __$PortForwardConfigCopyWithImpl<_PortForwardConfig>(this, _$identity);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return _$PortForwardConfigToJson(this, );
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is _PortForwardConfig&&(identical(other.id, id) || other.id == id)&&(identical(other.serverId, serverId) || other.serverId == serverId)&&(identical(other.name, name) || other.name == name)&&(identical(other.localHost, localHost) || other.localHost == localHost)&&(identical(other.localPort, localPort) || other.localPort == localPort)&&(identical(other.remoteHost, remoteHost) || other.remoteHost == remoteHost)&&(identical(other.remotePort, remotePort) || other.remotePort == remotePort)&&(identical(other.description, description) || other.description == description));
|
||||
return identical(this, other) || (other.runtimeType == runtimeType&&other is _PortForwardConfig&&(identical(other.id, id) || other.id == id)&&(identical(other.serverId, serverId) || other.serverId == serverId)&&(identical(other.name, name) || other.name == name)&&(identical(other.type, type) || other.type == type)&&(identical(other.localHost, localHost) || other.localHost == localHost)&&(identical(other.localPort, localPort) || other.localPort == localPort)&&(identical(other.remoteHost, remoteHost) || other.remoteHost == remoteHost)&&(identical(other.remotePort, remotePort) || other.remotePort == remotePort));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType,id,serverId,name,localHost,localPort,remoteHost,remotePort,description);
|
||||
int get hashCode => Object.hash(runtimeType,id,serverId,name,type,localHost,localPort,remoteHost,remotePort);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'PortForwardConfig(id: $id, serverId: $serverId, name: $name, localHost: $localHost, localPort: $localPort, remoteHost: $remoteHost, remotePort: $remotePort, description: $description)';
|
||||
return 'PortForwardConfig(id: $id, serverId: $serverId, name: $name, type: $type, localHost: $localHost, localPort: $localPort, remoteHost: $remoteHost, remotePort: $remotePort)';
|
||||
}
|
||||
|
||||
|
||||
@@ -261,7 +255,7 @@ abstract mixin class _$PortForwardConfigCopyWith<$Res> implements $PortForwardCo
|
||||
factory _$PortForwardConfigCopyWith(_PortForwardConfig value, $Res Function(_PortForwardConfig) _then) = __$PortForwardConfigCopyWithImpl;
|
||||
@override @useResult
|
||||
$Res call({
|
||||
String id, String serverId, String name, String localHost, int localPort, String remoteHost, int remotePort, String? description
|
||||
String id, String serverId, String name, PortForwardType type, String? localHost, int localPort, String? remoteHost, int? remotePort
|
||||
});
|
||||
|
||||
|
||||
@@ -278,17 +272,17 @@ class __$PortForwardConfigCopyWithImpl<$Res>
|
||||
|
||||
/// Create a copy of PortForwardConfig
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override @pragma('vm:prefer-inline') $Res call({Object? id = null,Object? serverId = null,Object? name = null,Object? localHost = null,Object? localPort = null,Object? remoteHost = null,Object? remotePort = null,Object? description = freezed,}) {
|
||||
@override @pragma('vm:prefer-inline') $Res call({Object? id = null,Object? serverId = null,Object? name = null,Object? type = null,Object? localHost = freezed,Object? localPort = null,Object? remoteHost = freezed,Object? remotePort = freezed,}) {
|
||||
return _then(_PortForwardConfig(
|
||||
id: null == id ? _self.id : id // ignore: cast_nullable_to_non_nullable
|
||||
as String,serverId: null == serverId ? _self.serverId : serverId // ignore: cast_nullable_to_non_nullable
|
||||
as String,name: null == name ? _self.name : name // ignore: cast_nullable_to_non_nullable
|
||||
as String,localHost: null == localHost ? _self.localHost : localHost // ignore: cast_nullable_to_non_nullable
|
||||
as String,localPort: null == localPort ? _self.localPort : localPort // ignore: cast_nullable_to_non_nullable
|
||||
as int,remoteHost: null == remoteHost ? _self.remoteHost : remoteHost // ignore: cast_nullable_to_non_nullable
|
||||
as String,remotePort: null == remotePort ? _self.remotePort : remotePort // ignore: cast_nullable_to_non_nullable
|
||||
as int,description: freezed == description ? _self.description : description // ignore: cast_nullable_to_non_nullable
|
||||
as String?,
|
||||
as String,type: null == type ? _self.type : type // ignore: cast_nullable_to_non_nullable
|
||||
as PortForwardType,localHost: freezed == localHost ? _self.localHost : localHost // ignore: cast_nullable_to_non_nullable
|
||||
as String?,localPort: null == localPort ? _self.localPort : localPort // ignore: cast_nullable_to_non_nullable
|
||||
as int,remoteHost: freezed == remoteHost ? _self.remoteHost : remoteHost // ignore: cast_nullable_to_non_nullable
|
||||
as String?,remotePort: freezed == remotePort ? _self.remotePort : remotePort // ignore: cast_nullable_to_non_nullable
|
||||
as int?,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'port_forward.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
_PortForwardConfig _$PortForwardConfigFromJson(Map<String, dynamic> json) =>
|
||||
_PortForwardConfig(
|
||||
id: json['id'] as String,
|
||||
serverId: json['serverId'] as String,
|
||||
name: json['name'] as String,
|
||||
localHost: json['localHost'] as String? ?? 'localhost',
|
||||
localPort: (json['localPort'] as num).toInt(),
|
||||
remoteHost: json['remoteHost'] as String,
|
||||
remotePort: (json['remotePort'] as num).toInt(),
|
||||
description: json['description'] as String?,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$PortForwardConfigToJson(_PortForwardConfig instance) =>
|
||||
<String, dynamic>{
|
||||
'id': instance.id,
|
||||
'serverId': instance.serverId,
|
||||
'name': instance.name,
|
||||
'localHost': instance.localHost,
|
||||
'localPort': instance.localPort,
|
||||
'remoteHost': instance.remoteHost,
|
||||
'remotePort': instance.remotePort,
|
||||
'description': instance.description,
|
||||
};
|
||||
@@ -12,7 +12,7 @@ part 'port_forward_provider.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
class PortForwardNotifier extends _$PortForwardNotifier {
|
||||
final Map<String, _LocalForwardEntry> _forwards = {};
|
||||
final Map<String, _ForwardEntry> _forwards = {};
|
||||
final Set<String> _inFlight = {};
|
||||
|
||||
@override
|
||||
@@ -56,11 +56,16 @@ class PortForwardNotifier extends _$PortForwardNotifier {
|
||||
state = state.copyWith(configs: configs);
|
||||
}
|
||||
|
||||
Future<void> updateConfig(PortForwardConfig oldConfig, PortForwardConfig newConfig) async {
|
||||
Future<void> updateConfig(
|
||||
PortForwardConfig oldConfig,
|
||||
PortForwardConfig newConfig,
|
||||
) async {
|
||||
await stopForward(oldConfig.id);
|
||||
final configWithServerId = newConfig.copyWith(serverId: _serverId);
|
||||
Stores.portForward.update(oldConfig, configWithServerId);
|
||||
final configs = state.configs.map((c) => c.id == oldConfig.id ? configWithServerId : c).toList();
|
||||
final configs = state.configs
|
||||
.map((c) => c.id == oldConfig.id ? configWithServerId : c)
|
||||
.toList();
|
||||
state = state.copyWith(configs: configs);
|
||||
}
|
||||
|
||||
@@ -71,7 +76,9 @@ class PortForwardNotifier extends _$PortForwardNotifier {
|
||||
Stores.portForward.delete(config);
|
||||
}
|
||||
final configs = state.configs.where((c) => c.id != id).toList();
|
||||
final activeForwards = Map<String, PortForwardStatus>.from(state.activeForwards)..remove(id);
|
||||
final activeForwards = Map<String, PortForwardStatus>.from(
|
||||
state.activeForwards,
|
||||
)..remove(id);
|
||||
state = state.copyWith(configs: configs, activeForwards: activeForwards);
|
||||
}
|
||||
|
||||
@@ -91,24 +98,88 @@ class PortForwardNotifier extends _$PortForwardNotifier {
|
||||
}
|
||||
|
||||
try {
|
||||
final serverSocket = await ServerSocket.bind(config.localHost, config.localPort);
|
||||
|
||||
Loggers.app.info('Port forward started: ${config.localHost}:${config.localPort} -> ${config.remoteHost}:${config.remotePort}');
|
||||
|
||||
final entry = _LocalForwardEntry(serverSocket: serverSocket);
|
||||
entry.start(config.remoteHost, config.remotePort, () => _client);
|
||||
_forwards[id] = entry;
|
||||
|
||||
_updateStatus(id, PortForwardStatus(id: id, isActive: true));
|
||||
switch (config.type) {
|
||||
case PortForwardType.local:
|
||||
await _startLocalForward(config);
|
||||
case PortForwardType.remote:
|
||||
await _startRemoteForward(config);
|
||||
case PortForwardType.dynamic:
|
||||
await _startDynamicForward(config);
|
||||
}
|
||||
} catch (e) {
|
||||
Loggers.app.warning('Port forward failed to start: $e');
|
||||
_updateStatus(id, PortForwardStatus(id: id, isActive: false, error: e.toString()));
|
||||
_updateStatus(
|
||||
id,
|
||||
PortForwardStatus(id: id, isActive: false, error: e.toString()),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
_inFlight.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _startLocalForward(PortForwardConfig config) async {
|
||||
if (config.remoteHost == null || config.remotePort == null) {
|
||||
throw Exception('Invalid local port forward: remote destination not set');
|
||||
}
|
||||
final serverSocket = await ServerSocket.bind(
|
||||
config.localHost ?? 'localhost',
|
||||
config.localPort,
|
||||
);
|
||||
Loggers.app.info(
|
||||
'Local port forward started: ${config.localHost ?? "localhost"}:${config.localPort} -> ${config.remoteHost}:${config.remotePort}',
|
||||
);
|
||||
final entry = _LocalForwardEntry(
|
||||
serverSocket: serverSocket,
|
||||
remoteHost: config.remoteHost!,
|
||||
remotePort: config.remotePort!,
|
||||
clientGetter: () => _client,
|
||||
);
|
||||
entry.start();
|
||||
_forwards[config.id] = entry;
|
||||
_updateStatus(config.id, PortForwardStatus(id: config.id, isActive: true));
|
||||
}
|
||||
|
||||
Future<void> _startRemoteForward(PortForwardConfig config) async {
|
||||
if (config.remoteHost == null || config.remotePort == null) {
|
||||
throw Exception(
|
||||
'Invalid remote port forward: remote destination not set',
|
||||
);
|
||||
}
|
||||
final forward = await _client.forwardRemote(
|
||||
host: config.remoteHost!,
|
||||
port: config.remotePort!,
|
||||
);
|
||||
if (forward == null) {
|
||||
throw Exception('Failed to start remote port forward: server rejected');
|
||||
}
|
||||
Loggers.app.info(
|
||||
'Remote port forward started: ${config.remoteHost}:${config.remotePort}',
|
||||
);
|
||||
final entry = _RemoteForwardEntry(
|
||||
forward: forward,
|
||||
remoteHost: config.localHost ?? 'localhost',
|
||||
remotePort: config.localPort,
|
||||
);
|
||||
entry.start();
|
||||
_forwards[config.id] = entry;
|
||||
_updateStatus(config.id, PortForwardStatus(id: config.id, isActive: true));
|
||||
}
|
||||
|
||||
Future<void> _startDynamicForward(PortForwardConfig config) async {
|
||||
final bindHost = config.localHost ?? 'localhost';
|
||||
final dynamicForward = await _client.forwardDynamic(
|
||||
bindHost: bindHost,
|
||||
bindPort: config.localPort,
|
||||
);
|
||||
Loggers.app.info(
|
||||
'Dynamic port forward (SOCKS5) started: $bindHost:${config.localPort}',
|
||||
);
|
||||
final entry = _DynamicForwardEntry(dynamicForward: dynamicForward);
|
||||
_forwards[config.id] = entry;
|
||||
_updateStatus(config.id, PortForwardStatus(id: config.id, isActive: true));
|
||||
}
|
||||
|
||||
Future<void> stopForward(String id) async {
|
||||
if (!_inFlight.add(id)) return;
|
||||
try {
|
||||
@@ -134,27 +205,50 @@ class PortForwardNotifier extends _$PortForwardNotifier {
|
||||
}
|
||||
|
||||
void _updateStatus(String id, PortForwardStatus status) {
|
||||
final activeForwards = Map<String, PortForwardStatus>.from(state.activeForwards);
|
||||
final activeForwards = Map<String, PortForwardStatus>.from(
|
||||
state.activeForwards,
|
||||
);
|
||||
activeForwards[id] = status;
|
||||
state = state.copyWith(activeForwards: activeForwards);
|
||||
}
|
||||
}
|
||||
|
||||
class _LocalForwardEntry {
|
||||
abstract class _ForwardEntry {
|
||||
Future<void> close();
|
||||
}
|
||||
|
||||
class _LocalForwardEntry extends _ForwardEntry {
|
||||
final ServerSocket serverSocket;
|
||||
final String remoteHost;
|
||||
final int remotePort;
|
||||
final SSHClient Function() clientGetter;
|
||||
final List<_ActiveConnection> _connections = [];
|
||||
StreamSubscription<Socket>? _subscription;
|
||||
|
||||
_LocalForwardEntry({required this.serverSocket});
|
||||
_LocalForwardEntry({
|
||||
required this.serverSocket,
|
||||
required this.remoteHost,
|
||||
required this.remotePort,
|
||||
required this.clientGetter,
|
||||
});
|
||||
|
||||
void start(String remoteHost, int remotePort, SSHClient Function() clientGetter) {
|
||||
void start() {
|
||||
_subscription = serverSocket.listen((socket) async {
|
||||
try {
|
||||
final forward = await clientGetter().forwardLocal(remoteHost, remotePort);
|
||||
final forward = await clientGetter().forwardLocal(
|
||||
remoteHost,
|
||||
remotePort,
|
||||
);
|
||||
final conn = _ActiveConnection(socket: socket, forward: forward);
|
||||
_connections.add(conn);
|
||||
final pipe1 = forward.stream.cast<List<int>>().pipe(socket).catchError((_) {});
|
||||
final pipe2 = socket.cast<List<int>>().pipe(forward.sink).catchError((_) {});
|
||||
final pipe1 = forward.stream
|
||||
.cast<List<int>>()
|
||||
.pipe(socket)
|
||||
.catchError((_) {});
|
||||
final pipe2 = socket
|
||||
.cast<List<int>>()
|
||||
.pipe(forward.sink)
|
||||
.catchError((_) {});
|
||||
Future.wait([pipe1, pipe2]).whenComplete(() {
|
||||
_connections.remove(conn);
|
||||
conn.close();
|
||||
@@ -166,6 +260,7 @@ class _LocalForwardEntry {
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await _subscription?.cancel();
|
||||
await serverSocket.close();
|
||||
@@ -177,14 +272,72 @@ class _LocalForwardEntry {
|
||||
}
|
||||
}
|
||||
|
||||
class _RemoteForwardEntry extends _ForwardEntry {
|
||||
final SSHRemoteForward forward;
|
||||
final String remoteHost;
|
||||
final int remotePort;
|
||||
final List<_ActiveConnection> _connections = [];
|
||||
StreamSubscription<SSHForwardChannel>? _subscription;
|
||||
|
||||
_RemoteForwardEntry({
|
||||
required this.forward,
|
||||
required this.remoteHost,
|
||||
required this.remotePort,
|
||||
});
|
||||
|
||||
void start() {
|
||||
_subscription = forward.connections.listen((channel) async {
|
||||
try {
|
||||
final socket = await Socket.connect(remoteHost, remotePort);
|
||||
final conn = _ActiveConnection(socket: socket, forward: channel);
|
||||
_connections.add(conn);
|
||||
final pipe1 = channel.stream
|
||||
.cast<List<int>>()
|
||||
.pipe(socket)
|
||||
.catchError((_) {});
|
||||
final pipe2 = socket
|
||||
.cast<List<int>>()
|
||||
.pipe(channel.sink)
|
||||
.catchError((_) {});
|
||||
Future.wait([pipe1, pipe2]).whenComplete(() {
|
||||
_connections.remove(conn);
|
||||
conn.close();
|
||||
});
|
||||
} catch (e, s) {
|
||||
Loggers.app.warning('Remote forward connection failed', e, s);
|
||||
channel.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> close() async {
|
||||
await _subscription?.cancel();
|
||||
final connections = _connections.toList();
|
||||
for (final conn in connections) {
|
||||
await conn.close().catchError((_) {});
|
||||
}
|
||||
_connections.clear();
|
||||
try {
|
||||
await Future.microtask(() => forward.close());
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
|
||||
class _DynamicForwardEntry extends _ForwardEntry {
|
||||
final SSHDynamicForward dynamicForward;
|
||||
|
||||
_DynamicForwardEntry({required this.dynamicForward});
|
||||
|
||||
@override
|
||||
Future<void> close() => dynamicForward.close();
|
||||
}
|
||||
|
||||
class _ActiveConnection {
|
||||
final Socket socket;
|
||||
final SSHForwardChannel forward;
|
||||
|
||||
_ActiveConnection({
|
||||
required this.socket,
|
||||
required this.forward,
|
||||
});
|
||||
_ActiveConnection({required this.socket, required this.forward});
|
||||
|
||||
Future<void> close() async {
|
||||
try {
|
||||
|
||||
@@ -59,7 +59,7 @@ final class PortForwardNotifierProvider
|
||||
}
|
||||
|
||||
String _$portForwardNotifierHash() =>
|
||||
r'c56425252253c276b6202f478d3475e8fe0c1c64';
|
||||
r'2406d86f55759c13977daab9ba9c40fb6aca370d';
|
||||
|
||||
final class PortForwardNotifierFamily extends $Family
|
||||
with
|
||||
|
||||
@@ -279,4 +279,7 @@ class SettingStore extends HiveStore {
|
||||
return val?.map((e) => e.name).toList() ?? [];
|
||||
},
|
||||
);
|
||||
|
||||
/// Hide port forward beta warning
|
||||
late final portForwardBetaWarned = propertyDefault('portForwardBetaWarned', false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user