Skip to content

Commit

Permalink
Fix infinite sub expire date (#334)
Browse files Browse the repository at this point in the history
* Fix infinite sub expire date

* fix expire

* fix build

* refactor

* make it better readable

* Fix infinite sub

* Add test for infinite sub

---------

Co-authored-by: Hiddify <[email protected]>
Co-authored-by: problematicconsumer <[email protected]>
  • Loading branch information
3 people authored Jan 15, 2024
1 parent e0a02d4 commit 46107f2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/features/profile/data/profile_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import 'package:uuid/uuid.dart';
/// - url filename extension (example: `https://example.com/config.json`) -> name=`config`
/// - if none of these methods return a non-blank string, fallback to `Remote Profile`
abstract class ProfileParser {
static const infiniteTrafficThreshold = 9223372036854775807;
static const infiniteTimeThreshold = 92233720368;

static RemoteProfileEntity parse(
String url,
Map<String, List<String>> headers,
Expand Down Expand Up @@ -89,16 +92,15 @@ abstract class ProfileParser {
"upload": final upload?,
"download": final download?,
"total": var total,
"expire": final expire
"expire": var expire
}) {
total = total ?? 0;
total = (total == null || total == 0) ? infiniteTrafficThreshold : total;
expire = (expire == null || expire == 0) ? infiniteTimeThreshold : expire;
return SubscriptionInfo(
upload: upload,
download: download,
total: total == 0 ? 9223372036854775807 : total,
expire: DateTime.fromMillisecondsSinceEpoch(
(expire ?? 92233720368) * 1000,
),
total: total,
expire: DateTime.fromMillisecondsSinceEpoch(expire * 1000),
);
}
return null;
Expand Down
30 changes: 30 additions & 0 deletions test/features/profile/data/profile_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ void main() {
);
},
);

test(
"with infinite traffic and time",
() {
final headers = <String, List<String>>{
"profile-title": ["title"],
"profile-update-interval": ["1"],
"subscription-userinfo": [
"upload=0;download=1024;total=0;expire=0",
],
"profile-web-page-url": [validBaseUrl],
"support-url": [validSupportUrl],
};
final profile = ProfileParser.parse(validExtendedUrl, headers);

expect(profile.subInfo, isNotNull);
expect(
profile.subInfo!.total,
equals(ProfileParser.infiniteTrafficThreshold),
);
expect(
profile.subInfo!.expire,
equals(
DateTime.fromMillisecondsSinceEpoch(
ProfileParser.infiniteTimeThreshold * 1000,
),
),
);
},
);
},
);
}

0 comments on commit 46107f2

Please sign in to comment.