fix: ip white list of app does not take effect (#140)
This commit is contained in:
@@ -59,7 +59,7 @@ public class App {
|
|||||||
|
|
||||||
public String config;
|
public String config;
|
||||||
|
|
||||||
private Map<String, String[]> ips = new HashMap<>(6);
|
public Map<String, String[]> ips = new HashMap<>(6);
|
||||||
|
|
||||||
public void setUseAuth(int i) {
|
public void setUseAuth(int i) {
|
||||||
if (i == AUTH_TYPE.SIGN || i == AUTH_TYPE.SECRETKEY || i == AUTH_TYPE.CUSTOM) {
|
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();
|
String end = a[1].trim();
|
||||||
this.ips.put(subnet, new String[]{beg, end});
|
this.ips.put(subnet, new String[]{beg, end});
|
||||||
} else {
|
} 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) {
|
public boolean allow(String ip) {
|
||||||
|
if (ips.containsKey(ip)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
int originSubnetLen = ip.lastIndexOf(Constants.Symbol.DOT);
|
int originSubnetLen = ip.lastIndexOf(Constants.Symbol.DOT);
|
||||||
for (Map.Entry<String, String[]> e : ips.entrySet()) {
|
for (Map.Entry<String, String[]> e : ips.entrySet()) {
|
||||||
String subnet = e.getKey();
|
String subnet = e.getKey();
|
||||||
@@ -116,21 +119,34 @@ public class App {
|
|||||||
if (originAddrLen < addrSegBeg.length() || addrSegEnd.length() < originAddrLen) {
|
if (originAddrLen < addrSegBeg.length() || addrSegEnd.length() < originAddrLen) {
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
|
boolean b = true;
|
||||||
if (originAddrLen == addrSegBeg.length()) {
|
if (originAddrLen == addrSegBeg.length()) {
|
||||||
for (byte j = 0; j < addrSegBeg.length(); j++) {
|
for (byte j = 0; j < addrSegBeg.length(); j++) {
|
||||||
if (ip.charAt(originSubnetLen + 1 + j) < addrSegBeg.charAt(j)) {
|
char o = ip.charAt(originSubnetLen + 1 + j);
|
||||||
return false;
|
char a = addrSegBeg.charAt(j);
|
||||||
|
if (o < a) {
|
||||||
|
b = false;
|
||||||
|
break;
|
||||||
|
} else if (o > a) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (originAddrLen == addrSegEnd.length()) {
|
if (b) {
|
||||||
for (byte j = 0; j < addrSegEnd.length(); j++) {
|
if (originAddrLen == addrSegEnd.length()) {
|
||||||
if (addrSegEnd.charAt(j) < ip.charAt(originSubnetLen + 1 + j)) {
|
for (byte j = 0; j < addrSegEnd.length(); j++) {
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
35
fizz-core/src/test/java/we/plugin/auth/AppTests.java
Normal file
35
fizz-core/src/test/java/we/plugin/auth/AppTests.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user