рефакторинг служб

This commit is contained in:
kirillius 2025-10-06 14:21:45 +03:00
parent adbd4ea6d4
commit d9aec2e986
7 changed files with 155 additions and 17 deletions

View File

@ -89,6 +89,7 @@
<artifactId>exec-maven-plugin</artifactId> <artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version> <version>3.5.0</version>
<executions> <executions>
<execution> <execution>
<phase>prepare-package</phase> <phase>prepare-package</phase>
<goals> <goals>
@ -102,6 +103,37 @@
</arguments> </arguments>
</configuration> </configuration>
</execution> </execution>
<execution>
<id>npm-install</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>install</argument>
</arguments>
<workingDirectory>${project.basedir}/../webui</workingDirectory>
</configuration>
</execution>
<execution>
<id>npm-build</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>run</argument>
<argument>build</argument>
</arguments>
<workingDirectory>${project.basedir}/../webui</workingDirectory>
</configuration>
</execution>
</executions> </executions>
</plugin> </plugin>

View File

@ -5,12 +5,14 @@ import lombok.SneakyThrows;
import ru.kirillius.pf.sdn.External.API.Components.FRR; import ru.kirillius.pf.sdn.External.API.Components.FRR;
import ru.kirillius.pf.sdn.External.API.Components.OVPN; import ru.kirillius.pf.sdn.External.API.Components.OVPN;
import ru.kirillius.pf.sdn.External.API.Components.TDNS; import ru.kirillius.pf.sdn.External.API.Components.TDNS;
import ru.kirillius.pf.sdn.External.API.GitSubscription;
import ru.kirillius.pf.sdn.External.API.HEInfoProvider; import ru.kirillius.pf.sdn.External.API.HEInfoProvider;
import ru.kirillius.pf.sdn.core.*; import ru.kirillius.pf.sdn.core.*;
import ru.kirillius.pf.sdn.core.Auth.AuthManager; import ru.kirillius.pf.sdn.core.Auth.AuthManager;
import ru.kirillius.pf.sdn.core.Auth.TokenService; import ru.kirillius.pf.sdn.core.Auth.TokenService;
import ru.kirillius.pf.sdn.core.Networking.BGPInfoService; import ru.kirillius.pf.sdn.core.Networking.BGPInfoService;
import ru.kirillius.pf.sdn.core.Networking.NetworkingService; import ru.kirillius.pf.sdn.core.Networking.NetworkingService;
import ru.kirillius.pf.sdn.core.Subscription.RepositoryConfig;
import ru.kirillius.pf.sdn.core.Subscription.SubscriptionService; import ru.kirillius.pf.sdn.core.Subscription.SubscriptionService;
import ru.kirillius.pf.sdn.core.Util.Wait; import ru.kirillius.pf.sdn.core.Util.Wait;
import ru.kirillius.pf.sdn.web.WebService; import ru.kirillius.pf.sdn.web.WebService;
@ -19,6 +21,7 @@ import ru.kirillius.utils.logging.SystemLogger;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level; import java.util.logging.Level;
@ -55,6 +58,9 @@ public class App implements Context, Closeable {
loadedConfig = Config.load(launcherConfig.getConfigFile()); loadedConfig = Config.load(launcherConfig.getConfigFile());
} catch (IOException e) { } catch (IOException e) {
loadedConfig = new Config(); loadedConfig = new Config();
loadedConfig.setSubscriptions(new ArrayList<>(List.of(
new RepositoryConfig("updates", GitSubscription.class, "https://git.kirillius.ru/kirillius/protected-resources-list.git")
)));
try { try {
Config.store(loadedConfig, launcherConfig.getConfigFile()); Config.store(loadedConfig, launcherConfig.getConfigFile());
} catch (IOException ex) { } catch (IOException ex) {

View File

@ -29,6 +29,10 @@ public class InMemoryLogHandler extends Handler {
return Collections.unmodifiableCollection(queue); return Collections.unmodifiableCollection(queue);
} }
public static void clear() {
queue.clear();
}
/** /**
* Stores the formatted message in the buffer, trimming old entries when full. * Stores the formatted message in the buffer, trimming old entries when full.
*/ */

View File

@ -36,6 +36,12 @@ public class Auth implements RPC {
return JSONUtility.serializeCollection(context.getServiceManager().getService(TokenService.class).getTokens(), AuthToken.class, null); return JSONUtility.serializeCollection(context.getServiceManager().getService(TokenService.class).getTokens(), AuthToken.class, null);
} }
@ProtectedMethod
@JRPCMethod
public void changePassword(@JRPCArgument(name = "password") String password) {
context.getServiceManager().getService(AuthManager.class).updatePassword(password);
}
/** /**
* Removes a token identified by its raw value. * Removes a token identified by its raw value.
*/ */

View File

@ -5,6 +5,8 @@ import org.json.JSONObject;
import ru.kirillius.json.JSONUtility; import ru.kirillius.json.JSONUtility;
import ru.kirillius.json.rpc.Annotations.JRPCArgument; import ru.kirillius.json.rpc.Annotations.JRPCArgument;
import ru.kirillius.json.rpc.Annotations.JRPCMethod; import ru.kirillius.json.rpc.Annotations.JRPCMethod;
import ru.kirillius.pf.sdn.External.API.GitSubscription;
import ru.kirillius.pf.sdn.InMemoryLogHandler;
import ru.kirillius.pf.sdn.core.*; import ru.kirillius.pf.sdn.core.*;
import ru.kirillius.pf.sdn.web.ProtectedMethod; import ru.kirillius.pf.sdn.web.ProtectedMethod;
@ -51,6 +53,14 @@ public class System implements RPC {
return JSONUtility.serializeStructure(context.getConfig()); return JSONUtility.serializeStructure(context.getConfig());
} }
@ProtectedMethod
@JRPCMethod
public void setConfig(@JRPCArgument(name = "config") JSONObject json) {
var config = JSONUtility.deserializeStructure(json, Config.class);
context.getConfig().merge(config);
}
/** /**
* Indicates whether the configuration has unsaved changes. * Indicates whether the configuration has unsaved changes.
*/ */
@ -109,6 +119,14 @@ public class System implements RPC {
updateService.updateApp(); updateService.updateApp();
} }
@ProtectedMethod
@JRPCMethod
public JSONArray getRepositoryTypes() {
var array = new JSONArray();
array.put(GitSubscription.class.getName());
return array;
}
/** /**
* Returns the list of enabled component class names. * Returns the list of enabled component class names.
*/ */
@ -171,4 +189,16 @@ public class System implements RPC {
context.getServiceManager().getService(ComponentHandlerService.class).reloadComponents(cls); context.getServiceManager().getService(ComponentHandlerService.class).reloadComponents(cls);
context.getConfig().getComponentsConfig().getConfig(cls); context.getConfig().getComponentsConfig().getConfig(cls);
} }
@ProtectedMethod
@JRPCMethod
public JSONArray getLogs() {
return JSONUtility.serializeCollection(InMemoryLogHandler.getMessages(), String.class, null);
}
@ProtectedMethod
@JRPCMethod
public void clearLogs() {
InMemoryLogHandler.clear();
}
} }

