mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 11:19:04 +01:00
Proof-of-Concept Headless Client
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package org.briarproject.bramble.account;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
class HeadlessAccountManager extends AccountManagerImpl
|
||||
implements AccountManager {
|
||||
|
||||
@Inject
|
||||
HeadlessAccountManager(DatabaseConfig databaseConfig,
|
||||
CryptoComponent crypto, IdentityManager identityManager) {
|
||||
super(databaseConfig, crypto, identityManager);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.briarproject.bramble.account;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
@Module
|
||||
public class HeadlessAccountModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
AccountManager provideAccountManager(HeadlessAccountManager accountManager) {
|
||||
return accountManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.briarproject.briar.headless;
|
||||
import org.briarproject.bramble.BrambleCoreModule;
|
||||
import org.briarproject.bramble.account.HeadlessAccountModule;
|
||||
import org.briarproject.bramble.system.DesktopSecureRandomModule;
|
||||
import org.briarproject.briar.BriarCoreModule;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Component;
|
||||
|
||||
@Component(modules = {
|
||||
BrambleCoreModule.class,
|
||||
BriarCoreModule.class,
|
||||
DesktopSecureRandomModule.class,
|
||||
HeadlessAccountModule.class,
|
||||
HeadlessModule.class
|
||||
})
|
||||
@Singleton
|
||||
public interface BriarHeadlessApp {
|
||||
Router router();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.briarproject.briar.headless;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.briar.api.forum.Forum;
|
||||
import org.briarproject.briar.api.forum.ForumManager;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import io.javalin.Context;
|
||||
|
||||
import static io.javalin.translator.json.JavalinJsonPlugin.getObjectToJsonMapper;
|
||||
|
||||
public class ForumController {
|
||||
|
||||
private final ForumManager forumManager;
|
||||
|
||||
@Inject
|
||||
public ForumController(ForumManager forumManager) {
|
||||
this.forumManager = forumManager;
|
||||
}
|
||||
|
||||
public Context list(Context ctx) throws DbException {
|
||||
Collection<Forum> forums = forumManager.getForums();
|
||||
return ctx.result(getObjectToJsonMapper().map(forums));
|
||||
}
|
||||
|
||||
public Context create(Context ctx) throws DbException {
|
||||
String name = ctx.formParam("name");
|
||||
if (name == null || name.length() < 1) {
|
||||
return ctx.status(500).result("Expecting Forum Name");
|
||||
} else {
|
||||
Forum forum = forumManager.addForum(name);
|
||||
return ctx.result(getObjectToJsonMapper().map(forum));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.briarproject.briar.headless;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@NotNullByDefault
|
||||
class HeadlessDatabaseConfig implements DatabaseConfig {
|
||||
|
||||
private final File dbDir, keyDir;
|
||||
|
||||
HeadlessDatabaseConfig(File dbDir, File keyDir) {
|
||||
this.dbDir = dbDir;
|
||||
this.keyDir = keyDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getDatabaseDirectory() {
|
||||
return dbDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getDatabaseKeyDirectory() {
|
||||
return keyDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getMaxSize() {
|
||||
return Long.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package org.briarproject.briar.headless;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.PublicKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.PluginConfig;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.bramble.api.reporting.DevConfig;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Collection;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_ONION_ADDRESS;
|
||||
import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_PUBLIC_KEY_HEX;
|
||||
|
||||
@Module
|
||||
public class HeadlessModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
DatabaseConfig provideDatabaseConfig() {
|
||||
File dbDir = new File("dbDir");
|
||||
File keyDir = new File("keyDir");
|
||||
return new HeadlessDatabaseConfig(dbDir, keyDir);
|
||||
}
|
||||
|
||||
@Provides
|
||||
PluginConfig providePluginConfig() {
|
||||
Collection<DuplexPluginFactory> duplex = emptyList();
|
||||
@NotNullByDefault
|
||||
PluginConfig pluginConfig = new PluginConfig() {
|
||||
|
||||
@Override
|
||||
public Collection<DuplexPluginFactory> getDuplexFactories() {
|
||||
return duplex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SimplexPluginFactory> getSimplexFactories() {
|
||||
return emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldPoll() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
return pluginConfig;
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
DevConfig provideDevConfig(CryptoComponent crypto) {
|
||||
@NotNullByDefault
|
||||
DevConfig devConfig = new DevConfig() {
|
||||
|
||||
@Override
|
||||
public PublicKey getDevPublicKey() {
|
||||
try {
|
||||
return crypto.getMessageKeyParser().parsePublicKey(
|
||||
StringUtils.fromHexString(DEV_PUBLIC_KEY_HEX));
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDevOnionAddress() {
|
||||
return DEV_ONION_ADDRESS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getReportDir() {
|
||||
return new File("reportDir");
|
||||
}
|
||||
};
|
||||
return devConfig;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package org.briarproject.briar.headless;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
DaggerBriarHeadlessApp
|
||||
.create()
|
||||
.router()
|
||||
.start();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package org.briarproject.briar.headless;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import io.javalin.Javalin;
|
||||
|
||||
@Singleton
|
||||
public class Router {
|
||||
|
||||
private final AccountManager accountManager;
|
||||
private final LifecycleManager lifecycleManager;
|
||||
private final ForumController forumController;
|
||||
|
||||
@Inject
|
||||
public Router(AccountManager accountManager,
|
||||
LifecycleManager lifecycleManager,
|
||||
ForumController forumController) {
|
||||
this.accountManager = accountManager;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.forumController = forumController;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if (accountManager.accountExists()) {
|
||||
accountManager.signIn("test");
|
||||
} else {
|
||||
accountManager.createAccount("test", "test");
|
||||
}
|
||||
assert accountManager.getDatabaseKey() != null;
|
||||
|
||||
lifecycleManager.startServices(accountManager.getDatabaseKey());
|
||||
|
||||
Javalin app = Javalin.create()
|
||||
.port(7000)
|
||||
.disableStartupBanner()
|
||||
.enableStandardRequestLogging()
|
||||
.start();
|
||||
app.get("/forums", forumController::list);
|
||||
app.post("/forums", forumController::create);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user