-
-
Notifications
You must be signed in to change notification settings - Fork 859
Expand file tree
/
Copy pathProxyOptions.java
More file actions
145 lines (127 loc) · 5.1 KB
/
ProxyOptions.java
File metadata and controls
145 lines (127 loc) · 5.1 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
142
143
144
145
/*
* Copyright (C) 2018-2023 Velocity 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 <https://www.gnu.org/licenses/>.
*/
package com.velocitypowered.proxy;
import com.velocitypowered.api.proxy.server.ServerInfo;
import com.velocitypowered.proxy.util.AddressUtil;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import joptsimple.ValueConversionException;
import joptsimple.ValueConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Holds parsed command line options.
*/
public final class ProxyOptions {
private static final Logger logger = LogManager.getLogger(ProxyOptions.class);
private final boolean help;
private final @Nullable Integer port;
private final @Nullable Boolean haproxy;
private final boolean ignoreConfigServers;
private final List<ServerInfo> servers;
private final List<File> extraPluginDirectories;
ProxyOptions(final String[] args) {
final OptionParser parser = new OptionParser();
final OptionSpec<Void> help = parser.acceptsAll(Arrays.asList("h", "help"), "Print help")
.forHelp();
final OptionSpec<Integer> port = parser.acceptsAll(Arrays.asList("p", "port"),
"Specify the bind port to be used. The configuration bind port will be ignored.")
.withRequiredArg().ofType(Integer.class);
final OptionSpec<Boolean> haproxy = parser.acceptsAll(
Arrays.asList("haproxy", "haproxy-protocol"),
"Choose whether to enable haproxy protocol. "
+ "The configuration haproxy protocol will be ignored.")
.withRequiredArg().ofType(Boolean.class);
final OptionSpec<ServerInfo> servers = parser.accepts("add-server",
"Define a server mapping. "
+ "You must ensure that server name is not also registered in the config or use --ignore-config-servers.")
.withRequiredArg().withValuesConvertedBy(new ServerInfoConverter());
final OptionSpec<Void> ignoreConfigServers = parser.accepts("ignore-config-servers",
"Skip registering servers from the config file. "
+ "Useful in dynamic setups or with the --add-server flag.");
final OptionSpec<File> extraPluginDirectories = parser.acceptsAll(List.of("add-plugin-dir", "add-extra-plugin-dir"),
"Specify paths to extra plugin directories to be loaded in addition to the plugins folder. "
+ "This argument can be specified multiple times, once for each extra plugin dir path.")
.withRequiredArg()
.ofType(File.class)
.defaultsTo(new File[] {})
.describedAs("Plugin directory");
final OptionSet set = parser.parse(args);
this.help = set.has(help);
this.port = port.value(set);
this.haproxy = haproxy.value(set);
this.servers = servers.values(set);
this.ignoreConfigServers = set.has(ignoreConfigServers);
this.extraPluginDirectories = extraPluginDirectories.values(set);
if (this.help) {
try {
parser.printHelpOn(System.out);
} catch (final IOException e) {
logger.error("Could not print help", e);
}
}
}
boolean isHelp() {
return this.help;
}
public @Nullable Integer getPort() {
return this.port;
}
public @Nullable Boolean isHaproxy() {
return this.haproxy;
}
public boolean isIgnoreConfigServers() {
return this.ignoreConfigServers;
}
public List<ServerInfo> getServers() {
return this.servers;
}
public List<File> getExtraPluginDirectories() {
return this.extraPluginDirectories;
}
private static class ServerInfoConverter implements ValueConverter<ServerInfo> {
@Override
public ServerInfo convert(String s) {
String[] split = s.split(":", 2);
if (split.length < 2) {
throw new ValueConversionException("Invalid server format. Use <name>:<address>");
}
InetSocketAddress address;
try {
address = AddressUtil.parseAddress(split[1]);
} catch (IllegalStateException e) {
throw new ValueConversionException("Invalid hostname for server flag with name: " + split[0]);
}
return new ServerInfo(split[0], address);
}
@Override
public Class<? extends ServerInfo> valueType() {
return ServerInfo.class;
}
@Override
public String valuePattern() {
return "name>:<address";
}
}
}