mirror of
https://github.com/silenceper/wechat.git
synced 2026-03-01 00:35:26 +08:00
Compare commits
6
Commits
6091e09657
...
500f144860
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
500f144860 | ||
|
|
4a8371e178 | ||
|
|
a571bf3546 | ||
|
|
d3d35387b7 | ||
|
|
0bca4d5792 | ||
|
|
bd0dce6f47 |
Vendored
+96
@@ -2,9 +2,13 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Redis .redis cache
|
// Redis .redis cache
|
||||||
@@ -22,6 +26,7 @@ type RedisOpts struct {
|
|||||||
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
||||||
MaxActive int `yml:"max_active" json:"max_active"`
|
MaxActive int `yml:"max_active" json:"max_active"`
|
||||||
IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` // second
|
IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` // second
|
||||||
|
Dialer func(ctx context.Context, network, addr string) (net.Conn, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedis 实例化
|
// NewRedis 实例化
|
||||||
@@ -33,6 +38,20 @@ func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
|
|||||||
Password: opts.Password,
|
Password: opts.Password,
|
||||||
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
||||||
MinIdleConns: opts.MaxIdle,
|
MinIdleConns: opts.MaxIdle,
|
||||||
|
Dialer: opts.Dialer,
|
||||||
|
})
|
||||||
|
return &Redis{ctx: ctx, conn: conn}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRedisOverSSH 实例化(通过 SSH 代理连接 Redis )
|
||||||
|
func NewRedisOverSSH(ctx context.Context, opts *RedisOpts, overSSH *OverSSH) *Redis {
|
||||||
|
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
||||||
|
Addrs: []string{opts.Host},
|
||||||
|
DB: opts.Database,
|
||||||
|
Password: opts.Password,
|
||||||
|
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
||||||
|
MinIdleConns: opts.MaxIdle,
|
||||||
|
Dialer: overSSH.MakeDialer(),
|
||||||
})
|
})
|
||||||
return &Redis{ctx: ctx, conn: conn}
|
return &Redis{ctx: ctx, conn: conn}
|
||||||
}
|
}
|
||||||
@@ -92,3 +111,80 @@ func (r *Redis) Delete(key string) error {
|
|||||||
func (r *Redis) DeleteContext(ctx context.Context, key string) error {
|
func (r *Redis) DeleteContext(ctx context.Context, key string) error {
|
||||||
return r.conn.Del(ctx, key).Err()
|
return r.conn.Del(ctx, key).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SSHAuthMethod SSH认证方式
|
||||||
|
type SSHAuthMethod uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
// PubKeyAuth SSH公钥方式认证
|
||||||
|
PubKeyAuth SSHAuthMethod = 1
|
||||||
|
// PwdAuth SSH密码方式认证
|
||||||
|
PwdAuth SSHAuthMethod = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
// OverSSH SSH 代理配置
|
||||||
|
type OverSSH struct {
|
||||||
|
Host string `yml:"host" json:"host"`
|
||||||
|
Port int `yml:"port" json:"port"`
|
||||||
|
AuthMethod SSHAuthMethod `yml:"auth_method" json:"auth_method"`
|
||||||
|
Username string `yml:"username" json:"username"`
|
||||||
|
Password string `yml:"password" json:"password"`
|
||||||
|
KeyFile string `yml:"key_file" json:"key_file"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialWithPassword 返回密码方式认证的 SSH 客户端
|
||||||
|
func (s *OverSSH) DialWithPassword() (*ssh.Client, error) {
|
||||||
|
return ssh.Dial(
|
||||||
|
"tcp",
|
||||||
|
fmt.Sprintf("%s:%d", s.Host, s.Port),
|
||||||
|
&ssh.ClientConfig{
|
||||||
|
User: s.Username,
|
||||||
|
Auth: []ssh.AuthMethod{
|
||||||
|
ssh.Password(s.Password),
|
||||||
|
},
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialWithKeyFile 返回公钥方式认证的 SSH 客户端
|
||||||
|
func (s *OverSSH) DialWithKeyFile() (*ssh.Client, error) {
|
||||||
|
k, err := os.ReadFile(s.KeyFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
signer, err := ssh.ParsePrivateKey(k)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ssh.Dial(
|
||||||
|
"tcp",
|
||||||
|
fmt.Sprintf("%s:%d", s.Host, s.Port),
|
||||||
|
&ssh.ClientConfig{
|
||||||
|
User: s.Username,
|
||||||
|
Auth: []ssh.AuthMethod{
|
||||||
|
ssh.PublicKeys(signer),
|
||||||
|
},
|
||||||
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeDialer 创建 SSH 代理拨号器
|
||||||
|
func (s *OverSSH) MakeDialer() func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
var err error
|
||||||
|
var sshclient *ssh.Client
|
||||||
|
switch s.AuthMethod {
|
||||||
|
case PwdAuth:
|
||||||
|
sshclient, err = s.DialWithPassword()
|
||||||
|
case PubKeyAuth:
|
||||||
|
sshclient, err = s.DialWithKeyFile()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return sshclient.Dial(network, addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ type StableAccessToken struct {
|
|||||||
appSecret string
|
appSecret string
|
||||||
cacheKeyPrefix string
|
cacheKeyPrefix string
|
||||||
cache cache.Cache
|
cache cache.Cache
|
||||||
|
accessTokenLock *sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStableAccessToken new StableAccessToken
|
// NewStableAccessToken new StableAccessToken
|
||||||
@@ -117,6 +118,7 @@ func NewStableAccessToken(appID, appSecret, cacheKeyPrefix string, cache cache.C
|
|||||||
appSecret: appSecret,
|
appSecret: appSecret,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
cacheKeyPrefix: cacheKeyPrefix,
|
cacheKeyPrefix: cacheKeyPrefix,
|
||||||
|
accessTokenLock: new(sync.Mutex),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +132,20 @@ func (ak *StableAccessToken) GetAccessTokenContext(ctx context.Context) (accessT
|
|||||||
// 先从cache中取
|
// 先从cache中取
|
||||||
accessTokenCacheKey := fmt.Sprintf("%s_stable_access_token_%s", ak.cacheKeyPrefix, ak.appID)
|
accessTokenCacheKey := fmt.Sprintf("%s_stable_access_token_%s", ak.cacheKeyPrefix, ak.appID)
|
||||||
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
|
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
|
||||||
return val.(string), nil
|
if accessToken = val.(string); accessToken != "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加上lock,是为了防止在并发获取token时,cache刚好失效,导致从微信服务器上获取到不同token
|
||||||
|
ak.accessTokenLock.Lock()
|
||||||
|
defer ak.accessTokenLock.Unlock()
|
||||||
|
|
||||||
|
// 双检,防止重复从微信服务器获取
|
||||||
|
if val := ak.cache.Get(accessTokenCacheKey); val != nil {
|
||||||
|
if accessToken = val.(string); accessToken != "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cache失效,从微信服务器获取
|
// cache失效,从微信服务器获取
|
||||||
|
|||||||
@@ -111,6 +111,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package subscribe
|
package subscribe
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/silenceper/wechat/v2/miniprogram/context"
|
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||||
@@ -70,6 +71,13 @@ type TemplateList struct {
|
|||||||
Data []TemplateItem `json:"data"`
|
Data []TemplateItem `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resTemplateSend 发送获取 msg id
|
||||||
|
type resTemplateSend struct {
|
||||||
|
util.CommonError
|
||||||
|
|
||||||
|
MsgID int64 `json:"msgid"`
|
||||||
|
}
|
||||||
|
|
||||||
// Send 发送订阅消息
|
// Send 发送订阅消息
|
||||||
func (s *Subscribe) Send(msg *Message) (err error) {
|
func (s *Subscribe) Send(msg *Message) (err error) {
|
||||||
var accessToken string
|
var accessToken string
|
||||||
@@ -85,6 +93,33 @@ func (s *Subscribe) Send(msg *Message) (err error) {
|
|||||||
return util.DecodeWithCommonError(response, "Send")
|
return util.DecodeWithCommonError(response, "Send")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SendGetMsgID 发送订阅消息返回 msgid
|
||||||
|
func (s *Subscribe) SendGetMsgID(msg *Message) (msgID int64, err error) {
|
||||||
|
var accessToken string
|
||||||
|
accessToken, err = s.GetAccessToken()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uri := fmt.Sprintf("%s?access_token=%s", subscribeSendURL, accessToken)
|
||||||
|
response, err := util.PostJSON(uri, msg)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var result resTemplateSend
|
||||||
|
if err = json.Unmarshal(response, &result); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result.ErrCode != 0 {
|
||||||
|
err = fmt.Errorf("template msg send error : errcode=%v , errmsg=%v", result.ErrCode, result.ErrMsg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
msgID = result.MsgID
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// ListTemplates 获取当前帐号下的个人模板列表
|
// ListTemplates 获取当前帐号下的个人模板列表
|
||||||
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
|
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
|
||||||
func (s *Subscribe) ListTemplates() (*TemplateList, error) {
|
func (s *Subscribe) ListTemplates() (*TemplateList, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user