commit initial version of PieCannon library and desktop app.

Documentation is sparse right now, I'm working on getting the desktop and android apps working first.
This commit is contained in:
2020-10-22 04:01:33 -05:00
parent 8790054c64
commit 569e174b78
12 changed files with 396 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
package net.monarchpass.piecannon;
import java.net.URI;
import java.io.InputStream;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import com.google.common.base.Charsets;
import com.google.common.io.CharSource;
import com.google.common.io.ByteStreams;
public class PieCannon {
private final List<Server> servers = new ArrayList<>();
public void addServer (final Server server) {
servers.add(server);
}
public List<Server> getServers () {
return Collections.unmodifiableList(servers);
}
public static boolean testServer (final Server server) {
final String testString = "piecannon-test-" + System.currentTimeMillis();
final URI result = server.upload("piecannon.test", CharSource.wrap(testString).asByteSource(Charsets.UTF_8));
try (final InputStream in = result.toURL().openStream()) {
return new String(ByteStreams.toByteArray(in), Charsets.UTF_8).equals(testString);
} catch (final IOException exception) {
exception.printStackTrace();
}
return false;
}
}

View File

@@ -0,0 +1,12 @@
package net.monarchpass.piecannon;
import java.io.File;
import java.net.URI;
import com.google.common.io.Files;
import com.google.common.io.ByteSource;
public interface Server {
public String getLabel ();
public URI upload (String name, ByteSource source);
public default URI upload (File file) {return upload(file.getName(), Files.asByteSource(file));}
}

View File

@@ -0,0 +1,53 @@
package net.monarchpass.piecannon.impl;
import net.monarchpass.piecannon.Server;
import com.google.common.io.ByteSource;
import java.io.InputStream;
import java.io.IOException;
import java.net.URI;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.SftpException;
import lombok.Data;
@Data
public class SftpServer implements Server {
private final String label;
private final String host;
private final int port;
private final String username;
private final String password;
private final String path;
private final URI uri;
public URI upload (String name, ByteSource source) {
try {
final JSch jsch = new JSch();
final Session session = jsch.getSession(username, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
final Channel channel = session.openChannel("sftp");
channel.connect();
final ChannelSftp sftp = (ChannelSftp) channel;
sftp.cd(path);
try (InputStream in = source.openStream()) {
sftp.put(in, name);
}
session.disconnect();
return URI.create(uri.toString() + "/" + name);
} catch (final JSchException | IOException | SftpException exception) {
throw new RuntimeException(exception);
}
}
}

View File

@@ -0,0 +1,30 @@
package net.monarchpass.piecannon;
import java.net.URI;
import org.junit.jupiter.api.Test;
import net.monarchpass.piecannon.impl.SftpServer;
import static com.google.common.truth.Truth.assertThat;
public class SftpTest {
@Test
public void test () throws Exception {
final String testServerHost = System.getProperty("piecannon.test.host");
if (testServerHost == null) {
return;
}
final String testServerUser = System.getProperty("piecannon.test.user");
final String testServerPassword = System.getProperty("piecannon.test.password");
final String testServerPath = System.getProperty("piecannon.test.path");
final String testServerURL = System.getProperty("piecannon.test.url");
final Server server = new SftpServer(
"Test Server Instance", testServerHost, 22,
testServerUser, testServerPassword, testServerPath,
URI.create(testServerURL)
);
assertThat(PieCannon.testServer(server)).isTrue();
}
}