wxccserver

package module
v0.0.0-...-82e5f0e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2020 License: MIT Imports: 20 Imported by: 0

README

go-wx-ccserver

微信中控服务器,统一管理access token和jsapi ticket。

特点

  • 公众号账号可配置,并支持不关机重新加载
  • 支持IP限制访问
  • 支持TLS

api调用

api文档

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTimeout = errors.New("签名超时")
	ErrInvalid = errors.New("无效签名")
)

Functions

This section is empty.

Types

type APIRes

type APIRes struct {
	ErrCode int    `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

APIRes 接口通用响应

type Account

type Account struct {
	AppID        string
	AppSecret    string
	SignatureKey string `default:""` // 为安全性,每个account可以独立一个签名key
}

Account 表示一个公众号账号

type AccountProvider

type AccountProvider interface {
	// 返回公众号账号列表
	Obtain() ([]*Account, error)
}

AccountProvider 公众号账号信息提供器

type Config

type Config struct {
	Port         int      `default:"9527"`  // 服务监听端口
	EnableTLS    bool     `default:"false"` // 启用TLS
	TLSCertFile  string   `default:""`
	TLSKeyFile   string   `default:""`
	AccountsFile string   `default:"./config/accounts.toml"` // 公众号账号配置文件
	AheadTimeout int      `default:"60"`                     // access token 和 jsapi ticket提前超时时间(单位: 秒);
	AllowIP      []string // 允许访问的ip; 为空表示不限制
	Signature    struct {
		Key     string // 默认签名密钥
		Timeout int    `default:"10"` // 签名超时时间(单位:秒)
	}
	Log struct {
		Level        string `default:"debug"`  // 日志记录级别
		Path         string `default:"./logs"` // 日志保存路径
		RotationTime int    `default:"24"`     // 日志分割时间(单位:时)
		MaxAge       int    `default:"168"`    // 日志最大保存时间(单位:时)
	}
}

Config 配置

type Fetcher

type Fetcher interface {
	FetchToken(appID, appSecret string) (*TokenRes, error)
	FetchTicket(accessToken string) (*TicketRes, error)
}

Fetcher 是access token/jsapi ticket的请求器接口

type HMacSignatureValidator

type HMacSignatureValidator struct {
	// contains filtered or unexported fields
}

HMacSignatureValidator 基于HMac算法(md5)的签名验证组件

func NewHMacSignatureValidator

func NewHMacSignatureValidator(key string, timeout int) *HMacSignatureValidator

NewHMacSignatureValidator 创建基于HMac算法的签名验证组件

func (*HMacSignatureValidator) Validate

func (validator *HMacSignatureValidator) Validate(appID string, timestamp int64, signature string) error

Validate 验证签名合法性

type PIDFile

type PIDFile struct {
	// contains filtered or unexported fields
}

PIDFile pidfile锁

func NewPIDFile

func NewPIDFile(filename string) *PIDFile

NewPIDFile 创建pidfile对象

func (*PIDFile) GetPID

func (p *PIDFile) GetPID() (int, error)

GetPID 获取pidfile文件保存的进行id

func (*PIDFile) Remove

func (p *PIDFile) Remove() error

Remove 移除

func (*PIDFile) SetPID

func (p *PIDFile) SetPID(pid int) error

SetPID 设置pidfile文件保存的进程id

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 中控服务器

func NewServer

func NewServer(config *Config) *Server

NewServer 创建中控服务器

func (*Server) Reload

func (s *Server) Reload() error

Reload 重新加载公众号账号信息

func (*Server) Start

func (s *Server) Start()

Start 启动服务

func (*Server) Stop

func (s *Server) Stop()

Stop 停止服务

type SignatureValidator

type SignatureValidator interface {
	Validate(appID string, timestamp int64, signature string) error
}

SignatureValidator 签名验证器接口

type StoreItem

type StoreItem struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

StoreItem 保存access token和jsapi ticket

func NewStoreItem

func NewStoreItem(appID, appSecret, signatureKey string) *StoreItem

NewStoreItem 创建存储项

func (*StoreItem) IsValidTicket

func (item *StoreItem) IsValidTicket() bool

IsValidTicket 判断jsapi ticket是否有效; ticket不存在或超时均为无效

func (*StoreItem) IsValidToken

func (item *StoreItem) IsValidToken() bool

IsValidToken 判断access token是否有效; token不存在或超时均为无效

func (*StoreItem) SetTicket

func (item *StoreItem) SetTicket(ticket string, expired time.Time)

SetTicket 设置jsapi ticket

func (*StoreItem) SetToken

func (item *StoreItem) SetToken(token string, expired time.Time)

SetToken 设置access token

func (*StoreItem) Ticket

func (item *StoreItem) Ticket() string

Ticket 返回jsapi ticket

func (*StoreItem) Token

func (item *StoreItem) Token() string

Token 返回access token

type StoreManager

type StoreManager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

StoreManager 存储管理器; 用于管理各个公众号的access token和jsapi ticket

func NewStoreManager

func NewStoreManager(provider AccountProvider, fetcher Fetcher, aheadTimeout int) *StoreManager

NewStoreManager 创建存储管理器

func (*StoreManager) LoadAccounts

func (m *StoreManager) LoadAccounts() error

LoadAccounts 从配置加载公众号账号

func (*StoreManager) RemoveTicket

func (m *StoreManager) RemoveTicket(appID string)

RemoveTicket 移除ticket

func (*StoreManager) RemoveToken

func (m *StoreManager) RemoveToken(appID string)

RemoveToken 移除token

func (*StoreManager) Ticket

func (m *StoreManager) Ticket(appID string) (string, error)

Ticket 返回指定appID的jsapi ticket

func (*StoreManager) Token

func (m *StoreManager) Token(appID string) (string, error)

Token 返回指定appID的access token

type TicketRes

type TicketRes struct {
	APIRes
	Ticket    string `json:"ticket"`
	ExpiresIn int    `json:"expires_in"`
}

TicketRes 获取jsapi_ticket的接口的响应

type TokenRes

type TokenRes struct {
	APIRes
	AccessToken string `json:"access_token"`
	ExpiresIn   int    `json:"expires_in"`
}

TokenRes 获取access token的接口的响应

type TomlAccountProvider

type TomlAccountProvider struct {
	// contains filtered or unexported fields
}

TomlAccountProvider 基于toml格式文件的公众号账号提供器

func NewTomlAccountProvider

func NewTomlAccountProvider(filename string) *TomlAccountProvider

NewTomlAccountProvider 创建公众号账号提供器

func (*TomlAccountProvider) Obtain

func (p *TomlAccountProvider) Obtain() ([]*Account, error)

Obtain 返回公众号账号列表

type WxFetcher

type WxFetcher struct {
	// contains filtered or unexported fields
}

WxFetcher 微信公众号请求器; 调用公众号API获取相应的token和ticket

func NewWxFetcher

func NewWxFetcher() *WxFetcher

NewWxFetcher 创建请求器

func (*WxFetcher) FetchTicket

func (f *WxFetcher) FetchTicket(accessToken string) (*TicketRes, error)

FetchTicket 获取jsapi ticket

func (*WxFetcher) FetchToken

func (f *WxFetcher) FetchToken(appID, appSecret string) (*TokenRes, error)

FetchToken 获取access token

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL