diff --git a/controllers/webhookController.go b/controllers/webhookController.go index b617b7b..70e1a0a 100644 --- a/controllers/webhookController.go +++ b/controllers/webhookController.go @@ -109,12 +109,41 @@ func OnPubStart(c *fiber.Ctx) error { return err } - log.Printf("======================== Start StreamName %s, UrlParam %s, SessionId %s\n", p.StreamName, p.UrlParam, p.SessionId) + // Parse the URL parameters, in ordere to get the transmission key and the player key + transmissionkey, playerkey := utils.ParseTransmissionString(p.UrlParam) + + log.Printf("======================== Start StreamName %s, Transmission Key %s, PlayerKey %s, SessionId %s\n", p.StreamName, transmissionkey, playerkey, p.SessionId) + + // Get the channel from the database + channel := services.GetChannelByName(p.StreamName) + + // If the channel is not found, kick the session + if channel.ID == 0 { + services.AddTransmissionlog(p.StreamName, "Canal inexistente") + KickSession(p.StreamName, p.SessionId) + return fiber.ErrForbidden + } + + // If the channel is not active, kick the session + if channel.Status != "A" { + services.AddTransmissionlog(p.StreamName, "Canal não está ativo") + KickSession(p.StreamName, p.SessionId) + return fiber.ErrForbidden + } + + // If the transmission key does not match, kick the session + if transmissionkey != channel.TransmissionKey { + services.AddTransmissionlog(p.StreamName, "Transmissão com chave de transmissão errada") + KickSession(p.StreamName, p.SessionId) + return fiber.ErrForbidden + } + + // Check if the channel has authorized time to transmit - // TODO: Verify if the key is correct. If not, Kick the Session. // KickSession(p.StreamName, p.SessionId) // return fiber.ErrForbidden + services.AddTransmissionlog(p.StreamName, "Transmissão iniciada") return c.SendString("On_Pub_Start: " + string(c.Body())) } @@ -236,7 +265,7 @@ func WixTest(c *fiber.Ctx) error { channel := models.Channel{ Name: cnpj + "-01", TransmissionKey: tkey, - CustomerID: customerdb.UserID, + CustomerID: uint(customerdb.UserID), ServerID: 1, } err = globals.DB.Create(&channel) diff --git a/models/models.go b/models/models.go index 33ebb23..b0a969d 100644 --- a/models/models.go +++ b/models/models.go @@ -1,5 +1,7 @@ package models +import "time" + // ==================================================================== // Adianti tables @@ -145,10 +147,84 @@ type Email struct { EmailText string } -type Channel struct { - ID int - Name string - TransmissionKey string - CustomerID int - ServerID int +// Server represents the servers table in the database +type Server struct { + ID uint `gorm:"primaryKey;column:id;autoIncrement"` + Name string `gorm:"column:name;type:text;not null"` + IP string `gorm:"column:ip;type:text;not null"` + Subdomain string `gorm:"column:subdomain;type:text;not null"` + + // Relationship (back-reference) + Channels []Channel `gorm:"foreignKey:ServerID"` +} + +// TableName overrides the table name +func (Server) TableName() string { + return "servers" +} + +// Plan represents the plans table in the database +type Plan struct { + ID uint `gorm:"primaryKey;column:id;autoIncrement"` + Name string `gorm:"column:name;type:text;not null"` + DailyLimitTransmission int `gorm:"column:daily_limit_transmission;type:integer;not null"` + MonthValue float64 `gorm:"column:month_value;type:numeric(10,2);not null"` + YearValue float64 `gorm:"column:year_value;type:numeric(10,2);not null"` + + // Relationship (back-reference) + Channels []Channel `gorm:"foreignKey:PlanID"` +} + +// TableName overrides the table name +func (Plan) TableName() string { + return "plans" +} + +// ChannelStat represents the channel_stats table in the database +type ChannelStat struct { + ID uint `gorm:"primaryKey;column:id;autoIncrement"` + Name string `gorm:"column:name;type:text;not null;uniqueIndex"` + Description string `gorm:"column:description;type:text;not null"` + + // Relationship (back-reference) + Channels []Channel `gorm:"foreignKey:ChannelStatID"` +} + +// TableName overrides the table name +func (ChannelStat) TableName() string { + return "channel_stats" +} + +type Channel struct { + ID uint `gorm:"primaryKey;column:id;autoIncrement"` + Name string `gorm:"column:name;type:text;not null;uniqueIndex"` + TransmissionKey string `gorm:"column:transmission_key;type:text;not null;uniqueIndex"` + DateLimit time.Time `gorm:"column:date_limit;type:date;not null"` + CustomerID uint `gorm:"column:customer_id;not null"` + ServerID uint `gorm:"column:server_id;not null"` + PlanID uint `gorm:"column:plan_id;not null"` + Status string `gorm:"column:status;not null"` + + // Define relationships + Customer Customer `gorm:"foreignKey:CustomerID;references:ID"` + Server Server `gorm:"foreignKey:ServerID;references:ID"` + Plan Plan `gorm:"foreignKey:PlanID;references:ID"` +} + +// TableName overrides the table name +func (Channel) TableName() string { + return "channels" +} + +// TransmissionLog represents the transmissionlog table in the database +type TransmissionLog struct { + ID uint `gorm:"primaryKey;column:id;autoIncrement"` + Datetime time.Time `gorm:"column:datetime;type:timestamp;not null"` + Channel string `gorm:"column:channel;type:text;not null"` + Message string `gorm:"column:message;type:text;not null"` +} + +// TableName overrides the table name +func (TransmissionLog) TableName() string { + return "transmissionlog" } diff --git a/services/channelservices.go b/services/channelservices.go new file mode 100644 index 0000000..0d7f45b --- /dev/null +++ b/services/channelservices.go @@ -0,0 +1,14 @@ +package services + +import ( + "api/globals" + "api/models" +) + +func GetChannelByName(channelname string) models.Channel { + var channel models.Channel + + globals.DB.Where("name = ?", channelname).Find(&channel) + + return channel +} diff --git a/services/transmlogservices.go b/services/transmlogservices.go new file mode 100644 index 0000000..bb7bf75 --- /dev/null +++ b/services/transmlogservices.go @@ -0,0 +1,17 @@ +package services + +import ( + "api/globals" + "api/models" + "time" +) + +func AddTransmissionlog(channelname, message string) { + var log models.TransmissionLog + + log.Channel = channelname + log.Message = message + log.Datetime = time.Now() + + globals.DB.Create(&log) +} diff --git a/utils/utils.go b/utils/utils.go index 25e332a..6cf82b3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -255,3 +255,18 @@ func SendTestEmailApproval(email, name, transmkey, url string) { panic(err) } } + +func ParseTransmissionString(input string) (transmissionKey, password string) { + // Check if the string contains "&" + parts := strings.SplitN(input, "&", 2) + + // The first part is always the transmission key + transmissionKey = parts[0] + + // If there's a second part after "&", it's the password + if len(parts) > 1 { + password = parts[1] + } + + return transmissionKey, password +}