@Override public void shutdown() { es.shutdown(); node.shutdown(); super.shutdown(); }
BasicHttpServer(PrivateKey key, int port, int maxTrheads, BufferedLogger log) throws IOException { this.myKey = key; this.log = log; service = new MicroHTTPDService(); addEndpoint("/ping", params -> onPing(params)); addEndpoint("/connect", params -> onConnect(params)); addEndpoint("/get_token", params -> inSession(params.getLongOrThrow("session_id"), s -> s.getToken(params))); addEndpoint("/command", params -> inSession(params.getLongOrThrow("session_id"), s -> s.command(params))); service.start(port, maxTrheads); }
private Binder inSession(PublicKey key, Implementor function) throws EncryptionError { return inSession(getSession(key), function); }
@Test public void handshakeAndSecureCommand() throws Exception { PrivateKey nodeKey = TestKeys.privateKey(1); PrivateKey clientKey = TestKeys.privateKey(2); BasicHttpServer s = new BasicHttpServer(nodeKey, 15600, 32, log); BasicHttpClient c = new BasicHttpClient("http://localhost:15600"); c.start(clientKey, nodeKey.getPublicKey(), null); Binder res = c.command("sping"); assertEquals("spong", res.getStringOrThrow("sping")); s.addSecureEndpoint("getSessionInfo", (params,session)-> { // System.out.println("in sec, "+session); // System.out.println("\t "+session.getPublicKey()); return Binder.of("publicKey", session.getPublicKey().info().toString()); }); res = c.command("getSessionInfo"); s.shutdown(); }
public void addEndpoint(String path, Endpoint ep) { on(path, (request, response) -> { response.getHeaders().put("connection", "close"); Binder result; try { Result epResult = new Result(); // System.out.println("extracted params: " + extractParams(request)); ep.execute(extractParams(request), epResult); result = Binder.of( "result", "ok", "response", epResult); } catch (Exception e) { result = Binder.of( "result", "error", "error", e.toString(), "errorClass", e.getClass().getName() ); } response.setBody(Boss.pack(result)); }); }
private Binder inSession(long id, Implementor processor) { Session s = sessionsById.get(id); if (s == null) throw new IllegalArgumentException("bad session number"); return inSession(s, processor); }
void addEndpoint(String path, SimpleEndpoint sep) { addEndpoint(path, (params, result) -> { result.putAll(sep.execute(params)); }); }
@Override public void on(String path, BasicHTTPService.Handler handler) { super.on(path, (request, response) -> { if (localCors) { Binder hh = response.getHeaders(); hh.put("Access-Control-Allow-Origin", "*"); hh.put("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); hh.put("Access-Control-Allow-Headers", "DNT,X-CustomHeader,Keep-Alive,User-Age nt,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range"); hh.put("Access-Control-Expose-Headers", "DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range"); } handler.handle(request, response); }); }
@Test public void testError() throws Exception { PrivateKey nodeKey = TestKeys.privateKey(1); PrivateKey clientKey = TestKeys.privateKey(2); BasicHttpServer s = new BasicHttpServer(nodeKey, 15600, 32, log); BasicHttpClient c = new BasicHttpClient("http://localhost:15600"); c.start(clientKey, nodeKey.getPublicKey(), null); assertThrows(CommandFailedException.class, ()->c.command("test_error")); }
private Binder onConnect(Binder params) throws ClientError { try { PublicKey clientKey = new PublicKey(params.getBinaryOrThrow("client_key")); return inSession(clientKey, session -> session.connect()); } catch (Exception e) { throw new ClientError(Errors.BAD_CLIENT_KEY, "client_key", "bad client key"); } }