2023-03-27 17:34:29 +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:34:29 +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:34:29 +02:00
"github.com/sirupsen/logrus"
)
type shareWarningAction struct {
2023-06-05 22:01:04 +02:00
str * store . Store
cfg * emailUi . Config
2023-03-27 17:34:29 +02:00
}
2023-06-05 22:01:04 +02:00
func newShareWarningAction ( cfg * emailUi . Config , str * store . Store ) * shareWarningAction {
return & shareWarningAction { str , cfg }
2023-03-27 17:34:29 +02:00
}
2023-03-27 21:29:25 +02:00
func ( a * shareWarningAction ) HandleShare ( shr * store . Share , rxBytes , txBytes int64 , limit * BandwidthPerPeriod , trx * sqlx . Tx ) error {
logrus . Infof ( "warning '%v'" , shr . Token )
2023-05-01 20:03:34 +02:00
if a . cfg != nil {
env , err := a . str . GetEnvironment ( shr . EnvironmentId , trx )
2023-03-28 20:39:42 +02:00
if err != nil {
return err
}
2023-05-01 20:03:34 +02:00
if env . AccountId != nil {
acct , err := a . str . GetAccount ( * env . AccountId , trx )
if err != nil {
return err
}
rxLimit := "unlimited bytes"
if limit . Limit . Rx != Unlimited {
rxLimit = util . BytesToSize ( limit . Limit . Rx )
}
txLimit := "unlimited bytes"
if limit . Limit . Tx != Unlimited {
txLimit = util . BytesToSize ( limit . Limit . Tx )
}
totalLimit := "unlimited bytes"
if limit . Limit . Total != Unlimited {
totalLimit = util . BytesToSize ( limit . Limit . Total )
}
2023-04-03 20:19:43 +02:00
2023-05-01 20:03:34 +02:00
detail := newDetailMessage ( )
detail = detail . append ( "Your share '%v' has received %v and sent %v (for a total of %v), which has triggered a transfer limit warning." , shr . Token , util . BytesToSize ( rxBytes ) , util . BytesToSize ( txBytes ) , util . BytesToSize ( rxBytes + txBytes ) )
detail = detail . append ( "This zrok instance only allows a share to receive %v, send %v, totalling not more than %v for each %v." , rxLimit , txLimit , totalLimit , limit . Period )
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)." , limit . Period )
2023-03-29 22:14:57 +02:00
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 )
}
2023-03-28 20:39:42 +02:00
}
2023-05-01 20:03:34 +02:00
} else {
logrus . Warnf ( "skipping warning email for share limit; no email configuration specified" )
2023-03-27 21:29:25 +02:00
}
2023-03-27 17:34:29 +02:00
return nil
}