This commit is contained in:
problematicconsumer
2023-07-06 17:18:41 +03:30
commit b617c95f62
352 changed files with 21017 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
export 'clash_config.dart';
export 'clash_enums.dart';
export 'clash_facade.dart';
export 'clash_failures.dart';
export 'clash_log.dart';
export 'clash_proxy.dart';
export 'clash_traffic.dart';

View File

@@ -0,0 +1,72 @@
import 'package:fpdart/fpdart.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hiddify/domain/clash/clash_enums.dart';
part 'clash_config.freezed.dart';
part 'clash_config.g.dart';
@freezed
class ClashConfig with _$ClashConfig {
const ClashConfig._();
@JsonSerializable(includeIfNull: false, fieldRename: FieldRename.kebab)
const factory ClashConfig({
@JsonKey(name: 'port') int? httpPort,
int? socksPort,
int? redirPort,
int? tproxyPort,
int? mixedPort,
List<String>? authentication,
bool? allowLan,
String? bindAddress,
TunnelMode? mode,
LogLevel? logLevel,
bool? ipv6,
}) = _ClashConfig;
static const initial = ClashConfig(
httpPort: 12346,
socksPort: 12347,
mixedPort: 12348,
);
ClashConfig patch(ClashConfigPatch patch) {
return copyWith(
httpPort: (patch.httpPort ?? optionOf(httpPort)).toNullable(),
socksPort: (patch.socksPort ?? optionOf(socksPort)).toNullable(),
redirPort: (patch.redirPort ?? optionOf(redirPort)).toNullable(),
tproxyPort: (patch.tproxyPort ?? optionOf(tproxyPort)).toNullable(),
mixedPort: (patch.mixedPort ?? optionOf(mixedPort)).toNullable(),
authentication:
(patch.authentication ?? optionOf(authentication)).toNullable(),
allowLan: (patch.allowLan ?? optionOf(allowLan)).toNullable(),
bindAddress: (patch.bindAddress ?? optionOf(bindAddress)).toNullable(),
mode: (patch.mode ?? optionOf(mode)).toNullable(),
logLevel: (patch.logLevel ?? optionOf(logLevel)).toNullable(),
ipv6: (patch.ipv6 ?? optionOf(ipv6)).toNullable(),
);
}
factory ClashConfig.fromJson(Map<String, dynamic> json) =>
_$ClashConfigFromJson(json);
}
@freezed
class ClashConfigPatch with _$ClashConfigPatch {
const ClashConfigPatch._();
@JsonSerializable(includeIfNull: false)
const factory ClashConfigPatch({
Option<int>? httpPort,
Option<int>? socksPort,
Option<int>? redirPort,
Option<int>? tproxyPort,
Option<int>? mixedPort,
Option<List<String>>? authentication,
Option<bool>? allowLan,
Option<String>? bindAddress,
Option<TunnelMode>? mode,
Option<LogLevel>? logLevel,
Option<bool>? ipv6,
}) = _ClashConfigPatch;
}

View File

@@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
enum TunnelMode {
rule,
global,
direct;
}
enum LogLevel {
info,
warning,
error,
debug,
silent;
Color get color => switch (this) {
info => Colors.lightGreen,
warning => Colors.orangeAccent,
error => Colors.redAccent,
debug => Colors.lightBlue,
_ => Colors.white,
};
}
enum ProxyType {
direct("Direct"),
reject("Reject"),
compatible("Compatible"),
pass("Pass"),
shadowSocks("ShadowSocks"),
shadowSocksR("ShadowSocksR"),
snell("Snell"),
socks5("Socks5"),
http("Http"),
vmess("Vmess"),
vless("Vless"),
trojan("Trojan"),
hysteria("Hysteria"),
wireGuard("WireGuard"),
tuic("Tuic"),
relay("Relay"),
selector("Selector"),
fallback("Fallback"),
urlTest("URLTest", "urltest"),
loadBalance("LoadBalance"),
unknown("Unknown");
const ProxyType(this.label, [this._key]);
final String? _key;
final String label;
String get key => _key ?? name;
static List<ProxyType> groupValues = [
selector,
fallback,
urlTest,
loadBalance,
];
}

View File

