-
Notifications
You must be signed in to change notification settings - Fork 37
/
main.go
256 lines (228 loc) · 6.26 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// nolint
package main
import (
"errors"
"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, "")
qqclient.SetLogger(protocolLogger{})
qqclient.UseVersion(appInfo)
qqclient.AddSignServer("https://sign.lagrangecore.org/api/sign/25765")
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
}
})
qqclient.DisconnectedEvent.Subscribe(func(client *client.QQClient, event *client.DisconnectedEvent) {
logger.Infof("连接已断开:%v", event.Message)
})
err = func(c *client.QQClient, passwordLogin bool) error {
logger.Info("login with password")
err := c.FastLogin()
if err == nil {
return nil
}
if passwordLogin {
ret, err := c.PasswordLogin()
for {
if err != nil {
logger.Errorf("密码登录失败: %s", err)
break
}
if ret.Success {
return nil
}
switch ret.Error {
case client.SliderNeededError:
logger.Warnln("captcha verification required")
logger.Warnln(ret.VerifyURL)
aid := strings.Split(strings.Split(ret.VerifyURL, "sid=")[1], "&")[0]
logger.Warnln("ticket?->")
ticket := utils.ReadLine()
logger.Warnln("rand_str?->")
randStr := utils.ReadLine()
ret, err = c.SubmitCaptcha(ticket, randStr, aid)
continue
case client.UnsafeDeviceError:
vf, err := c.GetNewDeviceVerifyURL()
if err != nil {
return err
}
logger.Infoln(vf)
err = c.NewDeviceVerify(vf)
if err != nil {
return err
}
default:
logger.Errorf("Unhandled exception raised: %s", ret.ErrorMessage)
}
}
}
logger.Infoln("login with qrcode")
png, _, err := c.FetchQRCodeDefault()
if err != nil {
return err
}
qrcodePath := "qrcode.png"
err = os.WriteFile(qrcodePath, png, 0666)
if err != nil {
return err
}
logger.Infof("qrcode saved to %s", qrcodePath)
for {
retCode, err := c.GetQRCodeResult()
if err != nil {
logger.Errorln(err)
return err
}
if retCode.Waitable() {
time.Sleep(3 * time.Second)
continue
}
if !retCode.Success() {
return errors.New(retCode.Name())
}
break
}
_, err = c.QRCodeLogin()
return err
}(qqclient, false)
if err != nil {
logger.Errorln("login err:", err)
return
}
logger.Infoln("login successed")
defer qqclient.Release()
defer func() {
data, err = qqclient.Sig().Marshal()
if err != nil {
logger.Errorln("marshal sig.bin err:", err)
return
}
err = os.WriteFile("sig.bin", data, 0644)
if err != nil {
logger.Errorln("write sig.bin err:", err)
return
}
logger.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
}