Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void publishAlert(boolean push, @NotNull AlertLevel level, @NotNull Strin
alertEntity.setCreateAt(OffsetDateTime.now());
alertDao.saveOrUpdate(alertEntity);
if (push) {
if (!pushManager.pushMessage("[PeerBanHelper/" + level.name() + "] " + tlUI(title), tlUI(content))) {
if (!pushManager.pushMessage("[PeerBanHelper/" + level.name() + "] " + tlUI(title), tlUI(content), level)) {
log.error(tlUI(Lang.UNABLE_TO_PUSH_ALERT_VIA_PROVIDERS));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.module.impl.webapi;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.ghostchu.peerbanhelper.module.AbstractFeatureModule;
import com.ghostchu.peerbanhelper.module.impl.webapi.dto.PushWrapperDTO;
import com.ghostchu.peerbanhelper.text.Lang;
Expand Down Expand Up @@ -135,7 +136,7 @@ private void handlePushProviderTest(Context ctx) {
return;
}
try {
var testResult = pushProvider.push(tl(locale(ctx), Lang.PUSH_PROVIDER_TEST_TITLE), tl(locale(ctx), Lang.PUSH_PROVIDER_TEST_DESCRIPTION, name, pushProvider.getConfigType()));
var testResult = pushProvider.push(tl(locale(ctx), Lang.PUSH_PROVIDER_TEST_TITLE), tl(locale(ctx), Lang.PUSH_PROVIDER_TEST_DESCRIPTION, name, pushProvider.getConfigType()), AlertLevel.INFO);
if (testResult) {
ctx.json(new StdResp(true, tl(locale(ctx), Lang.PUSH_PROVIDER_API_TEST_OK), null));
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.google.gson.JsonObject;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;

Expand All @@ -16,7 +17,7 @@ public interface PushManager {

void savePushProviders() throws IOException;

boolean pushMessage(String title, String description);
boolean pushMessage(String title, String description, AlertLevel level);

java.util.List<PushProvider> getProviderList();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.ghostchu.peerbanhelper.util.push;

import com.ghostchu.peerbanhelper.Main;
import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.ghostchu.peerbanhelper.text.Lang;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.push.impl.*;
Expand Down Expand Up @@ -47,6 +48,8 @@ public PushProvider createPushProvider(String name, String type, ConfigurationSe
case "pushdeer" -> provider = PushDeerPushProvider.loadFromYaml(name, section, httpUtil);
case "gotify" -> provider = GotifyPushProvider.loadFromYaml(name, section, httpUtil);
case "ntfy" -> provider = NtfyPushProvider.loadFromYaml(name, section, httpUtil);
case "webhook" -> provider = WebhookPushProvider.loadFromYaml(name, section, httpUtil);
default -> provider = null;
Comment on lines +51 to +52
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

YAML 加载分支不要把未知类型降级成 null

reloadConfig() 会把这里的返回值直接加入 providerList。配置里只要出现拼写错误或遗留类型,后续列出渠道、保存配置或批量推送时都会在解引用 pushProvider 时触发 NPE。这里应直接抛出带 type 的异常,或在加载阶段显式跳过 null

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/com/ghostchu/peerbanhelper/util/push/PushManagerImpl.java`
around lines 51 - 52, The YAML loader currently assigns null for unknown
provider types in the switch inside PushManagerImpl (the branch that calls
WebhookPushProvider.loadFromYaml), which later causes NPEs when reloadConfig()
adds entries to providerList; change the default branch so it throws a
descriptive IllegalArgumentException (or a custom ConfigParseException) that
includes the unknown "type" and the provider "name" instead of returning null,
so reloadConfig()/pushProvider handling fails fast with a clear error message
rather than allowing nulls into providerList.

}
return provider;
}
Expand All @@ -63,6 +66,8 @@ public PushProvider createPushProvider(String name, String type, JsonObject json
case "pushdeer" -> provider = PushDeerPushProvider.loadFromJson(name, jsonObject, httpUtil);
case "gotify" -> provider = GotifyPushProvider.loadFromJson(name, jsonObject, httpUtil);
case "ntfy" -> provider = NtfyPushProvider.loadFromJson(name, jsonObject, httpUtil);
case "webhook" -> provider = WebhookPushProvider.loadFromJson(name, jsonObject, httpUtil);
default -> provider = null;
}
return provider;
}
Expand Down Expand Up @@ -103,11 +108,11 @@ public void savePushProviders() throws IOException {
}

@Override
public boolean pushMessage(String title, String description) {
public boolean pushMessage(String title, String description, AlertLevel level) {
AtomicBoolean anySuccess = new AtomicBoolean(false);
providerList.forEach(pushProvider -> {
try {
if (pushProvider.push(title, description)) {
if (pushProvider.push(title, description, level)) {
anySuccess.set(true);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.google.gson.JsonObject;
import org.bspfsystems.yamlconfiguration.configuration.ConfigurationSection;

Expand All @@ -8,6 +9,6 @@ public interface PushProvider {
String getConfigType();
JsonObject saveJson();
ConfigurationSection saveYaml();
boolean push(String title, String content);
boolean push(String title, String content, AlertLevel level);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.ghostchu.peerbanhelper.util.push.AbstractPushProvider;
Expand Down Expand Up @@ -67,7 +68,7 @@ public static BarkPushProvider loadFromYaml(String name, ConfigurationSection se
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {
Map<String, Object> map = new HashMap<>();
map.put("title", title);
map.put("body", content);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.ghostchu.peerbanhelper.util.push.AbstractPushProvider;
Expand Down Expand Up @@ -64,7 +65,7 @@ public static GotifyPushProvider loadFromYaml(String name, ConfigurationSection
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {
Map<String, Object> map = new HashMap<>();
map.put("title", title);
map.put("message", content);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.ghostchu.peerbanhelper.util.push.AbstractPushProvider;
Expand Down Expand Up @@ -71,7 +72,7 @@ public static NtfyPushProvider loadFromYaml(String name, ConfigurationSection se
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {

RequestBody requestBody = RequestBody.create(
content,
Expand All @@ -80,12 +81,14 @@ public boolean push(String title, String content) {

String encodedTitle = "=?UTF-8?B?" + Base64.getEncoder().encodeToString(title.getBytes(StandardCharsets.UTF_8)) + "?=";

int priority = mapLevelToPriority(level);

Request request = new Request.Builder()
.url(config.getServerUrl() + "/" + config.getTopic())
.post(requestBody)
.header("Authorization", "Bearer " + config.getToken())
.header("Title", encodedTitle)
.header("Priority", String.valueOf(config.getPriority()))
.header("Priority", String.valueOf(priority))
.header("Tags", config.getTags())
.header("Icon", "https://raw.githubusercontent.com/PBH-BTN/PeerBanHelper/refs/heads/master/src/main/resources/assets/icon.png")
.build();
Expand All @@ -100,6 +103,16 @@ public boolean push(String title, String content) {
return true;
}

private int mapLevelToPriority(AlertLevel level) {
return switch (level) {
case TIP -> 1;
case INFO -> 2;
case WARN -> 3;
case ERROR -> 4;
case FATAL -> 5;
};
}

@AllArgsConstructor
@Data
public static class Config {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.ghostchu.peerbanhelper.util.push.AbstractPushProvider;
Expand Down Expand Up @@ -65,7 +66,7 @@ public static PushDeerPushProvider loadFromYaml(String name, ConfigurationSectio
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {
Map<String, Object> map = new HashMap<>();
map.put("pushkey", config.getPushKey());
map.put("text", title);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
Expand Down Expand Up @@ -76,7 +77,7 @@ public String getConfigType() {
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {
Map<String, Object> args = new HashMap<>() {{
put("token", config.getToken());
if (config.getTopic() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static ServerChanPushProvider loadFromYaml(String name, ConfigurationSect
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {
Map<String, Object> map = new HashMap<>();
map.put("title", title);
map.put("desp", content);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
import com.ghostchu.peerbanhelper.util.push.AbstractPushProvider;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -142,7 +143,7 @@ public String getConfigType() {
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {
try {
sendMail(config.getReceivers(), title, content);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ghostchu.peerbanhelper.util.push.impl;

import com.ghostchu.peerbanhelper.alert.AlertLevel;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ghostchu.peerbanhelper.util.HTTPUtil;
import com.ghostchu.peerbanhelper.util.json.JsonUtil;
Expand Down Expand Up @@ -66,7 +67,7 @@ public ConfigurationSection saveYaml() {
}

@Override
public boolean push(String title, String content) {
public boolean push(String title, String content, AlertLevel level) {
String markdown = "*" + title + "*\n" + content;
Map<String, Object> map = new HashMap<>();
map.put("chat_id", config.getChatId());
Expand Down
Loading
Loading