-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Authentication and config handler to web api | other small tweaks
- Loading branch information
1 parent
a286b13
commit 9943705
Showing
17 changed files
with
366 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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
12 changes: 5 additions & 7 deletions
12
src/main/java/me/spartacus04/jext/webapi/ResourcePackHandler.kt
This file contains 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 |
---|---|---|
@@ -1,24 +1,22 @@ | ||
package me.spartacus04.jext.webapi | ||
|
||
import com.sun.net.httpserver.HttpExchange | ||
import com.sun.net.httpserver.HttpHandler | ||
import me.spartacus04.jext.State.PLUGIN | ||
import me.spartacus04.jext.webapi.utils.JextHttpHandler | ||
|
||
class ResourcePackHandler : HttpHandler { | ||
class ResourcePackHandler : JextHttpHandler(false) { | ||
private val file = PLUGIN.dataFolder.resolve("resource-pack.zip") | ||
override fun handle(exchange: HttpExchange) { | ||
override fun onGet(exchange: HttpExchange) { | ||
if(!file.exists()) { | ||
exchange.sendResponseHeaders(404, 0) | ||
exchange.close() | ||
return | ||
return notFound(exchange) | ||
} | ||
|
||
exchange.sendResponseHeaders(200, file.length()) | ||
|
||
file.inputStream().use { input -> | ||
exchange.responseBody.use { output -> | ||
input.copyTo(output) | ||
} | ||
} | ||
exchange.close() | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/me/spartacus04/jext/webapi/auth/ConnectHandler.kt
This file contains 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,36 @@ | ||
package me.spartacus04.jext.webapi.auth | ||
|
||
import com.sun.net.httpserver.HttpExchange | ||
import me.spartacus04.jext.State.CONFIG | ||
import me.spartacus04.jext.webapi.utils.JextHttpHandler | ||
import java.util.HashMap | ||
|
||
class ConnectHandler : JextHttpHandler(false) { | ||
override fun onPost(exchange: HttpExchange) { | ||
val addr = exchange.remoteAddress.address.address.map { it.toInt() }.joinToString(".") | ||
val body = exchange.requestBody.bufferedReader().readText() | ||
|
||
if(body == CONFIG.WEB_INTERFACE_PASSWORD) { | ||
val token = (0..64).map { (('a'..'z') + ('A'..'Z') + ('0'..'9')).random() }.joinToString("") | ||
|
||
connectedHashMap[addr] = token | ||
|
||
exchange.sendResponseHeaders(200, token.length.toLong()) | ||
|
||
exchange.responseBody.use { os -> | ||
os.write(token.toByteArray()) | ||
} | ||
} else { | ||
val response = "401 Unauthorized" | ||
exchange.sendResponseHeaders(401, response.length.toLong()) | ||
|
||
exchange.responseBody.use { os -> | ||
os.write(response.toByteArray()) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
internal val connectedHashMap = HashMap<String, String>() | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/me/spartacus04/jext/webapi/auth/DisconnectHandler.kt
This file contains 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,14 @@ | ||
package me.spartacus04.jext.webapi.auth | ||
|
||
import com.sun.net.httpserver.HttpExchange | ||
import me.spartacus04.jext.webapi.utils.JextHttpHandler | ||
|
||
class DisconnectHandler : JextHttpHandler(true) { | ||
override fun onPost(exchange: HttpExchange) { | ||
val addr = exchange.remoteAddress.address.address.map { it.toInt() }.joinToString(".") | ||
|
||
ConnectHandler.connectedHashMap.remove(addr) | ||
|
||
exchange.sendResponseHeaders(200, 0) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/me/spartacus04/jext/webapi/auth/HealthHandler.kt
This file contains 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,22 @@ | ||
package me.spartacus04.jext.webapi.auth | ||
|
||
import com.sun.net.httpserver.HttpExchange | ||
import me.spartacus04.jext.webapi.utils.JextHttpHandler | ||
|
||
class HealthHandler : JextHttpHandler(false) { | ||
override fun onGet(exchange: HttpExchange) { | ||
val token = exchange.requestHeaders["Authorization"]?.first()?.replace("Bearer ", "") | ||
|
||
if(token == null) { | ||
exchange.sendResponseHeaders(200, 0) | ||
} else { | ||
val addr = exchange.remoteAddress.address.address.map { it.toInt() }.joinToString(".") | ||
|
||
if(ConnectHandler.connectedHashMap[addr] == token) { | ||
exchange.sendResponseHeaders(200, 0) | ||
} else { | ||
exchange.sendResponseHeaders(401, 0) | ||
} | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/me/spartacus04/jext/webapi/config/ConfigApplyHandler.kt
This file contains 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,23 @@ | ||
package me.spartacus04.jext.webapi.config | ||
|
||
import com.sun.net.httpserver.HttpExchange | ||
import me.spartacus04.jext.State.CONFIG | ||
import me.spartacus04.jext.State.WEBSERVER | ||
import me.spartacus04.jext.utils.JextMetrics | ||
import me.spartacus04.jext.webapi.utils.JextHttpHandler | ||
|
||
class ConfigApplyHandler : JextHttpHandler(true) { | ||
override fun onPost(exchange: HttpExchange) { | ||
val body = exchange.requestBody.bufferedReader().use { it.readText() } | ||
|
||
if(CONFIG.fromText(body)) { | ||
CONFIG.save() | ||
|
||
exchange.sendResponseHeaders(200, 0) | ||
JextMetrics.reloadMetrics() | ||
WEBSERVER.reload() | ||
} else { | ||
exchange.sendResponseHeaders(400, 0) | ||
} | ||
} | ||
} |
Oops, something went wrong.