Skip to content

Commit

Permalink
Merge branch 'release/v1.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
aston committed Feb 20, 2021
2 parents a6e71e4 + 65de890 commit 0344c61
Show file tree
Hide file tree
Showing 21 changed files with 881 additions and 204 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# User-specific stuff:
.idea/
docker/
run/
*.iml
find.xml
ignore.xml
Expand Down
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
It is a tool original by actck. It is useful for me or any linux/ipset person using RouterOS.
I fork it to add some other minor functions, cause it was unactived for 2 years.

But... I actually refacted the whole project. So, enjoy.
But... I actually refacted the whole project. And, i implement my own ros api for async request.
So, enjoy.

Also I will do a docker job, and put it on github.

Expand Down Expand Up @@ -43,17 +44,16 @@ Config file name must be "jrodns.properties".

|key |require|default|desc|
|:---| :---:| :---: |:---|
|gfwlistPath|1| |gfwlist files path, seperated by comma. Value could be file name or absolute path
|whitelistPath|0| |white list files, exception from gfwlist.
|adblockPath|0| |ad-block list files. domains will be resolve to block-address
|blockAddress|0|224.0.0.1|the address for domains in ad-block list
|rosIp|1| | ros server ip
|rosUser|1| | ros router login username
|rosPwd|1| | ros router login password
|rosFwadrKey|1| | address-list key to set in ros
|rosIdle|0|30| ros api-connection check delay
|localPort|0|53|local port for client dns query request
|remote|1| |remote dns server for dns iterator request
|remotePort|1|53| remote dns server port for dns iterator request
|maxThread|0|10|server worker count
|gfwlistPath|Yes| |gfwlist files path, seperated by comma. Value could be file name or absolute path
|whitelistPath|No| |white list files, exception from gfwlist.
|adblockPath|No| |ad-block list files. domains will be resolve to block-address
|blockAddress|No|224.0.0.1|the address for domains in ad-block list
|rosIp|Yes| | ros server ip
|rosUser|Yes| | ros router login username
|rosPwd|Yes| | ros router login password
|rosFwadrKey|Yes| | address-list key to set in ros
|localPort|No|53|local port for client dns query request
|remote|Yes| |remote dns server for dns iterator request
|remotePort|Yes|53| remote dns server port for dns iterator request
|fallback|No|223.5.5.5|fallback dns server to use

12 changes: 12 additions & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ ROS本身没有ipset的功能,这可能带给习惯于使用ipset进行地址
Ros-IPSet以DNS服务的形式工作,并将DNS请求中符合blocklist列表的ipv4地址放入到ROS的特定address-list中。
这样在ROS中就可以使用这个地址列表进行分流、标记等常见的操作。

具体处理逻辑是:将DNS解析请求转发到romote服务器上;如果请求解析的域名在blocklist中,将DNS解析返回的ip地址
加到ROS对应的address-list列表中;但whitelist列表中的域名不会被加入。
例如:可以将google相关域名放在blocklist里,同时将中国地区可以直连的google网站放到whitelist里面,这样就
只有中国区不能直接访问的google服务会加入到address-list中。

如果remote服务器无法正常访问,Ros-IPSet会试着从后备DNS服务器获得地址解析请求,但其结果不会放到
address-list里。

address-list所加入的记录,有效期均设置为24小时,到期自动被清除。
由于address-list中仅包括访问时用到的域名所对应的ip地址,长度有限,对于ROS的工作效率影响也非常小。

如果手工清除了address-list中的记录,需要同时重启Ros-IPSet服务,确保Ros-IPSet中的缓存记录与ROS内的
记录数据同步。

所有选项在jrodns.properties中间设定,相当简单,在中文说明中不再另行解释。

## AdBlock
Expand Down
2 changes: 0 additions & 2 deletions jrodns.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ rosIp=192.168.88.1
rosUser=jdns
rosPwd=12345678
rosFwadrKey=jrodns
rosIdle=30
localPort=53
remote=114.114.114.114
remotePort=53
maxThread=10
fallback=
24 changes: 12 additions & 12 deletions 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.3</version>
<version>1.1.0</version>

<properties>
<kotlin.version>1.4.10</kotlin.version>
Expand All @@ -30,29 +30,24 @@
<version>3.3.1</version>
</dependency>

<dependency>
<groupId>me.legrange</groupId>
<artifactId>mikrotik</artifactId>
<version>3.0.7</version>
</dependency>

