bashhub-server/internal/server.go

265 lines
6.4 KiB
Go
Raw Normal View History

2020-02-08 00:14:22 +08:00
package internal
import (
"fmt"
"log"
"net/http"
"strconv"
"time"
2020-02-10 07:30:05 +08:00
"github.com/appleboy/gin-jwt/v2"
2020-02-08 00:14:22 +08:00
"github.com/gin-gonic/gin"
)
type User struct {
2020-02-10 11:45:20 +08:00
ID uint `json:"id" gorm:"primary_key"`
Username string `json:"Username" gorm:"type:varchar(200);unique_index"`
Email string `json:"email"`
Password string `json:"password"`
Mac *string `json:"mac" gorm:"-"`
RegistrationCode *string `json:"registrationCode"`
SystemName string `json:"systemName" gorm:"-"`
2020-02-08 00:14:22 +08:00
}
type Query struct {
2020-02-10 11:45:20 +08:00
Uuid string `json:"uuid"`
Command string `json:"command"`
Created int64 `json:"created"`
Path string `json:"path"`
ExitStatus int `json:"exitStatus"`
Username string `json:"username"`
2020-02-10 08:36:23 +08:00
SystemName string `gorm:"-" json:"systemName"`
2020-02-10 08:38:17 +08:00
//TODO: implement sessions
2020-02-10 11:45:20 +08:00
SessionID string `json:"session_id"`
2020-02-08 00:14:22 +08:00
}
type Command struct {
2020-02-10 11:45:20 +08:00
ProcessId int `json:"processId"`
ProcessStartTime int64 `json:"processStartTime"`
Uuid string `json:"uuid"`
Command string `json:"command"`
Created int64 `json:"created"`
Path string `json:"path"`
SystemName string `json:"systemName"`
ExitStatus int `json:"exitStatus"`
2020-02-08 00:14:22 +08:00
User User `gorm:"association_foreignkey:ID"`
UserId uint
Limit int `gorm:"-"`
Unique bool `gorm:"-"`
Query string `gorm:"-"`
2020-02-08 00:14:22 +08:00
}
type System struct {
2020-02-10 11:45:20 +08:00
ID uint `json:"id" gorm:"primary_key"`
2020-02-08 00:14:22 +08:00
Created int64
Updated int64
2020-02-10 11:45:20 +08:00
Mac string `json:"mac" gorm:"default:null"`
Hostname *string `json:"hostname"`
Name *string `json:"name"`
ClientVersion *string `json:"clientVersion"`
2020-02-08 00:14:22 +08:00
User User `gorm:"association_foreignkey:ID"`
2020-02-10 11:45:20 +08:00
UserId uint `json:"userId"`
2020-02-08 00:14:22 +08:00
}
2020-02-10 07:30:05 +08:00
var (
Addr string
)
//TODO: Figure out a better way to do this.
const secret = "bashub-server-secret"
2020-02-08 00:14:22 +08:00
func Run() {
DbInit()
r := gin.Default()
2020-02-10 07:30:05 +08:00
// the jwt middleware
authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
Realm: "bashhub-server zone",
Key: []byte(secret),
2020-02-10 08:30:26 +08:00
Timeout: 10000 * time.Hour,
MaxRefresh: 10000 * time.Hour,
2020-02-10 07:30:05 +08:00
IdentityKey: "username",
LoginResponse: func(c *gin.Context, code int, token string, expire time.Time) {
c.JSON(http.StatusOK, gin.H{
"accessToken": token,
})
},
PayloadFunc: func(data interface{}) jwt.MapClaims {
if v, ok := data.(*User); ok {
return jwt.MapClaims{
"username": v.Username,
"systemName": v.SystemName,
}
}
return jwt.MapClaims{}
},
IdentityHandler: func(c *gin.Context) interface{} {
claims := jwt.ExtractClaims(c)
return &User{
Username: claims["username"].(string),
SystemName: claims["systemName"].(string),
}
},
Authenticator: func(c *gin.Context) (interface{}, error) {
var user User
if err := c.ShouldBind(&user); err != nil {
return "", jwt.ErrMissingLoginValues
}
if user.userExists() {
return &User{
Username: user.Username,
SystemName: user.userGetSystemName(),
}, nil
}
fmt.Println("failed")
return nil, jwt.ErrFailedAuthentication
},
Authorizator: func(data interface{}, c *gin.Context) bool {
if v, ok := data.(*User); ok && v.usernameExists() {
return true
}
return false
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"code": code,
"message": message,
})
},
TokenLookup: "header: Authorization, query: token, cookie: jwt",
TokenHeadName: "Bearer",
TimeFunc: time.Now,
})
if err != nil {
log.Fatal("JWT Error:" + err.Error())
}
2020-02-08 00:14:22 +08:00
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
2020-02-10 07:30:05 +08:00
r.POST("/api/v1/login", authMiddleware.LoginHandler)
2020-02-08 00:14:22 +08:00
r.POST("/api/v1/user", func(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if user.Email == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "email required"})
return
}
2020-02-10 07:30:05 +08:00
if user.usernameExists() {
c.String(409, "Username already taken")
return
}
if user.emailExists() {
c.String(409, "This email address is already registered.")
return
}
2020-02-08 00:14:22 +08:00
user.userCreate()
})
2020-02-10 07:30:05 +08:00
r.Use(authMiddleware.MiddlewareFunc())
2020-02-10 08:34:16 +08:00
r.GET("/api/v1/command/:path", func(c *gin.Context) {
2020-02-08 00:14:22 +08:00
var command Command
2020-02-10 07:30:05 +08:00
var user User
claims := jwt.ExtractClaims(c)
user.Username = claims["username"].(string)
command.User.ID = user.userGetId()
2020-02-10 08:34:16 +08:00
if c.Param("path") == "search" {
command.Limit = 100
if c.Query("limit") != "" {
if num, err := strconv.Atoi(c.Query("limit")); err != nil {
command.Limit = 100
} else {
command.Limit = num
}
}
if c.Query("unique") == "true" {
command.Unique = true
2020-02-10 07:30:05 +08:00
} else {
2020-02-10 08:34:16 +08:00
command.Unique = false
2020-02-08 00:14:22 +08:00
}
2020-02-10 08:34:16 +08:00
command.Path = c.Query("path")
command.Query = c.Query("query")
command.SystemName = c.Query("systemName")
result := command.commandGet()
if len(result) == 0 {
c.JSON(http.StatusOK, gin.H{})
return
}
c.IndentedJSON(http.StatusOK, result)
} else {
2020-02-10 08:34:16 +08:00
command.Uuid = c.Param("path")
command.User.ID = user.userGetId()
result := command.commandGetUUID()
result.Username = user.Username
c.IndentedJSON(http.StatusOK, result)
}
2020-02-08 00:14:22 +08:00
})
2020-02-10 08:36:23 +08:00
2020-02-08 00:14:22 +08:00
r.POST("/api/v1/command", func(c *gin.Context) {
var command Command
if err := c.ShouldBindJSON(&command); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
2020-02-10 07:30:05 +08:00
var user User
claims := jwt.ExtractClaims(c)
user.Username = claims["username"].(string)
command.User.ID = user.userGetId()
2020-02-10 07:47:18 +08:00
command.SystemName = claims["systemName"].(string)
2020-02-08 00:14:22 +08:00
command.commandInsert()
})
r.POST("/api/v1/system", func(c *gin.Context) {
var system System
2020-02-10 07:30:05 +08:00
var user User
2020-02-08 00:14:22 +08:00
err := c.Bind(&system)
if err != nil {
log.Fatal(err)
}
2020-02-10 07:30:05 +08:00
claims := jwt.ExtractClaims(c)
user.Username = claims["username"].(string)
system.User.ID = user.userGetId()
2020-02-08 00:14:22 +08:00
system.systemInsert()
c.AbortWithStatus(201)
})
r.GET("/api/v1/system", func(c *gin.Context) {
var system System
2020-02-10 07:30:05 +08:00
var user User
claims := jwt.ExtractClaims(c)
mac := c.Query("mac")
if mac == "" {
2020-02-08 00:14:22 +08:00
c.AbortWithStatus(http.StatusBadRequest)
return
}
2020-02-10 07:30:05 +08:00
user.Username = claims["username"].(string)
system.User.ID = user.userGetId()
result := system.systemGet()
if len(result.Mac) == 0 {
2020-02-08 00:14:22 +08:00
c.AbortWithStatus(404)
return
}
c.IndentedJSON(http.StatusOK, result)
})
2020-02-10 07:30:05 +08:00
r.Run(Addr)
2020-02-08 00:14:22 +08:00
}