mirror of
https://github.com/nicksherron/bashhub-server.git
synced 2024-09-20 03:55:56 +08:00
internal/db: unexport db var and dbInit; cmd/root: startup message; internal/server: custom log
This commit is contained in:
parent
0c913d8215
commit
daa9349100
16
Dockerfile
16
Dockerfile
|
@ -1,3 +1,19 @@
|
|||
# Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
#
|
||||
|
||||
# GitHub: https://github.com/nicksherron/bashhub-server
|
||||
FROM golang:1.13-alpine AS build
|
||||
|
||||
|
|
42
cmd/root.go
42
cmd/root.go
|
@ -1,3 +1,21 @@
|
|||
/*
|
||||
*
|
||||
* Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
|
@ -6,6 +24,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/nicksherron/bashhub-server/internal"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
@ -16,6 +35,7 @@ var cfgFile string
|
|||
var rootCmd = &cobra.Command{
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
cmd.Flags().Parse(args)
|
||||
startupMessage()
|
||||
internal.Run()
|
||||
},
|
||||
}
|
||||
|
@ -29,11 +49,33 @@ func Execute() {
|
|||
|
||||
func init() {
|
||||
cobra.OnInitialize()
|
||||
rootCmd.PersistentFlags().StringVar(&internal.LogFile, "log", "", `Set filepath for HTTP log. "" logs to stderr.`)
|
||||
rootCmd.PersistentFlags().StringVar(&internal.DbPath, "db", dbPath(), "DB location (sqlite or postgres)")
|
||||
rootCmd.PersistentFlags().StringVarP(&internal.Addr, "addr", "a", listenAddr(), "Ip and port to listen and serve on.")
|
||||
|
||||
}
|
||||
|
||||
// StartupMessage prints startup banner
|
||||
func startupMessage() {
|
||||
banner := fmt.Sprintf(`
|
||||
_ _ _ _
|
||||
| | | | | | | | version: %v
|
||||
| |__ __ _ ___| |__ | |__ _ _| | address: %v
|
||||
| '_ \ / _' / __| '_ \| '_ \| | | | '_ \
|
||||
| |_) | (_| \__ \ | | | | | | |_| | |_) |
|
||||
|_.__/ \__,_|___/_| |_|_| |_|\__,_|_.__/
|
||||
___ ___ _ ____ _____ _ __
|
||||
/ __|/ _ \ '__\ \ / / _ \ '__|
|
||||
\__ \ __/ | \ V / __/ |
|
||||
|___/\___|_| \_/ \___|_|
|
||||
|
||||
`, Version, internal.Addr)
|
||||
color.HiGreen(banner)
|
||||
fmt.Print("\n")
|
||||
log.Printf("Listening and serving HTTP on %v", internal.Addr)
|
||||
fmt.Print("\n\n")
|
||||
}
|
||||
|
||||
func listenAddr() string {
|
||||
var a string
|
||||
if os.Getenv("BH_HOST") != "" {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
/*
|
||||
*
|
||||
* Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
@ -12,6 +13,7 @@
|
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
|
2
go.mod
2
go.mod
|
@ -2,7 +2,7 @@ module github.com/nicksherron/bashhub-server
|
|||
|
||||
require (
|
||||
github.com/appleboy/gin-jwt/v2 v2.6.3
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/fatih/color v1.9.0
|
||||
github.com/gin-gonic/gin v1.5.0
|
||||
github.com/inconshreveable/mousetrap v1.0.0 // indirect
|
||||
github.com/jinzhu/gorm v1.9.12
|
||||
|
|
9
go.sum
9
go.sum
|
@ -11,6 +11,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
|
|||
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
|
||||
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
|
||||
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
|
||||
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
|
||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
|
||||
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
|
@ -46,9 +48,14 @@ github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdA
|
|||
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.3.0 h1:/qkRGz8zljWiDcFvgpwUpwIAPu3r07TDvs3Rws+o/pU=
|
||||
github.com/lib/pq v1.3.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
|
||||
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
|
||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
|
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
||||
github.com/mattn/go-sqlite3 v2.0.1+incompatible h1:xQ15muvnzGBHpIpdrNi1DA5x0+TcBZzsIDwmw9uTHzw=
|
||||
github.com/mattn/go-sqlite3 v2.0.1+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
|
@ -92,6 +99,8 @@ golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5h
|
|||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
|
||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
|
|
105
internal/db.go
105
internal/db.go
|
@ -1,3 +1,21 @@
|
|||
/*
|
||||
*
|
||||
* Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
|
@ -17,18 +35,19 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
DB *sql.DB
|
||||
db *sql.DB
|
||||
// DbPath is the postgres connection uri or the sqlite db file location to use for backend.
|
||||
DbPath string
|
||||
connectionLimit int
|
||||
)
|
||||
|
||||
// DbInit initializes our db.
|
||||
func DbInit() {
|
||||
func dbInit() {
|
||||
var gormdb *gorm.DB
|
||||
var err error
|
||||
if strings.HasPrefix(DbPath, "postgres://") {
|
||||
// postgres
|
||||
DB, err = sql.Open("postgres", DbPath)
|
||||
db, err = sql.Open("postgres", DbPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -44,7 +63,7 @@ func DbInit() {
|
|||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
// regex function
|
||||
// sqlite regex function
|
||||
regex := func(re, s string) (bool, error) {
|
||||
b, e := regexp.MatchString(re, s)
|
||||
return b, e
|
||||
|
@ -58,24 +77,24 @@ func DbInit() {
|
|||
})
|
||||
|
||||
DbPath = fmt.Sprintf("file:%v?cache=shared&mode=rwc", DbPath)
|
||||
DB, err = sql.Open("sqlite3_with_regex", DbPath)
|
||||
db, err = sql.Open("sqlite3_with_regex", DbPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
DB.Exec("PRAGMA journal_mode=WAL;")
|
||||
db.Exec("PRAGMA journal_mode=WAL;")
|
||||
connectionLimit = 1
|
||||
|
||||
}
|
||||
DB.SetMaxOpenConns(connectionLimit)
|
||||
db.SetMaxOpenConns(connectionLimit)
|
||||
gormdb.AutoMigrate(&User{})
|
||||
gormdb.AutoMigrate(&Command{})
|
||||
gormdb.AutoMigrate(&System{})
|
||||
//TODO: ensure these are the most efficient indexes
|
||||
gormdb.Model(&User{}).AddIndex("idx_user", "username")
|
||||
gormdb.Model(&System{}).AddIndex("idx_mac", "mac")
|
||||
gormdb.Model(&Command{}).AddIndex("idx_exit_command_created", "exit_status, created, command")
|
||||
gormdb.Model(&Command{}).AddIndex("idx_user_exit_command_created", "user_id, exit_status, created, command")
|
||||
|
||||
// Just need gorm for migration and index creation.
|
||||
gormdb.Close()
|
||||
}
|
||||
|
@ -100,7 +119,7 @@ func comparePasswords(hashedPwd string, plainPwd string) bool {
|
|||
|
||||
func (user User) userExists() bool {
|
||||
var password string
|
||||
err := DB.QueryRow("SELECT password FROM users WHERE username = $1",
|
||||
err := db.QueryRow("SELECT password FROM users WHERE username = $1",
|
||||
user.Username).Scan(&password)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
log.Fatalf("error checking if row exists %v", err)
|
||||
|
@ -113,7 +132,7 @@ func (user User) userExists() bool {
|
|||
|
||||
func (user User) userGetId() uint {
|
||||
var id uint
|
||||
err := DB.QueryRow("SELECT id FROM users WHERE username = $1",
|
||||
err := db.QueryRow("SELECT id FROM users WHERE username = $1",
|
||||
user.Username).Scan(&id)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
log.Fatalf("error checking if row exists %v", err)
|
||||
|
@ -123,7 +142,7 @@ func (user User) userGetId() uint {
|
|||
|
||||
func (user User) userGetSystemName() string {
|
||||
var systemName string
|
||||
err := DB.QueryRow(`SELECT name
|
||||
err := db.QueryRow(`SELECT name
|
||||
FROM systems
|
||||
WHERE user_id in (select id from users where username = $1)
|
||||
AND mac = $2`,
|
||||
|
@ -136,7 +155,7 @@ func (user User) userGetSystemName() string {
|
|||
|
||||
func (user User) usernameExists() bool {
|
||||
var exists bool
|
||||
err := DB.QueryRow(`SELECT exists (select id FROM users WHERE "username" = $1)`,
|
||||
err := db.QueryRow(`SELECT exists (select id FROM users WHERE "username" = $1)`,
|
||||
user.Username).Scan(&exists)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
log.Fatalf("error checking if row exists %v", err)
|
||||
|
@ -146,7 +165,7 @@ func (user User) usernameExists() bool {
|
|||
|
||||
func (user User) emailExists() bool {
|
||||
var exists bool
|
||||
err := DB.QueryRow(`SELECT exists (select id FROM users WHERE "email" = $1)`,
|
||||
err := db.QueryRow(`SELECT exists (select id FROM users WHERE "email" = $1)`,
|
||||
user.Email).Scan(&exists)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
log.Fatalf("error checking if row exists %v", err)
|
||||
|
@ -156,7 +175,7 @@ func (user User) emailExists() bool {
|
|||
|
||||
func (user User) userCreate() int64 {
|
||||
user.Password = hashAndSalt(user.Password)
|
||||
res, err := DB.Exec(`INSERT INTO users("registration_code", "username","password","email")
|
||||
res, err := db.Exec(`INSERT INTO users("registration_code", "username","password","email")
|
||||
VALUES ($1,$2,$3,$4) ON CONFLICT(username) do nothing`, user.RegistrationCode,
|
||||
user.Username, user.Password, user.Email)
|
||||
if err != nil {
|
||||
|
@ -170,7 +189,7 @@ func (user User) userCreate() int64 {
|
|||
}
|
||||
|
||||
func (cmd Command) commandInsert() int64 {
|
||||
res, err := DB.Exec(`INSERT INTO commands("process_id","process_start_time","exit_status","uuid", "command", "created", "path", "user_id", "system_name")
|
||||
res, err := db.Exec(`INSERT INTO commands("process_id","process_start_time","exit_status","uuid", "command", "created", "path", "user_id", "system_name")
|
||||
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)`,
|
||||
cmd.ProcessId, cmd.ProcessStartTime, cmd.ExitStatus, cmd.Uuid, cmd.Command, cmd.Created, cmd.Path, cmd.User.ID, cmd.SystemName)
|
||||
if err != nil {
|
||||
|
@ -191,7 +210,7 @@ func (cmd Command) commandGet() []Query {
|
|||
//postgres
|
||||
if connectionLimit != 1 {
|
||||
if cmd.SystemName != "" && cmd.Path != "" && cmd.Query != "" && cmd.Unique {
|
||||
rows, err = DB.Query(`SELECT * FROM (
|
||||
rows, err = db.Query(`SELECT * FROM (
|
||||
SELECT DISTINCT ON ("command") command, "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
|
@ -204,7 +223,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit $2;`, cmd.User.ID, cmd.Limit, cmd.Path, cmd.SystemName, cmd.Query)
|
||||
|
||||
} else if cmd.Path != "" && cmd.Query != "" && cmd.Unique {
|
||||
rows, err = DB.Query(`SELECT * FROM (
|
||||
rows, err = db.Query(`SELECT * FROM (
|
||||
SELECT DISTINCT ON ("command") command, "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
|
@ -216,7 +235,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit $2;`, cmd.User.ID, cmd.Limit, cmd.Path, cmd.Query)
|
||||
|
||||
} else if cmd.SystemName != "" && cmd.Query != "" {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created"
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
|
@ -226,7 +245,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit $2;`, cmd.User.ID, cmd.Limit, cmd.SystemName, cmd.Query)
|
||||
|
||||
} else if cmd.Path != "" && cmd.Query != "" {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created"
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
|
@ -236,7 +255,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit $2;`, cmd.User.ID, cmd.Limit, cmd.Path, cmd.Query)
|
||||
|
||||
} else if cmd.SystemName != "" && cmd.Unique {
|
||||
rows, err = DB.Query(`SELECT * FROM (
|
||||
rows, err = db.Query(`SELECT * FROM (
|
||||
SELECT DISTINCT ON ("command") command, "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
|
@ -247,7 +266,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit $2;`, cmd.User.ID, cmd.Limit, cmd.SystemName)
|
||||
|
||||
} else if cmd.Path != "" && cmd.Unique {
|
||||
rows, err = DB.Query(`SELECT * FROM (
|
||||
rows, err = db.Query(`SELECT * FROM (
|
||||
SELECT DISTINCT ON ("command") command, "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
|
@ -258,7 +277,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit $2;`, cmd.User.ID, cmd.Limit, cmd.Path)
|
||||
|
||||
} else if cmd.Query != "" && cmd.Unique {
|
||||
rows, err = DB.Query(`SELECT * FROM (
|
||||
rows, err = db.Query(`SELECT * FROM (
|
||||
SELECT DISTINCT ON ("command") command, "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
|
@ -269,7 +288,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit $2;`, cmd.User.ID, cmd.Limit, cmd.Query)
|
||||
|
||||
} else if cmd.Query != "" {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created"
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
|
@ -279,7 +298,7 @@ func (cmd Command) commandGet() []Query {
|
|||
|
||||
} else {
|
||||
// unique
|
||||
rows, err = DB.Query(`SELECT * FROM (
|
||||
rows, err = db.Query(`SELECT * FROM (
|
||||
SELECT DISTINCT ON ("command") command, "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
|
@ -291,6 +310,7 @@ func (cmd Command) commandGet() []Query {
|
|||
} else {
|
||||
// sqlite
|
||||
if cmd.SystemName != "" && cmd.Path != "" && cmd.Query != "" && cmd.Unique {
|
||||
// Have to use fmt.Sprintf to build queries where sqlite regexp function is used because of single quotes. Haven't found any other work around.
|
||||
query := fmt.Sprintf(`SELECT "command", "uuid", "created" FROM commands
|
||||
WHERE "user_id" = '%v'
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
|
@ -300,7 +320,8 @@ func (cmd Command) commandGet() []Query {
|
|||
AND "command" regexp '%v'
|
||||
GROUP BY "command" ORDER BY "created" DESC limit '%v'`,
|
||||
cmd.User.ID, "bh%", cmd.Path, cmd.SystemName, cmd.Query, cmd.Limit)
|
||||
rows, err = DB.Query(query)
|
||||
|
||||
rows, err = db.Query(query)
|
||||
|
||||
} else if cmd.SystemName != "" && cmd.Query != "" && cmd.Unique {
|
||||
query := fmt.Sprintf(`SELECT "command", "uuid", "created" FROM commands
|
||||
|
@ -311,7 +332,8 @@ func (cmd Command) commandGet() []Query {
|
|||
AND "command" regexp '%v'
|
||||
GROUP BY "command" ORDER BY "created" DESC limit '%v'`,
|
||||
cmd.User.ID, "bh%", cmd.SystemName, cmd.Query, cmd.Limit)
|
||||
rows, err = DB.Query(query)
|
||||
|
||||
rows, err = db.Query(query)
|
||||
|
||||
} else if cmd.Path != "" && cmd.Query != "" && cmd.Unique {
|
||||
query := fmt.Sprintf(`SELECT "command", "uuid", "created" FROM commands
|
||||
|
@ -322,7 +344,8 @@ func (cmd Command) commandGet() []Query {
|
|||
AND "command" regexp '%v'
|
||||
GROUP BY "command" ORDER BY "created" DESC limit '%v'`,
|
||||
cmd.User.ID, "bh%", cmd.Path, cmd.Query, cmd.Limit)
|
||||
rows, err = DB.Query(query)
|
||||
|
||||
rows, err = db.Query(query)
|
||||
|
||||
} else if cmd.SystemName != "" && cmd.Query != "" {
|
||||
query := fmt.Sprintf(`SELECT "command", "uuid", "created" FROM commands
|
||||
|
@ -334,7 +357,7 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit '%v'`,
|
||||
cmd.User.ID, "bh%", cmd.SystemName, cmd.Query, cmd.Limit)
|
||||
|
||||
rows, err = DB.Query(query)
|
||||
rows, err = db.Query(query)
|
||||
|
||||
} else if cmd.Path != "" && cmd.Query != "" {
|
||||
query := fmt.Sprintf(`SELECT "command", "uuid", "created" FROM commands
|
||||
|
@ -346,10 +369,10 @@ func (cmd Command) commandGet() []Query {
|
|||
ORDER BY "created" DESC limit '%v'`,
|
||||
cmd.User.ID, "bh%", cmd.Path, cmd.Query, cmd.Limit)
|
||||
|
||||
rows, err = DB.Query(query)
|
||||
rows, err = db.Query(query)
|
||||
|
||||
} else if cmd.SystemName != "" && cmd.Unique {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
AND "command" not like 'bh%'
|
||||
|
@ -358,7 +381,7 @@ func (cmd Command) commandGet() []Query {
|
|||
cmd.User.ID, cmd.SystemName, cmd.Limit)
|
||||
|
||||
} else if cmd.Path != "" && cmd.Unique {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
AND "command" not like 'bh%'
|
||||
|
@ -374,7 +397,8 @@ func (cmd Command) commandGet() []Query {
|
|||
AND "command" regexp '%v'
|
||||
GROUP BY "command" ORDER BY "created" DESC limit '%v'`,
|
||||
cmd.User.ID, "bh%", cmd.Query, cmd.Limit)
|
||||
rows, err = DB.Query(query)
|
||||
|
||||
rows, err = db.Query(query)
|
||||
|
||||
} else if cmd.Query != "" {
|
||||
query := fmt.Sprintf(`SELECT "command", "uuid", "created" FROM commands
|
||||
|
@ -384,11 +408,12 @@ func (cmd Command) commandGet() []Query {
|
|||
AND "command" regexp'%v'
|
||||
ORDER BY "created" DESC limit '%v'`,
|
||||
cmd.User.ID, "bh%", cmd.Query, cmd.Limit)
|
||||
rows, err = DB.Query(query)
|
||||
|
||||
rows, err = db.Query(query)
|
||||
|
||||
} else {
|
||||
// unique
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created"
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created"
|
||||
FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
|
@ -398,20 +423,20 @@ func (cmd Command) commandGet() []Query {
|
|||
}
|
||||
} else {
|
||||
if cmd.Path != "" {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND "path" = $3
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
AND "command" not like 'bh%'
|
||||
ORDER BY "created" DESC limit $2`, cmd.User.ID, cmd.Limit, cmd.Path)
|
||||
} else if cmd.SystemName != "" {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND "system_name" = $3
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130) AND "command" not like 'bh%'
|
||||
ORDER BY "created" DESC limit $2`, cmd.User.ID, cmd.Limit, cmd.SystemName)
|
||||
} else {
|
||||
rows, err = DB.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
rows, err = db.Query(`SELECT "command", "uuid", "created" FROM commands
|
||||
WHERE "user_id" = $1
|
||||
AND ("exit_status" = 0 OR "exit_status" = 130)
|
||||
AND "command" not like 'bh%'
|
||||
|
@ -438,7 +463,7 @@ func (cmd Command) commandGet() []Query {
|
|||
|
||||
func (cmd Command) commandGetUUID() Query {
|
||||
var result Query
|
||||
err := DB.QueryRow(`SELECT "command","path", "created" , "uuid", "exit_status", "system_name"
|
||||
err := db.QueryRow(`SELECT "command","path", "created" , "uuid", "exit_status", "system_name"
|
||||
FROM commands
|
||||
WHERE "uuid" = $1
|
||||
AND "user_id" = $2`, cmd.Uuid, cmd.User.ID).Scan(
|
||||
|
@ -453,7 +478,7 @@ func (cmd Command) commandGetUUID() Query {
|
|||
func (sys System) systemInsert() int64 {
|
||||
|
||||
t := time.Now().Unix()
|
||||
res, err := DB.Exec(`INSERT INTO systems ("name", "mac", "user_id", "hostname", "client_version", "created", "updated")
|
||||
res, err := db.Exec(`INSERT INTO systems ("name", "mac", "user_id", "hostname", "client_version", "created", "updated")
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
||||
sys.Name, sys.Mac, sys.User.ID, sys.Hostname, sys.ClientVersion, t, t)
|
||||
if err != nil {
|
||||
|
@ -468,7 +493,7 @@ func (sys System) systemInsert() int64 {
|
|||
|
||||
func (sys System) systemGet() System {
|
||||
var row System
|
||||
err := DB.QueryRow(`SELECT "name", "mac", "user_id", "hostname", "client_version",
|
||||
err := db.QueryRow(`SELECT "name", "mac", "user_id", "hostname", "client_version",
|
||||
"id", "created", "updated" FROM systems
|
||||
WHERE "user_id" $1
|
||||
AND "mac" = $2`,
|
||||
|
|
|
@ -1,9 +1,28 @@
|
|||
/*
|
||||
*
|
||||
* Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -63,15 +82,51 @@ type System struct {
|
|||
|
||||
var (
|
||||
Addr string
|
||||
LogFile string
|
||||
)
|
||||
|
||||
//TODO: Figure out a better way to do this.
|
||||
const secret = "bashub-server-secret"
|
||||
|
||||
func Run() {
|
||||
func getLog() *os.File {
|
||||
|
||||
if LogFile != "" {
|
||||
f, err := os.Create(LogFile)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return f
|
||||
}
|
||||
return os.Stderr
|
||||
}
|
||||
|
||||
// LoggerWithFormatter instance a Logger middleware with the specified log format function.
|
||||
func loggerWithFormatterWriter(f gin.LogFormatter) gin.HandlerFunc {
|
||||
return gin.LoggerWithConfig(gin.LoggerConfig{
|
||||
Formatter: f,
|
||||
Output: getLog(),
|
||||
})
|
||||
}
|
||||
|
||||
func Run() {
|
||||
// Initialize backend
|
||||
dbInit()
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.New()
|
||||
r.Use(gin.Recovery())
|
||||
|
||||
r.Use(loggerWithFormatterWriter(func(param gin.LogFormatterParams) string {
|
||||
return fmt.Sprintf("[BASHHUB-SERVER] %v | %3d | %13v | %15s | %-7s %s\n",
|
||||
param.TimeStamp.Format("2006/01/02 - 15:04:05"),
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
param.ClientIP,
|
||||
param.Method,
|
||||
param.Path,
|
||||
)
|
||||
}))
|
||||
|
||||
DbInit()
|
||||
r := gin.Default()
|
||||
// the jwt middleware
|
||||
authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
|
||||
Realm: "bashhub-server zone",
|
||||
|
|
18
main.go
18
main.go
|
@ -1,3 +1,21 @@
|
|||
/*
|
||||
*
|
||||
* Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
|
|
|
@ -1,5 +1,21 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
#
|
||||
|
||||
set -eou pipefail
|
||||
|
||||
version="$(git tag | sort --version-sort -r | head -1)"
|
||||
|
|
|
@ -1,5 +1,21 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
# Copyright © 2020 nicksherron <nsherron90@gmail.com>
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
#
|
||||
|
||||
set -eou pipefail
|
||||
|
||||
version="$(git tag | sort --version-sort -r | head -1)"
|
||||
|
|
Loading…
Reference in a new issue