config.go 3.28 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"
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
9
	"path/filepath"
10 11

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
26 27 28 29 30 31 32 33
// 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 {
34
	Address string
35
	PeerID  string // until multiaddr supports ipfs, use another field.
36 37
}

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

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
46 47 48 49 50 51 52 53
const DefaultPathRoot = "~/.go-ipfs"
const DefaultConfigFile = "config"
const DefaultDataStoreDirectory = "datastore"
const EnvDir = "IPFS_DIR"

// PathRoot returns the default configuration root directory
func PathRoot() (string, error) {
	dir := os.Getenv(EnvDir)
54 55
	var err error
	if len(dir) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
56
		dir, err = u.TildeExpansion(DefaultPathRoot)
57 58 59
	}
	return dir, err
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
60

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
61 62 63
// Path returns the path `extension` relative to the configuration root. If an
// empty string is provided for `configroot`, the default root is used.
func Path(configroot, extension string) (string, error) {
64
	if len(configroot) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
65 66
		dir, err := PathRoot()
		return filepath.Join(dir, extension), err
67
	} else {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
68
		return filepath.Join(configroot, extension), nil
69 70
	}
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
71

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
72 73 74 75 76 77 78 79 80 81 82 83
// DataStorePath returns the default data store path given a configuration root
// (set an empty string to have the default configuration root)
func DataStorePath(configroot string) (string, error) {
	return Path(configroot, DefaultDataStoreDirectory);
}

// Filename returns the configuration file path given a configuration root
// directory. If the configuration root directory is empty, use the default one
func Filename(configroot string) (string, error) {
	return Path(configroot, DefaultConfigFile);
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
84
// DecodePrivateKey is a helper to decode the users PrivateKey
85 86 87 88 89 90
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
91 92
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
93 94 95
	return x509.ParsePKCS1PrivateKey(pkb)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
98
	// if nothing is there, fail. User must run 'ipfs init'
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
99
	if _, err := os.Stat(filename); os.IsNotExist(err) {
100
		return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
101 102 103
	}

	var cfg Config
104
	err := ReadConfigFile(filename, &cfg)
105 106 107 108 109 110
	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
111 112 113 114 115
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116
}
117 118 119

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