2023-03-27 17:53:18 +02:00
package limits
import (
2023-03-27 19:51:48 +02:00
"github.com/jmoiron/sqlx"
2023-03-27 21:29:25 +02:00
"github.com/openziti/zrok/controller/emailUi"
2023-03-27 17:53:18 +02:00
"github.com/openziti/zrok/controller/store"
2023-03-29 22:14:57 +02:00
"github.com/openziti/zrok/util"
2023-03-27 21:29:25 +02:00
"github.com/pkg/errors"
2023-03-27 17:53:18 +02:00
"github.com/sirupsen/logrus"
2024-06-04 20:06:44 +02:00
"time"
2023-03-27 17:53:18 +02:00
)
2024-06-04 16:33:39 +02:00
type warningAction struct {
2023-06-05 22:01:04 +02:00
str * store . Store
cfg * emailUi . Config
2023-03-27 17:53:18 +02:00
}
2024-06-04 16:33:39 +02:00
func newWarningAction ( cfg * emailUi . Config , str * store . Store ) * warningAction {
return & warningAction { str , cfg }
2023-03-27 17:53:18 +02:00
}
2024-06-06 19:49:36 +02:00
func ( a * warningAction ) HandleAccount ( acct * store . Account , rxBytes , txBytes int64 , bwc store . BandwidthClass , _ * userLimits , _ * sqlx . Tx ) error {
2023-03-27 17:53:18 +02:00
logrus . Infof ( "warning '%v'" , acct . Email )
2023-03-27 21:29:25 +02:00
2023-05-01 20:03:34 +02:00
if a . cfg != nil {
2024-06-05 21:20:33 +02:00
rxLimit := "(store.Unlimited bytes)"
2024-06-06 19:49:36 +02:00
if bwc . GetRxBytes ( ) != store . Unlimited {
rxLimit = util . BytesToSize ( bwc . GetRxBytes ( ) )
2023-05-01 20:03:34 +02:00
}
2024-06-05 21:20:33 +02:00
txLimit := "(store.Unlimited bytes)"
2024-06-06 19:49:36 +02:00
if bwc . GetTxBytes ( ) != store . Unlimited {
txLimit = util . BytesToSize ( bwc . GetTxBytes ( ) )
2023-05-01 20:03:34 +02:00
}
2024-06-05 21:20:33 +02:00
totalLimit := "(store.Unlimited bytes)"
2024-06-06 19:49:36 +02:00
if bwc . GetTotalBytes ( ) != store . Unlimited {
totalLimit = util . BytesToSize ( bwc . GetTotalBytes ( ) )
2023-05-01 20:03:34 +02:00
}
detail := newDetailMessage ( )
detail = detail . append ( "Your account has received %v and sent %v (for a total of %v), which has triggered a transfer limit warning." , util . BytesToSize ( rxBytes ) , util . BytesToSize ( txBytes ) , util . BytesToSize ( rxBytes + txBytes ) )
2024-06-06 19:49:36 +02:00
detail = detail . append ( "This zrok instance only allows an account to receive %v, send %v, totalling not more than %v for each %v." , rxLimit , txLimit , totalLimit , time . Duration ( bwc . GetPeriodMinutes ( ) ) * time . Minute )
detail = detail . append ( "If you exceed the transfer limit, access to your shares will be temporarily disabled (until the last %v falls below the transfer limit)" , time . Duration ( bwc . GetPeriodMinutes ( ) ) * time . Minute )
2023-05-01 20:03:34 +02:00
if err := sendLimitWarningEmail ( a . cfg , acct . Email , detail ) ; err != nil {
return errors . Wrapf ( err , "error sending limit warning email to '%v'" , acct . Email )
}
} else {
logrus . Warnf ( "skipping warning email for account limit; no email configuration specified" )
2023-03-27 21:29:25 +02:00
}
2023-03-27 17:53:18 +02:00
return nil
}