From e31372a11529c3e35f144d646f995d946e3ee686 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=B2=AB=E8=8A=B1=E7=81=ABzzz=F0=9F=8C=99?= Date: Tue, 7 Jan 2025 00:24:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=80=89=E6=8B=A9?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E7=A9=BA=E9=97=B2=E7=AB=AF=E5=8F=A3=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E9=89=B4=E6=9D=83=E6=9C=8D=E5=8A=A1=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nyalcf_cli/lib/commands/authorize.dart | 18 +++++++-------- nyalcf_core/lib/network/server/oauth.dart | 8 ++++--- .../nyalcf_ui/lib/views/auth/authorize.dart | 23 +++++++++---------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/nyalcf_cli/lib/commands/authorize.dart b/nyalcf_cli/lib/commands/authorize.dart index 35f108e..02352a4 100644 --- a/nyalcf_cli/lib/commands/authorize.dart +++ b/nyalcf_cli/lib/commands/authorize.dart @@ -4,8 +4,12 @@ import 'dart:io'; // Package imports: import 'package:crypto/crypto.dart'; +// Project imports: +import 'package:nyalcf/templates/command.dart'; import 'package:nyalcf_core/models/user_info_model.dart'; import 'package:nyalcf_core/network/client/api/auth/oauth/access_token.dart'; +import 'package:nyalcf_core/network/client/api/user/frp/token.dart' + as user_frp_token; import 'package:nyalcf_core/network/client/api/user/info.dart' as user_info; import 'package:nyalcf_core/network/client/api_client.dart'; import 'package:nyalcf_core/network/server/oauth.dart'; @@ -13,12 +17,6 @@ import 'package:nyalcf_core/storages/stores/user_info_storage.dart'; import 'package:nyalcf_core/utils/logger.dart'; import 'package:nyalcf_core_extend/storages/token_storage.dart'; -// Project imports: -import 'package:nyalcf/templates/command.dart'; - -import 'package:nyalcf_core/network/client/api/user/frp/token.dart' - as user_frp_token; - class Authorize implements Command { static final _tokenStorage = TokenStorage(); @@ -29,14 +27,14 @@ class Authorize implements Command { Future main(List args) async { final ApiClient api = ApiClient(); - await startHttpServer(); + final port = await startHttpServer(); Logger.info( 'Please open this link to authorize: ' 'http://dashboard.locyanfrp.cn/auth/oauth/authorize' '?app_id=1' '&scopes=User,Proxy,Sign' '&redirect_url=' - 'https%3A%2F%2Fdashboard.locyanfrp.cn%2Fcallback%2Fauth%2Foauth%2Flocalhost%3Fport%3D21131%26ssl%3Dfalse%26path%3D%2Foauth%2Fcallback', + 'https%3A%2F%2Fdashboard.locyanfrp.cn%2Fcallback%2Fauth%2Foauth%2Flocalhost%3Fport%3D$port%26ssl%3Dfalse%26path%3D%2Foauth%2Fcallback', ); Logger.write('Waiting callback...'); Future.doWhile(() async { @@ -77,12 +75,12 @@ class Authorize implements Command { _tokenStorage.setFrpToken(frpToken); } - Future startHttpServer() async { + Future startHttpServer() async { OAuth.initRoute( response: OAuthResponseBody(success: '授权成功', error: '授权失败'), callback: callback, ); - await OAuth.start(); + return await OAuth.start(); } void callback({String? refreshToken, String? error}) { diff --git a/nyalcf_core/lib/network/server/oauth.dart b/nyalcf_core/lib/network/server/oauth.dart index cd85f7f..3faa263 100644 --- a/nyalcf_core/lib/network/server/oauth.dart +++ b/nyalcf_core/lib/network/server/oauth.dart @@ -20,7 +20,8 @@ class OAuth { }) { app.get('/oauth/callback', (Request request) { String? error = request.requestedUri.queryParameters['error']; - String? refreshToken = request.requestedUri.queryParameters['refresh_token']; + String? refreshToken = + request.requestedUri.queryParameters['refresh_token']; if (error != null) { callback(error: error); @@ -40,8 +41,9 @@ class OAuth { }); } - static start() async { - _server = await io.serve(app.call, 'localhost', 21131); + static Future start() async { + _server = await io.serve(app.call, 'localhost', 0); + return _server!.port; } static close() { diff --git a/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart b/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart index d2302e5..5118c09 100644 --- a/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart +++ b/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart @@ -1,11 +1,10 @@ // Dart imports: import 'dart:convert'; -// Flutter imports: -import 'package:flutter/material.dart'; - // Package imports: import 'package:crypto/crypto.dart'; +// Flutter imports: +import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:nyalcf_core/models/user_info_model.dart'; import 'package:nyalcf_core/network/client/api/auth/oauth/access_token.dart'; @@ -15,12 +14,11 @@ import 'package:nyalcf_core/network/client/api_client.dart'; import 'package:nyalcf_core/network/server/oauth.dart'; import 'package:nyalcf_core_extend/storages/prefs/token_info_prefs.dart'; import 'package:nyalcf_core_extend/storages/prefs/user_info_prefs.dart'; -import 'package:url_launcher/url_launcher.dart'; - // Project imports: import 'package:nyalcf_ui/models/appbar_actions.dart'; import 'package:nyalcf_ui/widgets/nya_loading_circle.dart'; import 'package:nyalcf_ui/widgets/nya_scaffold.dart'; +import 'package:url_launcher/url_launcher.dart'; class AuthorizeUI extends StatefulWidget { const AuthorizeUI({super.key}); @@ -108,18 +106,19 @@ class _AuthorizeState extends State { @override void initState() { - const url = 'https://dashboard.locyanfrp.cn/auth/oauth/authorize' - '?app_id=1' - '&scopes=User,Proxy,Sign' - '&redirect_url=' - 'https%3A%2F%2Fdashboard.locyanfrp.cn%2Fcallback%2Fauth%2Foauth%2Flocalhost%3Fport%3D21131%26ssl%3Dfalse%26path%3D%2Foauth%2Fcallback'; - launchUrl(Uri.parse(url)); OAuth.initRoute( response: OAuthResponseBody(success: '授权成功', error: '授权失败'), callback: ctr.callback, ); OAuth.close(); - OAuth.start(); + OAuth.start().then((port) { + final url = 'https://dashboard.locyanfrp.cn/auth/oauth/authorize' + '?app_id=1' + '&scopes=User,Proxy,Sign' + '&redirect_url=' + 'https%3A%2F%2Fdashboard.locyanfrp.cn%2Fcallback%2Fauth%2Foauth%2Flocalhost%3Fport%3D$port%26ssl%3Dfalse%26path%3D%2Foauth%2Fcallback'; + launchUrl(Uri.parse(url)); + }); super.initState(); } From 0c22a0c5e05e94501571f5519e9c437bbdcfebd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=B2=AB=E8=8A=B1=E7=81=ABzzz=F0=9F=8C=99?= Date: Tue, 7 Jan 2025 00:32:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nyalcf_core/lib/storages/stores/user_info_storage.dart | 10 +++++----- nyalcf_gui/nyalcf_core_extend/lib/tasks/basic.dart | 2 +- .../lib/tasks/update_proxies_list.dart | 7 +++---- nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart | 1 + 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/nyalcf_core/lib/storages/stores/user_info_storage.dart b/nyalcf_core/lib/storages/stores/user_info_storage.dart index b5b79a0..9f16fbb 100644 --- a/nyalcf_core/lib/storages/stores/user_info_storage.dart +++ b/nyalcf_core/lib/storages/stores/user_info_storage.dart @@ -2,12 +2,11 @@ import 'dart:convert'; import 'dart:io'; -// Package imports: -import 'package:nyalcf_inject/nyalcf_inject.dart'; - // Project imports: import 'package:nyalcf_core/models/user_info_model.dart'; import 'package:nyalcf_core/utils/logger.dart'; +// Package imports: +import 'package:nyalcf_inject/nyalcf_inject.dart'; class UserInfoStorage { static final _path = appSupportPath; @@ -22,8 +21,9 @@ class UserInfoStorage { /// 读取用户数据 static Future read() async { try { - final String result = - await File('$_path/session.json').readAsString(encoding: utf8); + final file = File('$_path/session.json'); + if (!await file.exists()) return null; + final String result = await file.readAsString(encoding: utf8); return UserInfoModel.fromJson(jsonDecode(result)); } catch (e, t) { Logger.error(e, t: t); diff --git a/nyalcf_gui/nyalcf_core_extend/lib/tasks/basic.dart b/nyalcf_gui/nyalcf_core_extend/lib/tasks/basic.dart index a05a81c..ffe6737 100644 --- a/nyalcf_gui/nyalcf_core_extend/lib/tasks/basic.dart +++ b/nyalcf_gui/nyalcf_core_extend/lib/tasks/basic.dart @@ -1,4 +1,4 @@ abstract class TaskBasic { - late Function? callback; + Function? callback; startUp({Function? callback}); } diff --git a/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart b/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart index f72ae8e..76cb982 100644 --- a/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart +++ b/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart @@ -7,13 +7,12 @@ import 'package:nyalcf_core/network/client/api_client.dart'; import 'package:nyalcf_core/storages/configurations/autostart_proxies_storage.dart'; import 'package:nyalcf_core/storages/stores/proxies_storage.dart'; import 'package:nyalcf_core/utils/logger.dart'; -import 'package:nyalcf_inject_extend/nyalcf_inject_extend.dart'; -import 'package:nyalcf_ui/controllers/proxies_controller.dart'; - // Project imports: import 'package:nyalcf_core_extend/storages/prefs/token_info_prefs.dart'; import 'package:nyalcf_core_extend/storages/prefs/user_info_prefs.dart'; import 'package:nyalcf_core_extend/tasks/basic.dart'; +import 'package:nyalcf_inject_extend/nyalcf_inject_extend.dart'; +import 'package:nyalcf_ui/controllers/proxies_controller.dart'; class TaskUpdateProxiesList extends TaskBasic { @override @@ -32,7 +31,7 @@ class TaskUpdateProxiesList extends TaskBasic { return; } if (rs.statusCode == 200) { - final List> list = rs.data['data']['list']; + final List list = rs.data['data']['list']; final List proxies = []; for (Map proxy in list) { diff --git a/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart b/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart index 5118c09..295a304 100644 --- a/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart +++ b/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart @@ -141,6 +141,7 @@ class _Controller extends GetxController { if (error != null) { this.error.value = error; } else { + OAuth.close(); ApiClient api = ApiClient(); message.value = '已取得返回数据,正在获取访问令牌,请稍后...'; final rs = await api From 05daefa9f28b13bf6943a335c75ae200bb456647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=8F=E6=B2=AB=E8=8A=B1=E7=81=ABzzz=F0=9F=8C=99?= Date: Tue, 7 Jan 2025 00:35:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=EF=BC=8CCHANGELOG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- RELEASE_CHANGELOG.md | 15 +++++++++++++-- nyalcf_cli/lib/commands/authorize.dart | 10 ++++++---- nyalcf_cli/nyalcf_core_extend/pubspec.yaml | 2 +- nyalcf_cli/pubspec.yaml | 2 +- .../lib/storages/stores/user_info_storage.dart | 5 +++-- nyalcf_core/pubspec.yaml | 2 +- .../lib/tasks/update_proxies_list.dart | 5 +++-- nyalcf_gui/nyalcf_core_extend/pubspec.yaml | 2 +- .../nyalcf_ui/lib/views/auth/authorize.dart | 8 +++++--- nyalcf_gui/pubspec.yaml | 2 +- 10 files changed, 35 insertions(+), 18 deletions(-) diff --git a/RELEASE_CHANGELOG.md b/RELEASE_CHANGELOG.md index 21e7bc4..82a7756 100644 --- a/RELEASE_CHANGELOG.md +++ b/RELEASE_CHANGELOG.md @@ -4,13 +4,24 @@ ### GUI -- 修复无法读取本地数据自动查询授权的问题 +- 使用随机可用端口进行授权 +- 修复了 Task 潜在的异常问题 +- 修复一个错误的日志输出 + +### CLI + +- 使用随机可用端口进行授权 + +### NyaLCF Core + +- 使用随机端口分配启动鉴权回调服务 ## 版本信息 - nyalcf_gui: 1.0.0 - nyalcf_cli: 1.0.0 - nyalcf_env: 2.0.0 -- nyalcf_core,nyalcf_inject: 2.0.0 +- nyalcf_core: 2.0.1 +- nyalcf_inject: 2.0.0 diff --git a/nyalcf_cli/lib/commands/authorize.dart b/nyalcf_cli/lib/commands/authorize.dart index 02352a4..03b6dfd 100644 --- a/nyalcf_cli/lib/commands/authorize.dart +++ b/nyalcf_cli/lib/commands/authorize.dart @@ -4,12 +4,8 @@ import 'dart:io'; // Package imports: import 'package:crypto/crypto.dart'; -// Project imports: -import 'package:nyalcf/templates/command.dart'; import 'package:nyalcf_core/models/user_info_model.dart'; import 'package:nyalcf_core/network/client/api/auth/oauth/access_token.dart'; -import 'package:nyalcf_core/network/client/api/user/frp/token.dart' - as user_frp_token; import 'package:nyalcf_core/network/client/api/user/info.dart' as user_info; import 'package:nyalcf_core/network/client/api_client.dart'; import 'package:nyalcf_core/network/server/oauth.dart'; @@ -17,6 +13,12 @@ import 'package:nyalcf_core/storages/stores/user_info_storage.dart'; import 'package:nyalcf_core/utils/logger.dart'; import 'package:nyalcf_core_extend/storages/token_storage.dart'; +// Project imports: +import 'package:nyalcf/templates/command.dart'; + +import 'package:nyalcf_core/network/client/api/user/frp/token.dart' + as user_frp_token; + class Authorize implements Command { static final _tokenStorage = TokenStorage(); diff --git a/nyalcf_cli/nyalcf_core_extend/pubspec.yaml b/nyalcf_cli/nyalcf_core_extend/pubspec.yaml index 2fa4ff5..dcc2246 100644 --- a/nyalcf_cli/nyalcf_core_extend/pubspec.yaml +++ b/nyalcf_cli/nyalcf_core_extend/pubspec.yaml @@ -1,6 +1,6 @@ name: nyalcf_core_extend description: "Nya LoCyanFrp! core extend module." -version: 2.0.0 +version: 2.0.1 homepage: https://nyalcf.1l1.icu publish_to: none diff --git a/nyalcf_cli/pubspec.yaml b/nyalcf_cli/pubspec.yaml index a0d4efe..01aa065 100644 --- a/nyalcf_cli/pubspec.yaml +++ b/nyalcf_cli/pubspec.yaml @@ -1,6 +1,6 @@ name: nyalcf description: "[CLI]The next generation of LoCyanFrp launcher." -version: 1.0.0+2 +version: 1.0.0+4 homepage: https://nyalcf.1l1.icu publish_to: none # repository: https://github.com/my_org/my_repo diff --git a/nyalcf_core/lib/storages/stores/user_info_storage.dart b/nyalcf_core/lib/storages/stores/user_info_storage.dart index 9f16fbb..5d0c029 100644 --- a/nyalcf_core/lib/storages/stores/user_info_storage.dart +++ b/nyalcf_core/lib/storages/stores/user_info_storage.dart @@ -2,11 +2,12 @@ import 'dart:convert'; import 'dart:io'; +// Package imports: +import 'package:nyalcf_inject/nyalcf_inject.dart'; + // Project imports: import 'package:nyalcf_core/models/user_info_model.dart'; import 'package:nyalcf_core/utils/logger.dart'; -// Package imports: -import 'package:nyalcf_inject/nyalcf_inject.dart'; class UserInfoStorage { static final _path = appSupportPath; diff --git a/nyalcf_core/pubspec.yaml b/nyalcf_core/pubspec.yaml index 6162979..9033b38 100644 --- a/nyalcf_core/pubspec.yaml +++ b/nyalcf_core/pubspec.yaml @@ -1,6 +1,6 @@ name: nyalcf_core description: 'Nya LoCyanFrp! core module.' -version: 2.0.0 +version: 2.0.1 homepage: https://nyalcf.1l1.icu publish_to: none # repository: https://github.com/my_org/my_repo diff --git a/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart b/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart index 76cb982..0303d61 100644 --- a/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart +++ b/nyalcf_gui/nyalcf_core_extend/lib/tasks/update_proxies_list.dart @@ -7,12 +7,13 @@ import 'package:nyalcf_core/network/client/api_client.dart'; import 'package:nyalcf_core/storages/configurations/autostart_proxies_storage.dart'; import 'package:nyalcf_core/storages/stores/proxies_storage.dart'; import 'package:nyalcf_core/utils/logger.dart'; +import 'package:nyalcf_inject_extend/nyalcf_inject_extend.dart'; +import 'package:nyalcf_ui/controllers/proxies_controller.dart'; + // Project imports: import 'package:nyalcf_core_extend/storages/prefs/token_info_prefs.dart'; import 'package:nyalcf_core_extend/storages/prefs/user_info_prefs.dart'; import 'package:nyalcf_core_extend/tasks/basic.dart'; -import 'package:nyalcf_inject_extend/nyalcf_inject_extend.dart'; -import 'package:nyalcf_ui/controllers/proxies_controller.dart'; class TaskUpdateProxiesList extends TaskBasic { @override diff --git a/nyalcf_gui/nyalcf_core_extend/pubspec.yaml b/nyalcf_gui/nyalcf_core_extend/pubspec.yaml index ea31a38..cea663c 100644 --- a/nyalcf_gui/nyalcf_core_extend/pubspec.yaml +++ b/nyalcf_gui/nyalcf_core_extend/pubspec.yaml @@ -1,6 +1,6 @@ name: nyalcf_core_extend description: "Nya LoCyanFrp! core extend module." -version: 2.0.0 +version: 2.0.1 homepage: https://nyalcf.1l1.icu publish_to: none diff --git a/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart b/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart index 295a304..9850436 100644 --- a/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart +++ b/nyalcf_gui/nyalcf_ui/lib/views/auth/authorize.dart @@ -1,10 +1,11 @@ // Dart imports: import 'dart:convert'; -// Package imports: -import 'package:crypto/crypto.dart'; // Flutter imports: import 'package:flutter/material.dart'; + +// Package imports: +import 'package:crypto/crypto.dart'; import 'package:get/get.dart'; import 'package:nyalcf_core/models/user_info_model.dart'; import 'package:nyalcf_core/network/client/api/auth/oauth/access_token.dart'; @@ -14,11 +15,12 @@ import 'package:nyalcf_core/network/client/api_client.dart'; import 'package:nyalcf_core/network/server/oauth.dart'; import 'package:nyalcf_core_extend/storages/prefs/token_info_prefs.dart'; import 'package:nyalcf_core_extend/storages/prefs/user_info_prefs.dart'; +import 'package:url_launcher/url_launcher.dart'; + // Project imports: import 'package:nyalcf_ui/models/appbar_actions.dart'; import 'package:nyalcf_ui/widgets/nya_loading_circle.dart'; import 'package:nyalcf_ui/widgets/nya_scaffold.dart'; -import 'package:url_launcher/url_launcher.dart'; class AuthorizeUI extends StatefulWidget { const AuthorizeUI({super.key}); diff --git a/nyalcf_gui/pubspec.yaml b/nyalcf_gui/pubspec.yaml index 1a8d90c..598cd60 100644 --- a/nyalcf_gui/pubspec.yaml +++ b/nyalcf_gui/pubspec.yaml @@ -1,6 +1,6 @@ name: nyalcf description: "[GUI]The next generation of LoCyanFrp launcher." -version: 1.0.0+3 +version: 1.0.0+4 homepage: https://nyalcf.1l1.icu publish_to: 'none'