-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
98896bf
commit 514e582
Showing
4 changed files
with
161 additions
and
87 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/ru/pinkgoosik/kitsun/http/GithubWebhook.java
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,42 @@ | ||
package ru.pinkgoosik.kitsun.http; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import ru.pinkgoosik.kitsun.Bot; | ||
import ru.pinkgoosik.kitsun.feature.KitsunDebugger; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class GithubWebhook extends KitsunHttpHandler { | ||
|
||
@Override | ||
public void handle(HttpExchange exchange) { | ||
var map = parseParams(exchange); | ||
|
||
if(map.containsKey("token") && Bot.secrets.get().http.token.equals(map.get("token")) && exchange.getRequestMethod().equals("POST") && exchange.getRequestHeaders().containsKey("x-github-event")) { | ||
var event = exchange.getRequestHeaders().get("x-github-event").get(0); | ||
var is = exchange.getRequestBody(); | ||
StringBuilder textBuilder = new StringBuilder(); | ||
|
||
try (Reader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { | ||
int c; | ||
while ((c = reader.read()) != -1) { | ||
textBuilder.append((char) c); | ||
} | ||
} | ||
catch (Exception e) { | ||
Bot.LOGGER.info("Failed to handle github webhook due to an exception: " + e); | ||
} | ||
String body = textBuilder.toString(); | ||
|
||
KitsunDebugger.info(event + ": \n" + body); | ||
|
||
success(exchange, "Accepted", 202); | ||
return; | ||
} | ||
|
||
notFound(exchange); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/ru/pinkgoosik/kitsun/http/KitsunHttpHandler.java
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,77 @@ | ||
package ru.pinkgoosik.kitsun.http; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpHandler; | ||
import ru.pinkgoosik.kitsun.Bot; | ||
|
||
import java.io.OutputStream; | ||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public abstract class KitsunHttpHandler implements HttpHandler { | ||
|
||
static Map<String, String> parseParams(HttpExchange exchange) { | ||
String[] split = exchange.getRequestURI().toString().split("\\?"); | ||
if (split.length >= 2) { | ||
return splitParams(split[1]); | ||
} | ||
return new LinkedHashMap<>(); | ||
} | ||
|
||
static Map<String, String> splitParams(String params) { | ||
Map<String, String> queryPairs = new LinkedHashMap<>(); | ||
|
||
try { | ||
String[] pairs = params.split("&"); | ||
for (String pair : pairs) { | ||
int idx = pair.indexOf("="); | ||
queryPairs.put(URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8), URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8)); | ||
} | ||
} | ||
catch (Exception e) { | ||
Bot.LOGGER.error("Failed to split parameters: " + params + ", " + e); | ||
} | ||
return queryPairs; | ||
} | ||
|
||
static void notFound(HttpExchange exchange) { | ||
try { | ||
String text = "<h1>404 Not Found</h1>No context found for request"; | ||
exchange.sendResponseHeaders(404, text.length()); | ||
OutputStream os = exchange.getResponseBody(); | ||
|
||
os.write(text.getBytes()); | ||
os.flush(); | ||
exchange.close(); | ||
} | ||
catch (Exception e) { | ||
Bot.LOGGER.error("Failed to send not found response " + e); | ||
} | ||
} | ||
|
||
static void success(HttpExchange exchange, String text) { | ||
try { | ||
exchange.sendResponseHeaders(200, text.length()); | ||
OutputStream os = exchange.getResponseBody(); | ||
os.write(text.getBytes()); | ||
os.close(); | ||
} | ||
catch (Exception e) { | ||
Bot.LOGGER.error("Failed to send success response " + e); | ||
} | ||
} | ||
|
||
static void success(HttpExchange exchange, String text, int status) { | ||
try { | ||
exchange.sendResponseHeaders(status, text.length()); | ||
OutputStream os = exchange.getResponseBody(); | ||
os.write(text.getBytes()); | ||
os.close(); | ||
} | ||
catch (Exception e) { | ||
Bot.LOGGER.error("Failed to send success response " + e); | ||
} | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/main/java/ru/pinkgoosik/kitsun/http/ModUpdateWebhook.java
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,40 @@ | ||
package ru.pinkgoosik.kitsun.http; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import ru.pinkgoosik.kitsun.Bot; | ||
import ru.pinkgoosik.kitsun.cache.ServerData; | ||
import ru.pinkgoosik.kitsun.util.ServerUtils; | ||
|
||
public class ModUpdateWebhook extends KitsunHttpHandler { | ||
|
||
@Override | ||
public void handle(HttpExchange exchange) { | ||
var map = parseParams(exchange); | ||
|
||
if (map.containsKey("token") && Bot.secrets.get().http.token.equals(map.get("token"))) { | ||
if(map.containsKey("server") && map.containsKey("project")) { | ||
if (!ServerUtils.exist(map.get("server"))) { | ||
success(exchange, "Server not found"); | ||
return; | ||
} | ||
var data = ServerData.get(map.get("server")); | ||
|
||
for(var publisher : data.modUpdates.get()) { | ||
if(publisher.project.equals(map.get("project"))) { | ||
success(exchange, "Success"); | ||
try { | ||
Thread.sleep(5 * 1000); | ||
publisher.check(0); | ||
} | ||
catch (Exception ignored) {} | ||
return; | ||
} | ||
} | ||
success(exchange, "Project not found"); | ||
return; | ||
} | ||
} | ||
|
||
notFound(exchange); | ||
} | ||
} |