Skip to content

Commit 47e28dc

Browse files
authored
Merge pull request #38 from Samarium150/develop
feat: add custom proxy settings; fix #37
2 parents 56a6824 + 5e837eb commit 47e28dc

File tree

7 files changed

+109
-9
lines changed

7 files changed

+109
-9
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# [3.3.0](https://github.com/Samarium150/mirai-console-lolicon/compare/3.2.1...3.3.0) (2021-04-26)
2+
3+
4+
### feat
5+
6+
* add custom proxy settings; fix #37 ([a7a6ee5](https://github.com/Samarium150/mirai-console-lolicon/commit/a7a6ee5484f1d01bcbb12d4ecd0fb0bb4ccf3ea5)), closes [#37](https://github.com/Samarium150/mirai-console-lolicon/issues/37)
7+
8+
9+
110
## [3.2.1](https://github.com/Samarium150/mirai-console-lolicon/compare/3.2.0...3.2.1) (2021-04-17)
211

312

build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ plugins {
55
kotlin("jvm") version kotlinVersion
66
kotlin("plugin.serialization") version kotlinVersion
77

8-
id("net.mamoe.mirai-console") version "2.6.1"
9-
id("org.jetbrains.dokka") version "1.4.0"
8+
id("net.mamoe.mirai-console") version "2.6.2"
9+
id("org.jetbrains.dokka") version "1.4.32"
1010
}
1111

1212
group = "com.github.samarium150"
13-
version = "3.2.1"
13+
version = "3.3.0"
1414

