config.go 4.79 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 6 7
	"crypto"
	"crypto/x509"
	"encoding/base64"
8
	"os"
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
9
	"path/filepath"
10 11

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

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

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

23 24 25 26 27 28 29 30
// 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
31
// Datastore tracks the configuration of the datastore.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
32
type Datastore struct {
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
33 34
	Type string
	Path string
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
37 38 39 40 41 42
// 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)
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
134
// DecodePrivateKey is a helper to decode the users PrivateKey
135 136 137 138 139 140
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
141 142
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
143 144 145
	return x509.ParsePKCS1PrivateKey(pkb)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
146 147
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
148
	// if nothing is there, fail. User must run 'ipfs init'
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
149
	if _, err := os.Stat(filename); os.IsNotExist(err) {
150
		return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
151 152 153
	}

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

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

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