-
Notifications
You must be signed in to change notification settings - Fork 34
/
main.go
176 lines (151 loc) · 4.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// nolint
package main
import (
"fmt"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/LagrangeDev/LagrangeGo/client"
"github.com/LagrangeDev/LagrangeGo/client/auth"
"github.com/LagrangeDev/LagrangeGo/message"
"github.com/LagrangeDev/LagrangeGo/utils"
"github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
)
var (
dumpspath = "dump"
)
func main() {
appInfo := auth.AppList["linux"]["3.2.10-25765"]
deviceInfo := &auth.DeviceInfo{
Guid: "cfcd208495d565ef66e7dff9f98764da",
DeviceName: "Lagrange-DCFCD07E",
SystemKernel: "Windows 10.0.22631",
KernelVersion: "10.0.22631",
}
qqclient := client.NewClient(0, appInfo, "https://sign.lagrangecore.org/api/sign")
qqclient.SetLogger(protocolLogger{})
qqclient.UseDevice(deviceInfo)
data, err := os.ReadFile("sig.bin")
if err != nil {
logrus.Warnln("read sig error:", err)
} else {
sig, err := auth.UnmarshalSigInfo(data, true)
if err != nil {
logrus.Warnln("load sig error:", err)
} else {
qqclient.UseSig(sig)
}
}
qqclient.GroupMessageEvent.Subscribe(func(client *client.QQClient, event *message.GroupMessage) {
if event.ToString() == "114514" {
img, _ := message.NewFileImage("testgroup.png")
_, err := client.SendGroupMessage(event.GroupUin, []message.IMessageElement{img})
if err != nil {
return
}
}
})
qqclient.PrivateMessageEvent.Subscribe(func(client *client.QQClient, event *message.PrivateMessage) {
img, _ := message.NewFileImage("testprivate.png")
_, err := client.SendPrivateMessage(event.Sender.Uin, []message.IMessageElement{img})
if err != nil {
return
}
})
err = qqclient.Login("", "qrcode.png")
if err != nil {
logrus.Errorln("login err:", err)
return
}
defer qqclient.Release()
defer func() {
data, err = qqclient.Sig().Marshal()
if err != nil {
logrus.Errorln("marshal sig.bin err:", err)
return
}
err = os.WriteFile("sig.bin", data, 0644)
if err != nil {
logrus.Errorln("write sig.bin err:", err)
return
}
logrus.Infoln("sig saved into sig.bin")
}()
// setup the main stop channel
mc := make(chan os.Signal, 2)
signal.Notify(mc, os.Interrupt, syscall.SIGTERM)
for {
switch <-mc {
case os.Interrupt, syscall.SIGTERM:
return
}
}
}
// protocolLogger from https://github.com/Mrs4s/go-cqhttp/blob/a5923f179b360331786a6509eb33481e775a7bd1/cmd/gocq/main.go#L501
type protocolLogger struct{}
const fromProtocol = "Lgr -> "
func (p protocolLogger) Info(format string, arg ...any) {
logger.Infof(fromProtocol+format, arg...)
}
func (p protocolLogger) Warning(format string, arg ...any) {
logger.Warnf(fromProtocol+format, arg...)
}
func (p protocolLogger) Debug(format string, arg ...any) {
logger.Debugf(fromProtocol+format, arg...)
}
func (p protocolLogger) Error(format string, arg ...any) {
logger.Errorf(fromProtocol+format, arg...)
}
func (p protocolLogger) Dump(data []byte, format string, arg ...any) {
message := fmt.Sprintf(format, arg...)
if _, err := os.Stat(dumpspath); err != nil {
err = os.MkdirAll(dumpspath, 0o755)
if err != nil {
logger.Errorf("出现错误 %v. 详细信息转储失败", message)
return
}
}
dumpFile := path.Join(dumpspath, fmt.Sprintf("%v.dump", time.Now().Unix()))
logger.Errorf("出现错误 %v. 详细信息已转储至文件 %v 请连同日志提交给开发者处理", message, dumpFile)
_ = os.WriteFile(dumpFile, data, 0o644)
}
const (
// 定义颜色代码
colorReset = "\x1b[0m"
colorRed = "\x1b[31m"
colorYellow = "\x1b[33m"
colorGreen = "\x1b[32m"
colorBlue = "\x1b[34m"
colorWhite = "\x1b[37m"
)
var logger = logrus.New()
func init() {
logger.SetLevel(logrus.TraceLevel)
logger.SetFormatter(&ColoredFormatter{})
logger.SetOutput(colorable.NewColorableStdout())
}
type ColoredFormatter struct{}
func (f *ColoredFormatter) Format(entry *logrus.Entry) ([]byte, error) {
// 获取当前时间戳
timestamp := time.Now().Format("2006-01-02 15:04:05")
// 根据日志级别设置相应的颜色
var levelColor string
switch entry.Level {
case logrus.DebugLevel:
levelColor = colorBlue
case logrus.InfoLevel:
levelColor = colorGreen
case logrus.WarnLevel:
levelColor = colorYellow
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
levelColor = colorRed
default:
levelColor = colorWhite
}
return utils.S2B(fmt.Sprintf("[%s] [%s%s%s]: %s\n",
timestamp, levelColor, strings.ToUpper(entry.Level.String()), colorReset, entry.Message)), nil
}