Skip to content

Commit

Permalink
chore: Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
PerfectSlayer committed Jan 17, 2021
1 parent 4335218 commit e428577
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
1 change: 0 additions & 1 deletion app/src/main/java/org/adaway/db/dao/HostListItemDao.java
Expand Up @@ -5,7 +5,6 @@
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

Expand Down
106 changes: 53 additions & 53 deletions app/src/main/java/org/adaway/model/source/SourceLoader.java
Expand Up @@ -58,7 +58,7 @@ void parse(Reader reader, HostListItemDao hostListItemDao) {
ItemInserter inserter = new ItemInserter(hostsListItemQueue, hostListItemDao, parserCount);
ExecutorService executorService = Executors.newFixedThreadPool(
parserCount + 2,
r -> new Thread(r, "SourceLoader")
r -> new Thread(r, TAG)
);
executorService.execute(sourceReader);
for (int i = 0; i < parserCount; i++) {
Expand All @@ -77,49 +77,6 @@ void parse(Reader reader, HostListItemDao hostListItemDao) {
executorService.shutdown();
}

private HostListItem parseHostListItem(String line) {
Matcher matcher = HOSTS_PARSER_PATTERN.matcher(line);
if (!matcher.matches()) {
Log.d(TAG, "Does not match: " + line);
return null;
}
// Check IP address validity or while list entry (if allowed)
String ip = matcher.group(1);
String hostname = matcher.group(2);
// Skip localhost name
if (LOCALHOST_HOSTNAME.equals(hostname)) {
return null;
}
// check if ip is 127.0.0.1 or 0.0.0.0
ListType type;
if (LOCALHOST_IPv4.equals(ip)
|| BOGUS_IPv4.equals(ip)
|| LOCALHOST_IPv6.equals(ip)) {
type = BLOCKED;
} else if (this.parseRedirectedHosts) {
type = REDIRECTED;
} else {
return null;
}
HostListItem item = new HostListItem();
item.setType(type);
item.setHost(hostname);
item.setEnabled(true);
if (type == REDIRECTED) {
item.setRedirection(ip);
}
item.setSourceId(this.sourceId);
return item;
}

private boolean isRedirectionValid(HostListItem item) {
return item.getType() != REDIRECTED || RegexUtils.isValidIP(item.getRedirection());
}

private boolean isHostValid(HostListItem item) {
return RegexUtils.isValidWildcardHostname(item.getHost());
}

private static class SourceReader implements Runnable {
private final Reader reader;
private final BlockingQueue<String> queue;
Expand All @@ -144,32 +101,32 @@ public void run() {
}

private class HostListItemParser implements Runnable {
private final BlockingQueue<String> hostsLineQueue;
private final BlockingQueue<HostListItem> hostListItemQueue;
private final BlockingQueue<String> lineQueue;
private final BlockingQueue<HostListItem> itemQueue;

private HostListItemParser(BlockingQueue<String> lineQueue, BlockingQueue<HostListItem> itemQueue) {
this.hostsLineQueue = lineQueue;
this.hostListItemQueue = itemQueue;
this.lineQueue = lineQueue;
this.itemQueue = itemQueue;
}

@Override
public void run() {
boolean endOfSource = false;
while (!endOfSource) {
try {
String line = this.hostsLineQueue.take();
String line = this.lineQueue.take();
// Check end of queue marker
//noinspection StringEquality
if (line == END_OF_QUEUE_MARKER) {
endOfSource = true;
// Send end of queue marker to inserter
HostListItem endItem = new HostListItem();
endItem.setHost(line);
this.hostListItemQueue.add(endItem);
this.itemQueue.add(endItem);
} else {
HostListItem item = parseHostListItem(line);
if (item != null && isRedirectionValid(item) && isHostValid(item)) {
this.hostListItemQueue.add(item);
this.itemQueue.add(item);
}
}
} catch (InterruptedException e) {
Expand All @@ -179,15 +136,58 @@ public void run() {
}
}
}

private HostListItem parseHostListItem(String line) {
Matcher matcher = HOSTS_PARSER_PATTERN.matcher(line);
if (!matcher.matches()) {
Log.d(TAG, "Does not match: " + line);
return null;
}
// Check IP address validity or while list entry (if allowed)
String ip = matcher.group(1);
String hostname = matcher.group(2);
// Skip localhost name
if (LOCALHOST_HOSTNAME.equals(hostname)) {
return null;
}
// check if ip is 127.0.0.1 or 0.0.0.0
ListType type;
if (LOCALHOST_IPv4.equals(ip)
|| BOGUS_IPv4.equals(ip)
|| LOCALHOST_IPv6.equals(ip)) {
type = BLOCKED;
} else if (SourceLoader.this.parseRedirectedHosts) {
type = REDIRECTED;
} else {
return null;
}
HostListItem item = new HostListItem();
item.setType(type);
item.setHost(hostname);
item.setEnabled(true);
if (type == REDIRECTED) {
item.setRedirection(ip);
}
item.setSourceId(SourceLoader.this.sourceId);
return item;
}

private boolean isRedirectionValid(HostListItem item) {
return item.getType() != REDIRECTED || RegexUtils.isValidIP(item.getRedirection());
}

private boolean isHostValid(HostListItem item) {
return RegexUtils.isValidWildcardHostname(item.getHost());
}
}

private static class ItemInserter implements Callable<Integer> {
private final BlockingQueue<HostListItem> hostListItemQueue;
private final HostListItemDao hostListItemDao;
private final int parserCount;

private ItemInserter(BlockingQueue<HostListItem> ItemQueue, HostListItemDao hostListItemDao, int parserCount) {
this.hostListItemQueue = ItemQueue;
private ItemInserter(BlockingQueue<HostListItem> itemQueue, HostListItemDao hostListItemDao, int parserCount) {
this.hostListItemQueue = itemQueue;
this.hostListItemDao = hostListItemDao;
this.parserCount = parserCount;
}
Expand Down

0 comments on commit e428577

Please sign in to comment.