-
Notifications
You must be signed in to change notification settings - Fork 0
/
V2VoteSender.kt
71 lines (60 loc) · 2.35 KB
/
V2VoteSender.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package io.dotrun.mcvotifierlib
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReader
import java.net.InetSocketAddress
import java.net.Socket
import java.security.Key
import java.util.Base64
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
data class V2VoteSender(
override val address: InetSocketAddress,
val key: Key
) : VoteSender(address) {
companion object {
const val MAGIC = 0x733A
}
data class V2Message(val signature: String, val payload: String)
data class V2Response(val status: String, val cause: String?, val error: String?)
constructor(address: InetSocketAddress, token: String) : this(
address,
SecretKeySpec(token.toByteArray(), "HmacSHA256")
)
private val mapper = jacksonObjectMapper()
private val mac = Mac.getInstance("HmacSHA256").also { it.init(key) }
override fun sendVote(vote: Vote) {
Socket(address.address, address.port).use { socket ->
val reader = BufferedReader(InputStreamReader(socket.getInputStream()))
val out = DataOutputStream(socket.getOutputStream())
// read challenge
val challenge = reader.readLine().split(' ').let {
check(it.size == 3) { "Greeting does not include challenge, not a v2 server!" }
it[2]
}
// construct and send vote
val payload: String = mapper.valueToTree<ObjectNode>(vote).let {
it.put("challenge", challenge)
it.toString()
}
val signature = Base64.getEncoder().encodeToString(mac.doFinal(payload.toByteArray()))
val message = mapper.writeValueAsBytes(
V2Message(
signature,
payload
)
)
out.writeShort(MAGIC)
out.writeShort(message.size)
out.write(message)
// read response
val response = mapper.readValue<V2Response>(reader.readText())
check(response.status == "ok") { "Vote failed! '${response.cause}: ${response.error}'" }
reader.close()
out.close()
}
}
}