acmanager.go 3.04 KB
Newer Older
vicotor's avatar
vicotor committed
1 2 3 4
package acmanager

import (
	"encoding/json"
vicotor's avatar
vicotor committed
5
	"github.com/gofiber/fiber/v2/log"
vicotor's avatar
vicotor committed
6 7
	"github.com/supabase-community/supabase-go"
	"net/http"
vicotor's avatar
vicotor committed
8
	"time"
vicotor's avatar
vicotor committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22
)

type Account struct {
	Username string `json:"username"`
	Password string `json:"password"`
	Email    string `json:"email"`
	// Cookies   []byte `json:"cookies"`
	F2A string `json:"two_fa_pk"`

	Cookies   []*http.Cookie `json:"cookies"`
	Available bool           `json:"available"`
}

type Manager struct {
vicotor's avatar
vicotor committed
23 24 25 26 27
	quit              chan struct{}
	client            *supabase.Client
	availableUser     map[string]Account
	userBlockedRecord map[string]time.Time
	warningHistory    []WarningRecord
vicotor's avatar
vicotor committed
28 29
}

vicotor's avatar
vicotor committed
30 31 32 33 34 35
func NewManager(client *supabase.Client) *Manager {
	m := &Manager{
		quit:           make(chan struct{}),
		availableUser:  make(map[string]Account),
		warningHistory: make([]WarningRecord, 0),
		client:         client,
vicotor's avatar
vicotor committed
36
	}
vicotor's avatar
vicotor committed
37 38 39 40 41 42
	m.warningHistory = append(m.warningHistory, WarningRecord{
		IsWarning: false,
		Time:      time.Now(),
	})

	return m
vicotor's avatar
vicotor committed
43 44
}

vicotor's avatar
vicotor committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
func (m *Manager) report(current int) {
	if current < warningCount {
		// check need to report.
		last := m.warningHistory[len(m.warningHistory)-1]
		// if last is not warning or last warning passed 5 minutes, send warning email.
		if !last.IsWarning || (last.IsWarning && time.Now().After(last.Time.Add(time.Minute*5))) {
			// send warning email.
			if err := Warning(current); err != nil {
				// log error.
				log.Errorf("send warning email error: %s", err.Error())
			} else {
				m.warningHistory = append(m.warningHistory, WarningRecord{
					IsWarning: true,
					Time:      time.Now(),
				})
			}
		}
	} else {
		last := m.warningHistory[len(m.warningHistory)-1]
		if last.IsWarning {
			// send success email.
			if err := Recovered(current); err != nil {
				// log error.
				log.Errorf("send success email error: %s", err.Error())
			} else {
				m.warningHistory = append(m.warningHistory, WarningRecord{
					IsWarning: false,
					Time:      time.Now(),
				})
			}
		}
vicotor's avatar
vicotor committed
76
	}
vicotor's avatar
vicotor committed
77 78 79 80
	if len(m.warningHistory) > 10 {
		old := m.warningHistory
		m.warningHistory = make([]WarningRecord, 0)
		m.warningHistory = append(m.warningHistory, old[len(old)-1])
vicotor's avatar
vicotor committed
81 82 83
	}
}

vicotor's avatar
vicotor committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
func (m *Manager) loop() {
	// loop check available account count.
	tc := time.NewTicker(time.Minute * 1)
	defer tc.Stop()
	for {
		select {
		case <-tc.C:
			all, err := GetAllAccounts(m.client)
			if err == nil {
				available := make([]Account, 0)
				unavailable := make([]Account, 0)
				for _, v := range all {
					if v.Available {
						available = append(available, v)
					} else {
						unavailable = append(unavailable, v)
					}
				}
				m.report(len(available))
			}
			tc.Reset(time.Minute * 3)
		case <-m.quit:
			return
		}
vicotor's avatar
vicotor committed
108 109 110
	}
}

vicotor's avatar
vicotor committed
111 112
func (m *Manager) Start() {
	go m.loop()
vicotor's avatar
vicotor committed
113 114
}

vicotor's avatar
vicotor committed
115 116
func (m *Manager) Stop() {
	close(m.quit)
vicotor's avatar
vicotor committed
117 118
}

vicotor's avatar
vicotor committed
119
func GetAllAccounts(client *supabase.Client) ([]Account, error) {
vicotor's avatar
vicotor committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	data, count, err := client.From("accounts").Select("*", "exact", false).Execute()
	if err != nil {
		return nil, err
	}

	_ = count

	res := make([]Account, 0, count)
	if err := json.Unmarshal(data, &res); err != nil {
		return nil, err
	}

	return res, nil

}