Compare commits
No commits in common. "212996521a2693ce0bc502421780b979105637b3" and "b004320ffe1486d7cb6aeb206e5cc7421d58727d" have entirely different histories.
212996521a
...
b004320ffe
@ -28,4 +28,4 @@ TODO
|
|||||||
## How to use
|
## How to use
|
||||||
Place a `servers.json` in the Pie Cannon data directory. For GNU/Linux this is `XDG_DATA_HOME` (by default this would be `~/.local/share/piecannon`) (create this directory if it does not exist. For Android this would be the app's "externalFilesDir" (probably something like `Android/data/net.monarchpass.piecannon/files` - the app will tell you when you launch it). See `servers.example.json` for an example of such a file. Whenever you initiate a file upload, Pie Cannon will select one of your defined servers at random and upload the file.
|
Place a `servers.json` in the Pie Cannon data directory. For GNU/Linux this is `XDG_DATA_HOME` (by default this would be `~/.local/share/piecannon`) (create this directory if it does not exist. For Android this would be the app's "externalFilesDir" (probably something like `Android/data/net.monarchpass.piecannon/files` - the app will tell you when you launch it). See `servers.example.json` for an example of such a file. Whenever you initiate a file upload, Pie Cannon will select one of your defined servers at random and upload the file.
|
||||||
|
|
||||||
For FTP/SFTP servers (the only supported types as of now), the `path` is relative to your home directory and should be where `url` points to. i.e. a file uploaded to `path` should be downloadable at `url`. In the future Pie Cannon should be able to get SSH credentials from your agent so you won't need to put a password in this file.
|
For SFTP servers (the only supported type as of now), the `path` is relative to your home directory and should be where `url` points to. i.e. a file uploaded to `path` should be downloadable at `url`. In the future Pie Cannon should be able to get SSH credentials from your agent so you won't need to put a password in this file.
|
||||||
|
@ -4,6 +4,8 @@ import java.net.URI;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import org.freedesktop.BaseDirectory;
|
import org.freedesktop.BaseDirectory;
|
||||||
|
|
||||||
import lombok.extern.java.Log;
|
import lombok.extern.java.Log;
|
||||||
@ -33,22 +35,16 @@ public class App {
|
|||||||
log.log(Level.SEVERE, "No servers defined, please define at least one in {0}", serversJson);
|
log.log(Level.SEVERE, "No servers defined, please define at least one in {0}", serversJson);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final Server server = cannon.selectServer();
|
||||||
|
log.log(Level.INFO, "Randomly selected server: {0}", server.getLabel());
|
||||||
|
|
||||||
if (args.length == 0) {
|
if (args.length == 0) {
|
||||||
log.log(Level.SEVERE, "No filename provided");
|
log.log(Level.SEVERE, "No filename provided");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Server server = cannon.selectServer();
|
final File source = new File(args[0]);
|
||||||
log.log(Level.INFO, "Randomly selected server: {0}", server.getLabel());
|
|
||||||
|
|
||||||
final String arg = args[0];
|
|
||||||
if (arg.equals("--test")) {
|
|
||||||
testServers(servers);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final File source = new File(arg);
|
|
||||||
if (!source.exists()) {
|
if (!source.exists()) {
|
||||||
log.log(Level.SEVERE, "No such file: {0}", source);
|
log.log(Level.SEVERE, "No such file: {0}", source);
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
@ -58,15 +54,6 @@ public class App {
|
|||||||
System.out.println(result);
|
System.out.println(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void testServers (final List<Server> servers) {
|
|
||||||
log.log(Level.INFO, "Testing all defined servers");
|
|
||||||
for (final Server server : servers) {
|
|
||||||
log.log(Level.INFO, "{0} {1}: {2}", new Object[] {
|
|
||||||
server.getClass().getSimpleName(), server.getLabel(), PieCannon.testServer(server)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static File getServersJson () {
|
public static File getServersJson () {
|
||||||
return new File(getDataDirectory(), "servers.json");
|
return new File(getDataDirectory(), "servers.json");
|
||||||
}
|
}
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
package net.monarchpass.piecannon.impl;
|
|
||||||
|
|
||||||
import net.monarchpass.piecannon.Server;
|
|
||||||
import com.google.common.io.ByteSource;
|
|
||||||
import com.google.common.io.ByteStreams;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLConnection;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public class FtpServer 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) {
|
|
||||||
name = name.replace(" ", "%20");
|
|
||||||
|
|
||||||
try {
|
|
||||||
final URI target = URI.create(uri.toString() + "/" + name);
|
|
||||||
final URL ftpUrl = new URL(String.format("ftp://%s:%s@%s:%d/%s/%s;type=i", username, password, host, port, path, name));
|
|
||||||
final URLConnection connection = ftpUrl.openConnection();
|
|
||||||
|
|
||||||
try (InputStream in = source.openStream()) {
|
|
||||||
try (OutputStream out = connection.getOutputStream()) {
|
|
||||||
ByteStreams.copy(in, out);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return target;
|
|
||||||
} catch (final IOException exception) {
|
|
||||||
throw new RuntimeException(exception);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,7 +8,6 @@ import com.google.gson.JsonObject;
|
|||||||
import com.jcraft.jsch.IdentityRepository;
|
import com.jcraft.jsch.IdentityRepository;
|
||||||
|
|
||||||
import net.monarchpass.piecannon.Server;
|
import net.monarchpass.piecannon.Server;
|
||||||
import net.monarchpass.piecannon.impl.FtpServer;
|
|
||||||
import net.monarchpass.piecannon.impl.SftpServer;
|
import net.monarchpass.piecannon.impl.SftpServer;
|
||||||
|
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
@ -17,14 +16,7 @@ public class ServerFactory implements Function<JsonObject, Server> {
|
|||||||
private @Setter IdentityRepository identityRepository;
|
private @Setter IdentityRepository identityRepository;
|
||||||
|
|
||||||
public Server apply (final JsonObject object) {
|
public Server apply (final JsonObject object) {
|
||||||
final String type = object.getAsJsonPrimitive("type").getAsString();
|
return makeSftpServer(object);
|
||||||
if (type.equalsIgnoreCase("ftp")) {
|
|
||||||
return makeFtpServer(object);
|
|
||||||
} else if (type.equalsIgnoreCase("sftp")) {
|
|
||||||
return makeSftpServer(object);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalArgumentException("Invalid server type: " + type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Server makeSftpServer (final JsonObject object) {
|
private Server makeSftpServer (final JsonObject object) {
|
||||||
@ -41,19 +33,4 @@ public class ServerFactory implements Function<JsonObject, Server> {
|
|||||||
path, URI.create(url), identityRepository
|
path, URI.create(url), identityRepository
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Server makeFtpServer (final JsonObject object) {
|
|
||||||
final String host = object.getAsJsonPrimitive("host").getAsString();
|
|
||||||
final String label = object.has("label") ? object.getAsJsonPrimitive("label").getAsString() : host;
|
|
||||||
final int port = object.has("port") ? object.getAsJsonPrimitive("port").getAsInt() : 21;
|
|
||||||
final String username = object.getAsJsonPrimitive("username").getAsString();
|
|
||||||
final String password = object.getAsJsonPrimitive("password").getAsString();
|
|
||||||
final String path = object.getAsJsonPrimitive("path").getAsString();
|
|
||||||
final String url = object.getAsJsonPrimitive("url").getAsString();
|
|
||||||
|
|
||||||
return new FtpServer(
|
|
||||||
label, host, port, username, password,
|
|
||||||
path, URI.create(url)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
[{
|
[{
|
||||||
"type": "ftp",
|
|
||||||
"host": "example.com",
|
"host": "example.com",
|
||||||
"username": "username",
|
"username": "username",
|
||||||
"password": "password",
|
"password": "password",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user