This repository was archived by the owner on Aug 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Adding RCON Connector #68
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e9a4520
Add Basic RCON Support
strifel 296d451
Merge branch 'master' of https://github.com/strifel/chatoverflow
strifel 6dea5bc
Cache connection errors. Wait 5 secounds till login to cause less con…
strifel f51df0f
Add reading of incomming RCON messages
strifel 2cc0fe8
Fixed some typos in the README.md file.
a6bac81
Merge pull request #77 from DragonCoder01/fixreadme
sebinside caf186b
Wait for authentication in twitch bot and report invalid credentials
hlxid ab8031d
Clear joined channels of TwitchChatConnector on stop
hlxid ee0d583
Fix #89: Clear all event handlers when shutting down an event input
J0B10 4de98d7
Fix #89: Remove an inputs listeners from the connector when shutting …
J0B10 3039885
Pass functions to register method from inputs instead of methods
hlxid 70956ce
Merge pull request #90 from daniel0611/fix/89-handler-called-twice
J0B10 2b80cc3
Also fix function passing for discord
J0B10 71fc2c9
Update version number to 0.2.1-prealpha
J0B10 8fdba80
Merge branch 'hotfix/0.2.1-prealpha'
J0B10 7181a78
Merge branch 'master' of https://github.com/codeoverflow-org/chatover…
strifel 2315ecb
Make RCON Connector Ready for new ChatOverflow version
strifel c509b13
Change requested implementations in RCON service.
strifel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/main/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/RconConnector.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package org.codeoverflow.chatoverflow.requirement.service.rcon | ||
|
||
import java.io.{DataInputStream, IOException, InputStream, OutputStream} | ||
import java.net.{Socket, SocketException} | ||
import java.nio.{ByteBuffer, ByteOrder} | ||
import java.util.Random | ||
|
||
import org.codeoverflow.chatoverflow.WithLogger | ||
import org.codeoverflow.chatoverflow.connector.Connector | ||
|
||
class RconConnector(override val sourceIdentifier: String) extends Connector(sourceIdentifier) with WithLogger { | ||
override protected var requiredCredentialKeys: List[String] = List("password", "address") | ||
override protected var optionalCredentialKeys: List[String] = List("port") | ||
|
||
private var socket: Socket = _ | ||
private var outputStream: OutputStream = _ | ||
private var inputStream: InputStream = _ | ||
private var requestId: Int = 0 | ||
|
||
def sendCommand(command: String): String = { | ||
logger debug s"Sending $command to RCON" | ||
requestId += 1 | ||
if (write(2, command.getBytes("ASCII"))) { | ||
return read() | ||
} | ||
null | ||
} | ||
|
||
|
||
/** | ||
* Starts the connector, e.g. creates a connection with its platform. | ||
*/ | ||
override def start(): Boolean = { | ||
logger info s"Starting rcon connection to ${credentials.get.getValue("address").get}" | ||
var port: Int = 25575 | ||
if (credentials.get.exists("port")) { | ||
try{ | ||
port = credentials.get.getValue("port").get.toInt | ||
} catch { | ||
case e: NumberFormatException => { | ||
logger error "Please enter a valid port" | ||
return false | ||
} | ||
} | ||
if (port < 1 || port > 65535) { | ||
logger error "Please enter a valid port" | ||
return false | ||
} | ||
} | ||
try { | ||
socket = new Socket(credentials.get.getValue("address").get, port) | ||
socket.setKeepAlive(true) | ||
outputStream = socket.getOutputStream | ||
inputStream = socket.getInputStream | ||
} catch { | ||
case e: IOException => { | ||
logger error "No Connection to RCON Server. Is it up?" | ||
return false | ||
} | ||
} | ||
val loggedIn = login() | ||
// Sleeping here to allow the (minecraft) server to start its own rcon procedure. Otherwise it caused errors in my tests. | ||
Thread.sleep(5000) | ||
loggedIn | ||
} | ||
|
||
private def login(): Boolean = { | ||
requestId = new Random().nextInt(Integer.MAX_VALUE) | ||
logger info "Logging RCON in..." | ||
val password = credentials.get.getValue("password").get | ||
if (write(3, password.getBytes("ASCII"))) { | ||
if (read() == null) { | ||
logger error "Could not log in to RCON Server. Password is Wrong!" | ||
return false | ||
} else { | ||
logger debug "Login to RCON was successful" | ||
return true | ||
} | ||
} | ||
false | ||
} | ||
strifel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private def write(packageType: Int, payload: Array[Byte]): Boolean = { | ||
try { | ||
val length = 4 + 4 + payload.length + 1 + 1 | ||
var byteBuffer: ByteBuffer = ByteBuffer.allocate(length + 4) | ||
byteBuffer.order(ByteOrder.LITTLE_ENDIAN) | ||
|
||
byteBuffer.putInt(length) | ||
byteBuffer.putInt(requestId) | ||
byteBuffer.putInt(packageType) | ||
byteBuffer.put(payload) | ||
byteBuffer.put(0x00.toByte) | ||
byteBuffer.put(0x00.toByte) | ||
|
||
outputStream.write(byteBuffer.array()) | ||
outputStream.flush() | ||
} catch { | ||
case e: SocketException => { | ||
logger error "Connection Error to RCON Server. This request will not be sended!" | ||
return false | ||
} | ||
} | ||
true | ||
} | ||
|
||
private def read(): String = { | ||
try { | ||
val header: Array[Byte] = Array.ofDim[Byte](4*3) | ||
inputStream.read(header) | ||
val headerBuffer: ByteBuffer = ByteBuffer.wrap(header) | ||
headerBuffer.order(ByteOrder.LITTLE_ENDIAN) | ||
val length = headerBuffer.getInt() | ||
val packageType = headerBuffer.getInt | ||
val payload: Array[Byte] = Array.ofDim[Byte](length - 4 - 4 - 2) | ||
val dataInputStream: DataInputStream = new DataInputStream(inputStream) | ||
dataInputStream.readFully(payload) | ||
dataInputStream.read(Array.ofDim[Byte](2)) | ||
if (packageType == -1) { | ||
return null | ||
} | ||
new String(payload, "ASCII") | ||
} catch { | ||
case e: NegativeArraySizeException => null; | ||
} | ||
} | ||
|
||
/** | ||
* This stops the activity of the connector, e.g. by closing the platform connection. | ||
*/ | ||
override def stop(): Boolean = { | ||
logger info s"Stopped RCON connector to ${credentials.get.getValue("address").get}!" | ||
socket.close() | ||
true | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ain/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/impl/RconInputImpl.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.codeoverflow.chatoverflow.requirement.service.rcon.impl | ||
|
||
import org.codeoverflow.chatoverflow.WithLogger | ||
import org.codeoverflow.chatoverflow.api.io.input.RconInput | ||
import org.codeoverflow.chatoverflow.registry.Impl | ||
import org.codeoverflow.chatoverflow.requirement.impl.InputImpl | ||
import org.codeoverflow.chatoverflow.requirement.service.rcon.RconConnector | ||
|
||
@Impl(impl = classOf[RconInput], connector = classOf[RconConnector]) | ||
class RconInputImpl extends InputImpl[RconConnector] with RconInput with WithLogger { | ||
override def getCommandOutput(command: String): String = sourceConnector.get.sendCommand(command) | ||
|
||
/** | ||
* Start the input, called after source connector did init | ||
* | ||
* @return true if starting the input was successful, false if some problems occurred | ||
*/ | ||
override def start(): Boolean = true | ||
|
||
override def stop(): Boolean = true | ||
} | ||
J0B10 marked this conversation as resolved.
Show resolved
Hide resolved
|
28 changes: 28 additions & 0 deletions
28
...in/scala/org/codeoverflow/chatoverflow/requirement/service/rcon/impl/RconOutputImpl.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.codeoverflow.chatoverflow.requirement.service.rcon.impl | ||
|
||
import org.codeoverflow.chatoverflow.WithLogger | ||
import org.codeoverflow.chatoverflow.api.io.output.RconOutput | ||
import org.codeoverflow.chatoverflow.registry.Impl | ||
import org.codeoverflow.chatoverflow.requirement.impl.OutputImpl | ||
import org.codeoverflow.chatoverflow.requirement.service.rcon.RconConnector | ||
|
||
@Impl(impl = classOf[RconOutput], connector = classOf[RconConnector]) | ||
class RconOutputImpl extends OutputImpl[RconConnector] with RconOutput with WithLogger { | ||
override def sendCommand(command: String): Boolean = { | ||
sourceConnector.get.sendCommand(command) != null | ||
} | ||
|
||
/** | ||
* Start the input, called after source connector did init | ||
* | ||
* @return true if starting the input was successful, false if some problems occurred | ||
*/ | ||
override def start(): Boolean = true | ||
|
||
/** | ||
* Stops the output, called before source connector will shutdown | ||
* | ||
* @return true if stopping was successful | ||
*/ | ||
override def stop(): Boolean = true | ||
} | ||
J0B10 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.