diff --git a/internal/db/models/http_fastcgi_dao.go b/internal/db/models/http_fastcgi_dao.go new file mode 100644 index 00000000..f02d1490 --- /dev/null +++ b/internal/db/models/http_fastcgi_dao.go @@ -0,0 +1,201 @@ +package models + +import ( + "encoding/json" + "github.com/TeaOSLab/EdgeAPI/internal/errors" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" + _ "github.com/go-sql-driver/mysql" + "github.com/iwind/TeaGo/Tea" + "github.com/iwind/TeaGo/dbs" + "github.com/iwind/TeaGo/types" +) + +const ( + HTTPFastcgiStateEnabled = 1 // 已启用 + HTTPFastcgiStateDisabled = 0 // 已禁用 +) + +type HTTPFastcgiDAO dbs.DAO + +func NewHTTPFastcgiDAO() *HTTPFastcgiDAO { + return dbs.NewDAO(&HTTPFastcgiDAO{ + DAOObject: dbs.DAOObject{ + DB: Tea.Env, + Table: "edgeHTTPFastcgis", + Model: new(HTTPFastcgi), + PkName: "id", + }, + }).(*HTTPFastcgiDAO) +} + +var SharedHTTPFastcgiDAO *HTTPFastcgiDAO + +func init() { + dbs.OnReady(func() { + SharedHTTPFastcgiDAO = NewHTTPFastcgiDAO() + }) +} + +// EnableHTTPFastcgi 启用条目 +func (this *HTTPFastcgiDAO) EnableHTTPFastcgi(tx *dbs.Tx, id int64) error { + _, err := this.Query(tx). + Pk(id). + Set("state", HTTPFastcgiStateEnabled). + Update() + return err +} + +// DisableHTTPFastcgi 禁用条目 +func (this *HTTPFastcgiDAO) DisableHTTPFastcgi(tx *dbs.Tx, id int64) error { + _, err := this.Query(tx). + Pk(id). + Set("state", HTTPFastcgiStateDisabled). + Update() + return err +} + +// FindEnabledHTTPFastcgi 查找启用中的条目 +func (this *HTTPFastcgiDAO) FindEnabledHTTPFastcgi(tx *dbs.Tx, id int64) (*HTTPFastcgi, error) { + result, err := this.Query(tx). + Pk(id). + Attr("state", HTTPFastcgiStateEnabled). + Find() + if result == nil { + return nil, err + } + return result.(*HTTPFastcgi), err +} + +// ComposeFastcgiConfig 组合配置 +func (this *HTTPFastcgiDAO) ComposeFastcgiConfig(tx *dbs.Tx, fastcgiId int64) (*serverconfigs.HTTPFastcgiConfig, error) { + if fastcgiId <= 0 { + return nil, nil + } + fastcgi, err := this.FindEnabledHTTPFastcgi(tx, fastcgiId) + if err != nil { + return nil, err + } + if fastcgi == nil { + return nil, nil + } + config := &serverconfigs.HTTPFastcgiConfig{} + config.Id = int64(fastcgi.Id) + config.IsOn = fastcgi.IsOn == 1 + config.Address = fastcgi.Address + + if IsNotNull(fastcgi.Params) { + params := []*serverconfigs.HTTPFastcgiParam{} + err = json.Unmarshal([]byte(fastcgi.Params), ¶ms) + if err != nil { + return nil, err + } + config.Params = params + } + + if IsNotNull(fastcgi.ReadTimeout) { + duration := &shared.TimeDuration{} + err = json.Unmarshal([]byte(fastcgi.ReadTimeout), duration) + if err != nil { + return nil, err + } + config.ReadTimeout = duration + } + + if IsNotNull(fastcgi.ConnTimeout) { + duration := &shared.TimeDuration{} + err = json.Unmarshal([]byte(fastcgi.ConnTimeout), duration) + if err != nil { + return nil, err + } + config.ConnTimeout = duration + } + + if fastcgi.PoolSize > 0 { + config.PoolSize = types.Int(fastcgi.PoolSize) + } + config.PathInfoPattern = fastcgi.PathInfoPattern + + return config, nil +} + +// CreateFastcgi 创建Fastcgi +func (this *HTTPFastcgiDAO) CreateFastcgi(tx *dbs.Tx, adminId int64, userId int64, isOn bool, address string, paramsJSON []byte, readTimeoutJSON []byte, connTimeoutJSON []byte, poolSize int32, pathInfoPattern string) (int64, error) { + op := NewHTTPFastcgiOperator() + op.AdminId = adminId + op.UserId = userId + op.IsOn = isOn + op.Address = address + if len(paramsJSON) > 0 { + op.Params = paramsJSON + } + if len(readTimeoutJSON) > 0 { + op.ReadTimeout = readTimeoutJSON + } + if len(connTimeoutJSON) > 0 { + op.ConnTimeout = connTimeoutJSON + } + op.PoolSize = poolSize + op.PathInfoPattern = pathInfoPattern + + op.State = HTTPFastcgiStateEnabled + return this.SaveInt64(tx, op) +} + +// UpdateFastcgi 修改Fastcgi +func (this *HTTPFastcgiDAO) UpdateFastcgi(tx *dbs.Tx, fastcgiId int64, isOn bool, address string, paramsJSON []byte, readTimeoutJSON []byte, connTimeoutJSON []byte, poolSize int32, pathInfoPattern string) error { + if fastcgiId <= 0 { + return errors.New("invalid 'fastcgiId'") + } + op := NewHTTPFastcgiOperator() + op.Id = fastcgiId + op.IsOn = isOn + op.Address = address + if len(paramsJSON) > 0 { + op.Params = paramsJSON + } + if len(readTimeoutJSON) > 0 { + op.ReadTimeout = readTimeoutJSON + } + if len(connTimeoutJSON) > 0 { + op.ConnTimeout = connTimeoutJSON + } + op.PoolSize = poolSize + op.PathInfoPattern = pathInfoPattern + err := this.Save(tx, op) + if err != nil { + return err + } + return this.NotifyUpdate(tx, fastcgiId) +} + +// CheckUserFastcgi 检查用户Fastcgi权限 +func (this *HTTPFastcgiDAO) CheckUserFastcgi(tx *dbs.Tx, userId int64, fastcgiId int64) error { + if userId <= 0 || fastcgiId <= 0 { + return errors.New("permission error") + } + exists, err := this.Query(tx). + Pk(fastcgiId). + Attr("userId", userId). + State(HTTPFastcgiStateEnabled). + Exist() + if err != nil { + return err + } + if !exists { + return errors.New("permission error") + } + return nil +} + +// NotifyUpdate 通知更新 +func (this *HTTPFastcgiDAO) NotifyUpdate(tx *dbs.Tx, fastcgiId int64) error { + webId, err := SharedHTTPWebDAO.FindEnabledWebIdWithFastcgiId(tx, fastcgiId) + if err != nil { + return err + } + if webId > 0 { + return SharedHTTPWebDAO.NotifyUpdate(tx, webId) + } + return nil +} diff --git a/internal/db/models/http_fastcgi_dao_test.go b/internal/db/models/http_fastcgi_dao_test.go new file mode 100644 index 00000000..224e9db7 --- /dev/null +++ b/internal/db/models/http_fastcgi_dao_test.go @@ -0,0 +1,6 @@ +package models + +import ( + _ "github.com/go-sql-driver/mysql" + _ "github.com/iwind/TeaGo/bootstrap" +) diff --git a/internal/db/models/http_fastcgi_model.go b/internal/db/models/http_fastcgi_model.go new file mode 100644 index 00000000..d729817b --- /dev/null +++ b/internal/db/models/http_fastcgi_model.go @@ -0,0 +1,34 @@ +package models + +// HTTPFastcgi Fastcgi设置 +type HTTPFastcgi struct { + Id uint64 `field:"id"` // ID + AdminId uint32 `field:"adminId"` // 管理员ID + UserId uint32 `field:"userId"` // 用户ID + IsOn uint8 `field:"isOn"` // 是否启用 + Address string `field:"address"` // 地址 + Params string `field:"params"` // 参数 + ReadTimeout string `field:"readTimeout"` // 读取超时 + ConnTimeout string `field:"connTimeout"` // 连接超时 + PoolSize uint32 `field:"poolSize"` // 连接池尺寸 + PathInfoPattern string `field:"pathInfoPattern"` // PATH_INFO匹配 + State uint8 `field:"state"` // 状态 +} + +type HTTPFastcgiOperator struct { + Id interface{} // ID + AdminId interface{} // 管理员ID + UserId interface{} // 用户ID + IsOn interface{} // 是否启用 + Address interface{} // 地址 + Params interface{} // 参数 + ReadTimeout interface{} // 读取超时 + ConnTimeout interface{} // 连接超时 + PoolSize interface{} // 连接池尺寸 + PathInfoPattern interface{} // PATH_INFO匹配 + State interface{} // 状态 +} + +func NewHTTPFastcgiOperator() *HTTPFastcgiOperator { + return &HTTPFastcgiOperator{} +} diff --git a/internal/db/models/http_fastcgi_model_ext.go b/internal/db/models/http_fastcgi_model_ext.go new file mode 100644 index 00000000..2640e7f9 --- /dev/null +++ b/internal/db/models/http_fastcgi_model_ext.go @@ -0,0 +1 @@ +package models diff --git a/internal/db/models/http_web_dao.go b/internal/db/models/http_web_dao.go index 0e2bd25f..e8fefd7a 100644 --- a/internal/db/models/http_web_dao.go +++ b/internal/db/models/http_web_dao.go @@ -44,7 +44,7 @@ func (this *HTTPWebDAO) Init() { _ = this.DAOObject.Init() } -// 启用条目 +// EnableHTTPWeb 启用条目 func (this *HTTPWebDAO) EnableHTTPWeb(tx *dbs.Tx, id int64) error { _, err := this.Query(tx). Pk(id). @@ -53,7 +53,7 @@ func (this *HTTPWebDAO) EnableHTTPWeb(tx *dbs.Tx, id int64) error { return err } -// 禁用条目 +// DisableHTTPWeb 禁用条目 func (this *HTTPWebDAO) DisableHTTPWeb(tx *dbs.Tx, id int64) error { _, err := this.Query(tx). Pk(id). @@ -62,7 +62,7 @@ func (this *HTTPWebDAO) DisableHTTPWeb(tx *dbs.Tx, id int64) error { return err } -// 查找启用中的条目 +// FindEnabledHTTPWeb 查找启用中的条目 func (this *HTTPWebDAO) FindEnabledHTTPWeb(tx *dbs.Tx, id int64) (*HTTPWeb, error) { result, err := this.Query(tx). Pk(id). @@ -74,7 +74,7 @@ func (this *HTTPWebDAO) FindEnabledHTTPWeb(tx *dbs.Tx, id int64) (*HTTPWeb, erro return result.(*HTTPWeb), err } -// 组合配置 +// ComposeWebConfig 组合配置 func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64) (*serverconfigs.HTTPWebConfig, error) { web, err := SharedHTTPWebDAO.FindEnabledHTTPWeb(tx, webId) if err != nil { @@ -323,10 +323,34 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64) (*serverconfig config.HostRedirects = redirects } + // Fastcgi + if IsNotNull(web.Fastcgi) { + ref := &serverconfigs.HTTPFastcgiRef{} + err = json.Unmarshal([]byte(web.Fastcgi), ref) + if err != nil { + return nil, err + } + config.FastcgiRef = ref + + if len(ref.FastcgiIds) > 0 { + list := []*serverconfigs.HTTPFastcgiConfig{} + for _, fastcgiId := range ref.FastcgiIds { + fastcgiConfig, err := SharedHTTPFastcgiDAO.ComposeFastcgiConfig(tx, fastcgiId) + if err != nil { + return nil, err + } + if fastcgiConfig != nil { + list = append(list, fastcgiConfig) + } + } + config.FastcgiList = list + } + } + return config, nil } -// 创建Web配置 +// CreateWeb 创建Web配置 func (this *HTTPWebDAO) CreateWeb(tx *dbs.Tx, adminId int64, userId int64, rootJSON []byte) (int64, error) { op := NewHTTPWebOperator() op.State = HTTPWebStateEnabled @@ -342,7 +366,7 @@ func (this *HTTPWebDAO) CreateWeb(tx *dbs.Tx, adminId int64, userId int64, rootJ return types.Int64(op.Id), nil } -// 修改Web配置 +// UpdateWeb 修改Web配置 func (this *HTTPWebDAO) UpdateWeb(tx *dbs.Tx, webId int64, rootJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -358,7 +382,7 @@ func (this *HTTPWebDAO) UpdateWeb(tx *dbs.Tx, webId int64, rootJSON []byte) erro return this.NotifyUpdate(tx, webId) } -// 修改Gzip配置 +// UpdateWebGzip 修改Gzip配置 func (this *HTTPWebDAO) UpdateWebGzip(tx *dbs.Tx, webId int64, gzipJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -374,7 +398,7 @@ func (this *HTTPWebDAO) UpdateWebGzip(tx *dbs.Tx, webId int64, gzipJSON []byte) return this.NotifyUpdate(tx, webId) } -// 修改字符编码 +// UpdateWebCharset 修改字符编码 func (this *HTTPWebDAO) UpdateWebCharset(tx *dbs.Tx, webId int64, charsetJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -390,7 +414,7 @@ func (this *HTTPWebDAO) UpdateWebCharset(tx *dbs.Tx, webId int64, charsetJSON [] return this.NotifyUpdate(tx, webId) } -// 更改请求Header策略 +// UpdateWebRequestHeaderPolicy 更改请求Header策略 func (this *HTTPWebDAO) UpdateWebRequestHeaderPolicy(tx *dbs.Tx, webId int64, headerPolicyJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -406,7 +430,7 @@ func (this *HTTPWebDAO) UpdateWebRequestHeaderPolicy(tx *dbs.Tx, webId int64, he return this.NotifyUpdate(tx, webId) } -// 更改响应Header策略 +// UpdateWebResponseHeaderPolicy 更改响应Header策略 func (this *HTTPWebDAO) UpdateWebResponseHeaderPolicy(tx *dbs.Tx, webId int64, headerPolicyJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -422,7 +446,7 @@ func (this *HTTPWebDAO) UpdateWebResponseHeaderPolicy(tx *dbs.Tx, webId int64, h return this.NotifyUpdate(tx, webId) } -// 更改特殊页面配置 +// UpdateWebPages 更改特殊页面配置 func (this *HTTPWebDAO) UpdateWebPages(tx *dbs.Tx, webId int64, pagesJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -438,7 +462,7 @@ func (this *HTTPWebDAO) UpdateWebPages(tx *dbs.Tx, webId int64, pagesJSON []byte return this.NotifyUpdate(tx, webId) } -// 更改Shutdown配置 +// UpdateWebShutdown 更改Shutdown配置 func (this *HTTPWebDAO) UpdateWebShutdown(tx *dbs.Tx, webId int64, shutdownJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -454,7 +478,7 @@ func (this *HTTPWebDAO) UpdateWebShutdown(tx *dbs.Tx, webId int64, shutdownJSON return this.NotifyUpdate(tx, webId) } -// 更改访问日志策略 +// UpdateWebAccessLogConfig 更改访问日志策略 func (this *HTTPWebDAO) UpdateWebAccessLogConfig(tx *dbs.Tx, webId int64, accessLogJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -470,7 +494,7 @@ func (this *HTTPWebDAO) UpdateWebAccessLogConfig(tx *dbs.Tx, webId int64, access return this.NotifyUpdate(tx, webId) } -// 更改统计配置 +// UpdateWebStat 更改统计配置 func (this *HTTPWebDAO) UpdateWebStat(tx *dbs.Tx, webId int64, statJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -486,7 +510,7 @@ func (this *HTTPWebDAO) UpdateWebStat(tx *dbs.Tx, webId int64, statJSON []byte) return this.NotifyUpdate(tx, webId) } -// 更改缓存配置 +// UpdateWebCache 更改缓存配置 func (this *HTTPWebDAO) UpdateWebCache(tx *dbs.Tx, webId int64, cacheJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -502,7 +526,7 @@ func (this *HTTPWebDAO) UpdateWebCache(tx *dbs.Tx, webId int64, cacheJSON []byte return this.NotifyUpdate(tx, webId) } -// 更改防火墙配置 +// UpdateWebFirewall 更改防火墙配置 func (this *HTTPWebDAO) UpdateWebFirewall(tx *dbs.Tx, webId int64, firewallJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -518,7 +542,7 @@ func (this *HTTPWebDAO) UpdateWebFirewall(tx *dbs.Tx, webId int64, firewallJSON return this.NotifyUpdate(tx, webId) } -// 更改路径规则配置 +// UpdateWebLocations 更改路径规则配置 func (this *HTTPWebDAO) UpdateWebLocations(tx *dbs.Tx, webId int64, locationsJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -534,7 +558,7 @@ func (this *HTTPWebDAO) UpdateWebLocations(tx *dbs.Tx, webId int64, locationsJSO return this.NotifyUpdate(tx, webId) } -// 更改跳转到HTTPS设置 +// UpdateWebRedirectToHTTPS 更改跳转到HTTPS设置 func (this *HTTPWebDAO) UpdateWebRedirectToHTTPS(tx *dbs.Tx, webId int64, redirectToHTTPSJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -550,7 +574,7 @@ func (this *HTTPWebDAO) UpdateWebRedirectToHTTPS(tx *dbs.Tx, webId int64, redire return this.NotifyUpdate(tx, webId) } -// 修改Websocket设置 +// UpdateWebsocket 修改Websocket设置 func (this *HTTPWebDAO) UpdateWebsocket(tx *dbs.Tx, webId int64, websocketJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -566,7 +590,23 @@ func (this *HTTPWebDAO) UpdateWebsocket(tx *dbs.Tx, webId int64, websocketJSON [ return this.NotifyUpdate(tx, webId) } -// 修改重写规则设置 +// UpdateWebFastcgi 修改Fastcgi设置 +func (this *HTTPWebDAO) UpdateWebFastcgi(tx *dbs.Tx, webId int64, fastcgiJSON []byte) error { + if webId <= 0 { + return errors.New("invalid webId") + } + op := NewHTTPWebOperator() + op.Id = webId + op.Fastcgi = JSONBytes(fastcgiJSON) + err := this.Save(tx, op) + if err != nil { + return err + } + + return this.NotifyUpdate(tx, webId) +} + +// UpdateWebRewriteRules 修改重写规则设置 func (this *HTTPWebDAO) UpdateWebRewriteRules(tx *dbs.Tx, webId int64, rewriteRulesJSON []byte) error { if webId <= 0 { return errors.New("invalid webId") @@ -582,7 +622,7 @@ func (this *HTTPWebDAO) UpdateWebRewriteRules(tx *dbs.Tx, webId int64, rewriteRu return this.NotifyUpdate(tx, webId) } -// 根据缓存策略ID查找所有的WebId +// FindAllWebIdsWithCachePolicyId 根据缓存策略ID查找所有的WebId func (this *HTTPWebDAO) FindAllWebIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId int64) ([]int64, error) { ones, err := this.Query(tx). State(HTTPWebStateEnabled). @@ -626,7 +666,7 @@ func (this *HTTPWebDAO) FindAllWebIdsWithCachePolicyId(tx *dbs.Tx, cachePolicyId return result, nil } -// 根据防火墙策略ID查找所有的WebId +// FindAllWebIdsWithHTTPFirewallPolicyId 根据防火墙策略ID查找所有的WebId func (this *HTTPWebDAO) FindAllWebIdsWithHTTPFirewallPolicyId(tx *dbs.Tx, firewallPolicyId int64) ([]int64, error) { ones, err := this.Query(tx). State(HTTPWebStateEnabled). @@ -673,7 +713,7 @@ func (this *HTTPWebDAO) FindAllWebIdsWithHTTPFirewallPolicyId(tx *dbs.Tx, firewa return result, nil } -// 查找包含某个Location的Web +// FindEnabledWebIdWithLocationId 查找包含某个Location的Web func (this *HTTPWebDAO) FindEnabledWebIdWithLocationId(tx *dbs.Tx, locationId int64) (webId int64, err error) { return this.Query(tx). State(HTTPWebStateEnabled). @@ -683,7 +723,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithLocationId(tx *dbs.Tx, locationId in FindInt64Col(0) } -// 查找包含某个重写规则的Web +// FindEnabledWebIdWithRewriteRuleId 查找包含某个重写规则的Web func (this *HTTPWebDAO) FindEnabledWebIdWithRewriteRuleId(tx *dbs.Tx, rewriteRuleId int64) (webId int64, err error) { return this.Query(tx). State(HTTPWebStateEnabled). @@ -693,7 +733,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithRewriteRuleId(tx *dbs.Tx, rewriteRul FindInt64Col(0) } -// 查找包含某个页面的Web +// FindEnabledWebIdWithPageId 查找包含某个页面的Web func (this *HTTPWebDAO) FindEnabledWebIdWithPageId(tx *dbs.Tx, pageId int64) (webId int64, err error) { return this.Query(tx). State(HTTPWebStateEnabled). @@ -703,7 +743,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithPageId(tx *dbs.Tx, pageId int64) (we FindInt64Col(0) } -// 查找包含某个Header的Web +// FindEnabledWebIdWithHeaderPolicyId 查找包含某个Header的Web func (this *HTTPWebDAO) FindEnabledWebIdWithHeaderPolicyId(tx *dbs.Tx, headerPolicyId int64) (webId int64, err error) { return this.Query(tx). State(HTTPWebStateEnabled). @@ -713,7 +753,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithHeaderPolicyId(tx *dbs.Tx, headerPol FindInt64Col(0) } -// 查找包含某个Gzip配置的Web +// FindEnabledWebIdWithGzipId 查找包含某个Gzip配置的Web func (this *HTTPWebDAO) FindEnabledWebIdWithGzipId(tx *dbs.Tx, gzipId int64) (webId int64, err error) { return this.Query(tx). State(HTTPWebStateEnabled). @@ -723,7 +763,7 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithGzipId(tx *dbs.Tx, gzipId int64) (we FindInt64Col(0) } -// 查找包含某个Websocket配置的Web +// FindEnabledWebIdWithWebsocketId 查找包含某个Websocket配置的Web func (this *HTTPWebDAO) FindEnabledWebIdWithWebsocketId(tx *dbs.Tx, websocketId int64) (webId int64, err error) { return this.Query(tx). State(HTTPWebStateEnabled). @@ -733,7 +773,17 @@ func (this *HTTPWebDAO) FindEnabledWebIdWithWebsocketId(tx *dbs.Tx, websocketId FindInt64Col(0) } -// 查找使用此Web的Server +// FindEnabledWebIdWithFastcgiId 查找包含某个Fastcgi配置的Web +func (this *HTTPWebDAO) FindEnabledWebIdWithFastcgiId(tx *dbs.Tx, fastcgiId int64) (webId int64, err error) { + return this.Query(tx). + State(HTTPWebStateEnabled). + ResultPk(). + Where("JSON_CONTAINS(fastcgi, :jsonQuery)"). + Param("jsonQuery", maps.Map{"fastcgiIds": fastcgiId}.AsJSON()). + FindInt64Col(0) +} + +// FindWebServerId 查找使用此Web的Server func (this *HTTPWebDAO) FindWebServerId(tx *dbs.Tx, webId int64) (serverId int64, err error) { if webId <= 0 { return 0, nil @@ -766,7 +816,7 @@ func (this *HTTPWebDAO) FindWebServerId(tx *dbs.Tx, webId int64) (serverId int64 return this.FindWebServerId(tx, webId) } -// 检查用户权限 +// CheckUserWeb 检查用户权限 func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) error { serverId, err := this.FindWebServerId(tx, webId) if err != nil { @@ -778,7 +828,7 @@ func (this *HTTPWebDAO) CheckUserWeb(tx *dbs.Tx, userId int64, webId int64) erro return SharedServerDAO.CheckUserServer(tx, userId, serverId) } -// 设置主机跳转 +// UpdateWebHostRedirects 设置主机跳转 func (this *HTTPWebDAO) UpdateWebHostRedirects(tx *dbs.Tx, webId int64, hostRedirects []*serverconfigs.HTTPHostRedirectConfig) error { if webId <= 0 { return errors.New("invalid ") @@ -801,7 +851,7 @@ func (this *HTTPWebDAO) UpdateWebHostRedirects(tx *dbs.Tx, webId int64, hostRedi return this.NotifyUpdate(tx, webId) } -// 查找主机跳转 +// FindWebHostRedirects 查找主机跳转 func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, error) { col, err := this.Query(tx). Pk(webId). @@ -813,7 +863,7 @@ func (this *HTTPWebDAO) FindWebHostRedirects(tx *dbs.Tx, webId int64) ([]byte, e return []byte(col), nil } -// 通知更新 +// NotifyUpdate 通知更新 func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error { serverId, err := this.FindWebServerId(tx, webId) if err != nil { diff --git a/internal/db/models/http_web_model.go b/internal/db/models/http_web_model.go index c30f8255..edfb59e6 100644 --- a/internal/db/models/http_web_model.go +++ b/internal/db/models/http_web_model.go @@ -1,6 +1,6 @@ package models -// HTTP Web +// HTTPWeb HTTP Web type HTTPWeb struct { Id uint32 `field:"id"` // ID IsOn uint8 `field:"isOn"` // 是否启用 @@ -27,6 +27,7 @@ type HTTPWeb struct { Websocket string `field:"websocket"` // Websocket设置 RewriteRules string `field:"rewriteRules"` // 重写规则配置 HostRedirects string `field:"hostRedirects"` // 域名跳转 + Fastcgi string `field:"fastcgi"` // Fastcgi配置 } type HTTPWebOperator struct { @@ -55,6 +56,7 @@ type HTTPWebOperator struct { Websocket interface{} // Websocket设置 RewriteRules interface{} // 重写规则配置 HostRedirects interface{} // 域名跳转 + Fastcgi interface{} // Fastcgi配置 } func NewHTTPWebOperator() *HTTPWebOperator { diff --git a/internal/nodes/api_node.go b/internal/nodes/api_node.go index 739fb771..760a154f 100644 --- a/internal/nodes/api_node.go +++ b/internal/nodes/api_node.go @@ -266,6 +266,7 @@ func (this *APINode) listenRPC(listener net.Listener, tlsConfig *tls.Config) err pb.RegisterAuthorityNodeServiceServer(rpcServer, &services.AuthorityNodeService{}) pb.RegisterLatestItemServiceServer(rpcServer, &services.LatestItemService{}) pb.RegisterNodeThresholdServiceServer(rpcServer, &services.NodeThresholdService{}) + pb.RegisterHTTPFastcgiServiceServer(rpcServer, &services.HTTPFastcgiService{}) err := rpcServer.Serve(listener) if err != nil { return errors.New("[API_NODE]start rpc failed: " + err.Error()) diff --git a/internal/rpc/services/service_http_fastcgi.go b/internal/rpc/services/service_http_fastcgi.go new file mode 100644 index 00000000..7fe99d49 --- /dev/null +++ b/internal/rpc/services/service_http_fastcgi.go @@ -0,0 +1,112 @@ +// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package services + +import ( + "context" + "encoding/json" + "github.com/TeaOSLab/EdgeAPI/internal/db/models" + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" + "github.com/iwind/TeaGo/types" +) + +// HTTPFastcgiService HTTP Fastcgi服务 +type HTTPFastcgiService struct { + BaseService +} + +// CreateHTTPFastcgi 创建Fastcgi +func (this *HTTPFastcgiService) CreateHTTPFastcgi(ctx context.Context, req *pb.CreateHTTPFastcgiRequest) (*pb.CreateHTTPFastcgiResponse, error) { + adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + fastcgiId, err := models.SharedHTTPFastcgiDAO.CreateFastcgi(tx, adminId, userId, req.IsOn, req.Address, req.ParamsJSON, req.ReadTimeoutJSON, req.ConnTimeoutJSON, req.PoolSize, req.PathInfoPattern) + if err != nil { + return nil, err + } + return &pb.CreateHTTPFastcgiResponse{HttpFastcgiId: fastcgiId}, nil +} + +// UpdateHTTPFastcgi 修改Fastcgi +func (this *HTTPFastcgiService) UpdateHTTPFastcgi(ctx context.Context, req *pb.UpdateHTTPFastcgiRequest) (*pb.RPCSuccess, error) { + _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + if userId > 0 { + err = models.SharedHTTPFastcgiDAO.CheckUserFastcgi(tx, userId, req.HttpFastcgiId) + if err != nil { + return nil, err + } + } + err = models.SharedHTTPFastcgiDAO.UpdateFastcgi(tx, req.HttpFastcgiId, req.IsOn, req.Address, req.ParamsJSON, req.ReadTimeoutJSON, req.ConnTimeoutJSON, req.PoolSize, req.PathInfoPattern) + if err != nil { + return nil, err + } + return this.Success() +} + +// FindEnabledHTTPFastcgi 获取Fastcgi详情 +func (this *HTTPFastcgiService) FindEnabledHTTPFastcgi(ctx context.Context, req *pb.FindEnabledHTTPFastcgiRequest) (*pb.FindEnabledHTTPFastcgiResponse, error) { + _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + if userId > 0 { + err = models.SharedHTTPFastcgiDAO.CheckUserFastcgi(tx, userId, req.HttpFastcgiId) + if err != nil { + return nil, err + } + } + + fastcgi, err := models.SharedHTTPFastcgiDAO.FindEnabledHTTPFastcgi(tx, req.HttpFastcgiId) + if err != nil { + return nil, err + } + if fastcgi == nil { + return &pb.FindEnabledHTTPFastcgiResponse{HttpFastcgi: nil}, nil + } + return &pb.FindEnabledHTTPFastcgiResponse{HttpFastcgi: &pb.HTTPFastcgi{ + Id: int64(fastcgi.Id), + IsOn: fastcgi.IsOn == 1, + Address: fastcgi.Address, + ParamsJSON: []byte(fastcgi.Params), + ReadTimeoutJSON: []byte(fastcgi.ReadTimeout), + ConnTimeoutJSON: []byte(fastcgi.ConnTimeout), + PoolSize: types.Int32(fastcgi.PoolSize), + PathInfoPattern: fastcgi.PathInfoPattern, + }}, nil +} + +// FindEnabledHTTPFastcgiConfig 获取Fastcgi配置 +func (this *HTTPFastcgiService) FindEnabledHTTPFastcgiConfig(ctx context.Context, req *pb.FindEnabledHTTPFastcgiConfigRequest) (*pb.FindEnabledHTTPFastcgiConfigResponse, error) { + _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + var tx = this.NullTx() + if userId > 0 { + err = models.SharedHTTPFastcgiDAO.CheckUserFastcgi(tx, userId, req.HttpFastcgiId) + if err != nil { + return nil, err + } + } + + config, err := models.SharedHTTPFastcgiDAO.ComposeFastcgiConfig(tx, req.HttpFastcgiId) + if err != nil { + return nil, err + } + configJSON, err := json.Marshal(config) + if err != nil { + return nil, err + } + return &pb.FindEnabledHTTPFastcgiConfigResponse{HttpFastcgiJSON: configJSON}, nil +} diff --git a/internal/rpc/services/service_http_web.go b/internal/rpc/services/service_http_web.go index c24c3c2e..35012514 100644 --- a/internal/rpc/services/service_http_web.go +++ b/internal/rpc/services/service_http_web.go @@ -14,7 +14,7 @@ type HTTPWebService struct { BaseService } -// 创建Web配置 +// CreateHTTPWeb 创建Web配置 func (this *HTTPWebService) CreateHTTPWeb(ctx context.Context, req *pb.CreateHTTPWebRequest) (*pb.CreateHTTPWebResponse, error) { // 校验请求 adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -32,7 +32,7 @@ func (this *HTTPWebService) CreateHTTPWeb(ctx context.Context, req *pb.CreateHTT return &pb.CreateHTTPWebResponse{WebId: webId}, nil } -// 查找Web配置 +// FindEnabledHTTPWeb 查找Web配置 func (this *HTTPWebService) FindEnabledHTTPWeb(ctx context.Context, req *pb.FindEnabledHTTPWebRequest) (*pb.FindEnabledHTTPWebResponse, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -65,7 +65,7 @@ func (this *HTTPWebService) FindEnabledHTTPWeb(ctx context.Context, req *pb.Find return &pb.FindEnabledHTTPWebResponse{Web: result}, nil } -// 查找Web配置 +// FindEnabledHTTPWebConfig 查找Web配置 func (this *HTTPWebService) FindEnabledHTTPWebConfig(ctx context.Context, req *pb.FindEnabledHTTPWebConfigRequest) (*pb.FindEnabledHTTPWebConfigResponse, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -95,7 +95,7 @@ func (this *HTTPWebService) FindEnabledHTTPWebConfig(ctx context.Context, req *p return &pb.FindEnabledHTTPWebConfigResponse{WebJSON: configJSON}, nil } -// 修改Web配置 +// UpdateHTTPWeb 修改Web配置 func (this *HTTPWebService) UpdateHTTPWeb(ctx context.Context, req *pb.UpdateHTTPWebRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -121,7 +121,7 @@ func (this *HTTPWebService) UpdateHTTPWeb(ctx context.Context, req *pb.UpdateHTT return this.Success() } -// 修改Gzip配置 +// UpdateHTTPWebGzip 修改Gzip配置 func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.UpdateHTTPWebGzipRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -147,7 +147,7 @@ func (this *HTTPWebService) UpdateHTTPWebGzip(ctx context.Context, req *pb.Updat return this.Success() } -// 修改字符集配置 +// UpdateHTTPWebCharset 修改字符集配置 func (this *HTTPWebService) UpdateHTTPWebCharset(ctx context.Context, req *pb.UpdateHTTPWebCharsetRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -172,7 +172,7 @@ func (this *HTTPWebService) UpdateHTTPWebCharset(ctx context.Context, req *pb.Up return this.Success() } -// 更改请求Header策略 +// UpdateHTTPWebRequestHeader 更改请求Header策略 func (this *HTTPWebService) UpdateHTTPWebRequestHeader(ctx context.Context, req *pb.UpdateHTTPWebRequestHeaderRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -198,7 +198,7 @@ func (this *HTTPWebService) UpdateHTTPWebRequestHeader(ctx context.Context, req return this.Success() } -// 更改响应Header策略 +// UpdateHTTPWebResponseHeader 更改响应Header策略 func (this *HTTPWebService) UpdateHTTPWebResponseHeader(ctx context.Context, req *pb.UpdateHTTPWebResponseHeaderRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -224,7 +224,7 @@ func (this *HTTPWebService) UpdateHTTPWebResponseHeader(ctx context.Context, req return this.Success() } -// 更改Shutdown +// UpdateHTTPWebShutdown 更改Shutdown func (this *HTTPWebService) UpdateHTTPWebShutdown(ctx context.Context, req *pb.UpdateHTTPWebShutdownRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -249,7 +249,7 @@ func (this *HTTPWebService) UpdateHTTPWebShutdown(ctx context.Context, req *pb.U return this.Success() } -// 更改Pages +// UpdateHTTPWebPages 更改Pages func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.UpdateHTTPWebPagesRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -274,7 +274,7 @@ func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.Upda return this.Success() } -// 更改访问日志配置 +// UpdateHTTPWebAccessLog 更改访问日志配置 func (this *HTTPWebService) UpdateHTTPWebAccessLog(ctx context.Context, req *pb.UpdateHTTPWebAccessLogRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -299,7 +299,7 @@ func (this *HTTPWebService) UpdateHTTPWebAccessLog(ctx context.Context, req *pb. return this.Success() } -// 更改统计配置 +// UpdateHTTPWebStat 更改统计配置 func (this *HTTPWebService) UpdateHTTPWebStat(ctx context.Context, req *pb.UpdateHTTPWebStatRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -324,7 +324,7 @@ func (this *HTTPWebService) UpdateHTTPWebStat(ctx context.Context, req *pb.Updat return this.Success() } -// 更改缓存配置 +// UpdateHTTPWebCache 更改缓存配置 func (this *HTTPWebService) UpdateHTTPWebCache(ctx context.Context, req *pb.UpdateHTTPWebCacheRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -350,7 +350,7 @@ func (this *HTTPWebService) UpdateHTTPWebCache(ctx context.Context, req *pb.Upda return this.Success() } -// 更改防火墙设置 +// UpdateHTTPWebFirewall 更改防火墙设置 func (this *HTTPWebService) UpdateHTTPWebFirewall(ctx context.Context, req *pb.UpdateHTTPWebFirewallRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -376,7 +376,7 @@ func (this *HTTPWebService) UpdateHTTPWebFirewall(ctx context.Context, req *pb.U return this.Success() } -// 更改路径规则设置 +// UpdateHTTPWebLocations 更改路径规则设置 func (this *HTTPWebService) UpdateHTTPWebLocations(ctx context.Context, req *pb.UpdateHTTPWebLocationsRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -402,7 +402,7 @@ func (this *HTTPWebService) UpdateHTTPWebLocations(ctx context.Context, req *pb. return this.Success() } -// 更改跳转到HTTPS设置 +// UpdateHTTPWebRedirectToHTTPS 更改跳转到HTTPS设置 func (this *HTTPWebService) UpdateHTTPWebRedirectToHTTPS(ctx context.Context, req *pb.UpdateHTTPWebRedirectToHTTPSRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -427,7 +427,7 @@ func (this *HTTPWebService) UpdateHTTPWebRedirectToHTTPS(ctx context.Context, re return this.Success() } -// 更改Websocket设置 +// UpdateHTTPWebWebsocket 更改Websocket设置 func (this *HTTPWebService) UpdateHTTPWebWebsocket(ctx context.Context, req *pb.UpdateHTTPWebWebsocketRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -451,7 +451,31 @@ func (this *HTTPWebService) UpdateHTTPWebWebsocket(ctx context.Context, req *pb. return this.Success() } -// 更改重写规则设置 +// UpdateHTTPWebFastcgi 更改Fastcgi设置 +func (this *HTTPWebService) UpdateHTTPWebFastcgi(ctx context.Context, req *pb.UpdateHTTPWebFastcgiRequest) (*pb.RPCSuccess, error) { + // 校验请求 + _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) + if err != nil { + return nil, err + } + + if userId > 0 { + err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.WebId) + if err != nil { + return nil, err + } + } + + tx := this.NullTx() + + err = models.SharedHTTPWebDAO.UpdateWebFastcgi(tx, req.WebId, req.FastcgiJSON) + if err != nil { + return nil, err + } + return this.Success() +} + +// UpdateHTTPWebRewriteRules 更改重写规则设置 func (this *HTTPWebService) UpdateHTTPWebRewriteRules(ctx context.Context, req *pb.UpdateHTTPWebRewriteRulesRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -475,7 +499,7 @@ func (this *HTTPWebService) UpdateHTTPWebRewriteRules(ctx context.Context, req * return this.Success() } -// 更改主机跳转设置 +// UpdateHTTPWebHostRedirects 更改主机跳转设置 func (this *HTTPWebService) UpdateHTTPWebHostRedirects(ctx context.Context, req *pb.UpdateHTTPWebHostRedirectsRequest) (*pb.RPCSuccess, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) @@ -515,7 +539,7 @@ func (this *HTTPWebService) UpdateHTTPWebHostRedirects(ctx context.Context, req return this.Success() } -// 查找主机跳转设置 +// FindHTTPWebHostRedirects 查找主机跳转设置 func (this *HTTPWebService) FindHTTPWebHostRedirects(ctx context.Context, req *pb.FindHTTPWebHostRedirectsRequest) (*pb.FindHTTPWebHostRedirectsResponse, error) { // 校验请求 _, userId, err := this.ValidateAdminAndUser(ctx, 0, 0) diff --git a/internal/rpc/services/sevice_http_gzip.go b/internal/rpc/services/sevice_http_gzip.go index 4adddbae..921597f7 100644 --- a/internal/rpc/services/sevice_http_gzip.go +++ b/internal/rpc/services/sevice_http_gzip.go @@ -13,7 +13,7 @@ type HTTPGzipService struct { BaseService } -// 创建Gzip配置 +// CreateHTTPGzip 创建Gzip配置 func (this *HTTPGzipService) CreateHTTPGzip(ctx context.Context, req *pb.CreateHTTPGzipRequest) (*pb.CreateHTTPGzipResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -50,10 +50,10 @@ func (this *HTTPGzipService) CreateHTTPGzip(ctx context.Context, req *pb.CreateH return nil, err } - return &pb.CreateHTTPGzipResponse{GzipId: gzipId}, nil + return &pb.CreateHTTPGzipResponse{HttpGzipId: gzipId}, nil } -// 查找Gzip +// FindEnabledHTTPGzipConfig 查找Gzip func (this *HTTPGzipService) FindEnabledHTTPGzipConfig(ctx context.Context, req *pb.FindEnabledGzipConfigRequest) (*pb.FindEnabledGzipConfigResponse, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -63,7 +63,7 @@ func (this *HTTPGzipService) FindEnabledHTTPGzipConfig(ctx context.Context, req tx := this.NullTx() - config, err := models.SharedHTTPGzipDAO.ComposeGzipConfig(tx, req.GzipId) + config, err := models.SharedHTTPGzipDAO.ComposeGzipConfig(tx, req.HttpGzipId) if err != nil { return nil, err } @@ -72,10 +72,10 @@ func (this *HTTPGzipService) FindEnabledHTTPGzipConfig(ctx context.Context, req if err != nil { return nil, err } - return &pb.FindEnabledGzipConfigResponse{GzipJSON: configData}, nil + return &pb.FindEnabledGzipConfigResponse{HttpGzipJSON: configData}, nil } -// 修改Gzip配置 +// UpdateHTTPGzip 修改Gzip配置 func (this *HTTPGzipService) UpdateHTTPGzip(ctx context.Context, req *pb.UpdateHTTPGzipRequest) (*pb.RPCSuccess, error) { // 校验请求 _, _, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeAdmin) @@ -107,7 +107,7 @@ func (this *HTTPGzipService) UpdateHTTPGzip(ctx context.Context, req *pb.UpdateH tx := this.NullTx() - err = models.SharedHTTPGzipDAO.UpdateGzip(tx, req.GzipId, int(req.Level), minLengthJSON, maxLengthJSON, req.CondsJSON) + err = models.SharedHTTPGzipDAO.UpdateGzip(tx, req.HttpGzipId, int(req.Level), minLengthJSON, maxLengthJSON, req.CondsJSON) if err != nil { return nil, err }