-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathAccountCommand.java
More file actions
141 lines (133 loc) · 7.26 KB
/
Copy pathAccountCommand.java
File metadata and controls
141 lines (133 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2021-2026 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.cli.command.impl;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.raphimc.minecraftauth.MinecraftAuth;
import net.raphimc.minecraftauth.bedrock.BedrockAuthManager;
import net.raphimc.minecraftauth.java.JavaAuthManager;
import net.raphimc.minecraftauth.msa.model.MsaDeviceCode;
import net.raphimc.minecraftauth.msa.service.impl.DeviceCodeMsaAuthService;
import net.raphimc.viabedrock.protocol.data.ProtocolConstants;
import net.raphimc.viaproxy.ViaProxy;
import net.raphimc.viaproxy.cli.command.Command;
import net.raphimc.viaproxy.cli.command.executor.CommandExecutor;
import net.raphimc.viaproxy.saves.impl.accounts.Account;
import net.raphimc.viaproxy.saves.impl.accounts.BedrockAccount;
import net.raphimc.viaproxy.saves.impl.accounts.MicrosoftAccount;
import net.raphimc.viaproxy.util.TFunction;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import static com.mojang.brigadier.arguments.IntegerArgumentType.getInteger;
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.string;
public class AccountCommand extends Command {
public AccountCommand() {
super("account", "Manage the account list and the currently selected account", "account list/select <index>/deselect/add (offline <name>/microsoft/bedrock)/remove <index>");
}
@Override
public void register(LiteralArgumentBuilder<CommandExecutor> builder) {
builder.then(literal("list").executes(ctx -> {
List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
if (accounts.isEmpty()) {
ctx.getSource().sendMessage("No accounts added yet.");
} else {
for (int i = 0; i < accounts.size(); i++) {
boolean isSelected = ViaProxy.getConfig().getProxy().getAccount() == accounts.get(i);
ctx.getSource().sendMessage("[" + i + "] " + accounts.get(i).getDisplayString() + (isSelected ? " <--" : ""));
}
}
return 1;
}));
builder.then(literal("deselect").executes(ctx -> {
ViaProxy.getConfig().getProxy().setAccount(null);
ctx.getSource().sendMessage("Deselected current account.");
return 1;
}));
builder.then(literal("select").then(argument("index", integer(0)).executes(ctx -> {
List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
if (accounts.isEmpty()) {
ctx.getSource().sendMessage("No accounts to select from.");
return 0;
}
int index = getInteger(ctx, "index");
if (index < 0 || index >= accounts.size()) {
ctx.getSource().sendMessage("Invalid account index (run 'account list' to see a list of all accounts).");
return 0;
}
Account account = ViaProxy.getSaveManager().accountsSave.getAccounts().get(index);
ViaProxy.getConfig().getProxy().setAccount(account);
ctx.getSource().sendMessage("Selected account " + index + ": " + account.getDisplayString() + ".");
return 1;
})));
builder.then(literal("add")
.then(literal("offline").then(argument("name", string()).executes(ctx -> {
String name = getString(ctx, "name");
ViaProxy.getSaveManager().accountsSave.addAccount(name);
ViaProxy.getSaveManager().save();
ctx.getSource().sendMessage("Added offline account '" + name + "'.");
return 1;
})))
.then(literal("microsoft").executes(ctx -> {
return this.handleLogin(ctx.getSource(), msaDeviceCodeConsumer -> new MicrosoftAccount(JavaAuthManager.create(MinecraftAuth.createHttpClient()).login(DeviceCodeMsaAuthService::new, msaDeviceCodeConsumer)));
}))
.then(literal("bedrock").executes(ctx -> {
return this.handleLogin(ctx.getSource(), msaDeviceCodeConsumer -> new BedrockAccount(BedrockAuthManager.create(MinecraftAuth.createHttpClient(), ProtocolConstants.BEDROCK_VERSION_NAME).login(DeviceCodeMsaAuthService::new, msaDeviceCodeConsumer)));
}))
);
builder.then(literal("remove").then(argument("index", integer(0)).executes(ctx -> {
List<Account> accounts = ViaProxy.getSaveManager().accountsSave.getAccounts();
if (accounts.isEmpty()) {
ctx.getSource().sendMessage("No accounts to remove.");
return 0;
}
int index = getInteger(ctx, "index");
if (index < 0 || index >= accounts.size()) {
ctx.getSource().sendMessage("Invalid account index (run 'account list' to see a list of all accounts).");
return 0;
}
Account account = ViaProxy.getSaveManager().accountsSave.getAccounts().get(index);
ViaProxy.getSaveManager().accountsSave.removeAccount(account);
ViaProxy.getSaveManager().save();
if (ViaProxy.getConfig().getProxy().getAccount() == account) {
ViaProxy.getConfig().getProxy().setAccount(null);
}
ctx.getSource().sendMessage("Removed account " + index + ": " + account.getDisplayString() + ".");
return 1;
})));
}
private int handleLogin(final CommandExecutor source, final TFunction<Consumer<MsaDeviceCode>, Account> requestHandler) {
try {
Account account = requestHandler.apply(code -> {
source.sendMessage("Please open your browser and visit " + code.getDirectVerificationUri() + " and login with your Microsoft account.");
source.sendMessage("If the code is not inserted automatically, please enter the code: " + code.getUserCode() + ".");
});
ViaProxy.getSaveManager().accountsSave.addAccount(account);
ViaProxy.getSaveManager().save();
return 1;
} catch (InterruptedException ignored) {
} catch (TimeoutException e) {
source.sendMessage("The authentication process timed out.");
} catch (Throwable t) {
source.sendMessage("An error occurred while trying to authenticate: " + t.getMessage());
t.printStackTrace();
}
return 0;
}
}