Skip to content

Commit

Permalink
github webhooks test
Browse files Browse the repository at this point in the history
  • Loading branch information
PinkGoosik committed May 17, 2024
1 parent 98896bf commit 514e582
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 87 deletions.
42 changes: 42 additions & 0 deletions src/main/java/ru/pinkgoosik/kitsun/http/GithubWebhook.java
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 src/main/java/ru/pinkgoosik/kitsun/http/KitsunHttpHandler.java
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);
}
}
}
89 changes: 2 additions & 87 deletions src/main/java/ru/pinkgoosik/kitsun/http/KitsunHttpServer.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package ru.pinkgoosik.kitsun.http;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import ru.pinkgoosik.kitsun.Bot;
import ru.pinkgoosik.kitsun.cache.ServerData;
import ru.pinkgoosik.kitsun.util.ServerUtils;

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

public class KitsunHttpServer {
public static HttpServer server;
Expand All @@ -20,35 +11,9 @@ public static void init() {
if(Bot.secrets.get().http.port > 0) {
try {
HttpServer server = HttpServer.create(new InetSocketAddress(Bot.secrets.get().http.port), 0);
server.createContext("/mod_update", 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);
});
server.createContext("/github", new GithubWebhook());
server.createContext("/mod_update", new ModUpdateWebhook());

server.setExecutor(command -> new Thread(command).start());
server.start();
Expand All @@ -60,54 +25,4 @@ public static void init() {
}
}

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);
}
}
}
40 changes: 40 additions & 0 deletions src/main/java/ru/pinkgoosik/kitsun/http/ModUpdateWebhook.java
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);
}
}

0 comments on commit 514e582

Please sign in to comment.