Skip to content

Commit 023654e

Browse files
committed
feat(network): 更新 webhook 路径配置并优化 token 管理
- 将 BaseConnectedEvent 版本号从 1.5.2-L2 升级到 1.5.2-L3 - 修改 HookAuth 中的 webhook 服务器路径配置 -优化 Start0 中的 token 获取和更新逻辑 - 在 Starter 中添加 webhookpath 配置项
1 parent f6a8637 commit 023654e

6 files changed

Lines changed: 27 additions & 23 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ Maven
2727
<dependency>
2828
<groupId>io.github.kloping</groupId>
2929
<artifactId>bot-qqpd-java</artifactId>
30-
<version>1.5.2-L2</version>
30+
<version>1.5.2-L3</version>
3131
</dependency>
3232
```
3333

3434
Gradle
3535

36-
implementation 'io.github.kloping:bot-qqpd-java:1.5.2-L2'
36+
implementation 'io.github.kloping:bot-qqpd-java:1.5.2-L3'
3737

3838
### 使用前提
3939

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.kloping</groupId>
88
<artifactId>bot-qqpd-java</artifactId>
9-
<version>1.5.2-L2</version>
9+
<version>1.5.2-L3</version>
1010

1111
<packaging>jar</packaging>
1212
<name>bot-qqpd-java</name>

src/main/java/io/github/kloping/qqbot/Start0.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,43 @@
1717
public class Start0 {
1818

1919
public Map<String, String> headers = new HashMap();
20-
public Map<String, String> v2headers = new HashMap();
2120

2221
@AutoStand
2322
ContextManager contextManager;
2423

2524
//ALL req token header
2625
public synchronized Map<String, String> getHeaders() {
27-
if (isExpired(token)) headers.clear();
28-
if (headers.isEmpty()) {
29-
headers.put("Authorization", String.format("QQBot %s", getV2Token()));
30-
headers.put("Accept-Encoding", "application/json");
31-
headers.put("X-Union-Appid", contextManager.getContextEntity(String.class, Starter.APPID_ID));
32-
}
26+
if (headers.isEmpty() || isExpired(token)) updateToken();
3327
return headers;
3428
}
3529

3630
private boolean isExpired(Token token) {
3731
return token == null || token.isExpired();
3832
}
3933

40-
@AutoStand
41-
AuthV2Base authV2Base;
34+
private Token token;
4235

36+
public String getAccessToken() {
37+
if (isExpired(token)) updateToken();
38+
return token.getAccess_token();
39+
}
4340

44-
private Token token;
41+
public void updateToken() {
42+
headers.clear();
43+
String v2token = getV2Token();
44+
headers.put("Authorization", String.format("QQBot %s", v2token));
45+
headers.put("Accept-Encoding", "application/json");
46+
headers.put("X-Union-Appid", contextManager.getContextEntity(String.class, Starter.APPID_ID));
47+
}
48+
49+
@AutoStand
50+
AuthV2Base authV2Base;
4551

4652
private String getV2Token() {
4753
String appid = contextManager.getContextEntity(String.class, Starter.APPID_ID);
4854
String secret = contextManager.getContextEntity(String.class, Starter.SECRET_ID);
49-
token = authV2Base.auth(
50-
String.format("{\"appId\": \"%s\",\"clientSecret\": \"%s\"}\n", appid, secret)
51-
, Channel.SEND_MESSAGE_HEADERS);
52-
token.setT0(System.currentTimeMillis());
55+
token = authV2Base.auth(String.format("{\"appId\": \"%s\",\"clientSecret\": \"%s\"}\n", appid, secret)
56+
, Channel.SEND_MESSAGE_HEADERS).setT0(System.currentTimeMillis());
5357
return token.getAccess_token();
5458
}
55-
56-
public String getAccessToken() {
57-
return isExpired(token) ? getV2Token() : token.getAccess_token();
58-
}
5959
}

src/main/java/io/github/kloping/qqbot/Starter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ public static class Config {
199199
private Boolean anyCloseReconnect = false;
200200
private String wslink = null;
201201
private Integer webhookport = 0;
202+
/**
203+
* webhook服务路径 默认/webhook0
204+
*/
205+
private String webhookpath = "/webhook0";
202206
private Set<ListenerHost> listenerHosts = new HashSet<>();
203207
private ImageUploadInterceptor interceptor0;
204208
private WebSocketListener webSocketListener;

src/main/java/io/github/kloping/qqbot/impl/BaseConnectedEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public String getId() {
5050
return getMetadata().get("id").toString();
5151
}
5252

53-
public static final String VERSION = "1.5.2-L2";
53+
public static final String VERSION = "1.5.2-L3";
5454
public static final String PROJECT_NAME = "qqpd-bot-java";
5555
public static final String AUTHOR = "kloping";
5656
public static final String FORMAT = "Bot(%s) connected! By " + AUTHOR + " of " + PROJECT_NAME + " v" + VERSION;

src/main/java/io/github/kloping/qqbot/network/hookauth/HookAuth.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class HookAuth {
5656
public void webhookServerStart() {
5757
try {
5858
HttpServer server = HttpServer.create(new InetSocketAddress(config.getWebhookport()), 0);
59-
server.createContext("/webhook0", exchange -> {
59+
server.createContext(config.getWebhookpath(), exchange -> {
6060
String body = ReadUtils.readAll(exchange.getRequestBody(), "UTF-8");
6161
logger.log(String.format("webhook-r: %s", body));
6262
Pack pack = GSON.fromJson(body, Pack.class);

0 commit comments

Comments
 (0)