Skip to content

Commit

Permalink
Merge branch 'release/v1.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
aston committed Feb 15, 2021
2 parents d68f556 + 387bad6 commit b698d66
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.foperate</groupId>
<artifactId>ros-ipset</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>

<properties>
<kotlin.version>1.4.10</kotlin.version>
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/cn/foperate/ros/IPset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ object IPset {
private var remotePort: Int = 53
private var fallback: String = "223.5.5.5"

private lateinit var adblockPath: String
private var blockAddress: String = "224.0.0.1"

private fun checkFile(path: String): File {
val file = File(path)
if (!file.exists() || !file.isFile) {
Expand All @@ -49,6 +52,8 @@ object IPset {
properties.load(FileInputStream(file))
gfwlistPath = properties.getProperty("gfwlistPath", "gfwlist.txt")
whitelistPath = properties.getProperty("whitelistPath", "")
adblockPath = properties.getProperty("adblockPath", "")
blockAddress = properties.getProperty("blockAddress", blockAddress)
rosUser = properties.getProperty("rosUser")
rosPwd = properties.getProperty("rosPwd")
rosIp = properties.getProperty("rosIp")
Expand All @@ -61,17 +66,17 @@ object IPset {
}

val maxThreadStr = properties.getProperty("maxThread")
if (maxThreadStr.isNotBlank()) {
if (!maxThreadStr.isNullOrBlank()) {
maxThread = Integer.valueOf(maxThreadStr)
}

val localPortStr = properties.getProperty("localPort")
if (localPortStr.isNotBlank()) {
if (!localPortStr.isNullOrBlank()) {
localPort = Integer.valueOf(localPortStr)
}

val remotePortStr = properties.getProperty("remotePort")
if (remotePortStr.isNotBlank()) {
if (!remotePortStr.isNullOrBlank()) {
remotePort = Integer.valueOf(remotePortStr)
}

Expand Down Expand Up @@ -140,6 +145,11 @@ object IPset {
.forEach {
DomainUtil.loadWhiteList(it)
}
adblockPath.split(",")
.filter(String::isNotBlank)
.forEach {
DomainUtil.loadAdblockList(it)
}

logger.info("GFWList load completed")

Expand All @@ -161,7 +171,8 @@ object IPset {
"remotePort" to remotePort,
"remote" to remote,
"localPort" to localPort,
"fallback" to fallback
"fallback" to fallback,
"blockAddress" to blockAddress
)
))

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/cn/foperate/ros/pac/DomainUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object DomainUtil {
private val log = LoggerFactory.getLogger(DomainUtil::class.java)
private val blackList = HashSet<String>()
private val whiteList = HashSet<String>()
private val adblockList = HashSet<String>()

/*init {
blackList.add("google.com")
Expand Down Expand Up @@ -40,6 +41,23 @@ object DomainUtil {
log.info("${records.size} records of black list loaded")
}

fun loadAdblockList(fileName: String) {
if (fileName.isNotBlank()) try {
val file = File(fileName)
log.info("try load adblock list file $fileName")
loadAdblockList(file)
} catch (e:java.lang.RuntimeException) {}
}

private fun loadAdblockList(file:File) {
val reader = file.bufferedReader(Charset.defaultCharset())
val records = reader.lines()
.filter { it.isNotBlank() }
.toList()
adblockList.addAll(records)
log.info("${records.size} records of adblock list loaded")
}

fun match(name: String):Boolean {
val checkName = if (name.endsWith(".")) {
name.substring(0, name.length-1)
Expand Down Expand Up @@ -78,4 +96,16 @@ object DomainUtil {
hour*3600 + min*60 + sec
} ?: 24*3600
}

fun matchBlock(name: String): Boolean {
val checkName = if (name.endsWith(".")) {
name.substring(0, name.length-1)
} else name
parse(checkName).forEach {
if (adblockList.contains(it)) {
return true
}
}
return false
}
}
50 changes: 37 additions & 13 deletions src/main/java/cn/foperate/ros/verticle/DnsVeticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@ import io.smallrye.mutiny.Uni
import io.smallrye.mutiny.vertx.core.AbstractVerticle
import io.vertx.kotlin.core.datagram.datagramSocketOptionsOf
import io.vertx.kotlin.core.json.jsonObjectOf
import io.vertx.mutiny.core.buffer.Buffer
import io.vertx.mutiny.core.datagram.DatagramPacket
import io.vertx.mutiny.core.datagram.DatagramSocket
import io.vertx.mutiny.core.eventbus.EventBus
import org.slf4j.LoggerFactory
import org.xbill.DNS.ARecord
import org.xbill.DNS.Message
import org.xbill.DNS.Section
import org.xbill.DNS.Type
import org.xbill.DNS.*
import java.net.InetAddress
import kotlin.streams.toList


class DnsVeticle: AbstractVerticle() {

private var localPort: Int = 53 // DNS服务监听的端口
private lateinit var remote: String // upstream服务器地址
private var remotePort: Int = 53 // upstream服务器端口
private lateinit var fallback: String
private lateinit var blockAddress: InetAddress

private lateinit var serverSocket:DatagramSocket // 正在监听的服务端口
private lateinit var eb: EventBus
Expand All @@ -30,23 +31,32 @@ class DnsVeticle: AbstractVerticle() {
remote = config().getString("remote")
remotePort = config().getInteger("remotePort", remotePort)
fallback = config().getString("fallback")
val block = config().getString("blockAddress")
blockAddress = InetAddress.getByName(block)

this.eb = vertx.eventBus()
val serverSocket = vertx.createDatagramSocket(datagramSocketOptionsOf())
serverSocket.listen(localPort, "0.0.0.0")
.subscribe().with {
this.serverSocket = it
it.toMulti().subscribe().with { request ->
.subscribe().with { ss ->
this.serverSocket = ss
ss.toMulti().subscribe().with { request ->
val message = Message(request.data().bytes)
val questionName = message.question.name.toString()
val questionType = message.question.type
log.debug(questionName)
if (DomainUtil.match(questionName)){
log.debug("gfwlist hint")
forwardToRemote(request)
} else {
forwardToFallback(request)
if (questionType==Type.A) when {
DomainUtil.match(questionName) -> {
log.debug("gfwlist hint")
forwardToRemote(request)
}
DomainUtil.matchBlock(questionName) -> {
log.debug("adBlock matched")
val reply = blockMessage(message)
ss.send(Buffer.buffer(reply), request.sender().port(), request.sender().host())
.subscribe().with { }
}
else -> forwardToFallback(request)
}

}
}

Expand Down Expand Up @@ -127,6 +137,20 @@ class DnsVeticle: AbstractVerticle() {
}
}

/***
* Make the query result to blockAddress and expired in 1 day.
*/
fun blockMessage(request:Message):ByteArray {
val response = Message(request.header.id)
response.header.setFlag(Flags.QR.toInt())
response.addRecord(request.question, Section.QUESTION)
val questionName = request.question.name

response.addRecord(ARecord(questionName, DClass.IN, 86400, blockAddress), Section.ANSWER)

return response.toWire()
}

companion object {
private val log = LoggerFactory.getLogger(DnsVeticle::class.java)
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/cn/foperate/ros/verticle/RosVerticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ class RosVerticle : AbstractVerticle() {
.collect().asList()
.onItem().transformToUni { list ->
log.info("loaded ${list.size} records from ros-firewall")
log.info(cache.getIfPresent("123.118.97.191").toString())
val day = System.currentTimeMillis() + 24*3600*1000
log.info(day.toString())
Uni.createFrom().voidItem()
}
}
Expand Down

0 comments on commit b698d66

Please sign in to comment.