|
| 1 | +package org.codeoverflow.chatoverflow.requirement.service.rcon |
| 2 | + |
| 3 | +import java.io.{InputStream, OutputStream} |
| 4 | +import java.net.Socket |
| 5 | +import java.nio.{ByteBuffer, ByteOrder} |
| 6 | +import java.util.Random |
| 7 | + |
| 8 | +import org.codeoverflow.chatoverflow.WithLogger |
| 9 | +import org.codeoverflow.chatoverflow.connector.Connector |
| 10 | + |
| 11 | +class RconConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger { |
| 12 | + override protected var requiredCredentialKeys: List[String] = List("password", "address") |
| 13 | + override protected var optionalCredentialKeys: List[String] = List("port") |
| 14 | + |
| 15 | + private var socket: Socket = _ |
| 16 | + private var outputStream: OutputStream = _ |
| 17 | + private var inputStream: InputStream = _ |
| 18 | + private var requestId: Int = 0 |
| 19 | + |
| 20 | + def sendCommand(command: String): String = { |
| 21 | + logger debug s"Sending $command to RCON" |
| 22 | + requestId += 1 |
| 23 | + write(2, command.getBytes("ASCII")) |
| 24 | + "" |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + /** |
| 29 | + * Starts the connector, e.g. creates a connection with its platform. |
| 30 | + */ |
| 31 | + override def start(): Boolean = { |
| 32 | + logger info s"Starting rcon connection to ${credentials.get.getValue("address").get}" |
| 33 | + var port: Int = 25575 |
| 34 | + if (credentials.get.exists("port")) { |
| 35 | + port = credentials.get.getValue("port").get.toInt |
| 36 | + if (port < 1 || port > 65535) { |
| 37 | + return false |
| 38 | + } |
| 39 | + } |
| 40 | + socket = new Socket(credentials.get.getValue("address").get, port) |
| 41 | + outputStream = socket.getOutputStream |
| 42 | + inputStream = socket.getInputStream |
| 43 | + login() |
| 44 | + true |
| 45 | + } |
| 46 | + |
| 47 | + private def login(): Unit = { |
| 48 | + requestId = new Random().nextInt(Integer.MAX_VALUE) |
| 49 | + logger info "Logging RCON in..." |
| 50 | + val password = credentials.get.getValue("password").get |
| 51 | + write(3, password.getBytes("ASCII")) |
| 52 | + logger debug "RCON Login sent" |
| 53 | + } |
| 54 | + |
| 55 | + private def write(packageType: Int, payload: Array[Byte]): Boolean = { |
| 56 | + val length = 4 + 4 + payload.length + 1 + 1 |
| 57 | + var byteBuffer: ByteBuffer = ByteBuffer.allocate(length + 4) |
| 58 | + byteBuffer.order(ByteOrder.LITTLE_ENDIAN) |
| 59 | + |
| 60 | + byteBuffer.putInt(length) |
| 61 | + byteBuffer.putInt(requestId) |
| 62 | + byteBuffer.putInt(packageType) |
| 63 | + byteBuffer.put(payload) |
| 64 | + byteBuffer.put(0x00.toByte) |
| 65 | + byteBuffer.put(0x00.toByte) |
| 66 | + |
| 67 | + outputStream.write(byteBuffer.array()) |
| 68 | + outputStream.flush() |
| 69 | + true |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * This stops the activity of the connector, e.g. by closing the platform connection. |
| 74 | + */ |
| 75 | + override def stop(): Boolean = { |
| 76 | + logger info s"Stopped RCON connector to ${credentials.get.getValue("address")}!" |
| 77 | + socket.close() |
| 78 | + true |
| 79 | + } |
| 80 | +} |
0 commit comments