<!-- logback -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1-jre</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
Expand All @@ -75,6 +70,11 @@
<artifactId>vertx-lang-kotlin</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-client</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
41 changes: 6 additions & 35 deletions src/main/java/cn/foperate/ros/IPset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ object IPset {
private lateinit var rosPwd: String
private lateinit var rosIp: String
private lateinit var rosFwadrKey: String
private var rosIdle: Int? = null
private var maxThread = 8
private var localPort: Int = 53
private lateinit var remote: String
private var remotePort: Int = 53
Expand Down Expand Up @@ -65,11 +63,6 @@ object IPset {
fallback = fb
}

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

val localPortStr = properties.getProperty("localPort")
if (!localPortStr.isNullOrBlank()) {
localPort = Integer.valueOf(localPortStr)
Expand All @@ -79,28 +72,6 @@ object IPset {
if (!remotePortStr.isNullOrBlank()) {
remotePort = Integer.valueOf(remotePortStr)
}

val rosIdleStr = properties.getProperty("rosIdle")
rosIdle = if (rosIdleStr.isNotBlank()) {
Integer.valueOf(rosIdleStr)
} else {
30
}
if (listOf(
gfwlistPath,
rosIp,
rosUser,
rosPwd,
rosFwadrKey,
rosIdle,
maxThread,
localPort,
remote,
remotePort
).contains(null)
) {
throw RuntimeException("config error")
}
}

private fun setLogger() {
Expand All @@ -122,6 +93,7 @@ object IPset {

@JvmStatic
fun main(args:Array<String>) {

setLogger()

var configFilePath = "jrodns.properties"
Expand Down Expand Up @@ -153,28 +125,27 @@ object IPset {

logger.info("GFWList load completed")

val vertx = Vertx.vertx(VertxOptions().setWorkerPoolSize(maxThread))
val vertx = Vertx.vertx(VertxOptions())
vertx.deployVerticle(RosVerticle(), deploymentOptionsOf(
config = jsonObjectOf(
"rosFwadrKey" to rosFwadrKey,
"rosIp" to rosIp,
"rosUser" to rosUser,
"rosPwd" to rosPwd,
"maxThread" to maxThread
), worker = true
"rosPwd" to rosPwd
)
)).onFailure().invoke { e ->
logger.error(e.message)
vertx.close().subscribeAsCompletionStage()
}.subscribe().with { }
vertx.deployVerticleAndAwait(DnsVeticle(), deploymentOptionsOf(
vertx.deployVerticle(DnsVeticle(), deploymentOptionsOf(
config = jsonObjectOf(
"remotePort" to remotePort,
"remote" to remote,
"localPort" to localPort,
"fallback" to fallback,
"blockAddress" to blockAddress
)
))
)).subscribe().with { }

logger.info("server started")
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/cn/foperate/ros/api/ApiConnectionOptions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.foperate.ros.api

data class ApiConnectionOptions(
val username: String,
val password: String,
val host: String,
val port: Int = RxApiConnection.DEFAULT_PORT
)

fun apiConnectionOptionsOf(username: String,
password: String,
host: String)
= ApiConnectionOptions(username, password, host)
35 changes: 35 additions & 0 deletions src/main/java/cn/foperate/ros/api/ApiException.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cn.foperate.ros.api

import java.lang.Exception


/**
* Thrown by the Mikrotik API to indicate errors
*
* @author Aston Mei
*/
open class MikrotikApiException(msg: String? = null, err: Throwable? = null) : Exception(msg, err)


/**
* Exception thrown if the Api experiences a connection problem
*/
open class ApiConnectionException : MikrotikApiException {
constructor(msg: String?) : super(msg) {}
constructor(msg: String?, err: Throwable?) : super(msg, err) {}
}

class ApiTimeoutException(message: String):ApiConnectionException(message)

/**
* Thrown if there is a problem unpacking data from the Api.
* @author GideonLeGrange
*/
class ApiDataException : MikrotikApiException {
internal constructor(msg: String?) : super(msg) {}
internal constructor(msg: String?, err: Throwable) : super(msg, err) {}
}

class ApiCommandException(map: Map<String, String>) : MikrotikApiException(map["message"]) {
var category = map["category"]?.toInt() ?: 0
}
39 changes: 39 additions & 0 deletions src/main/java/cn/foperate/ros/api/AsyncClient.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cn.foperate.ros.api

import cn.foperate.ros.munity.AsyncSocketFactory
import io.vertx.kotlin.core.net.netClientOptionsOf
import io.vertx.mutiny.core.Vertx
import org.slf4j.LoggerFactory

object AsyncClient
{
private val log = LoggerFactory.getLogger(AsyncClient::class.java)
@JvmStatic
fun main(args:Array<String>) {
log.debug("异步客户端测试开始")

val vertx = Vertx.vertx()
val sf = AsyncSocketFactory(vertx, netClientOptionsOf(
connectTimeout = RxApiConnection.DEFAULT_CONNECTION_TIMEOUT
))
RxApiConnection.connection(sf, apiConnectionOptionsOf(
host = "192.168.28.1",
username = "jdns",
password = "Ocwesw2r3"
)).subscribe().with ({ conn ->
log.info("Login successed")
val command = Command("/ip/firewall/address-list/print", queries= mapOf(
"list" to "PROXY"
), props = listOf("address","timeout")) //,q where return address,timeout"
conn.executeAsMulti(command)
.onCompletion().invoke {
conn.close()
}
.subscribe().with {
log.debug(it.toString())
}
}){
log.error(it.toString())
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/cn/foperate/ros/api/Command.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cn.foperate.ros.api

/**
* A command sent to a Mikrotik. This internal class is used to build complex commands
* with parameters, queries and property lists.
*
* @author GideonLeGrange
*/
class Command(val cmd: String,
val params: Map<String, String?> = mapOf(),
val queries: Map<String, String> = mapOf(),
val props: List<String> = listOf()
) {
var tag = ""
override fun toString(): String {
return "cmd[$tag] = $cmd, params = $params, queries = $queries"
}

fun toSentence():List<String> {
val list = mutableListOf<String>()
list.add(cmd)
params.forEach { (k, v) ->
list.add("=$k=$v")
}
if (tag.isNotBlank()) {
list.add(".tag=$tag")
}
if (props.isNotEmpty()) {
val propList = props.joinToString(",")
list.add("=.proplist=$propList")
}
queries.forEach { (k, v) ->
list.add("?$k=$v")
}
return list
}
}
Loading

0 comments on commit 0344c61

Please sign in to comment.