config.go 4.77 KB
Newer Older
1
// package config implements the ipfs config file datastructures and utilities.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3 4
package config

import (
5
	"encoding/base64"
6
	"os"
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
7
	"path/filepath"
8

9
	ic "github.com/jbenet/go-ipfs/crypto"
10
	u "github.com/jbenet/go-ipfs/util"
Brian Tiger Chow's avatar
Brian Tiger Chow committed
11
	"github.com/jbenet/go-ipfs/util/debugerror"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
12 13
)

14 15
var log = u.Logger("config")

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

22 23 24 25 26 27 28 29
// Logs tracks the configuration of the event logger
type Logs struct {
	Filename   string
	MaxSizeMB  uint64
	MaxBackups uint64
	MaxAgeDays uint64
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
30
// Datastore tracks the configuration of the datastore.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31
type Datastore struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
32 33
	Type string
	Path string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37
// Addresses stores the (string) multiaddr addresses for the node.
type Addresses struct {
38 39
	Swarm []string // addresses for the swarm network
	API   string   // address for the local API (RPC)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41
}

42 43 44 45 46 47
// Mounts stores the (string) mount points
type Mounts struct {
	IPFS string
	IPNS string
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48 49
// BootstrapPeer is a peer used to bootstrap the network.
type BootstrapPeer struct {
50
	Address string
51
	PeerID  string // until multiaddr supports ipfs, use another field.
52 53
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55 56 57
func (bp *BootstrapPeer) String() string {
	return bp.Address + "/" + bp.PeerID
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
58 59 60 61 62 63
// Tour stores the ipfs tour read-list and resume point
type Tour struct {
	Last string // last tour topic read
	// Done []string // all topics done so far
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
64
// Config is used to load IPFS config files.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65
type Config struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
66 67 68
	Identity  Identity         // local node's peer identity
	Datastore Datastore        // local node's storage
	Addresses Addresses        // local node's addresses
69
	Mounts    Mounts           // local node's mount points
70
	Version   Version          // local node's version management
71
	Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
72
	Tour      Tour             // local node's tour position
73
	Logs      Logs             // local node's event log configuration
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74 75
}

Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
76
// DefaultPathRoot is the path to the default config dir location.
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
77
const DefaultPathRoot = "~/.go-ipfs"
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
78 79

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

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

// EnvDir is the environment variable used to change the path root.
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
86 87
const EnvDir = "IPFS_DIR"

88 89 90
// LogsDefaultDirectory is the directory to store all IPFS event logs.
var LogsDefaultDirectory = "logs"

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
91 92 93
// PathRoot returns the default configuration root directory
func PathRoot() (string, error) {
	dir := os.Getenv(EnvDir)
94 95
	var err error
	if len(dir) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
96
		dir, err = u.TildeExpansion(DefaultPathRoot)
97 98 99
	}
	return dir, err
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
101 102 103
// 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) {
104
	if len(configroot) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
105
		dir, err := PathRoot()
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
106 107 108
		if err != nil {
			return "", err
		}
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
109
		return filepath.Join(dir, extension), nil
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
110

111
	}
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
112
	return filepath.Join(configroot, extension), nil
113
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
114

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
115 116 117
// 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
118
	return Path(configroot, DefaultDataStoreDirectory)
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
119 120
}

121 122 123 124 125 126
// LogsPath returns the default path for event logs given a configuration root
// (set an empty string to have the default configuration root)
func LogsPath(configroot string) (string, error) {
	return Path(configroot, LogsDefaultDirectory)
}

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
127 128 129
// 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
130
	return Path(configroot, DefaultConfigFile)
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
131 132
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
133
// DecodePrivateKey is a helper to decode the users PrivateKey
134
func (i *Identity) DecodePrivateKey(passphrase string) (ic.PrivKey, error) {
135 136 137 138 139
	pkb, err := base64.StdEncoding.DecodeString(i.PrivKey)
	if err != nil {
		return nil, err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
142
	return ic.UnmarshalPrivateKey(pkb)
143 144
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
145 146
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
147
	// if nothing is there, fail. User must run 'ipfs init'
148
	if !u.FileExists(filename) {
Brian Tiger Chow's avatar
Brian Tiger Chow committed
149
		return nil, debugerror.New("ipfs not initialized, please run 'ipfs init'")
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
150 151 152
	}

	var cfg Config
153
	err := ReadConfigFile(filename, &cfg)
154 155 156 157 158 159
	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
160 161 162 163 164
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165
}
166 167 168

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