-
-
Notifications
You must be signed in to change notification settings - Fork 297
Open
Labels
Description
Is your feature request connected to a problem you're trying to solve? Please describe.
I would like to request a feature by which only selected domains will allowed to connect.
Describe the possible solution according to you (if any)
Add options in properties file e.g. block non listed domains and list of domains. If block non listed domains is true then override shouldInterceptRequest function to only allow domains that are listed in list.
Additional context
public static boolean BLOCK_DOMAINS = true;
public static List<String> ALLOWED_DOMAINS = Arrays.asList("allowed.domain");@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
// Skip if block domains is false
if (!SWVContext.BLOCK_DOMAINS) {
return super.shouldInterceptRequest(view, request);
}
String url = request.getUrl().toString();
// Necessary URLs
if (url.startsWith("file://") || url.startsWith("data:") || url.startsWith("about:")) {
return super.shouldInterceptRequest(view, request);
}
String domain = "";
try {
URI uri = new URI(url);
String host = uri.getHost();
if (host != null) {
domain = host.toLowerCase().replaceFirst("^www\\.", "");
}
} catch (Exception e) {
return super.shouldInterceptRequest(view, request);
}
boolean isAllowed = false;
for (String allowed : SWVContext.ALLOWED_DOMAINS) {
String normalizedAllowed = allowed.toLowerCase().replaceFirst("^www\\.", "");
if (domain.equals(normalizedAllowed) || domain.endsWith("." + normalizedAllowed)) {
isAllowed = true;
break;
}
}
// Block! Not listed in domains
if (!isAllowed) {
return new WebResourceResponse("text/plain", "utf-8", null);
}
return super.shouldInterceptRequest(view, request);
}Reactions are currently unavailable