2022-05-11 22:47:31 +00:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"npm/internal/database"
|
2023-05-26 01:04:43 +00:00
|
|
|
"npm/internal/entity"
|
2022-05-11 22:47:31 +00:00
|
|
|
"npm/internal/entity/certificate"
|
2023-01-04 05:36:56 +00:00
|
|
|
"npm/internal/entity/nginxtemplate"
|
|
|
|
"npm/internal/entity/upstream"
|
2022-05-11 22:47:31 +00:00
|
|
|
"npm/internal/entity/user"
|
2023-01-04 05:36:56 +00:00
|
|
|
"npm/internal/status"
|
2022-05-11 22:47:31 +00:00
|
|
|
"npm/internal/types"
|
|
|
|
"npm/internal/util"
|
2023-02-24 07:19:07 +00:00
|
|
|
|
|
|
|
"github.com/rotisserie/eris"
|
2022-05-11 22:47:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ProxyHostType is self explanatory
|
|
|
|
ProxyHostType = "proxy"
|
|
|
|
// RedirectionHostType is self explanatory
|
|
|
|
RedirectionHostType = "redirection"
|
|
|
|
// DeadHostType is self explanatory
|
|
|
|
DeadHostType = "dead"
|
|
|
|
)
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// Model is the model
|
2022-05-11 22:47:31 +00:00
|
|
|
type Model struct {
|
2023-05-26 01:04:43 +00:00
|
|
|
entity.ModelBase
|
|
|
|
UserID uint `json:"user_id" gorm:"column:user_id" filter:"user_id,integer"`
|
|
|
|
Type string `json:"type" gorm:"column:type" filter:"type,string"`
|
|
|
|
NginxTemplateID uint `json:"nginx_template_id" gorm:"column:nginx_template_id" filter:"nginx_template_id,integer"`
|
|
|
|
ListenInterface string `json:"listen_interface" gorm:"column:listen_interface" filter:"listen_interface,string"`
|
|
|
|
DomainNames types.JSONB `json:"domain_names" gorm:"column:domain_names" filter:"domain_names,string"`
|
|
|
|
UpstreamID uint `json:"upstream_id" gorm:"column:upstream_id" filter:"upstream_id,integer"`
|
|
|
|
ProxyScheme string `json:"proxy_scheme" gorm:"column:proxy_scheme" filter:"proxy_scheme,string"`
|
|
|
|
ProxyHost string `json:"proxy_host" gorm:"column:proxy_host" filter:"proxy_host,string"`
|
|
|
|
ProxyPort int `json:"proxy_port" gorm:"column:proxy_port" filter:"proxy_port,integer"`
|
|
|
|
CertificateID uint `json:"certificate_id" gorm:"column:certificate_id" filter:"certificate_id,integer"`
|
|
|
|
AccessListID uint `json:"access_list_id" gorm:"column:access_list_id" filter:"access_list_id,integer"`
|
|
|
|
SSLForced bool `json:"ssl_forced" gorm:"column:ssl_forced" filter:"ssl_forced,boolean"`
|
|
|
|
CachingEnabled bool `json:"caching_enabled" gorm:"column:caching_enabled" filter:"caching_enabled,boolean"`
|
|
|
|
BlockExploits bool `json:"block_exploits" gorm:"column:block_exploits" filter:"block_exploits,boolean"`
|
|
|
|
AllowWebsocketUpgrade bool `json:"allow_websocket_upgrade" gorm:"column:allow_websocket_upgrade" filter:"allow_websocket_upgrade,boolean"`
|
|
|
|
HTTP2Support bool `json:"http2_support" gorm:"column:http2_support" filter:"http2_support,boolean"`
|
|
|
|
HSTSEnabled bool `json:"hsts_enabled" gorm:"column:hsts_enabled" filter:"hsts_enabled,boolean"`
|
|
|
|
HSTSSubdomains bool `json:"hsts_subdomains" gorm:"column:hsts_subdomains" filter:"hsts_subdomains,boolean"`
|
|
|
|
Paths string `json:"paths" gorm:"column:paths" filter:"paths,string"`
|
|
|
|
AdvancedConfig string `json:"advanced_config" gorm:"column:advanced_config" filter:"advanced_config,string"`
|
|
|
|
Status string `json:"status" gorm:"column:status" filter:"status,string"`
|
|
|
|
ErrorMessage string `json:"error_message" gorm:"column:error_message" filter:"error_message,string"`
|
|
|
|
IsDisabled bool `json:"is_disabled" gorm:"column:is_disabled" filter:"is_disabled,boolean"`
|
2022-05-11 22:47:31 +00:00
|
|
|
// Expansions
|
2023-05-26 01:04:43 +00:00
|
|
|
Certificate *certificate.Model `json:"certificate,omitempty" gorm:"-"`
|
|
|
|
NginxTemplate *nginxtemplate.Model `json:"nginx_template,omitempty" gorm:"-"`
|
|
|
|
User *user.Model `json:"user,omitempty" gorm:"-"`
|
|
|
|
Upstream *upstream.Model `json:"upstream,omitempty" gorm:"-"`
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// TableName overrides the table name used by gorm
|
|
|
|
func (Model) TableName() string {
|
|
|
|
return "host"
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// LoadByID will load from an ID
|
2023-05-26 01:04:43 +00:00
|
|
|
func (m *Model) LoadByID(id uint) error {
|
|
|
|
db := database.GetDB()
|
|
|
|
result := db.First(&m, id)
|
|
|
|
return result.Error
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save will save this model to the DB
|
2022-07-21 08:02:07 +00:00
|
|
|
func (m *Model) Save(skipConfiguration bool) error {
|
2022-05-11 22:47:31 +00:00
|
|
|
if m.UserID == 0 {
|
2023-02-24 07:19:07 +00:00
|
|
|
return eris.Errorf("User ID must be specified")
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 08:02:07 +00:00
|
|
|
if !skipConfiguration {
|
|
|
|
// Set this host as requiring reconfiguration
|
2023-01-04 05:36:56 +00:00
|
|
|
m.Status = status.StatusReady
|
2022-07-21 08:02:07 +00:00
|
|
|
}
|
2022-07-14 22:52:38 +00:00
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.Save(m)
|
|
|
|
return result.Error
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-26 01:04:43 +00:00
|
|
|
// Delete will mark row as deleted
|
2022-05-11 22:47:31 +00:00
|
|
|
func (m *Model) Delete() bool {
|
2023-05-26 01:04:43 +00:00
|
|
|
if m.ID == 0 {
|
|
|
|
// Can't delete a new object
|
2022-05-11 22:47:31 +00:00
|
|
|
return false
|
|
|
|
}
|
2023-05-26 01:04:43 +00:00
|
|
|
db := database.GetDB()
|
|
|
|
result := db.Delete(m)
|
|
|
|
return result.Error == nil
|
2022-05-11 22:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expand will fill in more properties
|
|
|
|
func (m *Model) Expand(items []string) error {
|
|
|
|
var err error
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
// Always expand the upstream
|
|
|
|
if m.UpstreamID > 0 {
|
|
|
|
var u upstream.Model
|
|
|
|
u, err = upstream.GetByID(m.UpstreamID)
|
2023-01-09 03:18:11 +00:00
|
|
|
m.Upstream = &u
|
2023-01-04 05:36:56 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 22:47:31 +00:00
|
|
|
if util.SliceContainsItem(items, "user") && m.ID > 0 {
|
|
|
|
var usr user.Model
|
|
|
|
usr, err = user.GetByID(m.UserID)
|
|
|
|
m.User = &usr
|
|
|
|
}
|
|
|
|
|
|
|
|
if util.SliceContainsItem(items, "certificate") && m.CertificateID > 0 {
|
|
|
|
var cert certificate.Model
|
|
|
|
cert, err = certificate.GetByID(m.CertificateID)
|
|
|
|
m.Certificate = &cert
|
|
|
|
}
|
|
|
|
|
2023-01-04 05:36:56 +00:00
|
|
|
if util.SliceContainsItem(items, "nginxtemplate") && m.NginxTemplateID > 0 {
|
|
|
|
var templ nginxtemplate.Model
|
|
|
|
templ, err = nginxtemplate.GetByID(m.NginxTemplateID)
|
|
|
|
m.NginxTemplate = &templ
|
2022-07-21 08:02:07 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 01:42:02 +00:00
|
|
|
if util.SliceContainsItem(items, "upstream") && m.UpstreamID > 0 {
|
|
|
|
var ups upstream.Model
|
|
|
|
ups, err = upstream.GetByID(m.UpstreamID)
|
2023-01-09 03:18:11 +00:00
|
|
|
m.Upstream = &ups
|
2023-01-06 01:42:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 22:47:31 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-11-08 00:03:45 +00:00
|
|
|
|
|
|
|
// GetTemplate will convert the Model to a Template
|
|
|
|
func (m *Model) GetTemplate() Template {
|
|
|
|
domainNames, _ := m.DomainNames.AsStringArray()
|
|
|
|
|
|
|
|
t := Template{
|
|
|
|
ID: m.ID,
|
2023-05-26 01:04:43 +00:00
|
|
|
CreatedAt: fmt.Sprintf("%d", m.CreatedAt), // todo: format as nice string
|
|
|
|
UpdatedAt: fmt.Sprintf("%d", m.UpdatedAt), // todo: format as nice string
|
2022-11-08 00:03:45 +00:00
|
|
|
UserID: m.UserID,
|
|
|
|
Type: m.Type,
|
2023-01-04 05:36:56 +00:00
|
|
|
NginxTemplateID: m.NginxTemplateID,
|
2023-01-09 03:18:11 +00:00
|
|
|
ProxyScheme: m.ProxyScheme,
|
|
|
|
ProxyHost: m.ProxyHost,
|
|
|
|
ProxyPort: m.ProxyPort,
|
2022-11-08 00:03:45 +00:00
|
|
|
ListenInterface: m.ListenInterface,
|
|
|
|
DomainNames: domainNames,
|
|
|
|
UpstreamID: m.UpstreamID,
|
|
|
|
CertificateID: m.CertificateID,
|
|
|
|
AccessListID: m.AccessListID,
|
|
|
|
SSLForced: m.SSLForced,
|
|
|
|
CachingEnabled: m.CachingEnabled,
|
|
|
|
BlockExploits: m.BlockExploits,
|
|
|
|
AllowWebsocketUpgrade: m.AllowWebsocketUpgrade,
|
|
|
|
HTTP2Support: m.HTTP2Support,
|
|
|
|
HSTSEnabled: m.HSTSEnabled,
|
|
|
|
HSTSSubdomains: m.HSTSSubdomains,
|
|
|
|
Paths: m.Paths,
|
|
|
|
AdvancedConfig: m.AdvancedConfig,
|
|
|
|
Status: m.Status,
|
|
|
|
ErrorMessage: m.ErrorMessage,
|
|
|
|
IsDisabled: m.IsDisabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|