Skip to content

Commit

Permalink
Inter 076 (#132)
Browse files Browse the repository at this point in the history
* add /404

* add eth sign
  • Loading branch information
AstaFrode authored Mar 19, 2024
1 parent 419d1af commit ae6d482
Show file tree
Hide file tree
Showing 58 changed files with 29,547 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ service iptables restart
### Method one
Download the latest release of the binary application directly at:
```
wget https://github.com/CESSProject/DeOSS/releases/download/v0.3.5/DeOSS0.3.5.linux-amd64.tar.gz
wget https://github.com/CESSProject/DeOSS/releases/download/v0.3.6/DeOSS0.3.6.linux-amd64.tar.gz
```

### Method two
Expand Down
2 changes: 1 addition & 1 deletion cmd/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func cmd_run_func(cmd *cobra.Command, args []string) {
p2pgo.Workspace(filepath.Join(n.GetWorkspace(), n.GetSignatureAcc(), n.GetSDKName())),
p2pgo.BootPeers(n.GetBootNodes()),
p2pgo.ProtocolPrefix(protocolPrefix),
p2pgo.EnableBitswap(),
//p2pgo.EnableBitswap(),
)
if err != nil {
out.Err(err.Error())
Expand Down
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.5"
Version = Name + " " + "v0.3.6"
// description
Description = "Object storage service based on CESS network"
)
Expand Down
18 changes: 18 additions & 0 deletions example/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"fmt"

"github.com/CESSProject/DeOSS/node"
)

func main() {
n := node.New()
pk, err := n.VerifyAccountSignature("cXh5StobuVP4B7mGH9xn8dSsDtXks4qLAou8ZdkZ6DbB6zzxe", "<Bytes>timestemp1708566687367</Bytes>", "fe98b9f492ee94c0fdf5b3fef5b72555683a9747b744c360863e2a2b22560418b2c1b9d9d158553afd3005c07291484b59d1c72ee70cfbb8440106de5fd8b18b")
if err != nil {
fmt.Println(err)
} else {
fmt.Println("ok: ", pk)
}
//n.Run2(8080, "")
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce
github.com/cbergoon/merkletree v0.2.0
github.com/centrifuge/go-substrate-rpc-client/v4 v4.2.1
github.com/ethereum/go-ethereum v1.10.20
github.com/gin-contrib/cors v1.5.0
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.5.0
Expand Down Expand Up @@ -40,6 +41,8 @@ require (
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/bytedance/sonic v1.10.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
Expand All @@ -56,7 +59,6 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elastic/gosigar v0.14.2 // indirect
github.com/ethereum/go-ethereum v1.10.20 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bradfitz/go-smtpd v0.0.0-20170404230938-deb6d6237625/go.mod h1:HYsPBTaaSFSlLx/70C2HPIMNZpVV8+vt/A+FMnYP11g=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.22.0-beta h1:LTDpDKUM5EeOFBPM8IXpinEcmZ6FWfNZbE3lfrfdnWo=
github.com/btcsuite/btcd v0.22.0-beta/go.mod h1:9n5ntfhhHQBIhUvlhDvD3Qg6fRUj4jkN0VB8L8svzOA=
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ=
github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I=
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
Expand Down Expand Up @@ -106,6 +110,7 @@ github.com/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5il
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
Expand Down Expand Up @@ -310,6 +315,7 @@ github.com/jedib0t/go-pretty/v6 v6.5.0 h1:FI0L5PktzbafnZKuPae/D3150x3XfYbFe2hxMT
github.com/jedib0t/go-pretty/v6 v6.5.0/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jellevandenhooff/dkim v0.0.0-20150330215556-f50fe3d243e1/go.mod h1:E0B/fFc00Y+Rasa88328GlI/XbtyysCtTHZS8h7IrBU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -453,6 +459,7 @@ github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
Expand Down Expand Up @@ -656,6 +663,7 @@ golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200602180216-279210d13fed/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
Expand All @@ -678,6 +686,7 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
20 changes: 20 additions & 0 deletions node/404.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package node

import (
"log"

"github.com/gin-gonic/gin"
)

func (n *Node) notFoundHandler(c *gin.Context) {
clientIp := c.ClientIP()
log.Printf("[%s] %s", clientIp, c.Request.URL.Path)

// b := bytes.NewBuffer(make([]byte, 0))
// bw := bufio.NewWriter(b)
// tpl := template.Must(template.New("tplName").Parse(tmpl.Notfound))
// tpl.Execute(bw, nil)
// bw.Flush()

c.HTML(200, "notfound.html", nil)
}
48 changes: 47 additions & 1 deletion node/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ package node
import (
"encoding/hex"
"fmt"
"strconv"
"strings"

"github.com/CESSProject/DeOSS/configs"
sutils "github.com/CESSProject/cess-go-sdk/utils"
"github.com/CESSProject/go-keyring"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
"github.com/vedhavyas/go-subkey/v2/sr25519"
)

func (n *Node) verifyAccountSignature(account, msg, signature string) ([]byte, error) {
func (n *Node) VerifyAccountSignature(account, msg, signature string) ([]byte, error) {
var err error
var publicKey []byte

if account == "" {
return nil, errors.New("Account is missing in request header")
}
Expand Down Expand Up @@ -51,6 +55,34 @@ func (n *Node) verifyAccountSignature(account, msg, signature string) ([]byte, e
return publicKey, nil
}

func VerifyEthSign(message string, sign string) (string, error) {
// Hash the unsigned message using EIP-191
hashedMessage := []byte("\x19Ethereum Signed Message:\n" + strconv.Itoa(len(message)) + message)
hash := crypto.Keccak256Hash(hashedMessage)

// Get the bytes of the signed message
decodedMessage, err := hexutil.Decode(sign)
if err != nil {
return "", err
}

// Handles cases where EIP-115 is not implemented (most wallets don't implement it)
if decodedMessage[64] == 27 || decodedMessage[64] == 28 {
decodedMessage[64] -= 27
}

// Recover a public key from the signed message
sigPublicKeyECDSA, err := crypto.SigToPub(hash.Bytes(), decodedMessage)
if sigPublicKeyECDSA == nil {
err = errors.New("Could not get a public get from the message signature")
}
if err != nil {
return "", err
}

return crypto.PubkeyToAddress(*sigPublicKeyECDSA).String(), nil
}

// VerifyToken is used to parse and verify token
func (n *Node) verifySignature(account, message, signature string) ([]byte, error) {
pkey, err := sutils.ParsingPublickey(account)
Expand Down Expand Up @@ -132,6 +164,13 @@ func (n *Node) verifyJsSignatureBase58(account, message, signature string) ([]by
sign_array[i] = sign_bytes[i]
}

if strings.HasPrefix(message, "<Bytes>") && strings.HasSuffix(message, "</Bytes>") {
ok := verkr.Verify(verkr.SigningContext([]byte(message)), sign_array)
if ok {
return pkey, nil
}
}

// Verify signature
ok := verkr.Verify(verkr.SigningContext([]byte("<Bytes>"+message+"</Bytes>")), sign_array)
if ok {
Expand Down Expand Up @@ -172,6 +211,13 @@ func (n *Node) verifyJsSignatureHex(account, message, signature string) ([]byte,
}

// Verify signature
if strings.HasPrefix(message, "<Bytes>") && strings.HasSuffix(message, "</Bytes>") {
ok := verkr.Verify(verkr.SigningContext([]byte(message)), sign_array)
if ok {
return pkey, nil
}
}

ok := verkr.Verify(verkr.SigningContext([]byte("<Bytes>"+message+"</Bytes>")), sign_array)
if ok {
return pkey, nil
Expand Down
36 changes: 29 additions & 7 deletions node/delHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (n *Node) delHandle(c *gin.Context) {
account := c.Request.Header.Get(HTTPHeader_Account)
message := c.Request.Header.Get(HTTPHeader_Message)
signature := c.Request.Header.Get(HTTPHeader_Signature)
pkey, err := n.verifyAccountSignature(account, message, signature)
pkey, err := n.VerifyAccountSignature(account, message, signature)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -91,21 +91,43 @@ func (n *Node) delFilesHandle(c *gin.Context) {
n.Del("info", fmt.Sprintf("[%v] %v", clientIp, INFO_DelRequest))

account := c.Request.Header.Get(HTTPHeader_Account)
ethAccount := c.Request.Header.Get(HTTPHeader_EthAccount)
message := c.Request.Header.Get(HTTPHeader_Message)
signature := c.Request.Header.Get(HTTPHeader_Signature)
_, err = n.verifyAccountSignature(account, message, signature)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, err.Error())
return
}

if err = n.AccessControl(account); err != nil {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(http.StatusForbidden, err.Error())
return
}

if ethAccount != "" {
ethAccInSian, err := VerifyEthSign(message, signature)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, err.Error())
return
}
if ethAccInSian != ethAccount {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, "ETH signature verification failed"))
c.JSON(http.StatusBadRequest, "ETH signature verification failed")
return
}
pkey, err = sutils.ParsingPublickey(account)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, fmt.Sprintf("invalid cess account: %s", account))
return
}
} else {
pkey, err = n.VerifyAccountSignature(account, message, signature)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, err.Error())
return
}
}

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

