Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
0abb317c
Commit
0abb317c
authored
Jan 18, 2020
by
jiahua.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
HTTP API
parent
ccf85456
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
11 deletions
+67
-11
mirai-api-http/src/main/kotlin/net.mamoe.mirai.api.http/MiraiHttpApplication.kt
...n/kotlin/net.mamoe.mirai.api.http/MiraiHttpApplication.kt
+39
-1
mirai-api-http/src/main/kotlin/net.mamoe.mirai.api.http/Session.kt
...-http/src/main/kotlin/net.mamoe.mirai.api.http/Session.kt
+28
-10
No files found.
mirai-api-http/src/main/kotlin/net.mamoe.mirai.api.http/MiraiHttpApplication.kt
View file @
0abb317c
...
@@ -18,10 +18,48 @@ import io.ktor.util.pipeline.PipelineContext
...
@@ -18,10 +18,48 @@ import io.ktor.util.pipeline.PipelineContext
import
io.ktor.util.pipeline.PipelineInterceptor
import
io.ktor.util.pipeline.PipelineInterceptor
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.utils.DefaultLogger
import
net.mamoe.mirai.utils.io.hexToBytes
import
net.mamoe.mirai.utils.io.hexToBytes
import
net.mamoe.mirai.utils.io.hexToUBytes
import
net.mamoe.mirai.utils.io.hexToUBytes
fun
main
()
{
fun
main
(
args
:
Array
<
String
>)
{
val
logger
=
DefaultLogger
(
"Mirai HTTP API"
)
//write default first
SessionManager
.
authKey
=
generateSessionKey
()
//用于验证的key, 使用和SessionKey相同的方法生成, 但意义不同
var
port
=
8080
//start port
args
.
forEach
{
if
(
it
.
contains
(
"="
))
{
when
{
it
.
toLowerCase
().
contains
(
"authkey"
)
->
{
SessionManager
.
authKey
=
it
.
split
(
"="
)[
1
].
trim
()
if
(
it
.
length
!
in
8
..
128
){
logger
.
error
(
"Expected authKey length is between 8 to 128"
)
SessionManager
.
authKey
=
generateSessionKey
()
}
logger
.
info
(
"Session Auth Key now is ${SessionManager.authKey}"
)
}
it
.
toLowerCase
().
contains
(
"port"
)
->
{
try
{
port
=
it
.
split
(
"="
)[
1
].
trim
().
toInt
()
}
catch
(
e
:
Exception
){
logger
.
error
(
"Expected -port=xxxxx, xxxxx to be numbers"
)
}
if
(
port
!
in
1025
..
65535
){
logger
.
error
(
"Expected -port=xxxxx, xxxxx > 1024 && <65536"
)
port
=
8080
}
logger
.
info
(
"HTTP API Listening port now is $port"
)
}
}
}
if
(
it
.
contains
(
"help"
)){
logger
.
info
(
"-authkey=XXXXXXXX to use custom Session Auth Key, note that key is case sensitive"
)
logger
.
info
(
"-port=XXXXX to use custom listener port, default using 8080"
)
}
}
Application
(
applicationEngineEnvironment
{}).
apply
{
mirai
()
}
Application
(
applicationEngineEnvironment
{}).
apply
{
mirai
()
}
}
}
...
...
mirai-api-http/src/main/kotlin/net.mamoe.mirai.api.http/Session.kt
View file @
0abb317c
package
net.mamoe.mirai.api.http
package
net.mamoe.mirai.api.http
import
kotlinx.coroutines.
CompletableJob
import
kotlinx.coroutines.
*
import
kotlinx.
coroutines.CoroutineScope
import
kotlinx.
serialization.json.Json
import
kotlinx.
coroutines.SupervisorJob
import
kotlinx.
serialization.json.JsonConfiguration
import
java.lang.StringBuilder
import
java.lang.StringBuilder
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
...
@@ -29,11 +29,25 @@ object SessionManager {
...
@@ -29,11 +29,25 @@ object SessionManager {
val
allSession
:
MutableMap
<
String
,
Session
>
=
mutableMapOf
()
val
allSession
:
MutableMap
<
String
,
Session
>
=
mutableMapOf
()
fun
createTempSession
():
TempSession
=
TempSession
(
EmptyCoroutineContext
).
also
{
allSession
[
it
.
key
]
=
it
}
lateinit
var
authKey
:
String
fun
createTempSession
():
TempSession
=
TempSession
(
EmptyCoroutineContext
).
also
{
newTempSession
->
allSession
[
newTempSession
.
key
]
=
newTempSession
//设置180000ms后检测并回收
newTempSession
.
launch
{
delay
(
180000
)
allSession
[
newTempSession
.
key
]
?.
run
{
if
(
this
is
TempSession
)
closeSession
(
newTempSession
.
key
)
}
}
}
fun
closeSession
(
sessionKey
:
String
)
=
allSession
.
remove
(
sessionKey
)
?.
also
{
it
.
close
()
}
fun
closeSession
(
sessionKey
:
String
)
=
allSession
.
remove
(
sessionKey
)
?.
also
{
it
.
close
()
}
fun
closeSession
(
session
:
Session
)
=
closeSession
(
session
.
key
)
fun
closeSession
(
session
:
Session
)
=
closeSession
(
session
.
key
)
}
}
...
@@ -42,18 +56,22 @@ object SessionManager {
...
@@ -42,18 +56,22 @@ object SessionManager {
/**
/**
* @author NaturalHG
* @author NaturalHG
* 这个用于管理不同Client与Mirai HTTP的会话
* 这个用于管理不同Client与Mirai HTTP的会话
*
* [Session]均为内部操作用类
* 需使用[SessionManager]
*/
*/
abstract
class
Session
internal
constructor
(
abstract
class
Session
internal
constructor
(
coroutineContext
:
CoroutineContext
):
CoroutineScope
{
):
CoroutineScope
{
private
val
sessionJob
=
SupervisorJob
()
val
supervisorJob
=
SupervisorJob
(
coroutineContext
[
Job
])
final
override
val
coroutineContext
:
CoroutineContext
=
supervisorJob
+
coroutineContext
val
key
:
String
=
generateSessionKey
()
val
key
:
String
=
generateSessionKey
()
internal
fun
close
(){
internal
fun
close
(){
s
essionJob
.
cancel
()
s
upervisorJob
.
complete
()
}
}
}
}
...
@@ -63,7 +81,7 @@ abstract class Session internal constructor(
...
@@ -63,7 +81,7 @@ abstract class Session internal constructor(
*
*
* TempSession在建立180s内没有转变为[AuthedSession]应被清除
* TempSession在建立180s内没有转变为[AuthedSession]应被清除
*/
*/
class
TempSession
internal
constructor
(
override
val
coroutineContext
:
CoroutineContext
)
:
Session
(
)
{
class
TempSession
internal
constructor
(
coroutineContext
:
CoroutineContext
)
:
Session
(
coroutineContext
)
{
}
}
...
@@ -71,7 +89,7 @@ class TempSession internal constructor(override val coroutineContext: CoroutineC
...
@@ -71,7 +89,7 @@ class TempSession internal constructor(override val coroutineContext: CoroutineC
* 任何[TempSession]认证后转化为一个[AuthedSession]
* 任何[TempSession]认证后转化为一个[AuthedSession]
* 在这一步[AuthedSession]应该已经有assigned的bot
* 在这一步[AuthedSession]应该已经有assigned的bot
*/
*/
class
AuthedSession
internal
constructor
(
botNumber
:
Int
,
override
val
coroutineContext
:
CoroutineContext
):
Session
(
){
class
AuthedSession
internal
constructor
(
val
botNumber
:
Int
,
coroutineContext
:
CoroutineContext
):
Session
(
coroutineContext
){
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment