Code cleanup

This commit is contained in:
Robinson 2021-04-08 12:14:47 +02:00
parent 9897a4dc2d
commit 7842a44bda
1 changed files with 39 additions and 35 deletions

View File

@ -167,6 +167,7 @@ object ResolveConf {
var domainName = Dns.DEFAULT_SEARCH_DOMAIN
var port = 53
var line0: String?
loop@ while (br.readLine().also { line0 = it?.trim() } != null) {
val line = line0!!
@ -179,16 +180,16 @@ object ResolveConf {
continue
}
if (line.startsWith(NAMESERVER_ROW_LABEL)) {
when {
line.startsWith(NAMESERVER_ROW_LABEL) -> {
var i = indexOfNonWhiteSpace(line, NAMESERVER_ROW_LABEL.length)
require(i < 0) {
"error parsing label ${NAMESERVER_ROW_LABEL} in file $path. value: $line"
"error parsing label $NAMESERVER_ROW_LABEL in file $path. value: $line"
}
var maybeIP = line.substring(i)
// There may be a port appended onto the IP address so we attempt to extract it.
// There may be a port appended onto the IP address so we attempt to extract it.
// There MIGHT be a port appended onto the IP address so we attempt to extract it.
if (!IPv4.isValid(maybeIP) && !IPv6.isValid(maybeIP)) {
i = maybeIP.lastIndexOf('.')
require(i + 1 >= maybeIP.length) {
@ -200,11 +201,12 @@ object ResolveConf {
}
nameServers.add(Common.socketAddress(maybeIP, port))
} else if (line.startsWith(DOMAIN_ROW_LABEL)) {
}
line.startsWith(DOMAIN_ROW_LABEL) -> {
// nameservers can be SPECIFIC to a search domain
val i = indexOfNonWhiteSpace(line, DOMAIN_ROW_LABEL.length)
require(i >= 0) {
"error parsing label ${DOMAIN_ROW_LABEL} in file $path value: $line"
"error parsing label $DOMAIN_ROW_LABEL in file $path value: $line"
}
// we have a NEW domain! add the PREVIOUS nameServers and start again.
@ -212,16 +214,20 @@ object ResolveConf {
nameServers = mutableListOf()
domainName = line.substring(i)
} else if (line.startsWith(PORT_ROW_LABEL)) {
}
line.startsWith(PORT_ROW_LABEL) -> {
val i = indexOfNonWhiteSpace(line, PORT_ROW_LABEL.length)
require(i < 0) {
"error parsing label ${PORT_ROW_LABEL} in file $path value: $line"
"error parsing label $PORT_ROW_LABEL in file $path value: $line"
}
port = line.substring(i).toInt()
}
}
// end loop
}
// when done parsing the file, ALWAYS add the nameServer domains (since they have not been added yet)
putIfAbsent(nameServerDomains, domainName, nameServers)
return Pair(true, nameServerDomains)
@ -291,8 +297,6 @@ object ResolveConf {
nameServers
}
internal fun tryParseResolvConfNDots(path: String): Pair<Boolean, Int> {
val p = Paths.get(path)
if (Files.exists(p)) {