Merge master into develop (#259)
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<artifactId>fizz-gateway-community</artifactId>
|
||||
<groupId>com.fizzgate</groupId>
|
||||
<version>2.2.0</version>
|
||||
<version>2.2.1</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
@@ -59,6 +59,7 @@ import we.proxy.FizzWebClient;
|
||||
import we.proxy.http.HttpInstanceService;
|
||||
import we.util.JacksonUtils;
|
||||
import we.util.MapUtil;
|
||||
import we.util.TypeUtils;
|
||||
import we.xml.JsonToXml;
|
||||
import we.xml.XmlToJson;
|
||||
import we.xml.XmlToJson.Builder;
|
||||
@@ -363,10 +364,19 @@ public class RequestInput extends RPCInput implements IInput{
|
||||
if (CONTENT_TYPE_XML.equals(reqContentType) || CONTENT_TYPE_TEXT_XML.equals(reqContentType)) {
|
||||
// convert JSON to XML if it is XML content type
|
||||
request.put("jsonBody", request.get("body"));
|
||||
String jsonStr = JSON.toJSONString(request.get("body"));
|
||||
String jsonStr = null;
|
||||
if (TypeUtils.isBasicType(request.get("body"))) {
|
||||
jsonStr = request.get("body").toString();
|
||||
} else {
|
||||
jsonStr = JSON.toJSONString(request.get("body"));
|
||||
}
|
||||
LOGGER.info("jsonBody={}", jsonStr);
|
||||
JsonToXml jsonToXml = new JsonToXml.Builder(jsonStr).build();
|
||||
body = jsonToXml.toString();
|
||||
if (jsonStr.startsWith("{") || jsonStr.startsWith("[")) {
|
||||
JsonToXml jsonToXml = new JsonToXml.Builder(jsonStr).build();
|
||||
body = jsonToXml.toString();
|
||||
} else {
|
||||
body = jsonStr;
|
||||
}
|
||||
request.put("body", body);
|
||||
LOGGER.info("body={}", body);
|
||||
LOGGER.info("headers={}", JSON.toJSONString(headers));
|
||||
@@ -379,7 +389,11 @@ public class RequestInput extends RPCInput implements IInput{
|
||||
} else if (CONTENT_TYPE_FORM_URLENCODED.equals(reqContentType)) {
|
||||
body = BodyInserters.fromFormData(MapUtil.toMultiValueMap((Map<String, Object>) request.get("body")));
|
||||
} else {
|
||||
body = JSON.toJSONString(request.get("body"));
|
||||
if (TypeUtils.isBasicType(request.get("body"))) {
|
||||
body = request.get("body").toString();
|
||||
} else {
|
||||
body = JSON.toJSONString(request.get("body"));
|
||||
}
|
||||
}
|
||||
|
||||
HttpMethod aggrMethod = HttpMethod.valueOf(inputContext.getStepContext().getInputReqAttr("method").toString());
|
||||
|
||||
@@ -23,7 +23,6 @@ import we.util.Constants;
|
||||
import we.util.JacksonUtils;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author hongqiaowei
|
||||
@@ -41,25 +40,25 @@ public class App {
|
||||
static final int SECRETKEY = 3;
|
||||
}
|
||||
|
||||
public int isDeleted = 0; // tb_app_auth.is_deleted
|
||||
public int isDeleted = 0; // tb_app_auth.is_deleted
|
||||
|
||||
public int id; // tb_app_auth.id
|
||||
public int id; // tb_app_auth.id
|
||||
|
||||
public String app; // tb_app_auth.app
|
||||
public String app; // tb_app_auth.app
|
||||
|
||||
public String name; // tb_app_auth.app_name
|
||||
public String name; // tb_app_auth.app_name
|
||||
|
||||
public boolean useAuth = false; // 0:false, 1:true
|
||||
public boolean useAuth = false; // 0:false, 1:true
|
||||
|
||||
public int authType;
|
||||
public int authType;
|
||||
|
||||
public String secretkey;
|
||||
public String secretkey;
|
||||
|
||||
public boolean useWhiteList = false;
|
||||
public boolean useWhiteList = false;
|
||||
|
||||
public String config;
|
||||
public String config;
|
||||
|
||||
public Map<String, String[]> ips = new HashMap<>(8);
|
||||
public Map<String, List<String[]>> ips = new HashMap<>();
|
||||
|
||||
public void setUseAuth(int i) {
|
||||
if (i == AUTH_TYPE.SIGN || i == AUTH_TYPE.SECRETKEY || i == AUTH_TYPE.CUSTOM) {
|
||||
@@ -82,12 +81,17 @@ public class App {
|
||||
String subnet = ip.substring(0, i).trim();
|
||||
String addrSeg = ip.substring(i + 1).trim();
|
||||
if ("*".equals(addrSeg)) {
|
||||
this.ips.put(subnet, new String[]{"2", "254"});
|
||||
this.ips.put(subnet, Collections.singletonList(new String[]{"1", "255"}));
|
||||
} else if (addrSeg.indexOf('-') > 0) {
|
||||
String[] a = StringUtils.split(addrSeg, '-');
|
||||
String beg = a[0].trim();
|
||||
String end = a[1].trim();
|
||||
this.ips.put(subnet, new String[]{beg, end});
|
||||
List<String[]> lst = this.ips.get(subnet);
|
||||
if (lst == null) {
|
||||
lst = new ArrayList<>();
|
||||
this.ips.put(subnet, lst);
|
||||
}
|
||||
lst.add(new String[]{beg, end});
|
||||
} else {
|
||||
this.ips.put(ip, null);
|
||||
}
|
||||
@@ -101,7 +105,7 @@ public class App {
|
||||
return true;
|
||||
}
|
||||
int originSubnetLen = ip.lastIndexOf(Constants.Symbol.DOT);
|
||||
for (Map.Entry<String, String[]> e : ips.entrySet()) {
|
||||
for (Map.Entry<String, List<String[]>> e : ips.entrySet()) {
|
||||
String subnet = e.getKey();
|
||||
int subnetLen = subnet.length();
|
||||
byte i = 0;
|
||||
@@ -113,47 +117,57 @@ public class App {
|
||||
}
|
||||
if (i == subnetLen) {
|
||||
int originAddrLen = ip.length() - originSubnetLen - 1;
|
||||
String[] addrSeg = e.getValue();
|
||||
String addrSegBeg = addrSeg[0];
|
||||
String addrSegEnd = addrSeg[1];
|
||||
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++) {
|
||||
char o = ip.charAt(originSubnetLen + 1 + j);
|
||||
char a = addrSegBeg.charAt(j);
|
||||
if (o < a) {
|
||||
b = false;
|
||||
break;
|
||||
} else if (o > a) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
boolean in = false;
|
||||
for (String[] addrSeg : e.getValue()) {
|
||||
in = inAddrSeg(ip, originSubnetLen, originAddrLen, addrSeg);
|
||||
if (in) {
|
||||
return in;
|
||||
}
|
||||
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 b;
|
||||
}
|
||||
return in;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean inAddrSeg(String ip, int originSubnetLen, int originAddrLen, String[] addrSeg) {
|
||||
String addrSegBeg = addrSeg[0];
|
||||
String addrSegEnd = addrSeg[1];
|
||||
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++) {
|
||||
char o = ip.charAt(originSubnetLen + 1 + j);
|
||||
char a = addrSegBeg.charAt(j);
|
||||
if (o < a) {
|
||||
b = false;
|
||||
break;
|
||||
} else if (o > a) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
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 b;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JacksonUtils.writeValueAsString(this);
|
||||
|
||||
@@ -13,23 +13,53 @@ 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");
|
||||
app.setIps("10.237.148.107,10.237.148.134,172.25.33.*,172.25.63.*,172.25.102.*,172.25.104.136-138," +
|
||||
"101.236.11.34-37," +
|
||||
"101.236.11.50-53");
|
||||
System.out.println("app: " + app);
|
||||
boolean allow = app.allow("10.237.148.107");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("10.237.148.134");
|
||||
assertTrue(allow);
|
||||
|
||||
allow = app.allow("172.25.102.2");
|
||||
allow = app.allow("172.25.102.254");
|
||||
allow = app.allow("172.25.102.1");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.102.255");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.102.3");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.102.251");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.102.249");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.102.138");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.102.22");
|
||||
assertTrue(allow);
|
||||
|
||||
allow = app.allow("172.25.104.136");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.104.137");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("172.25.104.138");
|
||||
assertTrue(allow);
|
||||
|
||||
assertTrue(allow);
|
||||
allow = app.allow("101.236.11.34");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("101.236.11.35");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("101.236.11.36");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("101.236.11.37");
|
||||
assertTrue(allow);
|
||||
|
||||
allow = app.allow("101.236.11.50");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("101.236.11.51");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("101.236.11.52");
|
||||
assertTrue(allow);
|
||||
allow = app.allow("101.236.11.53");
|
||||
assertTrue(allow);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user