From 4f99fcbb93ce1d6381c7d1272ab2794019079bcc Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Wed, 22 Jan 2020 11:09:04 -0500 Subject: [PATCH] pass device to initRules --- .../resources/stream/ReverseProxyResource.java | 13 +++++++++++-- .../java/bubble/resources/stream/RuleEngine.java | 14 ++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/bubble-server/src/main/java/bubble/resources/stream/ReverseProxyResource.java b/bubble-server/src/main/java/bubble/resources/stream/ReverseProxyResource.java index 136444a8..cd959fe8 100644 --- a/bubble-server/src/main/java/bubble/resources/stream/ReverseProxyResource.java +++ b/bubble-server/src/main/java/bubble/resources/stream/ReverseProxyResource.java @@ -5,10 +5,13 @@ import bubble.dao.app.AppRuleDAO; import bubble.model.account.Account; import bubble.model.app.AppMatcher; import bubble.model.app.AppRule; +import bubble.model.device.Device; import bubble.server.BubbleConfiguration; +import bubble.service.cloud.DeviceIdService; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.cobbzilla.util.http.URIBean; +import org.glassfish.grizzly.http.server.Request; import org.glassfish.jersey.server.ContainerRequest; import org.glassfish.jersey.server.ContainerResponse; import org.springframework.beans.factory.annotation.Autowired; @@ -22,6 +25,7 @@ import java.net.URISyntaxException; import java.util.*; import static bubble.ApiConstants.PROXY_ENDPOINT; +import static bubble.ApiConstants.getRemoteHost; import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.util.daemon.ZillaRuntime.empty; import static org.cobbzilla.util.http.HttpContentTypes.CONTENT_TYPE_ANY; @@ -35,16 +39,21 @@ public class ReverseProxyResource { @Autowired private AppMatcherDAO matcherDAO; @Autowired private AppRuleDAO ruleDAO; @Autowired private RuleEngine ruleEngine; + @Autowired private DeviceIdService deviceIdService; @Getter(lazy=true) private final int prefixLength = configuration.getHttp().getBaseUri().length() + PROXY_ENDPOINT.length() + 1; @GET @Path("/{path: .*}") @Consumes(CONTENT_TYPE_ANY) @Produces(CONTENT_TYPE_ANY) - public Response get(@Context ContainerRequest request, + public Response get(@Context Request req, + @Context ContainerRequest request, @Context ContainerResponse response, @PathParam("path") String path) throws URISyntaxException, IOException { final Account account = userPrincipal(request); + final String remoteHost = getRemoteHost(req); + final Device device = deviceIdService.findDeviceByIp(remoteHost); + if (device == null) return ruleEngine.passthru(request); final URIBean ub = getUriBean(request); final List matchers = matcherDAO.findByAccountAndFqdnAndEnabled(account.getUuid(), ub.getHost()); @@ -77,7 +86,7 @@ public class ReverseProxyResource { } // if 'rules' is null or empty, this will passthru - return ruleEngine.applyRulesAndSendResponse(request, account, ub, new ArrayList<>(rules)); + return ruleEngine.applyRulesAndSendResponse(request, account, device, ub, new ArrayList<>(rules)); } } diff --git a/bubble-server/src/main/java/bubble/resources/stream/RuleEngine.java b/bubble-server/src/main/java/bubble/resources/stream/RuleEngine.java index 29a073fc..813fd190 100644 --- a/bubble-server/src/main/java/bubble/resources/stream/RuleEngine.java +++ b/bubble-server/src/main/java/bubble/resources/stream/RuleEngine.java @@ -63,7 +63,7 @@ public class RuleEngine { Account account, Device device, String matcherUuid) { - final AppRuleHarness ruleHarness = initRules(account, new String[]{ matcherUuid }).get(0); + final AppRuleHarness ruleHarness = initRules(account, device, new String[]{ matcherUuid }).get(0); return ruleHarness.getDriver().preprocess(ruleHarness, filter, account, req, request); } @@ -93,6 +93,7 @@ public class RuleEngine { public Response applyRulesAndSendResponse(ContainerRequest request, Account account, + Device device, URIBean ub, List rules) throws IOException { @@ -103,7 +104,7 @@ public class RuleEngine { // initialize drivers -- todo: cache drivers / todo: ensure cache is shorter than session timeout, // since drivers that talk thru API will get a session key in their config - rules = initRules(account, rules); + rules = initRules(account, device, rules); final AppRuleHarness firstRule = rules.get(0); // filter request @@ -130,19 +131,20 @@ public class RuleEngine { public Response applyRulesAndSendResponse(ContainerRequest request, Account account, + Device device, String[] matcherIds) throws IOException { if (empty(matcherIds)) return passthru(null, request); // init rules - final List ruleHarnesses = initRules(account, matcherIds); + final List ruleHarnesses = initRules(account, device, matcherIds); final AppRuleHarness firstRule = ruleHarnesses.get(0); final InputStream responseEntity = firstRule.getDriver().filterResponse(request.getEntityStream()); return sendResponse(responseEntity); } - public List initRules(Account account, String[] matcherIds) { + public List initRules(Account account, Device device, String[] matcherIds) { final List matchers = matcherDAO.findByUuids(matcherIds); if (matchers.size() != matcherIds.length) { log.warn("initRules: duplicate rules, or could not resolve some rule(s)"); @@ -156,11 +158,11 @@ public class RuleEngine { } ruleHarnesses.add(new AppRuleHarness(m, rule)); } - ruleHarnesses = initRules(account, ruleHarnesses); + ruleHarnesses = initRules(account, device, ruleHarnesses); return ruleHarnesses; } - public List initRules(Account account, List rules) { + public List initRules(Account account, Device device, List rules) { for (AppRuleHarness h : rules) { final RuleDriver ruleDriver = driverDAO.findByUuid(h.getRule().getDriver()); if (ruleDriver == null) {