From e13b4b269a7760a1fa445d8504e44ec6531278bd Mon Sep 17 00:00:00 2001 From: Hiddify Date: Tue, 30 Jan 2024 19:14:05 +0100 Subject: [PATCH] new: add postfix to name if it is not unique --- .../profile/data/profile_data_source.dart | 8 +++++ .../profile/data/profile_repository.dart | 12 +++++-- .../profile/notifier/profile_notifier.dart | 7 +++- lib/utils/link_parsers.dart | 35 +++++++++---------- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/lib/features/profile/data/profile_data_source.dart b/lib/features/profile/data/profile_data_source.dart index 8d8bc6bd..9fc50e60 100644 --- a/lib/features/profile/data/profile_data_source.dart +++ b/lib/features/profile/data/profile_data_source.dart @@ -9,6 +9,7 @@ part 'profile_data_source.g.dart'; abstract interface class ProfileDataSource { Future getById(String id); Future getByUrl(String url); + Future getByName(String name); Stream watchActiveProfile(); Stream watchProfilesCount(); Stream> watchAll({ @@ -44,6 +45,13 @@ class ProfileDao extends DatabaseAccessor ..limit(1)) .getSingleOrNull(); } + @override + Future getByName(String name) async { + return (select(profileEntries) + ..where((tbl) => tbl.name.equals(name)) + ..limit(1)) + .getSingleOrNull(); + } @override Stream watchActiveProfile() { diff --git a/lib/features/profile/data/profile_repository.dart b/lib/features/profile/data/profile_repository.dart index 09ec74e2..64591e1d 100644 --- a/lib/features/profile/data/profile_repository.dart +++ b/lib/features/profile/data/profile_repository.dart @@ -22,6 +22,7 @@ import 'package:uuid/uuid.dart'; abstract interface class ProfileRepository { TaskEither init(); TaskEither getById(String id); + Future getByName(String name); Stream> watchActiveProfile(); Stream> watchHasAnyProfile(); @@ -97,6 +98,11 @@ class ProfileRepositoryImpl ); } + @override + Future getByName(String name) async { + return (await profileDataSource.getByName(name))?.toEntity(); + } + @override Stream> watchActiveProfile() { return profileDataSource @@ -361,7 +367,7 @@ class ProfileRepositoryImpl ); } - static final _subInfoHeaders = [ + static final _subInfoHeaders = [ 'profile-title', 'content-disposition', 'subscription-userinfo', @@ -422,9 +428,9 @@ class ProfileRepositoryImpl for (final entry in contentHeaders.entries) { if (!headers.keys.contains(entry.key) && entry.value.isNotEmpty) { headers[entry.key] = entry.value; - } + } } - + return headers; } diff --git a/lib/features/profile/notifier/profile_notifier.dart b/lib/features/profile/notifier/profile_notifier.dart index 89437914..0e4abdca 100644 --- a/lib/features/profile/notifier/profile_notifier.dart +++ b/lib/features/profile/notifier/profile_notifier.dart @@ -68,9 +68,14 @@ class AddProfile extends _$AddProfile with AppLogger { ); } else if (LinkParser.protocol(rawInput) case (final parsed)?) { loggy.debug("adding profile, content"); + var name = parsed.name; + + while (await _profilesRepo.getByName(name) != null) { + name+= '${randomInt(0, 9).run()}'; + } task = _profilesRepo.addByContent( parsed.content, - name: parsed.name, + name: name, markAsActive: markAsActive, ); } else { diff --git a/lib/utils/link_parsers.dart b/lib/utils/link_parsers.dart index 9e1d17d0..23dee9c1 100644 --- a/lib/utils/link_parsers.dart +++ b/lib/utils/link_parsers.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:dartx/dartx.dart'; +import 'package:fpdart/fpdart.dart'; import 'package:hiddify/features/profile/data/profile_parser.dart'; import 'package:hiddify/features/profile/data/profile_repository.dart'; import 'package:hiddify/singbox/model/singbox_proxy_type.dart'; @@ -49,30 +50,28 @@ abstract class LinkParser { if (uri == null) continue; final fragment = uri.hasFragment ? Uri.decodeComponent(uri.fragment) : null; - if (name != null) { - name = switch (uri.scheme) { - 'ss' => fragment ?? ProxyType.shadowsocks.label, - 'ssconf' => fragment ?? ProxyType.shadowsocks.label, - 'vmess' => ProxyType.vmess.label, - 'vless' => fragment ?? ProxyType.vless.label, - 'trojan' => fragment ?? ProxyType.trojan.label, - 'tuic' => fragment ?? ProxyType.tuic.label, - 'hy2' || 'hysteria2' => fragment ?? ProxyType.hysteria2.label, - 'hy' || 'hysteria' => fragment ?? ProxyType.hysteria.label, - 'ssh' => fragment ?? ProxyType.ssh.label, - 'wg' => fragment ?? ProxyType.wireguard.label, - 'warp' => fragment ?? ProxyType.warp.label, - _ => null, - }; - } + name ??= switch (uri.scheme) { + 'ss' => fragment ?? ProxyType.shadowsocks.label, + 'ssconf' => fragment ?? ProxyType.shadowsocks.label, + 'vmess' => ProxyType.vmess.label, + 'vless' => fragment ?? ProxyType.vless.label, + 'trojan' => fragment ?? ProxyType.trojan.label, + 'tuic' => fragment ?? ProxyType.tuic.label, + 'hy2' || 'hysteria2' => fragment ?? ProxyType.hysteria2.label, + 'hy' || 'hysteria' => fragment ?? ProxyType.hysteria.label, + 'ssh' => fragment ?? ProxyType.ssh.label, + 'wg' => fragment ?? ProxyType.wireguard.label, + 'warp' => fragment ?? ProxyType.warp.label, + _ => null, + }; } final headers = ProfileRepositoryImpl.parseHeadersFromContent(content); final subinfo = ProfileParser.parse("", headers); - if (subinfo.name.isNotNullOrEmpty) { + if (subinfo.name.isNotNullOrEmpty && subinfo.name != "Remote Profile") { name = subinfo.name; } - + return (content: normalContent, name: name ?? ProxyType.unknown.label); }