Commit 27bf7104 authored by Iain Learmonth's avatar Iain Learmonth
Browse files

Allow for geoip files to be detected by filename.

Fixes #40005.
parent f071960f
Pipeline #7541 failed with stage
in 2 minutes and 25 seconds
......@@ -159,6 +159,13 @@ public class DescriptorParserImpl implements DescriptorParser {
&& fileName.endsWith(".onionperf.analysis.json.xz")) {
return new OnionPerfAnalysisConverter(rawDescriptorBytes, sourceFile)
.asTorperfResults();
} else if (null != fileName
&& (fileName.equals("geoip") || fileName.equals("geoip6"))) {
return GeoipFileImpl.parse(rawDescriptorBytes, sourceFile);
} else if (null != fileName
&& (fileName.equals("countries.txt")
|| fileName.equals("asn.txt"))) {
return GeoipNamesFileImpl.parse(rawDescriptorBytes, sourceFile);
} else {
throw new DescriptorParseException("Could not detect descriptor "
+ "type in descriptor starting with '" + firstLines + "'.");
......
package org.torproject.descriptor.impl;
import org.torproject.descriptor.Descriptor;
import org.torproject.descriptor.DescriptorParseException;
import org.torproject.descriptor.GeoipFile;
......@@ -123,4 +124,15 @@ public class GeoipFileImpl extends DescriptorImpl implements GeoipFile {
}
return Optional.empty();
}
/**
* Parse a GeoIP file.
*/
public static List<Descriptor> parse(byte[] rawDescriptorBytes,
File sourceFile) throws DescriptorParseException {
List<Descriptor> result = new ArrayList<>();
result.add(new GeoipFileImpl(rawDescriptorBytes,
new int[]{0, rawDescriptorBytes.length}, sourceFile));
return result;
}
}
package org.torproject.descriptor.impl;
import org.torproject.descriptor.Descriptor;
import org.torproject.descriptor.DescriptorParseException;
import org.torproject.descriptor.GeoipNamesFile;
......@@ -7,6 +8,7 @@ import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
......@@ -24,6 +26,17 @@ public class GeoipNamesFileImpl extends DescriptorImpl
this.splitAndParseEntries();
}
/**
* Parse a GeoIP file.
*/
public static List<Descriptor> parse(byte[] rawDescriptorBytes,
File sourceFile) throws DescriptorParseException {
List<Descriptor> result = new ArrayList<>();
result.add(new GeoipNamesFileImpl(rawDescriptorBytes,
new int[]{0, rawDescriptorBytes.length}, sourceFile));
return result;
}
private void splitAndParseEntries() throws DescriptorParseException {
Scanner scanner = this.newScanner().useDelimiter(EOL);
while (scanner.hasNext()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment