Skip to content

Commit

Permalink
Release 3.1.0 (#468)
Browse files Browse the repository at this point in the history
* feat: add dashboard login

* feat: add subcommand for dashboard login

* feat: add dashboard login command test

* feat: show encrypted file in web file list

* feat: add cros for token intercept

* feat: change dashboard api response

* feat: add two step check for some url

* feat: add dashboard logout

* feat: transfer api two step validate

* chore: format

* test: add dashboard command test

* feat: upgrade dependency of btfs

* style: go fmt

* ci: upgrade golang version

* test: fix test problem

* style: format

* feat: upload encrypted file

* feat: change encrypt file argument sequence

* fix: timeout for decrypt

* feat: ignore local cmd

* feat: get api host from config

* feat: remove file-related commands

* feat: deprecate object command

* feat: remove tar-related commands

* feat: ignore config show path.

* feat: update version and cid

* fix: two step check for transfer op

* feat: resource manage use json file

* feat: update dashboard cid

---------

Co-authored-by: cody <mengxiaole269@gmail.com>
Co-authored-by: cody <cody.meng@tron.com>
Co-authored-by: Cody <133026515+mengcody@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 30, 2024
1 parent 83008eb commit 6f917f0
Show file tree
Hide file tree
Showing 72 changed files with 1,454 additions and 1,046 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18
FROM golang:1.22
MAINTAINER TRON-US <support@tron.network>

# Install deps
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.testing
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18
FROM golang:1.22
MAINTAINER TRON-US <support@tron.network>

# Install deps
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.unit_testing
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.18
FROM golang:1.22
MAINTAINER TRON-US <support@tron.network>

# Dockerfile.unit_testing will build an image to run the go unit tests.
Expand Down
14 changes: 12 additions & 2 deletions cmd/btfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
loader "github.com/bittorrent/go-btfs/plugin/loader"
repo "github.com/bittorrent/go-btfs/repo"
fsrepo "github.com/bittorrent/go-btfs/repo/fsrepo"
"github.com/google/uuid"

cmds "github.com/bittorrent/go-btfs-cmds"
"github.com/bittorrent/go-btfs-cmds/cli"
cmdhttp "github.com/bittorrent/go-btfs-cmds/http"
config "github.com/bittorrent/go-btfs-config"
u "github.com/ipfs/go-ipfs-util"
logging "github.com/ipfs/go-log"
loggables "github.com/libp2p/go-libp2p-loggables"
ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
manet "github.com/multiformats/go-multiaddr/net"
Expand Down Expand Up @@ -73,7 +73,7 @@ func main() {

func mainRet() int {
rand.Seed(time.Now().UnixNano())
ctx := logging.ContextWithLoggable(context.Background(), loggables.Uuid("session"))
ctx := logging.ContextWithLoggable(context.Background(), newUUID("session"))
var err error

// we'll call this local helper to output errors.
Expand Down Expand Up @@ -382,3 +382,13 @@ func resolveAddr(ctx context.Context, addr ma.Multiaddr) (ma.Multiaddr, error) {

return addrs[0], nil
}

func newUUID(key string) logging.Metadata {
ids := "#UUID-ERROR#"
if id, err := uuid.NewRandom(); err == nil {
ids = id.String()
}
return logging.Metadata{
key: ids,
}
}
4 changes: 2 additions & 2 deletions core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

cmds "github.com/bittorrent/go-btfs-cmds"
humanize "github.com/dustin/go-humanize"
bitswap "github.com/ipfs/go-bitswap"
decision "github.com/ipfs/go-bitswap/decision"
bitswap "github.com/ipfs/boxo/bitswap"
decision "github.com/ipfs/boxo/bitswap/decision"
cidutil "github.com/ipfs/go-cidutil"
peer "github.com/libp2p/go-libp2p/core/peer"
)
Expand Down
19 changes: 11 additions & 8 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func TestCommands(t *testing.T) {
"/config/replace",
"/config/reset",
"/config/show",
//"/config/profile",
//"/config/profile/apply",
// "/config/profile",
// "/config/profile/apply",
"/config/storage-host-enable",
"/config/sync-chain-info",
"/config/sync-simple-mode",
Expand All @@ -120,8 +120,6 @@ func TestCommands(t *testing.T) {
"/diag/cmds/set-time",
"/diag/sys",
"/dns",
"/file",
"/file/ls",
"/files",
"/files/chcid",
"/files/cp",
Expand Down Expand Up @@ -217,9 +215,6 @@ func TestCommands(t *testing.T) {
"/swarm/filters/add",
"/swarm/filters/rm",
"/swarm/peers",
"/tar",
"/tar/add",
"/tar/cat",
"/urlstore",
"/urlstore/add",
"/version",
Expand Down Expand Up @@ -332,7 +327,7 @@ func TestCommands(t *testing.T) {
"/statuscontract/lastinfo",
"/statuscontract/config",
"/statuscontract/report_online_server",
//"/statuscontract/report_status_contract",
// "/statuscontract/report_status_contract",
"/statuscontract/daily_report_online_server",
"/statuscontract/daily_report_list",
"/statuscontract/daily_total",
Expand Down Expand Up @@ -361,6 +356,14 @@ func TestCommands(t *testing.T) {
"/cheque/fix_cheque_cashout",
"/encrypt",
"/decrypt",
"/dashboard",
"/dashboard/check",
"/dashboard/set",
"/dashboard/reset",
"/dashboard/login",
"/dashboard/logout",
"/dashboard/change",
"/dashboard/validate",
}

cmdSet := make(map[string]struct{})
Expand Down
5 changes: 3 additions & 2 deletions core/commands/dag/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
files "github.com/bittorrent/go-btfs-files"
"github.com/bittorrent/interface-go-btfs-core/options"
path "github.com/bittorrent/interface-go-btfs-core/path"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
cidenc "github.com/ipfs/go-cidutil/cidenc"
ipld "github.com/ipfs/go-ipld-format"
Expand Down Expand Up @@ -386,7 +387,7 @@ Maximum supported CAR version: 1

if block, err := node.Blockstore.Get(ctx, c); err != nil {
ret.PinErrorMsg = err.Error()
} else if nd, err := ipld.Decode(block); err != nil {
} else if nd, err := ipld.Decode(block, func(block blocks.Block) (ipld.Node, error) { return nil, nil }); err != nil {
ret.PinErrorMsg = err.Error()
} else if err := node.Pinning.Pin(req.Context, nd, true); err != nil {
ret.PinErrorMsg = err.Error()
Expand Down Expand Up @@ -494,7 +495,7 @@ func importWorker(req *cmds.Request, re cmds.ResponseEmitter, api iface.CoreAPI,
}

// the double-decode is suboptimal, but we need it for batching
nd, err := ipld.Decode(block)
nd, err := ipld.Decode(block, func(block blocks.Block) (ipld.Node, error) { return nil, nil })
if err != nil {
return err
}
Expand Down
234 changes: 234 additions & 0 deletions core/commands/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
package commands

import (
"encoding/hex"
"errors"
cmds "github.com/bittorrent/go-btfs-cmds"
"github.com/bittorrent/go-btfs/core/commands/cmdenv"
"github.com/bittorrent/go-btfs/utils"
ds "github.com/ipfs/go-datastore"
)

const DashboardPasswordPrefix = "/dashboard_password"
const TokenExpire = 60 * 60 * 24 * 1

var IsLogin bool

type DashboardResponse struct {
Success bool
Text string
}

var dashboardCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "dashboard password operation",
},

Subcommands: map[string]*cmds.Command{
"check": checkCmd,
"set": setCmd,
"reset": resetCmd,
"change": changeCmd,
"login": loginCmd,
"logout": logoutCmd,
"validate": validateCmd,
},
}

var checkCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "check if password is set",
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}

_, err = node.Repo.Datastore().Get(req.Context, ds.NewKey(DashboardPasswordPrefix))

if err != nil && errors.Is(err, ds.ErrNotFound) {
return re.Emit(&DashboardResponse{Success: false, Text: "passwd is not set"})
}

if err != nil {
log.Info("check password error", err)
return err
}
return re.Emit(DashboardResponse{Success: true, Text: "password was set"})
},
}

var setCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "set password",
},
Arguments: []cmds.Argument{
cmds.StringArg("password", true, false, "set password"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}
// check if password has set
_, err = node.Repo.Datastore().Get(req.Context, ds.NewKey(DashboardPasswordPrefix))
if err != nil && !errors.Is(err, ds.ErrNotFound) {
log.Info("set password error", err)
return err
}

if err == nil {
return re.Emit(&DashboardResponse{
Success: false,
Text: "password has set, if you want to reset your password, please use reset command instead",
})
}

datastore := node.Repo.Datastore()
key := ds.NewKey(DashboardPasswordPrefix)
err = datastore.Put(req.Context, key, []byte(req.Arguments[0]))
if err != nil {
return err
}
return re.Emit(&DashboardResponse{Success: true, Text: "password set success!"})
},
}

var loginCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "login with passwd and get the token",
},
Arguments: []cmds.Argument{
cmds.StringArg("password", true, false, "set password"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}
value, err := node.Repo.Datastore().Get(req.Context, ds.NewKey(DashboardPasswordPrefix))
if errors.Is(err, ds.ErrNotFound) {
return re.Emit(&DashboardResponse{Success: false, Text: "password has not set, please set passwd first"})
}
if err != nil {
return err
}
if string(value) != req.Arguments[0] {
log.Info("login password is correct")
return re.Emit(&DashboardResponse{Success: false, Text: "password is not correct"})
}

config, err := node.Repo.Config()

publicKey := config.Identity.PeerID

token, err := utils.GenerateToken(publicKey, string(value), TokenExpire)
if err != nil {
return err
}

IsLogin = true

return re.Emit(&DashboardResponse{
Success: true,
Text: token,
})
},
}

var resetCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "reset password",
},
Arguments: []cmds.Argument{
cmds.StringArg("privateKey", true, false, "private key"),
cmds.StringArg("newPassword", true, false, "new password"),
},

Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}

raw, err := node.PrivateKey.Raw()
if err != nil {
return err
}

if hex.EncodeToString(raw) != req.Arguments[0] {
return re.Emit(&DashboardResponse{Success: false, Text: "private key is not correct"})
}
datastore := node.Repo.Datastore()
err = datastore.Put(req.Context, ds.NewKey(DashboardPasswordPrefix), []byte(req.Arguments[1]))
if err != nil {
return err
}
return re.Emit(&DashboardResponse{Success: true, Text: "password reset success!"})
},
}

var changeCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "change password",
},
Arguments: []cmds.Argument{
cmds.StringArg("oldPassword", true, false, "change password"),
cmds.StringArg("newPassword", true, false, "change password"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}
datastore := node.Repo.Datastore()
value, err := datastore.Get(req.Context, ds.NewKey(DashboardPasswordPrefix))
if err != nil {
return err
}
if string(value) != req.Arguments[0] {
return re.Emit(&DashboardResponse{Success: false, Text: "the old password is not correct"})
}
err = datastore.Put(req.Context, ds.NewKey(DashboardPasswordPrefix), []byte(req.Arguments[1]))
if err != nil {
return err
}
return re.Emit(&DashboardResponse{Success: true, Text: "password change success!"})
},
}

var logoutCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "logout",
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
IsLogin = false
return re.Emit(&DashboardResponse{Success: true, Text: "logout success!"})
},
}

var validateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "validate passwd",
},
Arguments: []cmds.Argument{
cmds.StringArg("password", true, false, "validate passwd"),
},
Run: func(r *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}
datastore := node.Repo.Datastore()
value, err := datastore.Get(r.Context, ds.NewKey(DashboardPasswordPrefix))
if err != nil {
return err
}

if string(value) != r.Arguments[0] {
return re.Emit(&DashboardResponse{Success: false, Text: "password is not correct"})
}
return re.Emit(&DashboardResponse{Success: true, Text: "password is correct"})
},
}
Loading

0 comments on commit 6f917f0

Please sign in to comment.