Skip to content

Commit 59710c1

Browse files
author
Marco (Valandur)
committed
Merge branch 'release/v5.4.3'
2 parents 32600e6 + 869f6f2 commit 59710c1

10 files changed

Lines changed: 190 additions & 371 deletions

File tree

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ allprojects {
1111
}
1212

1313
subprojects {
14+
apply plugin: "idea"
1415
apply plugin: "java"
1516
apply plugin: "com.github.johnrengelman.shadow"
1617

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=5.4.2
1+
version=5.4.3
22

33
minecraftVersion=1.12.2
44
spongeVersion=7.1.0

webapi-lib/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
idea {
2+
module {
3+
inheritOutputDirs = true
4+
}
5+
}
6+
17
dependencies {
28
compileOnly group: "com.fasterxml.jackson.core", name: "jackson-annotations", version: project.jacksonVersion
39

webapi-server/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
idea {
2+
module {
3+
inheritOutputDirs = true
4+
}
5+
}
6+
17
dependencies {
28
compile project(":webapi-lib")
39

webapi-sponge/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ plugins {
33
id "ninja.miserable.blossom" version "1.0.1"
44
}
55

6+
idea {
7+
module {
8+
inheritOutputDirs = true
9+
}
10+
}
11+
612
blossom {
713
def locMain = "src/main/java/valandur/webapi/util/Constants.java"
814
replaceToken "@version@", project.version, locMain

webapi-sponge/src/main/java/valandur/webapi/cache/CacheService.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class CacheService {
8282

8383
private static final String configFileName = "cache.conf";
8484

85+
private List<String> pluginFolders = new ArrayList<>();
8586
private List<String> censoredCommands = new ArrayList<>();
8687
private Map<String, Long> cacheDurations = new HashMap<>();
8788
private int numChatMessages;
@@ -102,6 +103,9 @@ public void init() {
102103
numChatMessages = config.chat_amount;
103104
numCommandCalls = config.cmd_amount;
104105

106+
pluginFolders.clear();
107+
pluginFolders.addAll(config.pluginFolders);
108+
105109
censoredCommands.clear();
106110
for (String cmd : config.censoredCommands) {
107111
if (!cmd.startsWith("/"))
@@ -544,14 +548,16 @@ public void updatePlugins() {
544548
plugins.put(plugin.getId(), new CachedPluginContainer(plugin));
545549
}
546550

547-
// Look for .jar files in the current directory, and compare their "mcmod.info" file
551+
// Look for .jar files in the specified directories, and compare their "mcmod.info" file
548552
// with the data we have of the loaded plugins to see if there are any unloaded ones.
549553
try {
550-
List<Path> paths = Files.walk(Paths.get("./"))
554+
List<Path> paths = new ArrayList<>();
555+
for (String folder : pluginFolders) {
556+
paths.addAll(Files.walk(Paths.get(folder))
551557
.map(Path::normalize)
552-
.filter(p -> Files.isRegularFile(p) && !p.toString().startsWith("libraries"))
553-
.filter(p -> p.toString().endsWith(".jar") || p.toString().endsWith(".disabled"))
554-
.collect(Collectors.toList());
558+
.filter(p -> Files.isRegularFile(p) && (p.toString().endsWith(".jar") || p.toString().endsWith(".jar.disabled")))
559+
.collect(Collectors.toList()));
560+
}
555561

556562
for (Path path : paths) {
557563
JarFile jarFile = new JarFile(path.toFile());

webapi-sponge/src/main/java/valandur/webapi/config/CacheConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class CacheConfig extends BaseConfig {
2020
@Setting(comment = "The number of seconds that the different types of data is cached for")
2121
public Map<String, Long> duration = new HashMap<>();
2222

23+
@Setting(comment = "The folders in which Web-API looks for other plugins.")
24+
public List<String> pluginFolders = Lists.newArrayList("./mods", "./plugins");
25+
2326
@Setting(comment = "These are commands that should not show up in the command log.\n" +
2427
"For example if you have a second auth plugin, or something where\n" +
2528
"players enter private data, put the command here, so that it's\n" +

webapi-sponge/src/main/java/valandur/webapi/security/SecurityService.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
import valandur.webapi.config.BaseConfig;
77
import valandur.webapi.config.PermissionConfig;
88
import valandur.webapi.user.UserPermissionStruct;
9-
import valandur.webapi.util.SubnetUtils;
9+
import valandur.webapi.util.CIDRAddress;
1010
import valandur.webapi.util.TreeNode;
1111

1212
import javax.ws.rs.ForbiddenException;
13+
import java.net.UnknownHostException;
1314
import java.util.*;
1415

1516
/**
1617
* The security service handles access permissions to routes within the Web-API.
1718
*/
1819
public class SecurityService {
19-
2020
private static final String configFileName = "permissions.conf";
2121

2222
public static final String API_KEY_HEADER = "X-WEBAPI-KEY";
@@ -27,8 +27,9 @@ public class SecurityService {
2727
public static final String ACCESS_CONTROL_HEADERS = "origin, content-type, x-webapi-key";
2828

2929
private Set<String> allowedProxyIps = new HashSet<>();
30-
private Set<SubnetUtils.SubnetInfo> allowedProxyCidrs = new HashSet<>();
30+
private Set<CIDRAddress> allowedProxyCidrs = new HashSet<>();
3131

32+
private Logger logger;
3233
private long start = System.nanoTime();
3334

3435
private PermissionConfig config;
@@ -41,7 +42,7 @@ public class SecurityService {
4142

4243

4344
public void init() {
44-
Logger logger = WebAPI.getLogger();
45+
this.logger = WebAPI.getLogger();
4546
logger.info("Loading keys & permissions...");
4647

4748
start = System.nanoTime();
@@ -52,9 +53,13 @@ public void init() {
5253

5354
for (String proxy : config.allowedProxies) {
5455
if (proxy.contains("/")) {
55-
SubnetUtils utils = new SubnetUtils(proxy);
56-
utils.setInclusiveHostCount(true);
57-
allowedProxyCidrs.add(utils.getInfo());
56+
try {
57+
allowedProxyCidrs.add(new CIDRAddress(proxy));
58+
} catch (UnknownHostException e) {
59+
logger.error("Unknown host: " + proxy, e);
60+
} catch (IllegalArgumentException e) {
61+
logger.error("Invalid CIDR address: " + proxy, e);
62+
}
5863
} else {
5964
allowedProxyIps.add(proxy);
6065
}
@@ -113,7 +118,14 @@ public PermissionStruct getPermissions(String key) {
113118
}
114119

115120
public boolean containsProxyIP(String ip) {
116-
return allowedProxyIps.contains(ip) || allowedProxyCidrs.stream().anyMatch(c -> c.isInRange(ip));
121+
return allowedProxyIps.contains(ip) || allowedProxyCidrs.stream().anyMatch(c -> {
122+
try {
123+
return c.isInRange(ip);
124+
} catch (UnknownHostException e) {
125+
logger.error("Unknown host: " + ip, e);
126+
return false;
127+
}
128+
});
117129
}
118130

119131
public void addTempKey(String key, UserPermissionStruct user) {
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2013 Edin Dazdarevic (edin.dazdarevic@gmail.com)
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
* THE SOFTWARE.
20+
*
21+
* */
22+
23+
package valandur.webapi.util;
24+
25+
import java.math.BigInteger;
26+
import java.net.InetAddress;
27+
import java.net.UnknownHostException;
28+
import java.nio.ByteBuffer;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
/**
33+
* A class that enables to get an IP range from CIDR specification. It supports
34+
* both IPv4 and IPv6.
35+
*/
36+
public class CIDRAddress {
37+
private final String cidr;
38+
39+
private InetAddress inetAddress;
40+
private InetAddress startAddress;
41+
private InetAddress endAddress;
42+
private final int prefixLength;
43+
44+
45+
public CIDRAddress(String cidr) throws IllegalArgumentException, UnknownHostException {
46+
this.cidr = cidr;
47+
48+
/* split CIDR to address and prefix part */
49+
if (this.cidr.contains("/")) {
50+
int index = this.cidr.indexOf("/");
51+
String addressPart = this.cidr.substring(0, index);
52+
String networkPart = this.cidr.substring(index + 1);
53+
54+
inetAddress = InetAddress.getByName(addressPart);
55+
prefixLength = Integer.parseInt(networkPart);
56+
57+
calculate();
58+
} else {
59+
throw new IllegalArgumentException("not an valid CIDR format!");
60+
}
61+
}
62+
63+
64+
private void calculate() throws UnknownHostException {
65+
ByteBuffer maskBuffer;
66+
int targetSize;
67+
if (inetAddress.getAddress().length == 4) {
68+
maskBuffer =
69+
ByteBuffer
70+
.allocate(4)
71+
.putInt(-1);
72+
targetSize = 4;
73+
} else {
74+
maskBuffer = ByteBuffer.allocate(16)
75+
.putLong(-1L)
76+
.putLong(-1L);
77+
targetSize = 16;
78+
}
79+
80+
BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength);
81+
82+
ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress());
83+
BigInteger ipVal = new BigInteger(1, buffer.array());
84+
85+
BigInteger startIp = ipVal.and(mask);
86+
BigInteger endIp = startIp.add(mask.not());
87+
88+
byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize);
89+
byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize);
90+
91+
this.startAddress = InetAddress.getByAddress(startIpArr);
92+
this.endAddress = InetAddress.getByAddress(endIpArr);
93+
94+
}
95+
96+
private byte[] toBytes(byte[] array, int targetSize) {
97+
int counter = 0;
98+
List<Byte> newArr = new ArrayList<Byte>();
99+
while (counter < targetSize && (array.length - 1 - counter >= 0)) {
100+
newArr.add(0, array[array.length - 1 - counter]);
101+
counter++;
102+
}
103+
104+
int size = newArr.size();
105+
for (int i = 0; i < (targetSize - size); i++) {
106+
107+
newArr.add(0, (byte) 0);
108+
}
109+
110+
byte[] ret = new byte[newArr.size()];
111+
for (int i = 0; i < newArr.size(); i++) {
112+
ret[i] = newArr.get(i);
113+
}
114+
return ret;
115+
}
116+
117+
public String getNetworkAddress() {
118+
return this.startAddress.getHostAddress();
119+
}
120+
121+
public String getBroadcastAddress() {
122+
return this.endAddress.getHostAddress();
123+
}
124+
125+
public boolean isInRange(String ipAddress) throws UnknownHostException {
126+
InetAddress address = InetAddress.getByName(ipAddress);
127+
BigInteger start = new BigInteger(1, this.startAddress.getAddress());
128+
BigInteger end = new BigInteger(1, this.endAddress.getAddress());
129+
BigInteger target = new BigInteger(1, address.getAddress());
130+
131+
int st = start.compareTo(target);
132+
int te = target.compareTo(end);
133+
134+
return (st == -1 || st == 0) && (te == -1 || te == 0);
135+
}
136+
}

0 commit comments

Comments
 (0)