Skip to content

Commit

Permalink
Base bwlist (#117)
Browse files Browse the repository at this point in the history
* add access and account to cfg

* add access control

* add bwlist
  • Loading branch information
AstaFrode authored Nov 10, 2023
1 parent 3d59731 commit 6e4fd85
Show file tree
Hide file tree
Showing 16 changed files with 282 additions and 288 deletions.
16 changes: 8 additions & 8 deletions configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ const (
TokenDated = 60 * 60 * 24 * 30
)

const (
//
FileCacheExpirationTime = 720
)
const FileCacheExpirationTime = 720

// Time out waiting for transaction completion
const TimeOut_WaitBlock = time.Duration(time.Second * 15)

const DefaultConfig = "conf.yaml"

const (
// Time out waiting for transaction completion
TimeOut_WaitBlock = time.Duration(time.Second * 15)
//
DefaultConfig = "conf.yaml"
Access_Public = "public"
Access_Private = "private"
)
2 changes: 1 addition & 1 deletion configs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
// Name space
NameSpace = Name
// version
Version = Name + " " + "v0.3.2"
Version = Name + " " + "v0.3.3 dev"
// description
Description = "Object storage service based on CESS network"
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/CESSProject/DeOSS
go 1.20

require (
github.com/CESSProject/cess-go-sdk v0.3.19
github.com/CESSProject/cess-go-sdk v0.3.21-0.20231107093552-741d1c28e744
github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde
github.com/CESSProject/p2p-go v0.2.4
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGy
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/CESSProject/cess-go-sdk v0.3.19 h1:13nO9Ox4okPRqQgv9us2L6/04RAKDo4eP2IwuLzjrvI=
github.com/CESSProject/cess-go-sdk v0.3.19/go.mod h1:x37J5WrzBYwFK3c2BkU9oLvhTmN0hrGwFV6NczTHrkM=
github.com/CESSProject/cess-go-sdk v0.3.21-0.20231107093552-741d1c28e744 h1:y/ZqscljT2jbl05rsq+v6hzuGWzEhlPYLxAQhtRUqmc=
github.com/CESSProject/cess-go-sdk v0.3.21-0.20231107093552-741d1c28e744/go.mod h1:x37J5WrzBYwFK3c2BkU9oLvhTmN0hrGwFV6NczTHrkM=
github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde h1:5MDRjjtg6PEhqyVjupwaapN96cOZiddOGAYwKQeaTu0=
github.com/CESSProject/go-keyring v0.0.0-20220614131247-ee3a8da30fde/go.mod h1:RUXBd3ROP98MYepEEa0Y0l/T0vQlIKqFJxI/ocdnRLM=
github.com/CESSProject/p2p-go v0.2.4 h1:E/tJfeBGeLZ07jBec5dVUobT33pyA1aRDpEDgLqxvoM=
Expand Down
7 changes: 7 additions & 0 deletions node/authHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package node

import (
"errors"
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -42,6 +43,12 @@ func (n *Node) authHandle(c *gin.Context) {
return
}

if !n.AccessControl(req.Account) {
n.Log("info", fmt.Sprintf("[%v] %v", c.ClientIP(), ERR_Forbidden))
c.JSON(http.StatusForbidden, ERR_Forbidden)
return
}

// Check publickey
pubkey, err := sutils.ParsingPublickey(req.Account)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions node/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http"
"strings"

"github.com/CESSProject/DeOSS/configs"
sutils "github.com/CESSProject/cess-go-sdk/core/utils"
"github.com/CESSProject/go-keyring"
jwt "github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -106,3 +107,34 @@ func (n *Node) verifySignature(account, message, signature string) ([]byte, erro
}
return nil, errors.New("signature verification failed")
}

func (n *Node) AccessControl(account string) bool {
if account == "" {
return false
}
err := sutils.VerityAddress(account, sutils.CessPrefix)
if err != nil {
return false
}

bwlist := n.GetAccounts()

if n.GetAccess() == configs.Access_Public {
for _, v := range bwlist {
if v == account {
return false
}
}
return true
}

if n.GetAccess() == configs.Access_Private {
for _, v := range bwlist {
if v == account {
return true
}
}
}

return false
}
7 changes: 7 additions & 0 deletions node/delHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func (n *Node) delFilesHandle(c *gin.Context) {
c.JSON(respMsg.Code, err.Error())
return
}

if !n.AccessControl(account) {
n.Del("info", fmt.Sprintf("[%v] %v", c.ClientIP(), ERR_Forbidden))
c.JSON(http.StatusForbidden, ERR_Forbidden)
return
}

n.Del("info", fmt.Sprintf("[%v] %v", clientIp, account))

var delList DelList
Expand Down
8 changes: 7 additions & 1 deletion node/getHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ func (n *Node) getHandle(c *gin.Context) {
n.Query("info", fmt.Sprintf("[%s] %s", clientIp, INFO_GetRequest))

cipher := c.Request.Header.Get(HTTPHeader_Cipher)
account := c.Request.Header.Get(HTTPHeader_Account)

if !n.AccessControl(account) {
n.Query("info", fmt.Sprintf("[%v] %v", c.ClientIP(), ERR_Forbidden))
c.JSON(http.StatusForbidden, ERR_Forbidden)
return
}

queryName := c.Param(HTTP_ParameterName)
if queryName == "version" {
Expand Down Expand Up @@ -111,7 +118,6 @@ func (n *Node) getHandle(c *gin.Context) {
}

if len(queryName) != len(pattern.FileHash{}) {
account := c.Request.Header.Get(HTTPHeader_Account)
if account == "" {
n.Query("err", fmt.Sprintf("[%s] %s", clientIp, ERR_MissAccount))
c.JSON(http.StatusBadRequest, ERR_MissAccount)
Expand Down
6 changes: 6 additions & 0 deletions node/getRestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func (n *Node) getRestoreHandle(c *gin.Context) {
account = userAccount
}

if !n.AccessControl(account) {
n.Upfile("info", fmt.Sprintf("[%v] %v", c.ClientIP(), ERR_Forbidden))
c.JSON(http.StatusForbidden, ERR_Forbidden)
return
}

var userfils_cache userFiles
data, err := n.Get([]byte(Cache_UserFiles + account))
if err == nil {
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func (n *Node) WriteTrackFile(filehash string, data []byte) error {
return err
}

func (n *Node) ParseTrackFromFile(filehash string) (RecordInfo, error) {
func (n *Node) ParseTrackFile(filehash string) (RecordInfo, error) {
var result RecordInfo
n.trackLock.RLock()
defer n.trackLock.RUnlock()
Expand Down
30 changes: 18 additions & 12 deletions node/postRestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,25 @@ func (n *Node) postRestoreHandle(c *gin.Context) {
if account != "" && signature != "" {
pkey, err = n.verifySignature(account, message, signature)
if err != nil {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, err))
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(respMsg.Code, err.Error())
return
}
} else {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, err))
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(respMsg.Code, err.Error())
return
}
} else {
account = userAccount
}

if !n.AccessControl(account) {
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, ERR_Forbidden))
c.JSON(http.StatusForbidden, ERR_Forbidden)
return
}

var restoreList RestoreList
err = c.ShouldBind(&restoreList)
if err != nil {
Expand All @@ -69,7 +75,7 @@ func (n *Node) postRestoreHandle(c *gin.Context) {
// verify the bucket name
bucketName := c.Request.Header.Get(HTTPHeader_BucketName)
if !sutils.CheckBucketName(bucketName) {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, ERR_HeaderFieldBucketName))
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, ERR_HeaderFieldBucketName))
c.JSON(http.StatusBadRequest, ERR_HeaderFieldBucketName)
return
}
Expand All @@ -84,7 +90,7 @@ func (n *Node) postRestoreHandle(c *gin.Context) {
}
}
if !flag {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, ERR_SpaceNotAuth))
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, ERR_SpaceNotAuth))
c.JSON(http.StatusForbidden, ERR_SpaceNotAuth)
return
}
Expand All @@ -93,24 +99,24 @@ func (n *Node) postRestoreHandle(c *gin.Context) {
userInfo, err := n.QueryUserSpaceSt(pkey)
if err != nil {
if err.Error() == pattern.ERR_Empty {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, ERR_AccountNotExist))
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, ERR_AccountNotExist))
c.JSON(http.StatusForbidden, ERR_AccountNotExist)
return
}
n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err))
n.Log("err", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(http.StatusForbidden, ERR_RpcFailed)
return
}

