config.go 2.61 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package config

import (
4 5 6 7
	"crypto"
	"crypto/x509"
	"encoding/base64"
	"errors"
8
	"os"
9 10

	u "github.com/jbenet/go-ipfs/util"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12
)

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
13
// Identity tracks the configuration of the local node's identity.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14
type Identity struct {
15
	PeerID  string
16
	PrivKey string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17 18
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
19
// Datastore tracks the configuration of the datastore.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
20
type Datastore struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
21 22
	Type string
	Path string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
23 24
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26 27 28 29 30 31 32
// Addresses stores the (string) multiaddr addresses for the node.
type Addresses struct {
	Swarm string // address for the swarm network
	API   string // address for the local API (RPC)
}

// BootstrapPeer is a peer used to bootstrap the network.
type BootstrapPeer struct {
33
	Address string
34
	PeerID  string // until multiaddr supports ipfs, use another field.
35 36
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
37
// Config is used to load IPFS config files.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
type Config struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39 40 41
	Identity  Identity         // local node's peer identity
	Datastore Datastore        // local node's storage
	Addresses Addresses        // local node's addresses
42
	Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
43 44
}

45 46 47 48 49 50 51 52 53
// Return the default configuration root directory
func GetDefaultPathRoot() (string, error) {
	dir := os.Getenv("IPFS_CONFIG_DIR")
	var err error
	if len(dir) == 0 {
		dir, err = u.TildeExpansion("~/.go-ipfs")
	}
	return dir, err
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54

55 56 57 58 59 60 61 62 63 64
// Return the configuration file path given a configuration root directory
// If the configuration root directory is empty, use the default one
func GetConfigFilePath(configroot string) (string, error) {
	if len(configroot) == 0 {
		dir, err := GetDefaultPathRoot()
		return dir+"/config", err
	} else {
		return configroot+"/config", nil
	}
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66
// DecodePrivateKey is a helper to decode the users PrivateKey
67 68 69 70 71 72
func (i *Identity) DecodePrivateKey(passphrase string) (crypto.PrivateKey, error) {
	pkb, err := base64.StdEncoding.DecodeString(i.PrivKey)
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
75 76 77
	return x509.ParsePKCS1PrivateKey(pkb)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78 79
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
80
	// if nothing is there, fail. User must run 'ipfs init'
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
81
	if _, err := os.Stat(filename); os.IsNotExist(err) {
82
		return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
83 84 85
	}

	var cfg Config
86
	err := ReadConfigFile(filename, &cfg)
87 88 89 90 91 92
	if err != nil {
		return nil, err
	}

	// tilde expansion on datastore path
	cfg.Datastore.Path, err = u.TildeExpansion(cfg.Datastore.Path)
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
93 94 95 96 97
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
98
}
99 100 101

// Set sets the value of a particular config key
func Set(filename, key, value string) error {
102
	return WriteConfigKey(filename, key, value)
103
}