149 lines
4.3 KiB
Java
149 lines
4.3 KiB
Java
package it.cavallium;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import io.vertx.core.impl.ConcurrentHashSet;
|
|
import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.time.Duration;
|
|
import java.util.HashSet;
|
|
import java.util.Optional;
|
|
import java.util.Set;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import org.slf4j.event.Level;
|
|
|
|
public class SettingsServiceImpl implements SettingsService {
|
|
|
|
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
private final AtomicReference<Duration> delayBetweenAdds = new AtomicReference<>(Duration.ZERO);
|
|
private final AtomicReference<String> proxyIp = new AtomicReference<>("proxy.digitalresistance.dog");
|
|
private final AtomicReference<Integer> proxyPort = new AtomicReference<>(443);
|
|
private final AtomicReference<String> proxySecret = new AtomicReference<>("d41d8cd98f00b204e9800998ecf8427e");
|
|
private final ConcurrentHashSet<Long> phoneNumbers = new ConcurrentHashSet<>();
|
|
|
|
public SettingsServiceImpl() {
|
|
synchronized (gson) {
|
|
if (Files.exists(Paths.get("settings.json"))) {
|
|
try {
|
|
var settings = gson.fromJson(Files.readString(Paths.get("settings.json"), StandardCharsets.UTF_8), JsonObject.class);
|
|
if (settings != null) {
|
|
Optional.ofNullable(settings.getAsJsonPrimitive("delayBetweenAdds")).ifPresent(seconds -> {
|
|
this.delayBetweenAdds.set(Duration.ofSeconds(seconds.getAsInt()));
|
|
});
|
|
Optional.ofNullable(settings.getAsJsonArray("phoneNumbers")).ifPresent(phoneNumbers -> {
|
|
this.phoneNumbers.clear();
|
|
for (JsonElement phoneNumber : phoneNumbers) {
|
|
this.phoneNumbers.add(Long.parseLong(phoneNumber.getAsString()));
|
|
}
|
|
});
|
|
Optional.ofNullable(settings.getAsJsonPrimitive("proxyIp")).ifPresent(value -> {
|
|
this.proxyIp.set(value.getAsString());
|
|
});
|
|
Optional.ofNullable(settings.getAsJsonPrimitive("proxyPort")).ifPresent(value -> {
|
|
this.proxyPort.set(value.getAsInt());
|
|
});
|
|
Optional.ofNullable(settings.getAsJsonPrimitive("proxySecret")).ifPresent(value -> {
|
|
this.proxySecret.set(value.getAsString());
|
|
});
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
private void saveSettings() {
|
|
synchronized (gson) {
|
|
var settings = new JsonObject();
|
|
settings.addProperty("delayBetweenAdds", delayBetweenAdds.get().toSeconds());
|
|
var phoneNumbers = new JsonArray();
|
|
for (Long phoneNumber : this.phoneNumbers) {
|
|
phoneNumbers.add("" + phoneNumber);
|
|
}
|
|
settings.add("phoneNumbers", phoneNumbers);
|
|
settings.addProperty("proxyIp", proxyIp.get());
|
|
settings.addProperty("proxyPort", proxyPort.get());
|
|
settings.addProperty("proxySecret", proxySecret.get());
|
|
try {
|
|
Files.writeString(Paths.get("settings.json"), gson.toJson(settings), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
|
|
} catch (IOException e) {
|
|
App.getLogService().append(Level.ERROR, e.getLocalizedMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void setDelayBetweenAdds(Duration duration) {
|
|
delayBetweenAdds.set(duration);
|
|
saveSettings();
|
|
}
|
|
|
|
@Override
|
|
public Duration getDelayBetweenAdds() {
|
|
return delayBetweenAdds.get();
|
|
}
|
|
|
|
@Override
|
|
public void addPhoneNumber(Long phoneNumber) {
|
|
phoneNumbers.add(phoneNumber);
|
|
saveSettings();
|
|
}
|
|
|
|
@Override
|
|
public void removePhoneNumber(Long phoneNumber) {
|
|
phoneNumbers.remove(phoneNumber);
|
|
saveSettings();
|
|
}
|
|
|
|
@Override
|
|
public void clearPhoneNumbers() {
|
|
phoneNumbers.clear();
|
|
saveSettings();
|
|
}
|
|
|
|
@Override
|
|
public Set<Long> getPhoneNumbers() {
|
|
return new HashSet<>(phoneNumbers);
|
|
}
|
|
|
|
@Override
|
|
public String getProxyIp() {
|
|
return proxyIp.get();
|
|
}
|
|
|
|
@Override
|
|
public void setProxyIp(String value) {
|
|
this.proxyIp.set(value);
|
|
saveSettings();
|
|
}
|
|
|
|
@Override
|
|
public Integer getProxyPort() {
|
|
return proxyPort.get();
|
|
}
|
|
|
|
@Override
|
|
public void setProxyPort(int value) {
|
|
this.proxyPort.set(value);
|
|
saveSettings();
|
|
}
|
|
|
|
@Override
|
|
public String getProxySecret() {
|
|
return proxySecret.get();
|
|
}
|
|
|
|
@Override
|
|
public void setProxySecret(String value) {
|
|
this.proxySecret.set(value);
|
|
saveSettings();
|
|
}
|
|
}
|