-
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 #1 from cbluebird/main
Initial commit
- Loading branch information
Showing
10 changed files
with
679 additions
and
0 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 |
---|---|---|
|
@@ -19,3 +19,5 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
.idea |
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,2 +1,10 @@ | ||
# WeJH-SDK | ||
提供一些封装好的工具 | ||
- 邮件功能 | ||
- fetch | ||
- 加密 | ||
- redis初始化 | ||
- gin-session初始化 | ||
- wechat初始化 | ||
|
||
使用``go get github.com/zjutjh/WeJH-SDK``导入到项目mod |
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,80 @@ | ||
package sdk | ||
|
||
import ( | ||
"bytes" | ||
"crypto/aes" | ||
"crypto/cipher" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/hex" | ||
"fmt" | ||
) | ||
|
||
func AesSHA256(password string) string { | ||
h := sha256.New() | ||
h.Write([]byte(password)) | ||
pass := hex.EncodeToString(h.Sum(nil)) | ||
return pass | ||
} | ||
|
||
func AesEncrypt(orig string, key string) string { | ||
// 转成字节数组 | ||
origData := []byte(orig) | ||
k := []byte(key) | ||
|
||
// 分组秘钥 | ||
block, err := aes.NewCipher(k) | ||
if err != nil { | ||
panic(fmt.Sprintf("key 长度必须 16/24/32长度: %s", err.Error())) | ||
} | ||
// 获取秘钥块的长度 | ||
blockSize := block.BlockSize() | ||
// 补全码 | ||
origData = PKCS7Padding(origData, blockSize) | ||
// 加密模式 | ||
blockMode := cipher.NewCBCEncrypter(block, k[:blockSize]) | ||
// 创建数组 | ||
cryted := make([]byte, len(origData)) | ||
// 加密 | ||
blockMode.CryptBlocks(cryted, origData) | ||
//使用RawURLEncoding 不要使用StdEncoding | ||
//不要使用StdEncoding 放在url参数中回导致错误 | ||
return base64.RawURLEncoding.EncodeToString(cryted) | ||
|
||
} | ||
|
||
func AesDecrypt(cryted string, key string) string { | ||
//使用RawURLEncoding 不要使用StdEncoding | ||
//不要使用StdEncoding 放在url参数中回导致错误 | ||
crytedByte, _ := base64.RawURLEncoding.DecodeString(cryted) | ||
k := []byte(key) | ||
|
||
// 分组秘钥 | ||
block, err := aes.NewCipher(k) | ||
if err != nil { | ||
panic(fmt.Sprintf("key 长度必须 16/24/32长度: %s", err.Error())) | ||
} | ||
// 获取秘钥块的长度 | ||
blockSize := block.BlockSize() | ||
// 加密模式 | ||
blockMode := cipher.NewCBCDecrypter(block, k[:blockSize]) | ||
// 创建数组 | ||
orig := make([]byte, len(crytedByte)) | ||
// 解密 | ||
blockMode.CryptBlocks(orig, crytedByte) | ||
// 去补全码 | ||
orig = PKCS7UnPadding(orig) | ||
return string(orig) | ||
} | ||
|
||
func PKCS7Padding(ciphertext []byte, blocksize int) []byte { | ||
padding := blocksize - len(ciphertext)%blocksize | ||
padtext := bytes.Repeat([]byte{byte(padding)}, padding) | ||
return append(ciphertext, padtext...) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package sdk | ||
|
||
import ( | ||
"gopkg.in/gomail.v2" | ||
) | ||
|
||
// MailboxConf 发送邮件内容配置 | ||
type MailboxConf struct { | ||
// 邮件标题 | ||
Title string | ||
// 邮件内容 | ||
Body string | ||
// 收件人列表 | ||
RecipientList []string | ||
} | ||
|
||
// MailInfoConfig 发件人配置 | ||
type MailInfoConfig struct { | ||
// 发件人账号 | ||
Sender string | ||
// 发件人密码,QQ邮箱这里配置授权码 | ||
SPassword string | ||
// SMTP 服务器地址, QQ邮箱是smtp.qq.com | ||
SMTPAddr string | ||
// SMTP端口 QQ邮箱是25 | ||
SMTPPort int | ||
} | ||
|
||
// SendEmail 一个message可以是这样的: | ||
// message := fmt.Sprintf(`<div> | ||
// | ||
// <div> | ||
// hello | ||
// </div> | ||
// </div>`) | ||
func (config MailInfoConfig) SendEmail(message string, mailbox MailboxConf) error { | ||
m := gomail.NewMessage() | ||
m.SetHeader(`From`, config.Sender) | ||
m.SetHeader(`To`, mailbox.RecipientList...) | ||
m.SetHeader(`Subject`, mailbox.Title) | ||
m.SetBody(`text/html`, message) | ||
// m.Attach("./Dockerfile") //添加附件 | ||
d := gomail.NewDialer(config.SMTPAddr, config.SMTPPort, config.Sender, config.SPassword) | ||
return d.DialAndSend(m) | ||
} |
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,118 @@ | ||
package sdk | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Fetch struct { | ||
Cookie []*http.Cookie | ||
client *http.Client | ||
} | ||
|
||
func (f *Fetch) InitUnSafe() { | ||
f.client = &http.Client{ | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, | ||
Timeout: time.Second * 15, | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
} | ||
} | ||
|
||
func (f *Fetch) Init() { | ||
f.client = &http.Client{ | ||
CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, | ||
Timeout: time.Second * 15, | ||
} | ||
} | ||
func (f *Fetch) SkipTlsCheck() { | ||
f.client.Transport = &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
} | ||
|
||
func (f *Fetch) Get(url string) ([]byte, error) { | ||
response, err := f.GetRaw(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s, err := ioutil.ReadAll(response.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return s, nil | ||
} | ||
|
||
func (f *Fetch) GetRaw(url string) (*http.Response, error) { | ||
request, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, v := range f.Cookie { | ||
request.AddCookie(v) | ||
} | ||
response, err := f.client.Do(request) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f.Cookie = cookieMerge(f.Cookie, response.Cookies()) | ||
return response, err | ||
} | ||
|
||
func (f *Fetch) PostFormRaw(url string, requestData url.Values) (*http.Response, error) { | ||
request, _ := http.NewRequest("POST", url, strings.NewReader(requestData.Encode())) | ||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
for _, v := range f.Cookie { | ||
request.AddCookie(v) | ||
} | ||
return f.client.Do(request) | ||
} | ||
|
||
func (f *Fetch) PostForm(url string, requestData url.Values) ([]byte, error) { | ||
response, err := f.PostFormRaw(url, requestData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f.Cookie = cookieMerge(f.Cookie, response.Cookies()) | ||
return ioutil.ReadAll(response.Body) | ||
} | ||
|
||
func (f *Fetch) PostJsonFormRaw(url string, requestData map[string]string) (*http.Response, error) { | ||
bytesData, _ := json.Marshal(requestData) | ||
request, _ := http.NewRequest("POST", url, bytes.NewReader(bytesData)) | ||
request.Header.Set("Content-Type", "application/json") | ||
for _, v := range f.Cookie { | ||
request.AddCookie(v) | ||
} | ||
return f.client.Do(request) | ||
} | ||
|
||
func (f *Fetch) PostJsonForm(url string, requestData map[string]string) ([]byte, error) { | ||
response, err := f.PostJsonFormRaw(url, requestData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
f.Cookie = cookieMerge(f.Cookie, response.Cookies()) | ||
return ioutil.ReadAll(response.Body) | ||
} | ||
|
||
func cookieMerge(cookieA []*http.Cookie, cookieB []*http.Cookie) []*http.Cookie { | ||
|
||
for _, v := range cookieB { | ||
for k, v2 := range cookieA { | ||
if v.Name == v2.Name { | ||
cookieA = append(cookieA[:k], cookieA[k+1:]...) | ||
break | ||
} | ||
} | ||
} | ||
cookieA = append(cookieA, cookieB...) | ||
return cookieA | ||
} |
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,54 @@ | ||
module github.com/zjutjh/WeJH-SDK | ||
|
||
go 1.21.0 | ||
|
||
require ( | ||
github.com/gin-contrib/sessions v0.0.5 | ||
github.com/gin-gonic/gin v1.9.1 | ||
github.com/go-redis/redis/v8 v8.11.5 | ||
github.com/silenceper/wechat/v2 v2.1.6 | ||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df | ||
) | ||
|
||
require ( | ||
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect | ||
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d // indirect | ||
github.com/bytedance/sonic v1.9.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect | ||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect | ||
github.com/fatih/structs v1.1.0 // indirect | ||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/go-playground/locales v0.14.1 // indirect | ||
github.com/go-playground/universal-translator v0.18.1 // indirect | ||
github.com/go-playground/validator/v10 v10.14.0 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/gomodule/redigo v2.0.0+incompatible // indirect | ||
github.com/gorilla/context v1.1.1 // indirect | ||
github.com/gorilla/securecookie v1.1.1 // indirect | ||
github.com/gorilla/sessions v1.2.1 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect | ||
github.com/leodido/go-urn v1.2.4 // indirect | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect | ||
github.com/quasoft/memstore v0.0.0-20191010062613-2bce066d2b0b // indirect | ||
github.com/sirupsen/logrus v1.9.0 // indirect | ||
github.com/spf13/cast v1.4.1 // indirect | ||
github.com/tidwall/gjson v1.14.1 // indirect | ||
github.com/tidwall/match v1.1.1 // indirect | ||
github.com/tidwall/pretty v1.2.0 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.11 // indirect | ||
golang.org/x/arch v0.3.0 // indirect | ||
golang.org/x/crypto v0.9.0 // indirect | ||
golang.org/x/net v0.10.0 // indirect | ||
golang.org/x/sys v0.8.0 // indirect | ||
golang.org/x/text v0.9.0 // indirect | ||
google.golang.org/protobuf v1.30.0 // indirect | ||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
Oops, something went wrong.