var delList DelList
Expand Down
36 changes: 30 additions & 6 deletions node/getRestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"os"
"path/filepath"

sutils "github.com/CESSProject/cess-go-sdk/utils"
"github.com/gin-gonic/gin"
)

// getHandle
func (n *Node) getRestoreHandle(c *gin.Context) {
var (
err error
clientIp string
repeat bool
)
Expand All @@ -21,21 +23,43 @@ func (n *Node) getRestoreHandle(c *gin.Context) {
n.Query("info", fmt.Sprintf("[%s] %s", clientIp, INFO_GetRestoreRequest))

account := c.Request.Header.Get(HTTPHeader_Account)
ethAccount := c.Request.Header.Get(HTTPHeader_EthAccount)
message := c.Request.Header.Get(HTTPHeader_Message)
signature := c.Request.Header.Get(HTTPHeader_Signature)
_, err := n.verifyAccountSignature(account, message, signature)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, err.Error())
return
}

if err = n.AccessControl(account); err != nil {
n.Upfile("info", fmt.Sprintf("[%v] %v", clientIp, err))
c.JSON(http.StatusForbidden, err.Error())
return
}

if ethAccount != "" {
ethAccInSian, err := VerifyEthSign(message, signature)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, err.Error())
return
}
if ethAccInSian != ethAccount {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, "ETH signature verification failed"))
c.JSON(http.StatusBadRequest, "ETH signature verification failed")
return
}
_, err = sutils.ParsingPublickey(account)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, fmt.Sprintf("invalid cess account: %s", account))
return
}
} else {
_, err = n.VerifyAccountSignature(account, message, signature)
if err != nil {
n.Upfile("err", fmt.Sprintf("[%v] %s", clientIp, err.Error()))
c.JSON(http.StatusBadRequest, err.Error())
return
}
}

var userfils_cache userFiles
data, err := n.Get([]byte(Cache_UserFiles + account))
if err == nil {
Expand Down
Loading

0 comments on commit ae6d482

Please sign in to comment.