diff --git a/backend/certificate.go b/backend/certificate.go index 2fffd49..5f4623e 100644 --- a/backend/certificate.go +++ b/backend/certificate.go @@ -11,6 +11,7 @@ import ( "crypto/tls" "errors" "log" + "strconv" "janusec/data" "janusec/models" @@ -45,7 +46,7 @@ func LoadCerts() { cert.PrivKeyContent = string(privKey) cert.TlsCert = tlsCert cert.ExpireTime = dbCert.ExpireTime - if dbCert.Description.Valid == true { + if dbCert.Description.Valid { cert.Description = dbCert.Description.String } else { cert.Description = "" @@ -65,7 +66,7 @@ func LoadCerts() { // GetCertificateByDomain ... func GetCertificateByDomain(helloInfo *tls.ClientHelloInfo) (*tls.Certificate, error) { domain := helloInfo.ServerName - if domainRelation, ok := DomainsMap.Load(domain); ok == true { + if domainRelation, ok := DomainsMap.Load(domain); ok { certItem := domainRelation.(models.DomainRelation).Cert if certItem == nil { // autocert @@ -78,7 +79,7 @@ func GetCertificateByDomain(helloInfo *tls.ClientHelloInfo) (*tls.Certificate, e // GetCertificates ... func GetCertificates(authUser *models.AuthUser) ([]*models.CertItem, error) { - if authUser.IsCertAdmin == true { + if authUser.IsCertAdmin { return Certs, nil } // Remove private key @@ -104,7 +105,7 @@ func SysCallGetCertByID(certID int64) (*models.CertItem, error) { return cert, nil } } - return nil, errors.New("Certificate not found") + return nil, errors.New("certificate not found") } // GetCertificateByID ... @@ -140,7 +141,7 @@ func GetCertificateByCommonName(commonName string) *models.CertItem { } // UpdateCertificate ... -func UpdateCertificate(param map[string]interface{}, authUser *models.AuthUser) (*models.CertItem, error) { +func UpdateCertificate(param map[string]interface{}, clientIP string, authUser *models.AuthUser) (*models.CertItem, error) { certificate := param["object"].(map[string]interface{}) id := int64(certificate["id"].(float64)) commonName := certificate["common_name"].(string) @@ -165,6 +166,7 @@ func UpdateCertificate(param map[string]interface{}, authUser *models.AuthUser) certItem = &models.CertItem{} certItem.ID = newID Certs = append(Certs, certItem) + go utils.OperationLog(clientIP, authUser.Username, "Add Certificate", commonName) } else { certItem, err = GetCertificateByID(id, authUser) if err != nil { @@ -174,6 +176,7 @@ func UpdateCertificate(param map[string]interface{}, authUser *models.AuthUser) if err != nil { return nil, err } + go utils.OperationLog(clientIP, authUser.Username, "Update Certificate", commonName) } certItem.CommonName = commonName certItem.CertContent = certContent @@ -196,7 +199,7 @@ func GetCertificateIndex(certID int64) int { } // DeleteCertificateByID ... -func DeleteCertificateByID(certID int64) error { +func DeleteCertificateByID(certID int64, clientIP string, authUser *models.AuthUser) error { certDomainsCount := data.DAL.SelectDomainsCountByCertID(certID) if certDomainsCount > 0 { return errors.New("this certificate is in use, please delete relevant applications at first") @@ -206,6 +209,7 @@ func DeleteCertificateByID(certID int64) error { return err } i := GetCertificateIndex(certID) + go utils.OperationLog(clientIP, authUser.Username, "Delete Certificate", strconv.FormatInt(certID, 10)) Certs = append(Certs[:i], Certs[i+1:]...) data.UpdateBackendLastModified() return nil diff --git a/backend/destination.go b/backend/destination.go index 94ff053..46583aa 100644 --- a/backend/destination.go +++ b/backend/destination.go @@ -30,7 +30,7 @@ func InterfaceContainsDestinationID(destinations []interface{}, destID int64) bo func CheckOfflineDestinations(nowTimeStamp int64) { for _, app := range Apps { for _, dest := range app.Destinations { - if dest.RouteType == models.ReverseProxyRoute && dest.Online == false { + if dest.RouteType == models.ReverseProxyRoute && !dest.Online { go func() { conn, err := net.DialTimeout("tcp", dest.Destination, time.Second) if err == nil { diff --git a/backend/init.go b/backend/init.go index cc1fe71..c60b17e 100644 --- a/backend/init.go +++ b/backend/init.go @@ -65,28 +65,28 @@ func InitDatabase() { utils.DebugPrintln("InitDatabase CreateTableIfNotExistsTOTP", err) } // Upgrade to latest version - if dal.ExistColumnInTable("domains", "redirect") == false { + if !dal.ExistColumnInTable("domains", "redirect") { // v0.9.6+ required err = dal.ExecSQL(`ALTER TABLE "domains" ADD COLUMN "redirect" boolean default false, ADD COLUMN "location" VARCHAR(256) NOT NULL DEFAULT ''`) if err != nil { utils.DebugPrintln("InitDatabase ALTER TABLE domains", err) } } - if dal.ExistColumnInTable("applications", "oauth_required") == false { + if !dal.ExistColumnInTable("applications", "oauth_required") { // v0.9.7+ required err = dal.ExecSQL(`ALTER TABLE "applications" ADD COLUMN "oauth_required" boolean default false, ADD COLUMN "session_seconds" bigint default 7200, ADD COLUMN "owner" VARCHAR(128)`) if err != nil { utils.DebugPrintln("InitDatabase ALTER TABLE applications oauth", err) } } - if dal.ExistColumnInTable("destinations", "route_type") == false { + if !dal.ExistColumnInTable("destinations", "route_type") { // v0.9.8+ required err = dal.ExecSQL(`ALTER TABLE "destinations" ADD COLUMN "route_type" bigint default 1, ADD COLUMN "request_route" VARCHAR(128) NOT NULL DEFAULT '/', ADD COLUMN "backend_route" VARCHAR(128) NOT NULL DEFAULT '/'`) if err != nil { utils.DebugPrintln("InitDatabase ALTER TABLE destinations", err) } } - if dal.ExistColumnInTable("ccpolicies", "interval_seconds") == true { + if dal.ExistColumnInTable("ccpolicies", "interval_seconds") { // v0.9.9 interval_seconds, v0.9.10 interval_milliseconds err = dal.ExecSQL(`ALTER TABLE "ccpolicies" RENAME COLUMN "interval_seconds" TO "interval_milliseconds"`) if err != nil { @@ -97,14 +97,14 @@ func InitDatabase() { utils.DebugPrintln("InitDatabase UPDATE ccpolicies", err) } } - if dal.ExistColumnInTable("applications", "csp") == false { + if !dal.ExistColumnInTable("applications", "csp") { // v0.9.11 CSP err = dal.ExecSQL(`ALTER TABLE "applications" ADD COLUMN "csp_enabled" boolean default false, ADD COLUMN "csp" VARCHAR(1024) NOT NULL DEFAULT 'default-src ''self'''`) if err != nil { utils.DebugPrintln("InitDatabase ALTER TABLE applications", err) } } - if dal.ExistColumnInTable("totp", "uid") == true { + if dal.ExistColumnInTable("totp", "uid") { // 0.9.12+fix err = dal.ExecSQL(`ALTER TABLE "totp" RENAME COLUMN "uid" TO "totp_uid"`) if err != nil { diff --git a/backend/vip_app.go b/backend/vip_app.go index fdc4f54..54bbbcc 100644 --- a/backend/vip_app.go +++ b/backend/vip_app.go @@ -239,7 +239,7 @@ func GetVipApps(authUser *models.AuthUser) ([]*models.VipApp, error) { // UpdateVipApp create or update VipApp for port forwarding func UpdateVipApp(param map[string]interface{}, clientIP string, authUser *models.AuthUser) (*models.VipApp, error) { - if authUser.IsSuperAdmin == false { + if !authUser.IsSuperAdmin { return nil, errors.New("only super admin can configure port forwarding") } application := param["object"].(map[string]interface{}) @@ -288,7 +288,7 @@ func UpdateVipApp(param map[string]interface{}, clientIP string, authUser *model vipApp.ExitChan <- true go utils.OperationLog(clientIP, authUser.Username, "Update Port Forwarding", vipApp.Name) } else { - return nil, errors.New("Port Forwarding not found") + return nil, errors.New("port forwarding not found") } } // fmt.Println("update targets ...") @@ -371,7 +371,7 @@ func DeleteVipAppByID(id int64, clientIP string, authUser *models.AuthUser) erro i := GetVipAppIndex(id) VipApps[i].ExitChan <- true go utils.OperationLog(clientIP, authUser.Username, "Delete Port Forwarding", VipApps[i].Name) - VipApps = append(VipApps[:i], VipApps[i+1:]...) + VipApps = append(VipApps[:i], VipApps[i+1:]...) data.UpdateBackendLastModified() return nil } diff --git a/gateway/api.go b/gateway/api.go index d44f4e4..1a4b1a3 100644 --- a/gateway/api.go +++ b/gateway/api.go @@ -100,11 +100,11 @@ func AdminAPIHandlerFunc(w http.ResponseWriter, r *http.Request) { id := int64(param["id"].(float64)) obj, err = backend.GetCertificateByID(id, authUser) case "update_cert": - obj, err = backend.UpdateCertificate(param, authUser) + obj, err = backend.UpdateCertificate(param, clientIP, authUser) case "del_cert": id := int64(param["id"].(float64)) obj = nil - err = backend.DeleteCertificateByID(id) + err = backend.DeleteCertificateByID(id, clientIP, authUser) case "self_sign_cert": obj, err = utils.GenerateRSACertificate(param) case "get_domains": diff --git a/go.mod b/go.mod index 9b791f4..aaf1966 100644 --- a/go.mod +++ b/go.mod @@ -32,7 +32,7 @@ require ( golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1 golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe // indirect - golang.org/x/tools v0.1.0 // indirect + golang.org/x/tools/gopls v0.6.10 // indirect golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect diff --git a/go.sum b/go.sum index bf54a06..47ab384 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,7 @@ contrib.go.opencensus.io/exporter/stackdriver v0.13.4/go.mod h1:aXENhDJ1Y4lIg4EU dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c h1:/IBSNwUN8+eKzUzbJPqhK839ygXJ82sde8x3ogr6R28= github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= @@ -175,6 +176,7 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20200507031123-427632fa3b1c/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/safehtml v0.0.2/go.mod h1:L4KWwDsUJdECRAEpZoBn3O64bQaywRscowZjJAzjHnU= github.com/google/trillian v1.3.11/go.mod h1:0tPraVHrSDkA3BO6vKX67zgLXs6SsOAbHEivX+9mPgw= github.com/google/uuid v0.0.0-20161128191214-064e2069ce9c/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -215,6 +217,7 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1: github.com/imdario/mergo v0.3.4/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jba/templatecheck v0.5.0/go.mod h1:/1k7EajoSErFI9GLHAsiIJEaNLt3ALKNw2TV7z2SYv4= github.com/jhump/protoreflect v1.6.1/go.mod h1:RZQ/lnuN+zqeRVpQigTwO6o0AJUkxbnSnpuG7toUTG4= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= @@ -348,11 +351,15 @@ github.com/pseudomuto/protokit v0.2.0/go.mod h1:2PdH30hxVHsup8KpBTOXTBeMVhJZVio3 github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sanity-io/litter v1.3.0/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0= github.com/securego/gosec/v2 v2.7.0 h1:mOhJv5w6UyNLpSssQOQCc7eGkKLuicAxvf66Ey/X4xk= github.com/securego/gosec/v2 v2.7.0/go.mod h1:xNbGArrGUspJLuz3LS5XCY1EBW/0vABAl/LWfSklmiM= +github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shirou/gopsutil v3.21.2+incompatible h1:U+YvJfjCh6MslYlIAXvPtzhW3YZEtc9uncueUNpD/0A= github.com/shirou/gopsutil v3.21.2+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= @@ -386,6 +393,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v0.0.0-20170130113145-4d4bfba8f1d1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= @@ -492,6 +500,7 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.1 h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY= golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -713,8 +722,13 @@ golang.org/x/tools v0.0.0-20200630154851-b2d8b0336632/go.mod h1:EkVYQZoAsY45+roY golang.org/x/tools v0.0.0-20200706234117-b22de6825cf7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200908211811-12e1bf57a112/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.0.0-20210101214203-2dba1e4ea05c/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY= golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= +golang.org/x/tools v0.1.1-0.20210408155258-593413344da1 h1:B+9Jhwu5uM+kDMAkoQH6IUoIoS48VbRqwLyceNdpRK8= +golang.org/x/tools v0.1.1-0.20210408155258-593413344da1/go.mod h1:9bzcO0MWcOuT0tm1iBGzDVPshzfwoVvREIui8C+MHqU= +golang.org/x/tools/gopls v0.6.10 h1:8Ebz8PymS2umcuCFhoz67unyJfWsipjTIrkBUF9kypg= +golang.org/x/tools/gopls v0.6.10/go.mod h1:DnaI5Ldz4pkVYDmASxTg6lpMBGtjdqNv7MBzJsKskEo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -837,6 +851,12 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k= +honnef.co/go/tools v0.1.3 h1:qTakTkI6ni6LFD5sBwwsdSO+AQqbSIxOauHTTQKZ/7o= +honnef.co/go/tools v0.1.3/go.mod h1:NgwopIslSNH47DimFoV78dnkksY2EFtX0ajyb3K/las= +mvdan.cc/gofumpt v0.1.1 h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA= +mvdan.cc/gofumpt v0.1.1/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48= +mvdan.cc/xurls/v2 v2.2.0 h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A= +mvdan.cc/xurls/v2 v2.2.0/go.mod h1:EV1RMtya9D6G5DMYPGD8zTQzaHet6Jh8gFlRgGRJeO8= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8= rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=