Kaynağa Gözat

pass device to initRules

tags/v0.2.0
Jonathan Cobb 5 yıl önce
ebeveyn
işleme
4f99fcbb93
2 değiştirilmiş dosya ile 19 ekleme ve 8 silme
  1. +11
    -2
      bubble-server/src/main/java/bubble/resources/stream/ReverseProxyResource.java
  2. +8
    -6
      bubble-server/src/main/java/bubble/resources/stream/RuleEngine.java

+ 11
- 2
bubble-server/src/main/java/bubble/resources/stream/ReverseProxyResource.java Dosyayı Görüntüle

@@ -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<AppMatcher> 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));
}
}



+ 8
- 6
bubble-server/src/main/java/bubble/resources/stream/RuleEngine.java Dosyayı Görüntüle

@@ -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<AppRuleHarness> 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<AppRuleHarness> ruleHarnesses = initRules(account, matcherIds);
final List<AppRuleHarness> ruleHarnesses = initRules(account, device, matcherIds);
final AppRuleHarness firstRule = ruleHarnesses.get(0);

final InputStream responseEntity = firstRule.getDriver().filterResponse(request.getEntityStream());
return sendResponse(responseEntity);
}

public List<AppRuleHarness> initRules(Account account, String[] matcherIds) {
public List<AppRuleHarness> initRules(Account account, Device device, String[] matcherIds) {
final List<AppMatcher> 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<AppRuleHarness> initRules(Account account, List<AppRuleHarness> rules) {
public List<AppRuleHarness> initRules(Account account, Device device, List<AppRuleHarness> rules) {
for (AppRuleHarness h : rules) {
final RuleDriver ruleDriver = driverDAO.findByUuid(h.getRule().getDriver());
if (ruleDriver == null) {


Yükleniyor…
İptal
Kaydet