Skip to content

Allow only selected domains #370

@SilentCoderHere

Description

@SilentCoderHere

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions