21 | */
22 | @SuppressWarnings("restriction")
23 | public class HttpServerCreator {
24 |
25 | private static final File KEYSTORE_FILE = new File(System.getProperty("java.io.tmpdir"), "mpush.jks");
26 |
27 | private static final String KEYSTORE_PASSWORD = "mpush_2017";
28 |
29 | private static final String KEY_PASSWORD = "mpush_2017";
30 |
31 | /**
32 | * Generates a new self-signed certificate in /tmp/test.jks, if it does not
33 | * already exist.
34 | */
35 | private static void generateCertificate() throws Exception {
36 | if (KEYSTORE_FILE.exists()) return;
37 |
38 | System.setProperty("javax.net.debug", "all");
39 |
40 | File keytool = new File(System.getProperty("java.home"), "bin/keytool");
41 |
42 | String[] genkeyCmd = new String[]{
43 | keytool.toString(),
44 | "-genkey",
45 | "-keyalg", "RSA",
46 | "-alias", "mpush.com",
47 | "-validity", "365",
48 | "-keysize", "2048",
49 | "-dname", "cn=mpush.com,ou=mpush,o=OHUN .Inc,c=CN",
50 | "-keystore", KEYSTORE_FILE.getAbsolutePath(),
51 | "-storepass", KEYSTORE_PASSWORD,
52 | "-keypass", KEY_PASSWORD};
53 |
54 | System.out.println(String.join(" ", genkeyCmd));
55 |
56 | ProcessBuilder processBuilder = new ProcessBuilder(genkeyCmd);
57 | processBuilder.redirectErrorStream(true);
58 | processBuilder.redirectOutput(Redirect.INHERIT);
59 | processBuilder.redirectError(Redirect.INHERIT);
60 | Process exec = processBuilder.start();
61 | exec.waitFor();
62 |
63 | System.out.println("Exit value: " + exec.exitValue());
64 |
65 | }
66 |
67 | private static HttpsServer createHttpsServer(int port) throws Exception {
68 | generateCertificate();
69 | HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress(port), 0);
70 | SSLContext sslContext = getSslContext();
71 | httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
72 | return httpsServer;
73 | }
74 |
75 | private static HttpServer createHttpServer(int port) throws Exception {
76 | HttpServer httpServer = HttpServer.create(new InetSocketAddress(port), 0);
77 | return httpServer;
78 | }
79 |
80 | private static SSLContext getSslContext() throws Exception {
81 | KeyStore ks = KeyStore.getInstance("JKS");
82 | ks.load(new FileInputStream(KEYSTORE_FILE), KEYSTORE_PASSWORD.toCharArray());
83 |
84 | KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
85 | kmf.init(ks, KEY_PASSWORD.toCharArray());
86 |
87 | TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
88 | tmf.init(ks);
89 |
90 | SSLContext sslContext = SSLContext.getInstance("TLS");
91 | sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
92 |
93 | return sslContext;
94 | }
95 |
96 | public static HttpServer createServer(int port, boolean https) {
97 | try {
98 | if (https) {
99 | return HttpServerCreator.createHttpsServer(port);
100 | } else {
101 | return HttpServerCreator.createHttpServer(port);
102 | }
103 | } catch (Exception e) {
104 | throw new RuntimeException(e);
105 | }
106 | }
107 | }
--------------------------------------------------------------------------------
/src/main/java/com/shinemo/mpush/alloc/IndexPageHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Copyright 2015-2016 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * Contributors:
17 | * ohun@live.cn (夜色)
18 | */
19 |
20 | package com.shinemo.mpush.alloc;
21 |
22 | import com.sun.net.httpserver.HttpExchange;
23 | import com.sun.net.httpserver.HttpHandler;
24 |
25 | import java.io.IOException;
26 | import java.io.InputStream;
27 | import java.io.OutputStream;
28 | import java.nio.charset.StandardCharsets;
29 |
30 | /**
31 | * Created by ohun on 2016/12/5.
32 | *
33 | * @author ohun@live.cn (夜色)
34 | */
35 | public final class IndexPageHandler implements HttpHandler {
36 |
37 | private byte[] data;
38 |
39 | public IndexPageHandler() {
40 | try (InputStream in = this.getClass().getResourceAsStream("/index.html")) {
41 | this.data = new byte[in.available()];
42 | in.read(data);
43 | } catch (Exception e) {
44 | throw new RuntimeException(e);
45 | }
46 | }
47 |
48 | @Override
49 | public void handle(HttpExchange httpExchange) throws IOException {
50 | if (data != null && data.length > 0) {
51 | httpExchange.getResponseHeaders().set("Content-Type", "text/html; charset=utf-8");
52 | httpExchange.sendResponseHeaders(200, data.length);//200, content-length
53 | OutputStream out = httpExchange.getResponseBody();
54 | out.write(data);
55 | out.close();
56 | httpExchange.close();
57 | } else {
58 | byte[] data = "404 Not Found".getBytes(StandardCharsets.UTF_8);
59 | httpExchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
60 | httpExchange.sendResponseHeaders(404, data.length);
61 | OutputStream out = httpExchange.getResponseBody();
62 | out.write(data);
63 | out.close();
64 | httpExchange.close();
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/com/shinemo/mpush/alloc/Main.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Copyright 2015-2016 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * Contributors:
17 | * ohun@live.cn (夜色)
18 | */
19 |
20 | package com.shinemo.mpush.alloc;
21 |
22 | import com.mpush.tools.log.Logs;
23 |
24 | import java.io.IOException;
25 |
26 | /**
27 | * Created by ohun on 16/9/7.
28 | *
29 | * @author ohun@live.cn (夜色)
30 | */
31 | public final class Main {
32 |
33 | public static void main(String[] args) {
34 | Logs.init();
35 | Logs.Console.info("launch alloc server...");
36 | AllocServer server = new AllocServer();
37 | server.start();
38 | addHook(server);
39 | }
40 |
41 | private static void addHook(AllocServer server) {
42 | Runtime.getRuntime().addShutdownHook(
43 | new Thread(() -> {
44 | try {
45 | server.stop();
46 | } catch (Exception e) {
47 | Logs.Console.error("alloc server stop ex", e);
48 | }
49 | Logs.Console.info("jvm exit, all service stopped...");
50 |
51 | }, "mpush-shutdown-hook-thread")
52 | );
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/com/shinemo/mpush/alloc/PushHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * (C) Copyright 2015-2016 the original author or authors.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * Contributors:
17 | * ohun@live.cn (夜色)
18 | */
19 |
20 | package com.shinemo.mpush.alloc;
21 |
22 | import com.mpush.api.Constants;
23 | import com.mpush.api.push.*;
24 | import com.mpush.tools.Jsons;
25 | import com.mpush.tools.common.Strings;
26 | import com.sun.net.httpserver.HttpExchange;
27 | import com.sun.net.httpserver.HttpHandler;
28 | import org.slf4j.Logger;
29 | import org.slf4j.LoggerFactory;
30 |
31 | import java.io.ByteArrayOutputStream;
32 | import java.io.IOException;
33 | import java.io.InputStream;
34 | import java.io.OutputStream;
35 | import java.util.Map;
36 | import java.util.concurrent.atomic.AtomicInteger;
37 |
38 |
39 | /**
40 | * Created by ohun on 16/9/7.
41 | *
42 | * @author ohun@live.cn (夜色)
43 | */
44 | /*package*/ final class PushHandler implements HttpHandler {
45 | private final Logger logger = LoggerFactory.getLogger(this.getClass());
46 | private final PushSender pushSender = PushSender.create();
47 | private final AtomicInteger idSeq = new AtomicInteger();
48 |
49 | public void start() {
50 | pushSender.start();
51 | }
52 |
53 | public void stop() {
54 | pushSender.stop();
55 | }
56 |
57 | @SuppressWarnings("unchecked")
58 | @Override
59 | public void handle(HttpExchange httpExchange) throws IOException {
60 | String body = new String(readBody(httpExchange), Constants.UTF_8);
61 | Map