fix: ip white list of app does not take effect (#140)

This commit is contained in:
hongqiaowei
2021-04-25 17:06:09 +08:00
committed by GitHub
parent bee7f22de0
commit 02f42a92c5
2 changed files with 60 additions and 9 deletions

View File

@@ -59,7 +59,7 @@ public class App {
public String config;
private Map<String, String[]> ips = new HashMap<>(6);
public Map<String, String[]> ips = new HashMap<>(6);
public void setUseAuth(int i) {
if (i == AUTH_TYPE.SIGN || i == AUTH_TYPE.SECRETKEY || i == AUTH_TYPE.CUSTOM) {
@@ -89,7 +89,7 @@ public class App {
String end = a[1].trim();
this.ips.put(subnet, new String[]{beg, end});
} else {
this.ips.put(subnet, new String[]{addrSeg, addrSeg});
this.ips.put(ip, null);
}
}
);
@@ -97,6 +97,9 @@ public class App {
}
public boolean allow(String ip) {
if (ips.containsKey(ip)) {
return true;
}
int originSubnetLen = ip.lastIndexOf(Constants.Symbol.DOT);
for (Map.Entry<String, String[]> e : ips.entrySet()) {
String subnet = e.getKey();
@@ -116,21 +119,34 @@ public class App {
if (originAddrLen < addrSegBeg.length() || addrSegEnd.length() < originAddrLen) {
return false;
} else {
boolean b = true;
if (originAddrLen == addrSegBeg.length()) {
for (byte j = 0; j < addrSegBeg.length(); j++) {
if (ip.charAt(originSubnetLen + 1 + j) < addrSegBeg.charAt(j)) {
return false;
char o = ip.charAt(originSubnetLen + 1 + j);
char a = addrSegBeg.charAt(j);
if (o < a) {
b = false;
break;
} else if (o > a) {
break;
}
}
}
if (originAddrLen == addrSegEnd.length()) {
for (byte j = 0; j < addrSegEnd.length(); j++) {
if (addrSegEnd.charAt(j) < ip.charAt(originSubnetLen + 1 + j)) {
return false;
if (b) {
if (originAddrLen == addrSegEnd.length()) {
for (byte j = 0; j < addrSegEnd.length(); j++) {
char a = addrSegEnd.charAt(j);
char o = ip.charAt(originSubnetLen + 1 + j);
if (a < o) {
b = false;
break;
} else if (a > o) {
break;
}
}
}
}
return true;
return b;
}
}
}

View File

@@ -0,0 +1,35 @@
package we.plugin.auth;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author hongqiaowei
*/
public class AppTests {
@Test
void ipWhiteListTest() {
App app = new App();
app.setIps("10.237.148.107,10.237.148.134,172.25.33.*,172.25.63.*,172.25.102.*,172.25.104.136-138");
System.out.println("app: " + app);
boolean allow = app.allow("10.237.148.107");
allow = app.allow("10.237.148.134");
allow = app.allow("172.25.102.2");
allow = app.allow("172.25.102.254");
allow = app.allow("172.25.102.3");
allow = app.allow("172.25.102.251");
allow = app.allow("172.25.102.249");
allow = app.allow("172.25.102.138");
allow = app.allow("172.25.102.22");
allow = app.allow("172.25.104.136");
allow = app.allow("172.25.104.137");
allow = app.allow("172.25.104.138");
assertTrue(allow);
}
}