@@ -0,0 +1,32 @@
import 'dart:async';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/domain/clash/clash.dart';
import 'package:hiddify/domain/constants.dart';
abstract class ClashFacade {
TaskEither<ClashFailure, ClashConfig> getConfigs();
TaskEither<ClashFailure, bool> validateConfig(String configFileName);
/// change active configuration file by [configFileName]
TaskEither<ClashFailure, Unit> changeConfigs(String configFileName);
TaskEither<ClashFailure, Unit> patchOverrides(ClashConfig overrides);
TaskEither<ClashFailure, List<ClashProxy>> getProxies();
TaskEither<ClashFailure, Unit> changeProxy(
String selectorName,
String proxyName,
);
TaskEither<ClashFailure, int> testDelay(
String proxyName, {
String testUrl = Constants.delayTestUrl,
});
TaskEither<ClashFailure, ClashTraffic> getTraffic();
Stream<Either<ClashFailure, ClashLog>> watchLogs();
}

View File

@@ -0,0 +1,27 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hiddify/core/locale/locale.dart';
import 'package:hiddify/domain/failures.dart';
part 'clash_failures.freezed.dart';
// TODO: rewrite
@freezed
sealed class ClashFailure with _$ClashFailure, Failure {
const ClashFailure._();
const factory ClashFailure.unexpected(
Object error,
StackTrace stackTrace,
) = ClashUnexpectedFailure;
const factory ClashFailure.core([String? error]) = ClashCoreFailure;
@override
String present(TranslationsEn t) {
return switch (this) {
ClashUnexpectedFailure() => t.failure.clash.unexpected,
ClashCoreFailure(:final error) =>
t.failure.clash.core(reason: error ?? ""),
};
}
}

View File

@@ -0,0 +1,22 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hiddify/domain/clash/clash_enums.dart';
part 'clash_log.freezed.dart';
part 'clash_log.g.dart';
@freezed
class ClashLog with _$ClashLog {
const ClashLog._();
const factory ClashLog({
@JsonKey(name: 'type') required LogLevel level,
@JsonKey(name: 'payload') required String message,
@JsonKey(defaultValue: DateTime.now) required DateTime time,
}) = _ClashLog;
String get timeStamp =>
"${time.month}-${time.day} ${time.hour}:${time.minute}:${time.second}";
factory ClashLog.fromJson(Map<String, dynamic> json) =>
_$ClashLogFromJson(json);
}

View File

@@ -0,0 +1,59 @@
import 'package:dartx/dartx.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hiddify/domain/clash/clash_enums.dart';
part 'clash_proxy.freezed.dart';
part 'clash_proxy.g.dart';
// TODO: test and improve
@Freezed(fromJson: true)
class ClashProxy with _$ClashProxy {
const ClashProxy._();
const factory ClashProxy.group({
required String name,
@JsonKey(fromJson: _typeFromJson) required ProxyType type,
required List<String> all,
required String now,
List<ClashHistory>? history,
@JsonKey(includeFromJson: false, includeToJson: false) int? delay,
}) = ClashProxyGroup;
const factory ClashProxy.item({
required String name,
@JsonKey(fromJson: _typeFromJson) required ProxyType type,
List<ClashHistory>? history,
@JsonKey(includeFromJson: false, includeToJson: false) int? delay,
}) = ClashProxyItem;
factory ClashProxy.fromJson(Map<String, dynamic> json) {
final isGroup = json.containsKey('all') ||
json.containsKey('now') ||
ProxyType.groupValues.any(
(e) => e.label == json.getOrElse('type', () => null),
);
if (isGroup) {
return ClashProxyGroup.fromJson(json);
} else {
return ClashProxyItem.fromJson(json);
}
}
}
ProxyType _typeFromJson(dynamic type) =>
ProxyType.values
.firstOrNullWhere((e) => e.key == (type as String?)?.toLowerCase()) ??
ProxyType.unknown;
@freezed
class ClashHistory with _$ClashHistory {
const ClashHistory._();
const factory ClashHistory({
required String time,
required int delay,
}) = _ClashHistory;
factory ClashHistory.fromJson(Map<String, dynamic> json) =>
_$ClashHistoryFromJson(json);
}

View File

@@ -0,0 +1,17 @@
import 'package:freezed_annotation/freezed_annotation.dart';
part 'clash_traffic.freezed.dart';
part 'clash_traffic.g.dart';
@freezed
class ClashTraffic with _$ClashTraffic {
const ClashTraffic._();
const factory ClashTraffic({
@JsonKey(name: 'up') required int upload,
@JsonKey(name: 'down') required int download,
}) = _ClashTraffic;
factory ClashTraffic.fromJson(Map<String, dynamic> json) =>
_$ClashTrafficFromJson(json);
}