Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test4 #55

Merged
merged 108 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
fad0d50
Compiled main.go and pushed changes
Hoshinonyaruko Oct 23, 2023
fda2572
test
Hoshinonyaruko Oct 23, 2023
34cfac1
适配了频道私聊,用bolt数据库取代ini
Hoshinonyaruko Oct 23, 2023
7fc1f10
适配了nonebot2
Hoshinonyaruko Oct 24, 2023
28f9a73
Merge branch 'main' of https://github.com/Hoshinonyaruko/Gensokyo
Hoshinonyaruko Oct 24, 2023
9fadda4
适配私域
Hoshinonyaruko Oct 24, 2023
1336c6a
add license
Hoshinonyaruko Oct 24, 2023
b094949
add a lot
Hoshinonyaruko Oct 25, 2023
f4787f5
Resolved merge conflicts
Hoshinonyaruko Oct 25, 2023
86698c6
trss support
Hoshinonyaruko Oct 26, 2023
045f3e9
fix
Hoshinonyaruko Oct 26, 2023
618561c
add action
Hoshinonyaruko Oct 26, 2023
7e803aa
add action
Hoshinonyaruko Oct 26, 2023
ece4cef
add action
Hoshinonyaruko Oct 26, 2023
f864a50
fixbug
Hoshinonyaruko Oct 26, 2023
4564731
add wss
Hoshinonyaruko Oct 26, 2023
070e4df
merge
Hoshinonyaruko Oct 26, 2023
84e8fe9
bugfix
Hoshinonyaruko Oct 26, 2023
0e4fd50
fix action
Hoshinonyaruko Oct 26, 2023
fe2234a
fix action again
Hoshinonyaruko Oct 26, 2023
cf67a11
fix action againnn
Hoshinonyaruko Oct 26, 2023
0875837
fa
Hoshinonyaruko Oct 26, 2023
3a3c33f
fix
Hoshinonyaruko Oct 26, 2023
8789fa8
add a lot
Hoshinonyaruko Oct 27, 2023
7943466
add ws server token
Hoshinonyaruko Oct 28, 2023
4660983
merge
Hoshinonyaruko Oct 28, 2023
2a1026a
bugifx
Hoshinonyaruko Oct 28, 2023
4186970
fix
Hoshinonyaruko Oct 28, 2023
2f26828
fixat
Hoshinonyaruko Oct 28, 2023
007e1af
bugfix
Hoshinonyaruko Oct 28, 2023
c41fd77
bugfix
Hoshinonyaruko Oct 28, 2023
d8354b7
test
Hoshinonyaruko Oct 28, 2023
6e3c63c
test
Hoshinonyaruko Oct 28, 2023
f08d9d7
test2
Hoshinonyaruko Oct 28, 2023
4dd6e15
add url service
Hoshinonyaruko Oct 31, 2023
1f8c174
add url service
Hoshinonyaruko Oct 31, 2023
240f569
add url service
Hoshinonyaruko Oct 31, 2023
b191cb7
bugfix
Hoshinonyaruko Oct 31, 2023
42daab3
merge
Hoshinonyaruko Oct 31, 2023
8fec03f
fix
Hoshinonyaruko Oct 31, 2023
cca528b
fix
Hoshinonyaruko Oct 31, 2023
463a182
fix
Hoshinonyaruko Oct 31, 2023
e9187a1
bug fix
Hoshinonyaruko Nov 1, 2023
5ba2815
fix
Hoshinonyaruko Nov 1, 2023
c16e1af
fix
Hoshinonyaruko Nov 1, 2023
ec7863d
test
Hoshinonyaruko Nov 2, 2023
e8a7e73
ok
Hoshinonyaruko Nov 2, 2023
122d17d
add webui
Hoshinonyaruko Nov 2, 2023
4e9c209
merge
Hoshinonyaruko Nov 2, 2023
b502d61
add image_compress
Hoshinonyaruko Nov 3, 2023
3519e3e
merge
Hoshinonyaruko Nov 3, 2023
530ff96
bug fix
Hoshinonyaruko Nov 3, 2023
155a701
fix cq code
Hoshinonyaruko Nov 3, 2023
2f5ee4a
fixbug
Hoshinonyaruko Nov 3, 2023
afa03e3
bugfix
Hoshinonyaruko Nov 3, 2023
fdd1a73
merge
Hoshinonyaruko Nov 3, 2023
fe39cd2
bugfix
Hoshinonyaruko Nov 3, 2023
be08cbe
bugfix
Hoshinonyaruko Nov 3, 2023
931dfef
merge
Hoshinonyaruko Nov 3, 2023
73819c8
bugfix
Hoshinonyaruko Nov 3, 2023
b27f062
bugfix
Hoshinonyaruko Nov 3, 2023
1a94197
bugfix
Hoshinonyaruko Nov 3, 2023
745b28e
bugfix
Hoshinonyaruko Nov 3, 2023
1212c9a
bugfix
Hoshinonyaruko Nov 4, 2023
44d8669
bugfix
Hoshinonyaruko Nov 4, 2023
564aba5
add new feature
Hoshinonyaruko Nov 5, 2023
d7345c1
bugfix
Hoshinonyaruko Nov 5, 2023
472a224
bugfix
Hoshinonyaruko Nov 5, 2023
e22b58b
bugfix
Hoshinonyaruko Nov 5, 2023
22cb473
bugfix
Hoshinonyaruko Nov 5, 2023
e563929
bugfix
Hoshinonyaruko Nov 5, 2023
829b5d7
merge
Hoshinonyaruko Nov 5, 2023
b496ac1
bugfix
Hoshinonyaruko Nov 5, 2023
13e7700
bugfix
Hoshinonyaruko Nov 5, 2023
eb7e630
bugfix
Hoshinonyaruko Nov 5, 2023
32d490f
test
Hoshinonyaruko Nov 5, 2023
9815e3a
test
Hoshinonyaruko Nov 5, 2023
7747de6
test
Hoshinonyaruko Nov 5, 2023
fe2e4be
test
Hoshinonyaruko Nov 5, 2023
ec9e797
test
Hoshinonyaruko Nov 5, 2023
3548108
test
Hoshinonyaruko Nov 5, 2023
1e50a7c
test
Hoshinonyaruko Nov 5, 2023
812e6ea
test
Hoshinonyaruko Nov 5, 2023
76363a6
test
Hoshinonyaruko Nov 5, 2023
d8b05cd
test
Hoshinonyaruko Nov 5, 2023
c2e2b20
test
Hoshinonyaruko Nov 5, 2023
cefac16
test
Hoshinonyaruko Nov 5, 2023
273db15
test
Hoshinonyaruko Nov 5, 2023
5379f7d
test
Hoshinonyaruko Nov 5, 2023
56e2429
test
Hoshinonyaruko Nov 5, 2023
4466f30
test
Hoshinonyaruko Nov 6, 2023
a32a124
test
Hoshinonyaruko Nov 6, 2023
246d9c4
test
Hoshinonyaruko Nov 6, 2023
c91d9e2
test
Hoshinonyaruko Nov 6, 2023
6007979
bugfix
Hoshinonyaruko Nov 6, 2023
af64c33
bugfix
Hoshinonyaruko Nov 6, 2023
f5cfda3
Remove dist directory from tracking
Hoshinonyaruko Nov 6, 2023
53b6e5d
test
Hoshinonyaruko Nov 6, 2023
3991193
test
Hoshinonyaruko Nov 7, 2023
7af6759
bugfix2
Hoshinonyaruko Nov 7, 2023
fdec4ca
bugfix3
Hoshinonyaruko Nov 7, 2023
849c638
bugfix4
Hoshinonyaruko Nov 7, 2023
f268f3d
bugfix5
Hoshinonyaruko Nov 7, 2023
7ada595
bugfix5
Hoshinonyaruko Nov 8, 2023
76376a0
bugfix6
Hoshinonyaruko Nov 8, 2023
8332932
bugfix7
Hoshinonyaruko Nov 8, 2023
ac05d43
bugfix8
Hoshinonyaruko Nov 8, 2023
47b672c
bugfix9
Hoshinonyaruko Nov 8, 2023
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
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type Settings struct {
DevBotid string `yaml:"develop_bot_id"`
SandBoxMode bool `yaml:"sandbox_mode"`
Title string `yaml:"title"`
HashID bool `yaml:"hash_id"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -389,6 +390,18 @@ func GetLotusValue() bool {
return instance.Settings.Lotus
}

// 获取HashID
func GetHashIDValue() bool {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to get hashid value.")
return false
}
return instance.Settings.HashID
}

// 获取RemoveAt的值
func GetRemoveAt() bool {
mu.Lock()
Expand Down
79 changes: 71 additions & 8 deletions idmap/service.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package idmap

import (
"crypto/md5"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/boltdb/bolt"
"github.com/hoshinonyaruko/gensokyo/config"
Expand Down Expand Up @@ -42,6 +46,38 @@ func InitializeDB() {
func CloseDB() {
db.Close()
}
func generateRowID(id string, length int) (int64, error) {
// 计算MD5哈希值
hasher := md5.New()
hasher.Write([]byte(id))
hash := hex.EncodeToString(hasher.Sum(nil))

// 只保留数字
var digitsBuilder strings.Builder
for _, r := range hash {
if r >= '0' && r <= '9' {
digitsBuilder.WriteRune(r)
}
}
digits := digitsBuilder.String()

// 取出需要长度的数字
var rowIDStr string
if len(digits) >= length {
rowIDStr = digits[:length]
} else {
// 如果数字不足指定长度,返回错误
return 0, fmt.Errorf("not enough digits in MD5 hash")
}

// 将数字字符串转换为int64
rowID, err := strconv.ParseInt(rowIDStr, 10, 64)
if err != nil {
return 0, err
}

return rowID, nil
}

// 根据a储存b
func StoreID(id string) (int64, error) {
Expand All @@ -56,19 +92,46 @@ func StoreID(id string) (int64, error) {
newRow = int64(binary.BigEndian.Uint64(existingRowBytes))
return nil
}

// 如果ID不存在,则为它分配一个新的行号
currentRowBytes := b.Get([]byte(CounterKey))
if currentRowBytes == nil {
newRow = 1
//写入虚拟值
if !config.GetHashIDValue() {
// 如果ID不存在,则为它分配一个新的行号 数字递增
currentRowBytes := b.Get([]byte(CounterKey))
if currentRowBytes == nil {
newRow = 1
} else {
currentRow := binary.BigEndian.Uint64(currentRowBytes)
newRow = int64(currentRow) + 1
}
} else {
currentRow := binary.BigEndian.Uint64(currentRowBytes)
newRow = int64(currentRow) + 1
// 生成新的行号
var err error
newRow, err = generateRowID(id, 9)
if err != nil {
return err
}
// 检查新生成的行号是否重复
rowKey := fmt.Sprintf("row-%d", newRow)
if b.Get([]byte(rowKey)) != nil {
// 如果行号重复,使用10位数字生成行号
newRow, err = generateRowID(id, 10)
if err != nil {
return err
}
rowKey = fmt.Sprintf("row-%d", newRow)
// 再次检查重复性,如果还是重复,则返回错误
if b.Get([]byte(rowKey)) != nil {
return fmt.Errorf("unable to find a unique row ID")
}
}
}

rowBytes := make([]byte, 8)
binary.BigEndian.PutUint64(rowBytes, uint64(newRow))
b.Put([]byte(CounterKey), rowBytes)
//写入递增值
if !config.GetHashIDValue() {
b.Put([]byte(CounterKey), rowBytes)
}
//真实对应虚拟 用来直接判断是否存在,并快速返回
b.Put([]byte(id), rowBytes)

reverseKey := fmt.Sprintf("row-%d", newRow)
Expand Down
8 changes: 0 additions & 8 deletions mylog/mylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ func WsHandlerWithDependencies(c *gin.Context) {
// 输出新的 WebSocket 客户端连接信息
fmt.Println("新的webui用户已连接!")

// Start a goroutine for heartbeats
go func() {
for {
time.Sleep(5 * time.Second) // send a heartbeat every 30 seconds
client.conn.WriteMessage(websocket.PingMessage, nil)
}
}()

go client.writePump()
go client.readPump()

Expand Down
1 change: 1 addition & 0 deletions template/config_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ settings:
develop_bot_id : "1234" #开发者环境需自行获取botid 填入 用户请不要设置这两行...开发者调试用
sandbox_mode : false #默认false 如果你只希望沙箱频道使用,请改为true
title : "Gensokyo © 2023 - Hoshinonyaruko" #程序的标题 如果多个机器人 可根据标题区分
hash_id : false #使用hash来进行idmaps转换,可以让user_id不是123开始的递增值
`
const Logo = `
'
Expand Down
3 changes: 2 additions & 1 deletion template/config_template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ settings:
develop_access_token_dir : "" #开发者测试环境access_token自定义获取地址 默认留空 请留空忽略
develop_bot_id : "1234" #开发者环境需自行获取botid 填入 用户请不要设置这两行...开发者调试用
sandbox_mode : false #默认false 如果你只希望沙箱频道使用,请改为true
title : "Gensokyo © 2023 - Hoshinonyaruko" #程序的标题 如果多个机器人 可根据标题区分
title : "Gensokyo © 2023 - Hoshinonyaruko" #程序的标题 如果多个机器人 可根据标题区分
hash_id : false #使用hash来进行idmaps转换,可以让user_id不是123开始的递增值
Loading