-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from SugarMGP/main
refactor: 从4U迁移部分修改
- Loading branch information
Showing
18 changed files
with
731 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Lint | ||
|
||
# 定义触发条件 | ||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
lint: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: stable | ||
|
||
- name: Run golangci-lint | ||
uses: golangci/golangci-lint-action@v6 | ||
with: | ||
version: v1.62.2 | ||
args: '--config .golangci.yml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# golangci-lint 配置文件 | ||
|
||
run: | ||
concurrency: 4 # 并行执行的 linter 数量,默认为可用 CPU 数量 | ||
deadline: 5m # 分析的最大超时时间,默认为 5 分钟 | ||
timeout: 10m # 超时时间 | ||
issues-exit-code: 1 # 如果发现至少一个问题,退出码为 1 | ||
tests: true # 包含测试文件进行分析 | ||
modules-download-mode: readonly # 防止分析过程中修改 go.mod 文件 | ||
|
||
output: | ||
formats: # 设置多个输出格式 | ||
- format: colored-line-number # 使用颜色化的行号格式 | ||
sort-results: true # 按文件名和行号排序结果 | ||
print-issued-lines: true # 输出包含问题的代码行 | ||
print-linter-name: true # 输出 linter 名称 | ||
|
||
linters-settings: | ||
errcheck: | ||
check-type-assertions: true # 检查类型断言的错误处理 | ||
check-blank: false # 检查将错误分配给空标识符的情况 | ||
exclude-functions: # 不检查这些函数 | ||
- fmt:.* | ||
- io/ioutil:^Read.* | ||
revive: | ||
# 是否启用所有可用的规则 | ||
enable-all-rules: true | ||
# 默认失败置信度 | ||
confidence: 0.1 | ||
rules: | ||
- name: var-naming # 变量命名规则 | ||
severity: warning | ||
disabled: true | ||
- name: line-length-limit # 行长度限制 | ||
severity: warning | ||
disabled: false | ||
exclude: [ "" ] | ||
arguments: [ 120 ] | ||
- name: add-constant # 使用命名常量而非魔法数字 | ||
severity: warning | ||
disabled: true | ||
- name: package-comments # 包注释 | ||
severity: warning | ||
disabled: true | ||
- name: import-alias-naming # 导入别名命名规则 | ||
severity: warning | ||
disabled: true | ||
- name: get-return # 使用get必须有返回值 | ||
severity: warning | ||
disabled: true | ||
- name: max-public-structs # 最大公共结构体数目 | ||
severity: warning | ||
disabled: true | ||
- name: argument-limit # 函数参数限制 | ||
severity: warning | ||
disabled: true | ||
- name: function-length # 函数长度限制 | ||
severity: warning | ||
disabled: true | ||
- name: cyclomatic # 圈复杂度 | ||
severity: warning | ||
disabled: true | ||
- name: cognitive-complexity # 认知复杂度 | ||
severity: warning | ||
disabled: false | ||
arguments: [ 10 ] | ||
gofmt: | ||
simplify: true # 使用 gofmt 的 `-s` 选项简化代码 | ||
gocyclo: | ||
min-complexity: 10 # 触发报告的最小代码复杂度 | ||
dupl: | ||
threshold: 100 # 触发报告的最小令牌数 | ||
goconst: | ||
min-len: 3 # 字符串常量的最小长度 | ||
min-occurrences: 3 # 触发报告的最小出现次数 | ||
gosec: | ||
severity: medium # 安全问题的最小严重性 | ||
unused: | ||
check-exported: true # 报告未使用的导出标识符 | ||
unparam: | ||
check-exported: false # 检查导出的函数 | ||
nakedret: | ||
max-func-lines: 0 # 允许带裸返回的函数的最大行数 | ||
staticcheck: # 使用 staticcheck 替代 govet | ||
checks: | ||
- SA1000 # 使用了某些无效操作 | ||
- SA1001 # 无效的时间格式或解析 | ||
- SA1012 # 非法的 waitGroup 使用 | ||
prealloc: | ||
simple: true # 在简单循环中报告预分配建议 | ||
range-loops: true # 在 range 循环中报告预分配建议 | ||
for-loops: true # 在 for 循环中报告预分配建议 | ||
|
||
linters: | ||
disable-all: true # 禁用所有 linter | ||
enable: | ||
- errcheck # 检查未处理的错误 | ||
- staticcheck # 静态代码分析工具,检测潜在的错误和反模式 | ||
- gofmt # 检查代码格式是否符合 gofmt 规范 | ||
- revive # 替代 golint 的代码风格检查工具 | ||
- gocyclo # 检测代码的圈复杂度 | ||
- dupl # 检查重复代码 | ||
- goconst # 检查重复的常量 | ||
- gosec # 检查安全问题 | ||
- unused # 检查未使用的代码 | ||
- unparam # 检查未使用的函数参数 | ||
- prealloc # 检查预分配建议 | ||
- gci # 检查并排序导入语句 | ||
|
||
issues: | ||
exclude-use-default: false # 禁用默认排除模式 | ||
max-per-linter: 0 # 每个 linter 的最大问题数量,设置为 0 禁用限制 | ||
max-same-issues: 0 # 同一问题的最大数量,设置为 0 禁用限制 | ||
new: false # 仅显示新问题,默认为 false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,28 @@ | ||
# WeJH-SDK | ||
提供一些封装好的工具 | ||
## WeJH-SDK | ||
|
||
### 工具 | ||
|
||
- AES 加密解密 | ||
- MinIO 对象服务操作 | ||
- Zap 日志初始化 | ||
- Redis 初始化 | ||
- 基于 Redis 的 Session 初始化 | ||
- 基于 Redis 的 Wechat 初始化 | ||
- 邮件功能 | ||
- fetch | ||
- 加密 | ||
- redis初始化 | ||
- gin-session初始化 | ||
- wechat初始化 | ||
- Fetch | ||
|
||
### 使用 | ||
|
||
``` | ||
go get github.com/zjutjh/WeJH-SDK | ||
``` | ||
|
||
### 代码规范检查 | ||
|
||
需要安装 [gci](https://github.com/daixiang0/gci) 和 [golangci-lint](https://golangci-lint.run/) | ||
|
||
使用``go get github.com/zjutjh/WeJH-SDK``导入到项目mod | ||
``` | ||
gofmt -w . | ||
gci write . -s standard -s default | ||
golangci-lint run --config .golangci.yml | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package aes | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"encoding/base64" | ||
"errors" | ||
) | ||
|
||
var encryptKey []byte | ||
|
||
var initKey = false | ||
|
||
var ( | ||
// ErrKeyNotInit 未初始化密钥 | ||
ErrKeyNotInit = errors.New("请先初始化 AES 密钥") | ||
|
||
// ErrKeyLengthNotMatch 密钥长度不匹配 | ||
ErrKeyLengthNotMatch = errors.New("AES 密钥长度必须为 16、24 或 32 字节") | ||
) | ||
|
||
// Init 读入 AES 密钥配置 | ||
func Init(key string) error { | ||
if len(key) != 16 && len(key) != 24 && len(key) != 32 { | ||
return ErrKeyLengthNotMatch | ||
} | ||
encryptKey = []byte(key) | ||
initKey = true | ||
return nil | ||
} | ||
|
||
// Encrypt AES 加密 | ||
func Encrypt(orig string) (string, error) { | ||
if !initKey { | ||
return "", ErrKeyNotInit | ||
} | ||
|
||
origData := []byte(orig) | ||
|
||
// 分组秘钥 | ||
block, err := aes.NewCipher(encryptKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// 进行 PKCS7 填充 | ||
blockSize := block.BlockSize() | ||
origData = PKCS7Padding(origData, blockSize) | ||
|
||
// 使用 CBC 加密模式 | ||
blockMode := cipher.NewCBCEncrypter(block, encryptKey[:blockSize]) | ||
cryted := make([]byte, len(origData)) | ||
blockMode.CryptBlocks(cryted, origData) | ||
|
||
// 使用 RawURLEncoding 编码为 Base64,适合放入 URL | ||
return base64.RawURLEncoding.EncodeToString(cryted), nil | ||
} | ||
|
||
// Decrypt AES 解密 | ||
func Decrypt(cryted string) (string, error) { | ||
if !initKey { | ||
return "", ErrKeyNotInit | ||
} | ||
|
||
// 解码 Base64 字符串 | ||
crytedByte, err := base64.RawURLEncoding.DecodeString(cryted) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// 分组秘钥 | ||
block, err := aes.NewCipher(encryptKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// CBC 模式解密 | ||
blockSize := block.BlockSize() | ||
blockMode := cipher.NewCBCDecrypter(block, encryptKey[:blockSize]) | ||
orig := make([]byte, len(crytedByte)) | ||
blockMode.CryptBlocks(orig, crytedByte) | ||
|
||
// 去除 PKCS7 填充 | ||
orig = PKCS7UnPadding(orig) | ||
|
||
return string(orig), nil | ||
} | ||
|
||
// PKCS7Padding 填充数据,使长度为 blockSize 的倍数 | ||
func PKCS7Padding(data []byte, blockSize int) []byte { | ||
padding := blockSize - len(data)%blockSize | ||
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(data, padtext...) | ||
} | ||
|
||
// PKCS7UnPadding 去除填充 | ||
func PKCS7UnPadding(origData []byte) []byte { | ||
length := len(origData) | ||
unpadding := int(origData[length-1]) | ||
return origData[:(length - unpadding)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//nolint:all | ||
package sdk | ||
|
||
import ( | ||
|
Oops, something went wrong.