Search in sources :

Example 1 with CryptoManager

use of com.biglybt.core.security.CryptoManager in project BiglyBT by BiglySoftware.

the class PairingManagerImpl method sendRequest.

private Map<String, Object> sendRequest(String command, Map<String, Object> payload) throws PairingException {
    try {
        Map<String, Object> request = new HashMap<>();
        CryptoManager cman = CryptoManagerFactory.getSingleton();
        String azid = Base32.encode(cman.getSecureID());
        payload.put("_azid", azid);
        try {
            String pk = Base32.encode(cman.getECCHandler().getPublicKey("pairing"));
            payload.put("_pk", pk);
        } catch (Throwable e) {
        }
        request.put("req", payload);
        String request_str = Base32.encode(BEncoder.encode(request));
        String sig = null;
        try {
            sig = Base32.encode(cman.getECCHandler().sign(request_str.getBytes("UTF-8"), "pairing"));
        } catch (Throwable e) {
        }
        String other_params = "&ver=" + UrlUtils.encode(Constants.AZUREUS_VERSION) + "&app=" + UrlUtils.encode(SystemProperties.getApplicationName()) + "&locale=" + UrlUtils.encode(MessageText.getCurrentLocale().toString());
        if (sig != null) {
            other_params += "&sig=" + sig;
        }
        URL target = new URL(SERVICE_URL + "/client/" + command + "?request=" + request_str + other_params);
        Properties http_properties = new Properties();
        http_properties.put(ClientIDGenerator.PR_URL, target);
        try {
            ClientIDManagerImpl.getSingleton().generateHTTPProperties(null, http_properties);
        } catch (ClientIDException e) {
            throw (new IOException(e.getMessage()));
        }
        target = (URL) http_properties.get(ClientIDGenerator.PR_URL);
        HttpURLConnection connection = (HttpURLConnection) target.openConnection();
        connection.setConnectTimeout(30 * 1000);
        InputStream is = connection.getInputStream();
        Map<String, Object> response = (Map<String, Object>) BDecoder.decode(new BufferedInputStream(is));
        synchronized (this) {
            Long min_retry = (Long) response.get("min_secs");
            if (min_retry != null) {
                min_update_period = min_retry.intValue() * 1000;
            }
            Long max_retry = (Long) response.get("max_secs");
            if (max_retry != null) {
                max_update_period = max_retry.intValue() * 1000;
            }
        }
        final String message = getString(response, "message");
        if (message != null) {
            if (last_message == null || !last_message.equals(message)) {
                last_message = message;
                try {
                    byte[] message_sig = (byte[]) response.get("message_sig");
                    AEVerifier.verifyData(message, message_sig);
                    new AEThread2("PairMsg", true) {

                        @Override
                        public void run() {
                            UIManager ui_manager = StaticUtilities.getUIManager(120 * 1000);
                            if (ui_manager != null) {
                                ui_manager.showMessageBox("pairing.server.warning.title", "!" + message + "!", UIManagerEvent.MT_OK);
                            }
                        }
                    }.start();
                } catch (Throwable e) {
                }
            }
        }
        String error = getString(response, "error");
        if (error != null) {
            throw (new PairingException(error));
        }
        setLastServerError(null);
        Map<String, Object> reply = (Map<String, Object>) response.get("rep");
        Long qr_v = (Long) reply.get("qr_v");
        if (qr_v != null) {
            if (qr_version != qr_v.longValue()) {
                qr_version = qr_v;
                COConfigurationManager.setParameter("pairing.qr.ver", qr_version);
            }
        }
        return (reply);
    } catch (Throwable e) {
        setLastServerError(Debug.getNestedExceptionMessage(e));
        if (e instanceof PairingException) {
            throw ((PairingException) e);
        }
        throw (new PairingException("invocation failed", e));
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) UIManager(com.biglybt.pif.ui.UIManager) CryptoManager(com.biglybt.core.security.CryptoManager) IOException(java.io.IOException) ClientIDException(com.biglybt.pif.clientid.ClientIDException) BufferedInputStream(java.io.BufferedInputStream)

Aggregations

CryptoManager (com.biglybt.core.security.CryptoManager)1 ClientIDException (com.biglybt.pif.clientid.ClientIDException)1 UIManager (com.biglybt.pif.ui.UIManager)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1