|
| 1 | +package org.codeoverflow.chatoverflow.requirement.service.rcon |
| 2 | + |
| 3 | +import java.io.{DataInputStream, IOException, InputStream, OutputStream} |
| 4 | +import java.net.{Socket, SocketException} |
| 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 | + if (write(2, command.getBytes("ASCII"))) { |
| 24 | + return read() |
| 25 | + } |
| 26 | + null |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | + /** |
| 31 | + * Starts the connector, e.g. creates a connection with its platform. |
| 32 | + */ |
| 33 | + override def start(): Boolean = { |
| 34 | + logger info s"Starting rcon connection to ${credentials.get.getValue("address").get}" |
| 35 | + var port: Int = 25575 |
| 36 | + if (credentials.get.exists("port")) { |
| 37 | + try{ |
| 38 | + port = credentials.get.getValue("port").get.toInt |
| 39 | + } catch { |
| 40 | + case e: NumberFormatException => { |
| 41 | + logger error "Please enter a valid port" |
| 42 | + return false |
| 43 | + } |
| 44 | + } |
| 45 | + if (port < 1 || port > 65535) { |
| 46 | + logger error "Please enter a valid port" |
| 47 | + return false |
| 48 | + } |
| 49 | + } |
| 50 | + try { |
| 51 | + socket = new Socket(credentials.get.getValue("address").get, port) |
| 52 | + socket.setKeepAlive(true) |
| 53 | + outputStream = socket.getOutputStream |
| 54 | + inputStream = socket.getInputStream |
| 55 | + } catch { |
| 56 | + case e: IOException => { |
| 57 | + logger error "No Connection to RCON Server. Is it up?" |
| 58 | + return false |
| 59 | + } |
| 60 | + } |
| 61 | + val loggedIn = login() |
| 62 | + // Sleeping here to allow the (minecraft) server to start its own rcon procedure. Otherwise it caused errors in my tests. |
| 63 | + Thread.sleep(5000) |
| 64 | + loggedIn |
| 65 | + } |
| 66 | + |
| 67 | + private def login(): Boolean = { |
| 68 | + requestId = new Random().nextInt(Integer.MAX_VALUE) |
| 69 | + logger info "Logging RCON in..." |
| 70 | + val password = credentials.get.getValue("password").get |
| 71 | + if (write(3, password.getBytes("ASCII"))) { |
| 72 | + if (read() == null) { |
| 73 | + logger error "Could not log in to RCON Server. Password is Wrong!" |
| 74 | + return false |
| 75 | + } else { |
| 76 | + logger debug "Login to RCON was successful" |
| 77 | + return true |
| 78 | + } |
| 79 | + } |
| 80 | + false |
| 81 | + } |
| 82 | + |
| 83 | + private def write(packageType: Int, payload: Array[Byte]): Boolean = { |
| 84 | + try { |
| 85 | + val length = 4 + 4 + payload.length + 1 + 1 |
| 86 | + var byteBuffer: ByteBuffer = ByteBuffer.allocate(length + 4) |
| 87 | + byteBuffer.order(ByteOrder.LITTLE_ENDIAN) |
| 88 | + |
| 89 | + byteBuffer.putInt(length) |
| 90 | + byteBuffer.putInt(requestId) |
| 91 | + byteBuffer.putInt(packageType) |
| 92 | + byteBuffer.put(payload) |
| 93 | + byteBuffer.put(0x00.toByte) |
| 94 | + byteBuffer.put(0x00.toByte) |
| 95 | + |
| 96 | + outputStream.write(byteBuffer.array()) |
| 97 | + outputStream.flush() |
| 98 | + } catch { |
| 99 | + case e: SocketException => { |
| 100 | + logger error "Connection Error to RCON Server. This request will not be sended!" |
| 101 | + return false |
| 102 | + } |
| 103 | + } |
| 104 | + true |
| 105 | + } |
| 106 | + |
| 107 | + private def read(): String = { |
| 108 | + try { |
| 109 | + val header: Array[Byte] = Array.ofDim[Byte](4*3) |
| 110 | + inputStream.read(header) |
| 111 | + val headerBuffer: ByteBuffer = ByteBuffer.wrap(header) |
| 112 | + headerBuffer.order(ByteOrder.LITTLE_ENDIAN) |
| 113 | + val length = headerBuffer.getInt() |
| 114 | + val packageType = headerBuffer.getInt |
| 115 | + val payload: Array[Byte] = Array.ofDim[Byte](length - 4 - 4 - 2) |
| 116 | + val dataInputStream: DataInputStream = new DataInputStream(inputStream) |
| 117 | + dataInputStream.readFully(payload) |
| 118 | + dataInputStream.read(Array.ofDim[Byte](2)) |
| 119 | + if (packageType == -1) { |
| 120 | + return null |
| 121 | + } |
| 122 | + new String(payload, "ASCII") |
| 123 | + } catch { |
| 124 | + case e: NegativeArraySizeException => null; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * This stops the activity of the connector, e.g. by closing the platform connection. |
| 130 | + */ |
| 131 | + override def stop(): Boolean = { |
| 132 | + logger info s"Stopped RCON connector to ${credentials.get.getValue("address").get}!" |
| 133 | + socket.close() |
| 134 | + true |
| 135 | + } |
| 136 | +} |
0 commit comments