103 lines
3.3 KiB
Java
103 lines
3.3 KiB
Java
|
package de.mrbesen.telegram;
|
||
|
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.io.OutputStream;
|
||
|
import java.net.InetSocketAddress;
|
||
|
import java.security.KeyStore;
|
||
|
|
||
|
import javax.net.ssl.KeyManagerFactory;
|
||
|
import javax.net.ssl.SSLContext;
|
||
|
import javax.net.ssl.SSLEngine;
|
||
|
import javax.net.ssl.SSLParameters;
|
||
|
import javax.net.ssl.TrustManagerFactory;
|
||
|
|
||
|
import com.sun.net.httpserver.HttpExchange;
|
||
|
import com.sun.net.httpserver.HttpHandler;
|
||
|
import com.sun.net.httpserver.HttpsConfigurator;
|
||
|
import com.sun.net.httpserver.HttpsExchange;
|
||
|
import com.sun.net.httpserver.HttpsParameters;
|
||
|
import com.sun.net.httpserver.HttpsServer;
|
||
|
|
||
|
|
||
|
//for https server: https://stackoverflow.com/questions/2308479/simple-java-https-server
|
||
|
//for jks store: https://stackoverflow.com/questions/2138940/import-pem-into-java-key-store
|
||
|
public class WebhookTelegramAPI extends TelegramAPI {
|
||
|
|
||
|
private int port = 8000;
|
||
|
private String pw = "supersecret";
|
||
|
|
||
|
public static class MyHandler implements HttpHandler {
|
||
|
@Override
|
||
|
public void handle(HttpExchange t) throws IOException {
|
||
|
String response = "This is the response";
|
||
|
HttpsExchange httpsExchange = (HttpsExchange) t;
|
||
|
t.getResponseHeaders().add("Access-Control-Allow-Origin", "*");
|
||
|
t.sendResponseHeaders(200, response.getBytes().length);
|
||
|
OutputStream os = t.getResponseBody();
|
||
|
os.write(response.getBytes());
|
||
|
os.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void main() throws Exception {
|
||
|
try {
|
||
|
// setup the socket address
|
||
|
InetSocketAddress address = new InetSocketAddress(port);
|
||
|
|
||
|
// initialise the HTTPS server
|
||
|
HttpsServer httpsServer = HttpsServer.create(address, 0);
|
||
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||
|
|
||
|
// initialise the keystore
|
||
|
char[] password = "password".toCharArray();
|
||
|
KeyStore ks = KeyStore.getInstance("JKS");
|
||
|
FileInputStream fis = new FileInputStream("testkey.jks");
|
||
|
ks.load(fis, password);
|
||
|
|
||
|
// setup the key manager factory
|
||
|
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
||
|
kmf.init(ks, password);
|
||
|
|
||
|
// setup the trust manager factory
|
||
|
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
|
||
|
tmf.init(ks);
|
||
|
|
||
|
// setup the HTTPS context and parameters
|
||
|
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
||
|
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext) {
|
||
|
public void configure(HttpsParameters params) {
|
||
|
try {
|
||
|
// initialise the SSL context
|
||
|
SSLContext c = getSSLContext();
|
||
|
SSLEngine engine = c.createSSLEngine();
|
||
|
params.setNeedClientAuth(false);
|
||
|
params.setCipherSuites(engine.getEnabledCipherSuites());
|
||
|
params.setProtocols(engine.getEnabledProtocols());
|
||
|
|
||
|
// Set the SSL parameters
|
||
|
SSLParameters sslParameters = sslContext.getSupportedSSLParameters();
|
||
|
params.setSSLParameters(sslParameters);
|
||
|
|
||
|
} catch (Exception ex) {
|
||
|
System.out.println("Failed to create HTTPS port");
|
||
|
}
|
||
|
}
|
||
|
});
|
||
|
httpsServer.createContext("/test", new MyHandler());
|
||
|
httpsServer.setExecutor(null); // creates a default executor
|
||
|
httpsServer.start();
|
||
|
|
||
|
} catch (Exception exception) {
|
||
|
System.out.println("Failed to create HTTPS server on port " + port + " of localhost");
|
||
|
exception.printStackTrace();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public WebhookTelegramAPI(String apikey) {
|
||
|
super(apikey);
|
||
|
}
|
||
|
|
||
|
}
|