Search in sources :

Example 1 with BasicHttpClientSession

use of com.icodici.universa.node2.network.BasicHttpClientSession in project universa by UniversaBlockchain.

the class CLIMain method breakSession.

/**
 * Only for test purposes
 *
 * @param nodeNumber
 * @throws IOException
 */
public static void breakSession(int nodeNumber) throws IOException {
    BasicHttpClientSession s = getSession(nodeNumber);
    s.setSessionId(666);
    s.setSessionKey(new SymmetricKey());
    Path keysDir = Paths.get(System.getProperty("user.home") + "/.universa");
    if (!Files.exists(keysDir)) {
        reporter.verbose("creating new keys directory: " + keysDir.toString());
        final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwx------");
        final FileAttribute<Set<PosixFilePermission>> ownerOnly = PosixFilePermissions.asFileAttribute(perms);
        Files.createDirectory(keysDir, ownerOnly);
    }
    Path sessionFile = keysDir.resolve("node_" + nodeNumber + ".session");
    try (OutputStream out = Files.newOutputStream(sessionFile)) {
        out.write(Boss.pack(s.asBinder()));
    }
    final Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------");
    Files.setPosixFilePermissions(sessionFile, perms);
    prefs.put("session_" + nodeNumber, sessionFile.toString());
    reporter.verbose("Broken session has been stored to the " + keysDir + "/" + sessionFile);
}
Also used : OptionSet(joptsimple.OptionSet) SymmetricKey(com.icodici.crypto.SymmetricKey) BasicHttpClientSession(com.icodici.universa.node2.network.BasicHttpClientSession) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 2 with BasicHttpClientSession

use of com.icodici.universa.node2.network.BasicHttpClientSession in project universa by UniversaBlockchain.

the class CLIMain method getClientNetwork.

public static synchronized ClientNetwork getClientNetwork() throws IOException {
    if (clientNetwork == null) {
        reporter.verbose("ClientNetwork not exist, create one");
        BasicHttpClientSession s = null;
        reporter.verbose("ClientNetwork nodeUrl: " + nodeUrl);
        if (nodeUrl != null) {
            clientNetwork = new ClientNetwork(nodeUrl, null, true);
        } else {
            clientNetwork = new ClientNetwork(null, true);
        }
        if (clientNetwork.client == null)
            throw new IOException("failed to connect to to the universa network");
        s = getSession(clientNetwork.getNodeNumber());
        reporter.verbose("Session for " + clientNetwork.getNodeNumber() + " is exist: " + (s != null));
        if (s != null)
            reporter.verbose("Session id is " + s.getSessionId());
        clientNetwork.start(s);
    }
    if (clientNetwork != null)
        session = clientNetwork.getSession();
    return clientNetwork;
}
Also used : BasicHttpClientSession(com.icodici.universa.node2.network.BasicHttpClientSession)

Aggregations

BasicHttpClientSession (com.icodici.universa.node2.network.BasicHttpClientSession)2 SymmetricKey (com.icodici.crypto.SymmetricKey)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 OptionSet (joptsimple.OptionSet)1