Skip to content

Commit

Permalink
add operation log
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyale committed Apr 22, 2021
1 parent 56962ec commit 916468d
Show file tree
Hide file tree
Showing 32 changed files with 224 additions and 218 deletions.
6 changes: 3 additions & 3 deletions backend/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,11 @@ func UpdateDestinations(app *models.Application, destinations []interface{}) {
// UpdateAppDomains ...
func UpdateAppDomains(app *models.Application, appDomains []interface{}) {
newAppDomains := []*models.Domain{}
newDomainNames := []string{}
//newDomainNames := []string{}
for _, domainMap := range appDomains {
domain := UpdateDomain(app, domainMap)
newAppDomains = append(newAppDomains, domain)
newDomainNames = append(newDomainNames, domain.Name)
//newDomainNames = append(newDomainNames, domain.Name)
}
for _, oldDomain := range app.Domains {
if !InterfaceContainsDomainID(appDomains, oldDomain.ID) {
Expand Down Expand Up @@ -419,7 +419,7 @@ func DeleteApplicationByID(appID int64, clientIP string, authUser *models.AuthUs
}
DeleteDomainsByApp(app)
DeleteDestinationsByApp(appID)
err = firewall.DeleteCCPolicyByAppID(appID, authUser, false)
err = firewall.DeleteCCPolicyByAppID(appID, clientIP, authUser, false)
if err != nil {
utils.DebugPrintln("DeleteApplicationByID DeleteCCPolicyByAppID", err)
}
Expand Down
16 changes: 8 additions & 8 deletions backend/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ func InitDatabase() {
}
}
// 0.9.13 alter column ccpolicies type
err = dal.ExecSQL(`ALTER TABLE "ccpolicies" ALTER COLUMN "interval_milliseconds" TYPE double precision`)
if err != nil {
//utils.DebugPrintln("InitDatabase ALTER TABLE ccpolicies ALTER COLUMN interval_milliseconds", err)
}
_ = dal.ExecSQL(`ALTER TABLE "ccpolicies" ALTER COLUMN "interval_milliseconds" TYPE double precision`)
//if err != nil {
//utils.DebugPrintln("InitDatabase ALTER TABLE ccpolicies ALTER COLUMN interval_milliseconds", err)
//}
// 0.9.13 alter column block_seconds type
err = dal.ExecSQL(`ALTER TABLE "ccpolicies" ALTER COLUMN "block_seconds" TYPE double precision`)
if err != nil {
//utils.DebugPrintln("InitDatabase ALTER TABLE ccpolicies ALTER COLUMN block_seconds", err)
}
_ = dal.ExecSQL(`ALTER TABLE "ccpolicies" ALTER COLUMN "block_seconds" TYPE double precision`)
//if err != nil {
//utils.DebugPrintln("InitDatabase ALTER TABLE ccpolicies ALTER COLUMN block_seconds", err)
//}
}

// LoadAppConfiguration ...
Expand Down
2 changes: 1 addition & 1 deletion backend/vip_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func UDPForwarding(vipApp *models.VipApp, udpListenConn *net.UDPConn) {
}
if vipTarget != nil {
vipTarget.CheckTime = time.Now().Unix()
targetAddr, err := net.ResolveUDPAddr("udp", vipTarget.Destination)
targetAddr, _ := net.ResolveUDPAddr("udp", vipTarget.Destination)
udpTargetConn, err := net.DialUDP("udp", nil, targetAddr)
if err != nil {
utils.DebugPrintln("UDPForwarding DialUDP could not connect to target", vipTarget.Destination, err)
Expand Down
4 changes: 2 additions & 2 deletions backend/vip_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func DeleteVipTargetsByAppID(id int64) {
func CheckOfflineVipTargets(nowTimeStamp int64) {
for _, vipApp := range VipApps {
for _, target := range vipApp.Targets {
if target.Online == false {
if !target.Online {
go func(vApp *models.VipApp, vTarget *models.VipTarget) {
var conn net.Conn
var err error
Expand All @@ -39,7 +39,7 @@ func CheckOfflineVipTargets(nowTimeStamp int64) {
vTarget.CheckTime = nowTimeStamp
}
} else {
targetAddr, err := net.ResolveUDPAddr("udp", vTarget.Destination)
targetAddr, _ := net.ResolveUDPAddr("udp", vTarget.Destination)
udpTargetConn, err := net.DialUDP("udp", nil, targetAddr)
if err != nil {
vTarget.Online = false
Expand Down
8 changes: 4 additions & 4 deletions data/backend_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ func (dal *MyDAL) InsertApplication(appName string, internalScheme string, redir
// UpdateApplication update an Application
func (dal *MyDAL) UpdateApplication(appName string, internalScheme string, redirectHTTPS bool, hstsEnabled bool, wafEnabled bool, ipMethod models.IPMethod, description string, oauthRequired bool, sessionSeconds int64, owner string, cspEnabled bool, csp string, appID int64) error {
const sqlUpdateApplication = `UPDATE "applications" SET "name"=$1,"internal_scheme"=$2,"redirect_https"=$3,"hsts_enabled"=$4,"waf_enabled"=$5,"ip_method"=$6,"description"=$7,"oauth_required"=$8,"session_seconds"=$9,"owner"=$10,"csp_enabled"=$11,"csp"=$12 WHERE "id"=$13`
stmt, err := dal.db.Prepare(sqlUpdateApplication)
stmt, _ := dal.db.Prepare(sqlUpdateApplication)
defer stmt.Close()
_, err = stmt.Exec(appName, internalScheme, redirectHTTPS, hstsEnabled, wafEnabled, ipMethod, description, oauthRequired, sessionSeconds, owner, cspEnabled, csp, appID)
_, err := stmt.Exec(appName, internalScheme, redirectHTTPS, hstsEnabled, wafEnabled, ipMethod, description, oauthRequired, sessionSeconds, owner, cspEnabled, csp, appID)
utils.CheckError("UpdateApplication", err)
return err
}

// DeleteApplication delete an Application
func (dal *MyDAL) DeleteApplication(appID int64) error {
const sqlDeleteApplication = `DELETE FROM "applications" WHERE "id"=$1`
stmt, err := dal.db.Prepare(sqlDeleteApplication)
stmt, _ := dal.db.Prepare(sqlDeleteApplication)
defer stmt.Close()
_, err = stmt.Exec(appID)
_, err := stmt.Exec(appID)
utils.CheckError("DeleteApplication", err)
return err
}
14 changes: 7 additions & 7 deletions data/backend_appuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (dal *MyDAL) SelectAppUsers() []*models.QueryAppUser {
var queryUsers = []*models.QueryAppUser{}
for rows.Next() {
queryUser := &models.QueryAppUser{}
err = rows.Scan(&queryUser.ID, &queryUser.Username, &queryUser.Email, &queryUser.IsSuperAdmin, &queryUser.IsCertAdmin, &queryUser.IsAppAdmin)
_ = rows.Scan(&queryUser.ID, &queryUser.Username, &queryUser.Email, &queryUser.IsSuperAdmin, &queryUser.IsCertAdmin, &queryUser.IsAppAdmin)
queryUsers = append(queryUsers, queryUser)
}
return queryUsers
Expand All @@ -111,27 +111,27 @@ func (dal *MyDAL) SelectAppUserByID(userID int64) *models.QueryAppUser {

// UpdateAppUserWithPwd ...
func (dal *MyDAL) UpdateAppUserWithPwd(username string, hashpwd string, salt string, email string, isSuperAdmin, isCertAdmin, isAppAdmin bool, needModifyPwd bool, userID int64) error {
stmt, err := dal.db.Prepare(sqlUpdateAppUserWithPwd)
stmt, _ := dal.db.Prepare(sqlUpdateAppUserWithPwd)
defer stmt.Close()
_, err = stmt.Exec(username, hashpwd, salt, email, isSuperAdmin, isCertAdmin, isAppAdmin, needModifyPwd, userID)
_, err := stmt.Exec(username, hashpwd, salt, email, isSuperAdmin, isCertAdmin, isAppAdmin, needModifyPwd, userID)
utils.CheckError("UpdateAppUserWithPwd", err)
return err
}

// UpdateAppUserNoPwd ...
func (dal *MyDAL) UpdateAppUserNoPwd(username string, email string, isSuperAdmin, isCertAdmin, isAppAdmin bool, userID int64) error {
stmt, err := dal.db.Prepare(sqlUpdateAppUserNoPwd)
stmt, _ := dal.db.Prepare(sqlUpdateAppUserNoPwd)
defer stmt.Close()
_, err = stmt.Exec(username, email, isSuperAdmin, isCertAdmin, isAppAdmin, userID)
_, err := stmt.Exec(username, email, isSuperAdmin, isCertAdmin, isAppAdmin, userID)
utils.CheckError("UpdateAppUserNoPwd", err)
return err
}

// DeleteAppUser ...
func (dal *MyDAL) DeleteAppUser(userID int64) error {
stmt, err := dal.db.Prepare(sqlDeleteAppUser)
stmt, _ := dal.db.Prepare(sqlDeleteAppUser)
defer stmt.Close()
_, err = stmt.Exec(userID)
_, err := stmt.Exec(userID)
utils.CheckError("DeleteAppUser", err)
return err
}
10 changes: 5 additions & 5 deletions data/backend_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (dal *MyDAL) SelectCertificates() []*models.DBCertItem {
var dbCerts = []*models.DBCertItem{}
for rows.Next() {
dbCert := &models.DBCertItem{}
err = rows.Scan(&dbCert.ID, &dbCert.CommonName,
_ = rows.Scan(&dbCert.ID, &dbCert.CommonName,
&dbCert.CertContent, &dbCert.EncryptedPrivKey,
&dbCert.ExpireTime, &dbCert.Description)
dbCerts = append(dbCerts, dbCert)
Expand All @@ -54,18 +54,18 @@ func (dal *MyDAL) InsertCertificate(commonName string, certContent string, encry

// UpdateCertificate ...
func (dal *MyDAL) UpdateCertificate(commonName string, certContent string, encryptedPrivKey []byte, expireTime int64, description string, id int64) error {
stmt, err := dal.db.Prepare(sqlUpdateCertificate)
stmt, _ := dal.db.Prepare(sqlUpdateCertificate)
defer stmt.Close()
_, err = stmt.Exec(commonName, certContent, encryptedPrivKey, expireTime, description, id)
_, err := stmt.Exec(commonName, certContent, encryptedPrivKey, expireTime, description, id)
utils.CheckError("UpdateCertificate", err)
return err
}

// DeleteCertificate by id
func (dal *MyDAL) DeleteCertificate(certID int64) error {
stmt, err := dal.db.Prepare(sqlDeleteCertificate)
stmt, _ := dal.db.Prepare(sqlDeleteCertificate)
defer stmt.Close()
_, err = stmt.Exec(certID)
_, err := stmt.Exec(certID)
utils.CheckError("DeleteCertificate", err)
return err
}
Expand Down
17 changes: 7 additions & 10 deletions data/backend_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
// UpdateDestinationNode ...
func (dal *MyDAL) UpdateDestinationNode(routeType int64, requestRoute string, backendRoute string, destination string, appID int64, nodeID int64, id int64) error {
const sqlUpdateDestinationNode = `UPDATE "destinations" SET "route_type"=$1,"request_route"=$2,"backend_route"=$3,"destination"=$4,"app_id"=$5,"node_id"=$6 WHERE "id"=$7`
stmt, err := dal.db.Prepare(sqlUpdateDestinationNode)
stmt, _ := dal.db.Prepare(sqlUpdateDestinationNode)
defer stmt.Close()
_, err = stmt.Exec(routeType, requestRoute, backendRoute, destination, appID, nodeID, id)
_, err := stmt.Exec(routeType, requestRoute, backendRoute, destination, appID, nodeID, id)
utils.CheckError("UpdateDestinationNode", err)
return err
}
Expand All @@ -28,10 +28,7 @@ func (dal *MyDAL) ExistsDestinationID(id int64) bool {
const sqlExistsDestinationID = `SELECT COALESCE((SELECT 1 FROM "destinations" WHERE "id"=$1 limit 1),0)`
err := dal.db.QueryRow(sqlExistsDestinationID, id).Scan(&exist)
utils.CheckError("ExistsDestinationID", err)
if exist == 0 {
return false
}
return true
return exist != 0
}

// CreateTableIfNotExistsDestinations ...
Expand Down Expand Up @@ -73,19 +70,19 @@ func (dal *MyDAL) InsertDestination(routeType int64, requestRoute string, backen
// DeleteDestinationByID ...
func (dal *MyDAL) DeleteDestinationByID(id int64) error {
const sqlDeleteDestinationByID = `DELETE FROM "destinations" WHERE "id"=$1`
stmt, err := dal.db.Prepare(sqlDeleteDestinationByID)
stmt, _ := dal.db.Prepare(sqlDeleteDestinationByID)
defer stmt.Close()
_, err = stmt.Exec(id)
_, err := stmt.Exec(id)
utils.CheckError("DeleteDestinationByID", err)
return err
}

// DeleteDestinationsByAppID ...
func (dal *MyDAL) DeleteDestinationsByAppID(appID int64) error {
const sqlDeleteDestinationsByAppID = `DELETE FROM "destinations" WHERE "app_id"=$1`
stmt, err := dal.db.Prepare(sqlDeleteDestinationsByAppID)
stmt, _ := dal.db.Prepare(sqlDeleteDestinationsByAppID)
defer stmt.Close()
_, err = stmt.Exec(appID)
_, err := stmt.Exec(appID)
utils.CheckError("DeleteDestinationsByAppID", err)
return err
}
10 changes: 5 additions & 5 deletions data/backend_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (dal *MyDAL) SelectDomains() []*models.DBDomain {
dbDomains := []*models.DBDomain{}
for rows.Next() {
dbDomain := &models.DBDomain{}
err = rows.Scan(&dbDomain.ID, &dbDomain.Name, &dbDomain.AppID, &dbDomain.CertID, &dbDomain.Redirect, &dbDomain.Location)
_ = rows.Scan(&dbDomain.ID, &dbDomain.Name, &dbDomain.AppID, &dbDomain.CertID, &dbDomain.Redirect, &dbDomain.Location)
dbDomains = append(dbDomains, dbDomain)
}
return dbDomains
Expand Down Expand Up @@ -66,18 +66,18 @@ func (dal *MyDAL) UpdateDomain(name string, appID int64, certID int64, redirect

// DeleteDomainByDomainID ...
func (dal *MyDAL) DeleteDomainByDomainID(domainID int64) error {
stmt, err := dal.db.Prepare(sqlDeleteDomainByDomainID)
stmt, _ := dal.db.Prepare(sqlDeleteDomainByDomainID)
defer stmt.Close()
_, err = stmt.Exec(domainID)
_, err := stmt.Exec(domainID)
utils.CheckError("DeleteDomainByDomainID", err)
return err
}

// DeleteDomainByAppID ...
func (dal *MyDAL) DeleteDomainByAppID(appID int64) error {
stmt, err := dal.db.Prepare(sqlDeleteDomainByAppID)
stmt, _ := dal.db.Prepare(sqlDeleteDomainByAppID)
defer stmt.Close()
_, err = stmt.Exec(appID)
_, err := stmt.Exec(appID)
utils.CheckError("DeleteDomainByAppID", err)
return err
}
6 changes: 3 additions & 3 deletions data/backend_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (dal *MyDAL) SelectAllNodes() []*models.DBNode {
dbNodes := []*models.DBNode{}
for rows.Next() {
dbNode := &models.DBNode{}
err = rows.Scan(&dbNode.ID, &dbNode.Version, &dbNode.LastIP, &dbNode.LastRequestTime)
_ = rows.Scan(&dbNode.ID, &dbNode.Version, &dbNode.LastIP, &dbNode.LastRequestTime)
dbNodes = append(dbNodes, dbNode)
}
return dbNodes
Expand All @@ -55,9 +55,9 @@ func (dal *MyDAL) InsertNode(version string, lastIP string, lastReqTime int64) (

// UpdateNodeLastInfo ...
func (dal *MyDAL) UpdateNodeLastInfo(version string, lastIP string, lastReqTime int64, id int64) error {
stmt, err := dal.db.Prepare(sqlUpdateNodeLastInfo)
stmt, _ := dal.db.Prepare(sqlUpdateNodeLastInfo)
defer stmt.Close()
_, err = stmt.Exec(version, lastIP, lastReqTime, id)
_, err := stmt.Exec(version, lastIP, lastReqTime, id)
utils.CheckError("UpdateNodeLastInfo", err)
return err
}
13 changes: 5 additions & 8 deletions data/backend_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ func (dal *MyDAL) ExistsSetting(name string) bool {
var exist int
err := dal.db.QueryRow(sqlExistsSetting, name).Scan(&exist)
utils.CheckError("ExistsSetting", err)
if exist == 0 {
return false
}
return true
return exist != 0
}

// SelectBoolSetting ...
Expand Down Expand Up @@ -66,7 +63,7 @@ func (dal *MyDAL) SelectStringSetting(name string) (value string, err error) {

// SaveBoolSetting ...
func (dal *MyDAL) SaveBoolSetting(name string, value bool) (err error) {
if dal.ExistsSetting(name) == true {
if dal.ExistsSetting(name) {
_, err = dal.db.Exec(sqlUpdateBoolSetting, value, name)
} else {
_, err = dal.db.Exec(sqlInsertBoolSetting, name, value)
Expand All @@ -76,7 +73,7 @@ func (dal *MyDAL) SaveBoolSetting(name string, value bool) (err error) {

// SaveIntSetting ...
func (dal *MyDAL) SaveIntSetting(name string, value int64) (err error) {
if dal.ExistsSetting(name) == true {
if dal.ExistsSetting(name) {
_, err = dal.db.Exec(sqlUpdateIntSetting, value, name)
} else {
_, err = dal.db.Exec(sqlInsertIntSetting, name, value)
Expand All @@ -87,7 +84,7 @@ func (dal *MyDAL) SaveIntSetting(name string, value int64) (err error) {

// SaveFloatSetting ...
func (dal *MyDAL) SaveFloatSetting(name string, value float64) (err error) {
if dal.ExistsSetting(name) == true {
if dal.ExistsSetting(name) {
_, err = dal.db.Exec(sqlUpdateFloatSetting, value, name)
} else {
_, err = dal.db.Exec(sqlInsertFloatSetting, name, value)
Expand All @@ -97,7 +94,7 @@ func (dal *MyDAL) SaveFloatSetting(name string, value float64) (err error) {

// SaveStringSetting ...
func (dal *MyDAL) SaveStringSetting(name string, value string) (err error) {
if dal.ExistsSetting(name) == true {
if dal.ExistsSetting(name) {
_, err = dal.db.Exec(sqlUpdateStringSetting, value, name)
} else {
_, err = dal.db.Exec(sqlInsertStringSetting, name, value)
Expand Down
5 changes: 1 addition & 4 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,5 @@ func (dal *MyDAL) ExistColumnInTable(tableName string, columnName string) bool {
const sql = `select count(1) from information_schema.columns where table_name=$1 and column_name=$2`
err := dal.db.QueryRow(sql, tableName, columnName).Scan(&count)
utils.CheckError("ExistColumnInTable QueryRow", err)
if count > 0 {
return true
}
return false
return count > 0
}
2 changes: 1 addition & 1 deletion data/data_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewConfig(filename string) (*models.Config, error) {
encryptedConfig := models.EncryptedConfig(*config)
encryptedConfig.PrimaryNode.Database.Password = encryptedPassword
encryptedConfigBytes, _ := json.MarshalIndent(encryptedConfig, "", "\t")
err = ioutil.WriteFile(filename, encryptedConfigBytes, 0600)
_ = ioutil.WriteFile(filename, encryptedConfigBytes, 0600)
} else {
// Decrypt password
encryptedPassword, err := hex.DecodeString(dbPassword)
Expand Down
8 changes: 4 additions & 4 deletions data/db_crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var (

// LoadInstanceKey ...
func (dal *MyDAL) LoadInstanceKey() {
if dal.ExistsSetting("instance_key") == false {
if !dal.ExistsSetting("instance_key") {
instanceKey = GenRandomAES256Key()
encryptedInstanceKey := AES256Encrypt(instanceKey, true)
hexInstanceKey := hex.EncodeToString(encryptedInstanceKey)
Expand All @@ -51,7 +51,7 @@ func (dal *MyDAL) LoadInstanceKey() {

// LoadNodesKey ...
func (dal *MyDAL) LoadNodesKey() {
if dal.ExistsSetting("nodes_key") == false {
if !dal.ExistsSetting("nodes_key") {
NodesKey = GenRandomAES256Key()
encryptedNodesKey := AES256Encrypt(NodesKey, true)
HexEncryptedNodesKey = hex.EncodeToString(encryptedNodesKey)
Expand Down Expand Up @@ -100,7 +100,7 @@ func EncryptWithKey(plaintext []byte, key []byte) []byte {
// AES256Encrypt ...
func AES256Encrypt(plaintext []byte, useRootkey bool) []byte {
key := instanceKey
if useRootkey == true {
if useRootkey {
key = RootKey
}
ciphertext := EncryptWithKey(plaintext, key)
Expand Down Expand Up @@ -133,7 +133,7 @@ func DecryptWithKey(ciphertext []byte, key []byte) ([]byte, error) {
// AES256Decrypt ...
func AES256Decrypt(ciphertext []byte, useRootkey bool) ([]byte, error) {
key := instanceKey
if useRootkey == true {
if useRootkey {
key = RootKey
}
plaintext, err := DecryptWithKey(ciphertext, key)
Expand Down
Loading

0 comments on commit 916468d

Please sign in to comment.