1515
repositories {
1616
mavenLocal()

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mirai-console-lolicon",
3-
"version": "3.2.1",
3+
"version": "3.3.0",
44
"description": "基于mirai-console的涩图机器人",
55
"scripts": {
66
"changelog": "conventional-changelog -p eslint -i CHANGELOG.md -s"

src/main/kotlin/Main.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
3737
object Main: KotlinPlugin(
3838
JvmPluginDescription(
3939
id = "com.github.samarium150.mirai-console-lolicon",
40-
version = "3.2.1",
40+
version = "3.3.0",
4141
name = "mirai-console-lolicon"
4242
)
4343
) {
@@ -56,6 +56,7 @@ object Main: KotlinPlugin(
5656
PluginConfig.reload()
5757
CommandConfig.reload()
5858
ReplyConfig.reload()
59+
ProxyConfig.reload()
5960
PluginData.reload()
6061

6162
if (PluginConfig.master != 0L) {

src/main/kotlin/ProxyConfig.kt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.samarium150.mirai.plugin
2+
3+
import net.mamoe.mirai.console.data.AutoSavePluginConfig
4+
import net.mamoe.mirai.console.data.ValueDescription
5+
import net.mamoe.mirai.console.data.value
6+
7+
/**
8+
* Proxy config
9+
* <br>
10+
* 自定义代理
11+
*
12+
* @constructor Create empty Proxy config
13+
* @see net.mamoe.mirai.console.data.AutoSavePluginConfig
14+
*/
15+
object ProxyConfig: AutoSavePluginConfig("ProxyConfig") {
16+
17+
/**
18+
* Type
19+
* <br>
20+
* 代理类型
21+
*/
22+
@ValueDescription("可选:DIRECT/HTTP/SOCKS")
23+
val type: String by value("DIRECT")
24+
25+
/**
26+
* Hostname
27+
* <br>
28+
* IP地址
29+
*/
30+
@ValueDescription("地址")
31+
val hostname: String by value("localhost")
32+
33+
/**
34+
* Port
35+
* <br>
36+
* 端口
37+
*/
38+
@ValueDescription("端口")
39+
val port: Int by value(1080)
40+
}

src/main/kotlin/RequestHandler.kt

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
*/
1717
package com.github.samarium150.mirai.plugin
1818

19-
import com.github.kittinunf.fuel.Fuel
2019
import com.github.kittinunf.fuel.core.FuelError
20+
import com.github.kittinunf.fuel.core.FuelManager
2121
import com.github.kittinunf.result.Result
2222
import com.google.gson.Gson
2323
import com.google.gson.JsonSyntaxException
2424
import java.io.*
25+
import java.lang.IllegalArgumentException
26+
import java.net.InetSocketAddress
27+
import java.net.Proxy
2528

2629
/**
2730
* Object for handling GET request
@@ -55,7 +58,17 @@ object RequestHandler {
5558
@Throws(FuelError::class, JsonSyntaxException::class, APIException::class)
5659
fun get(parameters: RequestParams): Response {
5760
val url = "https://api.lolicon.app/setu/?$parameters"
58-
val (_, response, result) = Fuel.get(url).responseString()
61+
val (_, response, result) = FuelManager()
62+
.also {
63+
try {
64+
it.proxy = Proxy(
65+
Utils.getProxyType(ProxyConfig.type),
66+
InetSocketAddress(ProxyConfig.hostname, ProxyConfig.port)
67+
)
68+
} catch (iae: IllegalArgumentException) { }
69+
}
70+
.get(url)
71+
.responseString()
5972
if (result is Result.Failure) throw result.getException()
6073
val feedback: Response = gson.fromJson(String(response.data), Response::class.java)
6174
if (feedback.code != 0) throw APIException(feedback.code, feedback.msg)
@@ -76,7 +89,15 @@ object RequestHandler {
7689
fun download(url: String): InputStream {
7790
if (PluginConfig.save) {
7891
var file: File? = null
79-
val (_, _, result) = Fuel
92+
val (_, _, result) = FuelManager()
93+
.also {
94+
try {
95+
it.proxy = Proxy(
96+
Utils.getProxyType(ProxyConfig.type),
97+
InetSocketAddress(ProxyConfig.hostname, ProxyConfig.port)
98+
)
99+
} catch (iae: IllegalArgumentException) { }
100+
}
80101
.download(url)
81102
.fileDestination { _, _ ->
82103
val urlPaths = url.split("/")
@@ -89,7 +110,15 @@ object RequestHandler {
89110
return ByteArrayInputStream(file!!.readBytes())
90111
}
91112
var outputStream = OutputStream.nullOutputStream()
92-
val (_, _, result) = Fuel
113+
val (_, _, result) = FuelManager()
114+
.also {
115+
try {
116+
it.proxy = Proxy(
117+
Utils.getProxyType(ProxyConfig.type),
118+
InetSocketAddress(ProxyConfig.hostname, ProxyConfig.port)
119+
)
120+
} catch (iae: IllegalArgumentException) { }
121+
}
93122
.download(url)
94123
.streamDestination { response: com.github.kittinunf.fuel.core.Response, _ ->
95124
outputStream = ByteArrayOutputStream(response.contentLength.toInt())

src/main/kotlin/Utils.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import net.mamoe.mirai.contact.Member
2121
import net.mamoe.mirai.contact.MemberPermission
2222
import net.mamoe.mirai.contact.User
2323
import org.jetbrains.annotations.Nullable
24+
import java.net.Proxy
2425

2526
/**
2627
* Object for utility functions
@@ -89,4 +90,24 @@ object Utils {
8990
}
9091
return setting
9192
}
93+
94+
/**
95+
* Get proxy type from the given string
96+
* <br>
97+
* 根据输入值返回代理类型
98+
*
99+
* @param value input string value <br> 输入的字符串
100+
* @return [Proxy.Type] <br> 代理的类型
101+
* @throws IllegalArgumentException if [value] is not [Proxy.Type] <br> 数值非法时抛出
102+
* @see Proxy
103+
*/
104+
@Throws(IllegalArgumentException::class)
105+
fun getProxyType(value: String): Proxy.Type {
106+
return when (value) {
107+
"DIRECT" -> Proxy.Type.DIRECT
108+
"HTTP" -> Proxy.Type.HTTP
109+
"SOCKS" -> Proxy.Type.SOCKS
110+
else -> throw IllegalArgumentException(value)
111+
}
112+
}
92113
}

0 commit comments

Comments
 (0)