Merge branch 'master' of ssh://git.mrbesen.de:2222/MrBesen/TelegramAPI

This commit is contained in:
Oliver 2019-02-01 20:25:24 +01:00
commit 291e52dbb4
Signed by: okaestne
GPG Key ID: FAB3DB905B93001C
2 changed files with 103 additions and 1 deletions

View File

@ -153,7 +153,7 @@ public class TelegramAPI implements Runnable {
processUpdates(request("getUpdates", "offset=" + msg_offset));
} catch (IOException e) {
log.log("error getting updates.", e);
}
}
}
private void processUpdates(JSONObject object) {

View File

@ -0,0 +1,102 @@
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);
}
}