independent sign task

This commit is contained in:
iyear 2021-06-15 18:27:28 +08:00
parent 15e9d93abd
commit 956942419b
4 changed files with 35 additions and 34 deletions

View File

@ -5,9 +5,6 @@ import (
"github.com/iyear/E5SubBot/config"
"github.com/iyear/E5SubBot/logger"
"github.com/iyear/E5SubBot/model"
"github.com/iyear/E5SubBot/task"
"github.com/robfig/cron/v3"
"github.com/spf13/viper"
"go.uber.org/zap"
"golang.org/x/net/proxy"
tb "gopkg.in/tucnak/telebot.v2"
@ -40,6 +37,9 @@ func BotStart() {
logger.InitLogger()
//InitDB
model.InitDB()
//Init Task
InitTask()
Poller := &tb.LongPoller{Timeout: 15 * time.Second}
spamPoller := tb.NewMiddlewarePoller(Poller, func(upd *tb.Update) bool {
if upd.Message == nil {
@ -75,7 +75,6 @@ func BotStart() {
fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username)
MakeHandle()
TaskLaunch()
fmt.Println("Bot Start")
fmt.Println("------------")
bot.Start()
@ -94,9 +93,3 @@ func MakeHandle() {
bot.Handle("/task", bTask)
bot.Handle("/log", bLog)
}
func TaskLaunch() {
c := cron.New()
c.AddFunc(viper.GetString("cron"), task.SignTask)
fmt.Println("Cron Task Start……")
c.Start()
}

View File

@ -5,7 +5,6 @@ import (
"fmt"
"github.com/iyear/E5SubBot/config"
"github.com/iyear/E5SubBot/model"
"github.com/iyear/E5SubBot/task"
"github.com/iyear/E5SubBot/util"
"go.uber.org/zap"
tb "gopkg.in/tucnak/telebot.v2"
@ -226,7 +225,7 @@ func bOnText(m *tb.Message) {
func bTask(m *tb.Message) {
for _, a := range config.Admins {
if a == m.Chat.ID {
task.SignTask()
SignTask()
return
}
}

View File

@ -26,7 +26,9 @@ var (
BotToken string
Socks5 string
BindMaxNum int
ErrMaxTimes int
MaxGoroutines int
MaxErrTimes int
Cron string
Notice string
Admins []int64
)
@ -45,14 +47,17 @@ func InitConfig() {
viper.SetDefault("bindmax", 5)
BindMaxNum = viper.GetInt("bindmax")
ErrMaxTimes = viper.GetInt("errlimit")
MaxErrTimes = viper.GetInt("errlimit")
Notice = viper.GetString("notice")
Cron = viper.GetString("cron")
MaxGoroutines = viper.GetInt("goroutine")
Admins = getAdmins()
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
MaxGoroutines = viper.GetInt("goroutine")
BindMaxNum = viper.GetInt("bindmax")
ErrMaxTimes = viper.GetInt("errlimit")
MaxErrTimes = viper.GetInt("errlimit")
Notice = viper.GetString("notice")
Admins = getAdmins()
})

View File

@ -1,25 +1,29 @@
package model
import (
"fmt"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
type Client struct {
TgId int64 `gorm:"not null"`
RefreshToken string `gorm:"not null"`
MsId string `gorm:"unique;primaryKey;not null"`
MsId string `gorm:"not null"`
Uptime int64 `gorm:"autoUpdateTime;not null"`
Alias string `gorm:"not null"`
ClientId string `gorm:"not null"`
ClientSecret string `gorm:"not null"`
Other string
}
type ErrClient struct {
*Client
Err error
}
const (
msApiUrl string = "https://login.microsoftonline.com"
@ -28,6 +32,15 @@ const (
scope string = "openid offline_access mail.read user.read"
)
var client = &http.Client{}
func init() {
client.Timeout = 10 * time.Second
tp := http.DefaultTransport.(*http.Transport).Clone()
tp.MaxIdleConns = 100
tp.MaxIdleConnsPerHost = 100
client.Transport = tp
}
func NewClient(clientId string, clientSecret string) *Client {
return &Client{
ClientId: clientId,
@ -47,7 +60,6 @@ func GetMSRegisterAppUrl() string {
// GetTokenWithCode return access_token and refresh_token
func (c *Client) GetTokenWithCode(code string) (error error) {
var r http.Request
client := &http.Client{}
r.ParseForm()
r.Form.Add("client_id", c.ClientId)
r.Form.Add("client_secret", c.ClientSecret)
@ -60,17 +72,15 @@ func (c *Client) GetTokenWithCode(code string) (error error) {
if err != nil {
return err
}
//prevents the connection from being re-used
////https://stackoverflow.com/questions/17714494/golang-http-request-results-in-eof-errors-when-making-multiple-requests-successi
req.Close = true
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
@ -84,7 +94,6 @@ func (c *Client) GetTokenWithCode(code string) (error error) {
//return access_token and new refresh token
func (c *Client) getToken() (access string) {
var r http.Request
client := &http.Client{}
r.ParseForm()
r.Form.Add("client_id", c.ClientId)
r.Form.Add("client_secret", c.ClientSecret)
@ -98,7 +107,6 @@ func (c *Client) getToken() (access string) {
if err != nil {
return ""
}
req.Close = true
resp, err := client.Do(req)
if err != nil {
return ""
@ -117,14 +125,11 @@ func (c *Client) getToken() (access string) {
// GetUserInfo Get User's Information
func (c *Client) GetUserInfo() (json string, error error) {
client := http.Client{}
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me", nil)
if err != nil {
return "", err
}
req.Close = true
req.Header.Set("Authorization", c.getToken())
resp, err := client.Do(req)
if err != nil {
@ -132,7 +137,7 @@ func (c *Client) GetUserInfo() (json string, error error) {
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(content))
if err != nil {
return "", err
}
@ -144,12 +149,10 @@ func (c *Client) GetUserInfo() (json string, error error) {
}
func (c *Client) GetOutlookMails() error {
client := http.Client{}
req, err := http.NewRequest("GET", msGraUrl+"/v1.0/me/messages", nil)
if err != nil {
return err
}
req.Close = true
req.Header.Set("Authorization", c.getToken())
resp, err := client.Do(req)
if err != nil {
@ -157,6 +160,7 @@ func (c *Client) GetOutlookMails() error {
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}