Skip to content

Commit

Permalink
CLI: login & logout
Browse files Browse the repository at this point in the history
  • Loading branch information
Muska-Ami committed Jul 19, 2024
1 parent e6d5f73 commit 925a50f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/
/nyalcf_cli/build/
13 changes: 11 additions & 2 deletions nyalcf_cli/bin/nyalcf_cli.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:nyalcf/arguments.dart';
import 'package:nyalcf/utils/path_provider.dart';
import 'package:nyalcf/utils/state.dart';
import 'package:nyalcf_core/storages/injector.dart';
import 'package:nyalcf_core/storages/stores/user_info_storage.dart';
import 'package:nyalcf_core/utils/logger.dart';
import 'package:nyalcf_inject/nyalcf_inject.dart';

Expand All @@ -18,7 +19,7 @@ ArgParser buildParser() {

void printUsage(ArgParser argParser) {
Logger.info('Usage: dart nyalcf_cli.dart <flags> [arguments]');
Logger.info(argParser.usage.split('\n'));
argParser.usage.split('\n').forEach((val) => Logger.info(val));
}

void main(List<String> arguments) async {
Expand All @@ -30,6 +31,10 @@ void main(List<String> arguments) async {
await StoragesInjector.init();
await Logger.init();

if (await File('$appSupportPath/session.json').exists()) {
await UserInfoStorage.read();
}

Logger.debug('Append info has been set: $appendInfo');

final ArgParser argParser = buildParser();
Expand All @@ -44,7 +49,11 @@ void main(List<String> arguments) async {
}

if (results.wasParsed('login')) {
Login().main(results.rest);
await Login().main(results.rest);
}
if (results.wasParsed('logout')) {
await UserInfoStorage.sigo();
Logger.info('Session data removed.');
}

if (results.wasParsed('version')) {
Expand Down
1 change: 1 addition & 0 deletions nyalcf_cli/lib/actions/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Config implements CommandImplement {
_lcs.cfg.set(args[1], convertedValue);
_lcs.save();
Logger.info('Node "${args[1]}" has been set to: "${args[2]}"');
result = true;
} else {
Logger.error(
'Could not set ${args[1]} to ${args[2]}: could not automatic convert type to origin type, please edit the configuration manually.');
Expand Down
17 changes: 16 additions & 1 deletion nyalcf_cli/lib/actions/launch.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
class Launch {
import 'package:nyalcf/utils/template/command_implement.dart';

class Launch implements CommandImplement {
@override
bool result = false;

@override
bool end() {
// TODO: implement end
throw UnimplementedError();
}

@override
void main(List<String> args) {
// TODO: implement main
}

}
30 changes: 25 additions & 5 deletions nyalcf_cli/lib/actions/login.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:nyalcf/utils/template/command_implement.dart';
import 'package:nyalcf/utils/text_encrypt.dart';
import 'package:nyalcf_core/models/user_info_model.dart';
import 'package:nyalcf_core/network/dio/auth/auth.dart';
import 'package:nyalcf_core/storages/stores/user_info_storage.dart';
import 'package:nyalcf_core/utils/logger.dart';

class Login implements CommandImplement {

@override
bool result = false;

Expand All @@ -12,8 +15,25 @@ class Login implements CommandImplement {
}

@override
void main(List<String> args) {

// Logger.info(args);
Future<void> main(List<String> args) async {
if (args.length == 2) {
final res = await LoginAuth().requestLogin(args[0], args[1]);
if (res.status) {
final UserInfoModel userInfo = res.data['user_info'];
Logger.info('Login successfully.');
Logger.info('Info:');
Logger.info('- name: ${userInfo.user}');
Logger.info('- email: ${userInfo.email}');
Logger.info('- login token: ${TextEncrypt.obscure(userInfo.token)}');
Logger.info('- frp token: ${TextEncrypt.obscure(userInfo.frpToken)}');
Logger.info('- speed limit: ${userInfo.inbound / 1024 * 8}Mbps/${userInfo.outbound / 1024 * 8}Mbps');
Logger.info('- traffic left: ${userInfo.traffic / 1024}GiB');
await UserInfoStorage.save(userInfo);
Logger.info('Session saved.');
result = true;
}
} else {
Logger.error('No valid arguments provided.');
}
}
}
}
10 changes: 8 additions & 2 deletions nyalcf_cli/lib/arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ class Arguments {
'login',
abbr: 'l',
negatable: false,
help: 'Login to LoCyanFrp.',
help: 'Login to LoCyanFrp. usage: <username> <password>',
)
..addFlag(
'logout',
negatable: false,
help: 'Logout to LoCyanFrp.',
)
..addFlag(
'config',
abbr: 'c',
negatable: false,
help: 'Modify Nya LoCyanFrp! CLI configuration.',
help:
'Modify Nya LoCyanFrp! CLI configuration. usage: <configuration> <node> <value>',
)
..addFlag(
'verbose',
Expand Down
18 changes: 18 additions & 0 deletions nyalcf_cli/lib/utils/text_encrypt.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:nyalcf_core/utils/logger.dart';

class TextEncrypt {
static String obscure(String origin) {
Logger.debug('Input: $origin');
final length = origin.length;
String output = '';
output += origin.substring(0, 3);
var i = 0;
while (i != length - 6) {
output += '*';
i++;
}
output += origin.substring(length - 3, length);
Logger.debug('Output: $output');
return output;
}
}

0 comments on commit 925a50f

Please sign in to comment.