View File

@ -1,10 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
test ok
</body>
</html>

View File

@ -24,16 +24,25 @@ import java.util.UUID;
@NoArgsConstructor @NoArgsConstructor
@JSONSerializable @JSONSerializable
public class Config { public class Config {
/**
* Update subscriptions every N hours
*/
@Getter @Getter
@Setter @Setter
@JSONProperty @JSONProperty
private volatile int updateSubscriptionsInterval = 6; private volatile int updateSubscriptionsInterval = 6;
/**
* Remember downloaded ASN prefixes
*/
@Getter @Getter
@Setter @Setter
@JSONProperty @JSONProperty
private volatile boolean cachingAS = true; private volatile boolean cachingAS = true;
/**
* Update ASN prefixes every N hours
*/
@Getter @Getter
@Setter @Setter
@JSONProperty @JSONProperty
@ -41,77 +50,138 @@ public class Config {
@Getter @Getter
private volatile File loadedConfigFile = null; private volatile File loadedConfigFile = null;
/**
* Web interface host
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile String host = "0.0.0.0"; private volatile String host = "0.0.0.0";
/**
* Path where to store temporary data
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile File cacheDirectory = new File("./.cache"); private volatile File cacheDirectory = new File("./.cache");
/**
* List of subscription sources
*/
@Setter @Setter
@Getter @Getter
@JSONArrayProperty(type = RepositoryConfig.class) @JSONArrayProperty(type = RepositoryConfig.class)
private volatile List<RepositoryConfig> subscriptions = Collections.emptyList(); private volatile List<RepositoryConfig> subscriptions = new ArrayList<>();
/**
* List of subscribed resources
*/
@Setter @Setter
@Getter @Getter
@JSONArrayProperty(type = String.class) @JSONArrayProperty(type = String.class)
private volatile List<String> subscribedResources = Collections.emptyList(); private volatile List<String> subscribedResources = Collections.emptyList();
/**
* Config of every component
*/
@Setter @Setter
@Getter @Getter
@JSONProperty(required = false) @JSONProperty(required = false)
private volatile ComponentConfigStorage componentsConfig = new ComponentConfigStorage(); private volatile ComponentConfigStorage componentsConfig = new ComponentConfigStorage();
/**
* List of enabled components
*/
@Setter @Setter
@Getter @Getter
@JSONArrayProperty(type = Class.class, required = false) @JSONArrayProperty(type = Class.class, required = false)
private volatile List<Class<? extends Component<?>>> enabledComponents = new ArrayList<>(); private volatile List<Class<? extends Component<?>>> enabledComponents = new ArrayList<>();
/**
* Special hash salt for password hashing
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile String passwordSalt = UUID.randomUUID().toString(); private volatile String passwordSalt = UUID.randomUUID().toString();
/**
* Password hash
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile String passwordHash = ""; private volatile String passwordHash = "";
/**
* Web service listen port
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile int httpPort = 8081; private volatile int httpPort = 8081;
/**
* Use special algorithm to minify prefix count
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile boolean mergeSubnets = true; private volatile boolean mergeSubnets = true;
/**
* Display debug info in logs
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile boolean displayDebuggingInfo = true; private volatile boolean displayDebuggingInfo = true;
/**
* Merge two subnets into one bigger subnet when new subnet will be used more or equals this percents
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile int mergeSubnetsWithUsage = 51; private volatile int mergeSubnetsWithUsage = 51;
/**
* Custom routing config
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile NetworkResourceBundle customResources = new NetworkResourceBundle(); private volatile NetworkResourceBundle customResources = new NetworkResourceBundle();
/**
* Ignore these resources
*/
@Setter @Setter
@Getter @Getter
@JSONProperty @JSONProperty
private volatile NetworkResourceBundle filteredResources = new NetworkResourceBundle(); private volatile NetworkResourceBundle filteredResources = new NetworkResourceBundle();
public void merge(Config other) {
if (other == null) {
return;
}
setUpdateSubscriptionsInterval(other.getUpdateSubscriptionsInterval());
setCachingAS(other.isCachingAS());
setUpdateASInterval(other.getUpdateASInterval());
setHost(other.getHost());
setCacheDirectory(other.getCacheDirectory());
setSubscriptions(other.getSubscriptions());
setSubscribedResources(other.getSubscribedResources());
setEnabledComponents(other.getEnabledComponents());
setPasswordSalt(other.getPasswordSalt());
setPasswordHash(other.getPasswordHash());
setHttpPort(other.getHttpPort());
setMergeSubnets(other.isMergeSubnets());
setDisplayDebuggingInfo(other.isDisplayDebuggingInfo());
setMergeSubnetsWithUsage(other.getMergeSubnetsWithUsage());
setCustomResources(other.getCustomResources());
setFilteredResources(other.getFilteredResources());
}
/** /**
* Persists the given configuration into the supplied file as JSON. * Persists the given configuration into the supplied file as JSON.
*/ */