Skip to content
This repository was archived by the owner on Mar 4, 2021. It is now read-only.

Commit 49e841c

Browse files
committedDec 15, 2019
增加了对用户代码的不信任
1 parent c99d597 commit 49e841c

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed
 

‎.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# vscode settings
22
.vscode/
3-
!.vscode/settings.json
3+
4+
# goland settings
5+
.idea/
46

57
# windows
68
*.dll

‎cqp/apis.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ import (
77
sc "golang.org/x/text/encoding/simplifiedchinese"
88
)
99

10+
type Priority int32
11+
1012
// Log优先级(priority, AddLog的第一个参数)
1113
const (
12-
Debug = 0
13-
Info = 10
14-
InfoSuccess = 11
15-
InfoRecv = 12
16-
InfoSend = 13
17-
Warning = 20
18-
Error = 30
19-
Fatal = 40
14+
Debug Priority = 0
15+
Info = 10
16+
InfoSuccess = 11
17+
InfoRecv = 12
18+
InfoSend = 13
19+
Warning = 20
20+
Error = 30
21+
Fatal = 40
2022
)
2123

2224
func cString(str string) *C.char {
@@ -40,9 +42,9 @@ func cBool(b bool) C.int32_t {
4042
// priority 是Log的优先级,请使用cqp预定义好的值。
4143
// logType 是日志类型,酷Q日志窗口将将其显示在日志本身的前一列。
4244
// reason 是日志内容
43-
func AddLog(priority int32, logType, reason string) int32 {
45+
func AddLog(p Priority, logType, reason string) int32 {
4446
return int32(C.CQ_addLog(
45-
C.int32_t(priority),
47+
C.int32_t(p),
4648
cString(logType),
4749
cString(reason),
4850
))

‎cqp/events.go

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package cqp
22

33
// #include "events.h"
44
import "C"
5+
import (
6+
"fmt"
7+
"runtime/debug"
8+
)
59

610
//export _appinfo
711
func _appinfo() *C.char { return C.CString("9," + AppID) }
@@ -11,6 +15,7 @@ func _on_enable() int32 {
1115
if Enable == nil {
1216
return 0
1317
}
18+
defer panicToFatal()
1419
return Enable()
1520
}
1621

@@ -19,6 +24,7 @@ func _on_disable() int32 {
1924
if Disable == nil {
2025
return 0
2126
}
27+
defer panicToFatal()
2228
return Disable()
2329
}
2430

@@ -27,6 +33,7 @@ func _on_start() int32 {
2733
if Start == nil {
2834
return 0
2935
}
36+
defer panicToFatal()
3037
return Start()
3138
}
3239

@@ -35,6 +42,7 @@ func _on_exit() int32 {
3542
if Exit == nil {
3643
return 0
3744
}
45+
defer panicToFatal()
3846
return Exit()
3947
}
4048

@@ -43,6 +51,7 @@ func _on_private_msg(subType, msgID int32, fromQQ int64, msg *C.char, font int32
4351
if PrivateMsg == nil {
4452
return 0
4553
}
54+
defer panicToFatal()
4655
return PrivateMsg(subType, msgID, fromQQ, goString(msg), font)
4756
}
4857

@@ -51,6 +60,7 @@ func _on_group_msg(subType, msgID int32, fromGroup, fromQQ int64, fromAnonymous,
5160
if GroupMsg == nil {
5261
return 0
5362
}
63+
defer panicToFatal()
5464
return GroupMsg(subType, msgID, fromGroup, fromQQ, goString(fromAnonymous), goString(msg), font)
5565
}
5666

@@ -59,6 +69,7 @@ func _on_discuss_msg(sub_type, msg_id int32, from_discuss, from_qq int64, msg *C
5969
if DiscussMsg == nil {
6070
return 0
6171
}
72+
defer panicToFatal()
6273
return DiscussMsg(sub_type, msg_id, from_discuss, from_qq, goString(msg), font)
6374
}
6475

@@ -67,6 +78,7 @@ func _on_group_upload(sub_type, send_time int32, from_group, from_qq int64, file
6778
if GroupUpload == nil {
6879
return 0
6980
}
81+
defer panicToFatal()
7082
return GroupUpload(sub_type, send_time, from_group, from_qq, goString(file))
7183
}
7284

@@ -75,6 +87,7 @@ func _on_group_admin(sub_type, send_time int32, from_group, being_operate_qq int
7587
if GroupAdmin == nil {
7688
return 0
7789
}
90+
defer panicToFatal()
7891
return GroupAdmin(sub_type, send_time, from_group, being_operate_qq)
7992
}
8093

@@ -83,6 +96,7 @@ func _on_group_member_decrease(sub_type, send_time int32, from_group, from_qq, b
8396
if GroupMemberDecrease == nil {
8497
return 0
8598
}
99+
defer panicToFatal()
86100
return GroupMemberDecrease(sub_type, send_time, from_group, from_qq, being_operate_qq)
87101
}
88102

@@ -91,6 +105,7 @@ func _on_group_member_increase(sub_type, send_time int32, from_group, from_qq, b
91105
if GroupMemberIncrease == nil {
92106
return 0
93107
}
108+
defer panicToFatal()
94109
return GroupMemberIncrease(sub_type, send_time, from_group, from_qq, being_operate_qq)
95110
}
96111

@@ -99,6 +114,7 @@ func _on_friend_add(sub_type, send_time int32, from_qq int64) int32 {
99114
if FriendAdd == nil {
100115
return 0
101116
}
117+
defer panicToFatal()
102118
return FriendAdd(sub_type, send_time, from_qq)
103119
}
104120

@@ -107,6 +123,7 @@ func _on_friend_request(sub_type, send_time int32, from_qq int64, msg, response_
107123
if FriendRequest == nil {
108124
return 0
109125
}
126+
defer panicToFatal()
110127
return FriendRequest(sub_type, send_time, from_qq, goString(msg), goString(response_flag))
111128
}
112129

@@ -115,6 +132,7 @@ func _on_group_request(sub_type, send_time int32, from_group, from_qq int64, msg
115132
if GroupRequest == nil {
116133
return 0
117134
}
135+
defer panicToFatal()
118136
return GroupRequest(sub_type, send_time, from_group, from_qq, goString(msg), goString(response_flag))
119137
}
120138

@@ -151,3 +169,11 @@ var GroupMemberIncrease func(subType, sendTime int32, fromGroup, fromQQ, beingOp
151169
var FriendAdd func(subType, sendTime int32, fromQQ int64) int32
152170
var FriendRequest func(subType, sendTime int32, fromQQ int64, msg, responseFlag string) int32
153171
var GroupRequest func(subType, sendTime int32, fromGroup, fromQQ int64, msg, responseFlag string) int32
172+
173+
// 捕获panic并调用AddLog(Fatal)
174+
func panicToFatal() {
175+
if v := recover(); v != nil {
176+
// 在这里调用debug.Stack()获取调用栈
177+
AddLog(Fatal, "go-panic", fmt.Sprintf("%v\n%s", v, debug.Stack()))
178+
}
179+
}

0 commit comments

Comments
 (0)
This repository has been archived.