Properly implemented internal webserver
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,16 +1,20 @@
|
|||||||
tech/nevets/lunarbot/config/ConfigHandler.java
|
tech/nevets/lunarbot/config/ConfigHandler.java
|
||||||
tech.nevets.lunarbot.config.ConfigHandler
|
tech.nevets.lunarbot.config.ConfigHandler
|
||||||
tech/nevets/lunarbot/commands/DiceCmd.java
|
|
||||||
tech.nevets.lunarbot.commands.DiceCmd
|
|
||||||
tech/nevets/lunarbot/config/Config.java
|
|
||||||
tech.nevets.lunarbot.config.Config
|
|
||||||
tech/nevets/lunarbot/commands/CoinCmd.java
|
|
||||||
tech.nevets.lunarbot.commands.CoinCmd
|
|
||||||
tech/nevets/lunarbot/commands/InfoCmd.java
|
|
||||||
tech.nevets.lunarbot.commands.InfoCmd
|
|
||||||
tech/nevets/lunarbot/commands/PingCmd.java
|
|
||||||
tech.nevets.lunarbot.commands.PingCmd
|
|
||||||
tech/nevets/lunarbot/config/ConfigUtils.java
|
tech/nevets/lunarbot/config/ConfigUtils.java
|
||||||
tech.nevets.lunarbot.config.ConfigUtils
|
tech.nevets.lunarbot.config.ConfigUtils
|
||||||
|
tech/nevets/lunarbot/config/Config.java
|
||||||
|
tech.nevets.lunarbot.config.Config
|
||||||
|
tech/nevets/lunarbot/webserver/WebServer.java
|
||||||
|
tech.nevets.lunarbot.webserver.WebServer
|
||||||
|
tech/nevets/lunarbot/commands/games/CoinCmd.java
|
||||||
|
tech.nevets.lunarbot.commands.games.CoinCmd
|
||||||
|
tech/nevets/lunarbot/commands/wiki/InfoCmd.java
|
||||||
|
tech.nevets.lunarbot.commands.wiki.InfoCmd
|
||||||
|
tech/nevets/lunarbot/webserver/WebAPI.java
|
||||||
|
tech.nevets.lunarbot.webserver.WebAPI
|
||||||
tech/nevets/lunarbot/Bot.java
|
tech/nevets/lunarbot/Bot.java
|
||||||
tech.nevets.lunarbot.Bot
|
tech.nevets.lunarbot.Bot
|
||||||
|
tech/nevets/lunarbot/commands/games/DiceCmd.java
|
||||||
|
tech.nevets.lunarbot.commands.games.DiceCmd
|
||||||
|
tech/nevets/lunarbot/commands/games/PingCmd.java
|
||||||
|
tech.nevets.lunarbot.commands.games.PingCmd
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
prefix: "$"
|
prefix: "$"
|
||||||
botToken: "ODY2ODE5NzQ2MDM2ODQyNTI2.YPYGzA.qz-ELkl_GOWZ7qbCJSNvimLPZyo"
|
botToken: "ODY2ODE5NzQ2MDM2ODQyNTI2.YPYGzA.bEdQlvhZdMbvZdN5WtTvh_URJ2k"
|
||||||
@@ -10,6 +10,7 @@ import tech.nevets.lunarbot.commands.games.PingCmd;
|
|||||||
import tech.nevets.lunarbot.config.Config;
|
import tech.nevets.lunarbot.config.Config;
|
||||||
import tech.nevets.lunarbot.config.ConfigHandler;
|
import tech.nevets.lunarbot.config.ConfigHandler;
|
||||||
import tech.nevets.lunarbot.config.ConfigUtils;
|
import tech.nevets.lunarbot.config.ConfigUtils;
|
||||||
|
import tech.nevets.lunarbot.webserver.WebServer;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
@@ -37,5 +38,9 @@ public class Bot {
|
|||||||
.build();
|
.build();
|
||||||
jda.awaitReady();
|
jda.awaitReady();
|
||||||
System.out.println("Finished Building Bot!");
|
System.out.println("Finished Building Bot!");
|
||||||
|
|
||||||
|
WebServer.main(null);
|
||||||
|
System.out.println("WebServer successfully loaded in main class!");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,63 +1,13 @@
|
|||||||
package tech.nevets.lunarbot.webapi;
|
package tech.nevets.lunarbot.webserver;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
//TODO Make up the last 3 hours of my life - Alec
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
|
||||||
|
|
||||||
public class WebAPI {
|
public class WebAPI {
|
||||||
public static void main(String args[]) {
|
public static void webAPI() {
|
||||||
WebServer ws = new WebServer();
|
|
||||||
ws.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class WebServer {
|
|
||||||
|
|
||||||
|
|
||||||
protected void start() {
|
|
||||||
ServerSocket s;
|
|
||||||
|
|
||||||
System.out.println("Webserver starting up on port 80");
|
|
||||||
System.out.println("(press ctrl-c to exit)");
|
|
||||||
try {
|
|
||||||
// create the main server socket
|
|
||||||
s = new ServerSocket(80);
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("Error: " + e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Waiting for connection");
|
|
||||||
for (;;) {
|
|
||||||
try {
|
|
||||||
|
|
||||||
Socket remote = s.accept();
|
|
||||||
|
|
||||||
System.out.println("Connection, sending data.");
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader(
|
|
||||||
remote.getInputStream()));
|
|
||||||
PrintWriter out = new PrintWriter(remote.getOutputStream());
|
|
||||||
|
|
||||||
String str = ".";
|
|
||||||
while (!str.equals(""))
|
|
||||||
str = in.readLine();
|
|
||||||
|
|
||||||
out.println("HTTP/1.0 200 OK");
|
|
||||||
out.println("Content-Type: text/html");
|
|
||||||
out.println("Server: Bot");
|
|
||||||
out.println("");
|
|
||||||
out.println("<H1>WebAPI is running</H2>");
|
|
||||||
out.flush();
|
|
||||||
remote.close();
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println("Error: " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
200
src/main/java/tech/nevets/lunarbot/webserver/WebServer.java
Normal file
200
src/main/java/tech/nevets/lunarbot/webserver/WebServer.java
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
package tech.nevets.lunarbot.webserver;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
public class WebServer implements Runnable {
|
||||||
|
|
||||||
|
static final File WEB_ROOT = new File(".");
|
||||||
|
static final String DEFAULT_FILE = "index.html";
|
||||||
|
static final String FILE_NOT_FOUND = "404.html";
|
||||||
|
static final String METHOD_NOT_SUPPORTED = "mns.html";
|
||||||
|
|
||||||
|
static final int PORT = 80;
|
||||||
|
|
||||||
|
private Socket connect;
|
||||||
|
public WebServer(Socket c) {
|
||||||
|
connect = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
ServerSocket serverConnect = new ServerSocket(PORT);
|
||||||
|
while (true) {
|
||||||
|
WebServer myServer = new WebServer(serverConnect.accept());
|
||||||
|
Thread thread = new Thread(myServer);
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error occurred while connecting");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
BufferedReader in = null;
|
||||||
|
PrintWriter out = null;
|
||||||
|
BufferedOutputStream dataOut = null;
|
||||||
|
String fileRequested = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
in = new BufferedReader(new InputStreamReader(connect.getInputStream()));
|
||||||
|
out = new PrintWriter(connect.getOutputStream());
|
||||||
|
dataOut = new BufferedOutputStream(connect.getOutputStream());
|
||||||
|
String input = in.readLine();
|
||||||
|
StringTokenizer parse = new StringTokenizer(input);
|
||||||
|
String method = parse.nextToken().toUpperCase();
|
||||||
|
fileRequested = parse.nextToken().toLowerCase();
|
||||||
|
|
||||||
|
if (!method.equals("GET") && !method.equals("HEAD")) {
|
||||||
|
File file = new File(WEB_ROOT, METHOD_NOT_SUPPORTED);
|
||||||
|
int fileLength = (int) file.length();
|
||||||
|
String contentMimeType = "text/html";
|
||||||
|
byte[] fileData = readFileData(file, fileLength);
|
||||||
|
|
||||||
|
out.println("HTTP/1.1 501 Not Implemented");
|
||||||
|
out.println("Server: Java HTTP Server from SSaurel : 1.0");
|
||||||
|
out.println("Date: " + new Date());
|
||||||
|
out.println("Content-type: " + contentMimeType);
|
||||||
|
out.println("Content-length: " + fileLength);
|
||||||
|
out.println();
|
||||||
|
out.flush();
|
||||||
|
dataOut.write(fileData, 0, fileLength);
|
||||||
|
dataOut.flush();
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if (fileRequested.endsWith("/")) {
|
||||||
|
fileRequested += DEFAULT_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = new File(WEB_ROOT, fileRequested);
|
||||||
|
int fileLength = (int) file.length();
|
||||||
|
String content = getContentType(fileRequested);
|
||||||
|
|
||||||
|
if (method.equals("GET")) {
|
||||||
|
byte[] fileData = readFileData(file, fileLength);
|
||||||
|
|
||||||
|
out.println("HTTP/1.1 200 OK");
|
||||||
|
out.println("Server: Java HTTP Server from SSaurel : 1.0");
|
||||||
|
out.println("Date: " + new Date());
|
||||||
|
out.println("Content-type: " + content);
|
||||||
|
out.println("Content-length: " + fileLength);
|
||||||
|
out.println();
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
dataOut.write(fileData, 0, fileLength);
|
||||||
|
dataOut.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (FileNotFoundException fnfe) {
|
||||||
|
try {
|
||||||
|
fileNotFound(out, dataOut, fileRequested);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
System.err.println("Error with file not found exception : " + ioe.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
System.err.println("Server error : " + ioe);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
in.close();
|
||||||
|
out.close();
|
||||||
|
dataOut.close();
|
||||||
|
connect.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Error closing stream : " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readFileData(File file, int fileLength) throws IOException {
|
||||||
|
FileInputStream fileIn = null;
|
||||||
|
byte[] fileData = new byte[fileLength];
|
||||||
|
|
||||||
|
try {
|
||||||
|
fileIn = new FileInputStream(file);
|
||||||
|
fileIn.read(fileData);
|
||||||
|
} finally {
|
||||||
|
if (fileIn != null)
|
||||||
|
fileIn.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return fileData;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getContentType(String fileRequested) {
|
||||||
|
if (fileRequested.endsWith(".htm") || fileRequested.endsWith(".html"))
|
||||||
|
return "text/html";
|
||||||
|
else
|
||||||
|
return "text/plain";
|
||||||
|
}
|
||||||
|
|
||||||
|
private void fileNotFound(PrintWriter out, OutputStream dataOut, String fileRequested) throws IOException {
|
||||||
|
File file = new File(WEB_ROOT, FILE_NOT_FOUND);
|
||||||
|
int fileLength = (int) file.length();
|
||||||
|
String content = "text/html";
|
||||||
|
byte[] fileData = readFileData(file, fileLength);
|
||||||
|
|
||||||
|
out.println("HTTP/1.1 404 File Not Found");
|
||||||
|
out.println("Server: Java HTTP Server from SSaurel : 1.0");
|
||||||
|
out.println("Date: " + new Date());
|
||||||
|
out.println("Content-type: " + content);
|
||||||
|
out.println("Content-length: " + fileLength);
|
||||||
|
out.println();
|
||||||
|
out.flush();
|
||||||
|
|
||||||
|
dataOut.write(fileData, 0, fileLength);
|
||||||
|
dataOut.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
protected void start() {
|
||||||
|
ServerSocket s;
|
||||||
|
|
||||||
|
System.out.println("Webserver starting up on port 80");
|
||||||
|
System.out.println("(press ctrl-c to exit)");
|
||||||
|
try {
|
||||||
|
// create the main server socket
|
||||||
|
s = new ServerSocket(80);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error: " + e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Waiting for connection");
|
||||||
|
for (;;) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
Socket remote = s.accept();
|
||||||
|
|
||||||
|
System.out.println("Connection, sending data.");
|
||||||
|
BufferedReader in = new BufferedReader(new InputStreamReader(
|
||||||
|
remote.getInputStream()));
|
||||||
|
PrintWriter out = new PrintWriter(remote.getOutputStream());
|
||||||
|
|
||||||
|
String str = ".";
|
||||||
|
while (!str.equals(""))
|
||||||
|
str = in.readLine();
|
||||||
|
|
||||||
|
out.println("HTTP/1.0 200 OK");
|
||||||
|
out.println("Content-Type: text/html");
|
||||||
|
out.println("Server: Bot");
|
||||||
|
out.println("");
|
||||||
|
out.println("<h1>WebAPI is running</h1>");
|
||||||
|
out.flush();
|
||||||
|
remote.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("Error: " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
**/
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user