From 956942419b11246067df658446c48b40f7b5e2ea Mon Sep 17 00:00:00 2001 From: iyear Date: Tue, 15 Jun 2021 18:27:28 +0800 Subject: [PATCH] independent sign task --- bots/bots.go | 13 +++---------- bots/handle.go | 3 +-- config/config.go | 21 +++++++++++++-------- model/client.go | 32 ++++++++++++++++++-------------- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/bots/bots.go b/bots/bots.go index 1d55473..cc5f428 100644 --- a/bots/bots.go +++ b/bots/bots.go @@ -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() -} diff --git a/bots/handle.go b/bots/handle.go index 92216a2..78581ef 100644 --- a/bots/handle.go +++ b/bots/handle.go @@ -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 } } diff --git a/config/config.go b/config/config.go index a934042..25e7c99 100644 --- a/config/config.go +++ b/config/config.go @@ -23,12 +23,14 @@ const ( ) var ( - BotToken string - Socks5 string - BindMaxNum int - ErrMaxTimes int - Notice string - Admins []int64 + BotToken string + Socks5 string + BindMaxNum int + MaxGoroutines int + MaxErrTimes int + Cron string + Notice string + Admins []int64 ) func InitConfig() { @@ -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() }) diff --git a/model/client.go b/model/client.go index 006c8ee..40c5bdc 100644 --- a/model/client.go +++ b/model/client.go @@ -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 }