config.go 3.58 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
}

Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
46
// DefaultPathRoot is the path to the default config dir location.
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
47
const DefaultPathRoot = "~/.go-ipfs"
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
48 49

// DefaultConfigFile is the filename of the configuration file
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
50
const DefaultConfigFile = "config"
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
51 52

// DefaultDataStoreDirectory is the directory to store all the local IPFS data.
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
53
const DefaultDataStoreDirectory = "datastore"
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
54 55

// EnvDir is the environment variable used to change the path root.
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
56 57 58 59 60
const EnvDir = "IPFS_DIR"

// PathRoot returns the default configuration root directory
func PathRoot() (string, error) {
	dir := os.Getenv(EnvDir)
61 62
	var err error
	if len(dir) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
63
		dir, err = u.TildeExpansion(DefaultPathRoot)
64 65 66
	}
	return dir, err
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
68 69 70
// 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) {
71
	if len(configroot) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
72
		dir, err := PathRoot()
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
73 74 75
		if err != nil {
			return "", err
		}
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
76
		return filepath.Join(dir, extension), nil
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
77

78
	}
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
79
	return filepath.Join(configroot, extension), nil
80
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
81

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
82 83 84
// 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) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
85
	return Path(configroot, DefaultDataStoreDirectory)
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
86 87 88 89 90
}

// 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) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
91
	return Path(configroot, DefaultConfigFile)
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
92 93
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
94
// DecodePrivateKey is a helper to decode the users PrivateKey
95 96 97 98 99 100
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
101 102
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
103 104 105
	return x509.ParsePKCS1PrivateKey(pkb)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
106 107
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
108
	// if nothing is there, fail. User must run 'ipfs init'
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
109
	if _, err := os.Stat(filename); os.IsNotExist(err) {
110
		return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
111 112 113
	}

	var cfg Config
114
	err := ReadConfigFile(filename, &cfg)
115 116 117 118 119 120
	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
121 122 123 124 125
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
126
}
127 128 129

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