2019-07-22 12:42:39 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"log"
|
2019-07-25 12:03:32 +00:00
|
|
|
"net/http"
|
2019-07-22 12:42:39 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2019-07-25 11:17:29 +00:00
|
|
|
gmux "github.com/gorilla/mux"
|
2019-07-22 12:42:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var Uint32 = binary.BigEndian.Uint32
|
|
|
|
var Uint64 = binary.BigEndian.Uint64
|
|
|
|
var PutUint32 = binary.BigEndian.PutUint32
|
|
|
|
var PutUint64 = binary.BigEndian.PutUint64
|
|
|
|
|
2019-07-25 11:17:29 +00:00
|
|
|
func i64ToB(value int64) []byte {
|
|
|
|
oct := make([]byte, 8)
|
|
|
|
PutUint64(oct, uint64(value))
|
|
|
|
return oct
|
|
|
|
}
|
|
|
|
func i32ToB(value int32) []byte {
|
|
|
|
nib := make([]byte, 4)
|
|
|
|
PutUint32(nib, uint32(value))
|
|
|
|
return nib
|
|
|
|
}
|
|
|
|
|
2019-07-22 12:42:39 +00:00
|
|
|
type localManager struct {
|
2019-07-25 11:17:29 +00:00
|
|
|
db *bolt.DB
|
|
|
|
Router *gmux.Router
|
2019-07-22 12:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MakeLocalManager(dbPath string) (*localManager, error) {
|
|
|
|
db, err := bolt.Open(dbPath, 0600, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-25 11:17:29 +00:00
|
|
|
ret := &localManager{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
ret.Router = ret.registerMux()
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 12:03:32 +00:00
|
|
|
func corsMiddleware(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-25 11:17:29 +00:00
|
|
|
func (manager *localManager) registerMux() *gmux.Router {
|
|
|
|
r := gmux.NewRouter()
|
|
|
|
r.HandleFunc("/admin/users", manager.listAllUsersHlr).Methods("GET")
|
|
|
|
r.HandleFunc("/admin/users/{UID}", manager.getUserInfo).Methods("GET")
|
|
|
|
r.HandleFunc("/admin/users/{UID}", manager.writeUserInfo).Methods("POST")
|
|
|
|
r.HandleFunc("/admin/users/{UID}", manager.deleteUser).Methods("DELETE")
|
2019-07-25 12:03:32 +00:00
|
|
|
r.Use(corsMiddleware)
|
2019-07-25 11:17:29 +00:00
|
|
|
return r
|
2019-07-22 12:42:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *localManager) authenticateUser(UID []byte) (int64, int64, error) {
|
|
|
|
var upRate, downRate, upCredit, downCredit, expiryTime int64
|
|
|
|
err := manager.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(UID)
|
|
|
|
if bucket == nil {
|
|
|
|
return ErrUserNotFound
|
|
|
|
}
|
|
|
|
upRate = int64(Uint64(bucket.Get([]byte("UpRate"))))
|
|
|
|
downRate = int64(Uint64(bucket.Get([]byte("DownRate"))))
|
|
|
|
upCredit = int64(Uint64(bucket.Get([]byte("UpCredit"))))
|
|
|
|
downCredit = int64(Uint64(bucket.Get([]byte("DownCredit"))))
|
|
|
|
expiryTime = int64(Uint64(bucket.Get([]byte("ExpiryTime"))))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
if upCredit <= 0 {
|
|
|
|
return 0, 0, ErrNoUpCredit
|
|
|
|
}
|
|
|
|
if downCredit <= 0 {
|
|
|
|
return 0, 0, ErrNoDownCredit
|
|
|
|
}
|
|
|
|
if expiryTime < time.Now().Unix() {
|
|
|
|
return 0, 0, ErrUserExpired
|
|
|
|
}
|
|
|
|
|
|
|
|
return upRate, downRate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *localManager) authoriseNewSession(user *ActiveUser) error {
|
|
|
|
var sessionsCap int
|
|
|
|
var upCredit, downCredit, expiryTime int64
|
|
|
|
err := manager.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket(user.arrUID[:])
|
|
|
|
if bucket == nil {
|
|
|
|
return ErrUserNotFound
|
|
|
|
}
|
|
|
|
sessionsCap = int(Uint32(bucket.Get([]byte("SessionsCap"))))
|
|
|
|
upCredit = int64(Uint64(bucket.Get([]byte("UpCredit"))))
|
|
|
|
downCredit = int64(Uint64(bucket.Get([]byte("DownCredit"))))
|
|
|
|
expiryTime = int64(Uint64(bucket.Get([]byte("ExpiryTime"))))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if upCredit <= 0 {
|
|
|
|
return ErrNoUpCredit
|
|
|
|
}
|
|
|
|
if downCredit <= 0 {
|
|
|
|
return ErrNoDownCredit
|
|
|
|
}
|
|
|
|
if expiryTime < time.Now().Unix() {
|
|
|
|
return ErrUserExpired
|
|
|
|
}
|
|
|
|
//user.sessionsM.RLock()
|
|
|
|
if len(user.sessions) >= sessionsCap {
|
|
|
|
//user.sessionsM.RUnlock()
|
|
|
|
return ErrSessionsCapReached
|
|
|
|
}
|
|
|
|
//user.sessionsM.RUnlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 14:25:09 +00:00
|
|
|
func (manager *localManager) uploadStatus(uploads []statusUpdate) ([]statusResponse, error) {
|
|
|
|
var responses []statusResponse
|
2019-07-22 12:42:39 +00:00
|
|
|
err := manager.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
for _, status := range uploads {
|
2019-07-24 14:25:09 +00:00
|
|
|
var resp statusResponse
|
2019-07-22 12:42:39 +00:00
|
|
|
bucket := tx.Bucket(status.UID)
|
|
|
|
if bucket == nil {
|
2019-07-25 11:17:29 +00:00
|
|
|
resp = statusResponse{
|
|
|
|
status.UID,
|
|
|
|
TERMINATE,
|
|
|
|
"User no longer exists",
|
|
|
|
}
|
|
|
|
responses = append(responses, resp)
|
2019-07-22 12:42:39 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-07-24 14:25:09 +00:00
|
|
|
|
2019-07-22 12:42:39 +00:00
|
|
|
oldUp := int64(Uint64(bucket.Get([]byte("UpCredit"))))
|
2019-07-24 14:25:09 +00:00
|
|
|
newUp := oldUp - status.upUsage
|
|
|
|
if newUp <= 0 {
|
|
|
|
resp = statusResponse{
|
|
|
|
status.UID,
|
|
|
|
TERMINATE,
|
|
|
|
"No upload credit left",
|
|
|
|
}
|
2019-07-25 11:17:29 +00:00
|
|
|
responses = append(responses, resp)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err := bucket.Put([]byte("UpCredit"), i64ToB(newUp))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
2019-07-24 14:25:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 12:42:39 +00:00
|
|
|
oldDown := int64(Uint64(bucket.Get([]byte("DownCredit"))))
|
2019-07-24 14:25:09 +00:00
|
|
|
newDown := oldDown - status.downUsage
|
|
|
|
if newDown <= 0 {
|
|
|
|
resp = statusResponse{
|
|
|
|
status.UID,
|
|
|
|
TERMINATE,
|
|
|
|
"No download credit left",
|
|
|
|
}
|
2019-07-25 11:17:29 +00:00
|
|
|
responses = append(responses, resp)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = bucket.Put([]byte("DownCredit"), i64ToB(newDown))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
2019-07-24 14:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expiry := int64(Uint64(bucket.Get([]byte("ExpiryTime"))))
|
2019-07-25 11:17:29 +00:00
|
|
|
if time.Now().Unix() > expiry {
|
2019-07-24 14:25:09 +00:00
|
|
|
resp = statusResponse{
|
|
|
|
status.UID,
|
|
|
|
TERMINATE,
|
|
|
|
"User has expired",
|
|
|
|
}
|
|
|
|
responses = append(responses, resp)
|
2019-07-25 11:17:29 +00:00
|
|
|
continue
|
2019-07-24 14:25:09 +00:00
|
|
|
}
|
2019-07-22 12:42:39 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2019-07-24 14:25:09 +00:00
|
|
|
return responses, err
|
2019-07-22 12:42:39 +00:00
|
|
|
}
|