blockheight, err := n.QueryBlockHeight("")
if err != nil {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, err))
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(http.StatusForbidden, ERR_RpcFailed)
return
}

if userInfo.Deadline < (blockheight + 100) {
n.Upfile("info", fmt.Sprintf("[%v] %v [%d] [%d]", clientIp, ERR_SpaceExpiresSoon, userInfo.Deadline, blockheight))
n.Log("info", fmt.Sprintf("[%v] %v [%d] [%d]", clientIp, ERR_SpaceExpiresSoon, userInfo.Deadline, blockheight))
c.JSON(http.StatusForbidden, ERR_SpaceExpiresSoon)
return
}
Expand All @@ -133,13 +139,13 @@ func (n *Node) postRestoreHandle(c *gin.Context) {
usedSpace := allUsedSpace * 15 / 10
remainingSpace, err := strconv.ParseUint(userInfo.RemainingSpace, 10, 64)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err))
n.Log("err", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(http.StatusInternalServerError, ERR_InternalServer)
return
}

if usedSpace > int64(remainingSpace) {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, ERR_NotEnoughSpace))
n.Log("info", fmt.Sprintf("[%v] %v", clientIp, ERR_NotEnoughSpace))
c.JSON(http.StatusForbidden, ERR_NotEnoughSpace)
return
}
Expand All @@ -163,14 +169,14 @@ func (n *Node) postRestoreHandle(c *gin.Context) {

b, err := json.Marshal(recordInfo)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err))
n.Log("err", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(http.StatusInternalServerError, ERR_InternalServer)
continue
}

err = n.WriteTrackFile(restoreList.Files[i], b)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %v", clientIp, err))
n.Log("err", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(http.StatusInternalServerError, ERR_InternalServer)
continue
}
Expand Down
6 changes: 6 additions & 0 deletions node/putHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (n *Node) putHandle(c *gin.Context) {
account = userAccount
}

if !n.AccessControl(account) {
n.Upfile("info", fmt.Sprintf("[%v] %v", c.ClientIP(), ERR_Forbidden))
c.JSON(http.StatusForbidden, ERR_Forbidden)
return
}

// verify the bucket name
bucketName := c.Request.Header.Get(HTTPHeader_BucketName)
if !sutils.CheckBucketName(bucketName) {
Expand Down
Loading

0 comments on commit 6e4fd85

Please sign in to comment.