config.go 3.81 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
// 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)
}

32 33 34 35 36 37
// Mounts stores the (string) mount points
type Mounts struct {
	IPFS string
	IPNS string
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39
// BootstrapPeer is a peer used to bootstrap the network.
type BootstrapPeer struct {
40
	Address string
41
	PeerID  string // until multiaddr supports ipfs, use another field.
42 43
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44 45 46 47
func (bp *BootstrapPeer) String() string {
	return bp.Address + "/" + bp.PeerID
}

Juan Batiz-Benet's avatar
go lint  
Juan Batiz-Benet committed
48
// Config is used to load IPFS config files.
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
49
type Config struct {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
50 51 52
	Identity  Identity         // local node's peer identity
	Datastore Datastore        // local node's storage
	Addresses Addresses        // local node's addresses
53
	Mounts    Mounts           // local node's mount points
54
	Bootstrap []*BootstrapPeer // local nodes's bootstrap peers
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56
}

Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
57
// DefaultPathRoot is the path to the default config dir location.
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
58
const DefaultPathRoot = "~/.go-ipfs"
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
59 60

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

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

// EnvDir is the environment variable used to change the path root.
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
67 68 69 70 71
const EnvDir = "IPFS_DIR"

// PathRoot returns the default configuration root directory
func PathRoot() (string, error) {
	dir := os.Getenv(EnvDir)
72 73
	var err error
	if len(dir) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
74
		dir, err = u.TildeExpansion(DefaultPathRoot)
75 76 77
	}
	return dir, err
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
78

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
79 80 81
// 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) {
82
	if len(configroot) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
83
		dir, err := PathRoot()
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
84 85 86
		if err != nil {
			return "", err
		}
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
87
		return filepath.Join(dir, extension), nil
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
88

89
	}
Juan Batiz-Benet's avatar
lint  
Juan Batiz-Benet committed
90
	return filepath.Join(configroot, extension), nil
91
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
92

Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
93 94 95
// 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
96
	return Path(configroot, DefaultDataStoreDirectory)
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
97 98 99 100 101
}

// 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
102
	return Path(configroot, DefaultConfigFile)
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
103 104
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
105
// DecodePrivateKey is a helper to decode the users PrivateKey
106 107 108 109 110 111
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
112 113
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
114 115 116
	return x509.ParsePKCS1PrivateKey(pkb)
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
117 118
// Load reads given file and returns the read config, or error.
func Load(filename string) (*Config, error) {
119
	// if nothing is there, fail. User must run 'ipfs init'
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
120
	if _, err := os.Stat(filename); os.IsNotExist(err) {
121
		return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
Juan Batiz-Benet's avatar
gofmt  
Juan Batiz-Benet committed
122 123 124
	}

	var cfg Config
125
	err := ReadConfigFile(filename, &cfg)
126 127 128 129 130 131
	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
132 133 134 135 136
	if err != nil {
		return nil, err
	}

	return &cfg, err
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
137
}
138 139 140

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