Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
61c1e39a
提交
61c1e39a
authored
11月 16, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #266 from jbenet/defaulthash
add in a default file hash and cleaned up init function a bit
上级
5a372f69
ab09c364
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
190 行增加
和
81 行删除
+190
-81
init.go
cmd/ipfs/init.go
+95
-42
init.go
cmd/ipfs2/init.go
+90
-39
pin.go
pin/pin.go
+5
-0
没有找到文件。
cmd/ipfs/init.go
浏览文件 @
61c1e39a
package
main
import
(
"bytes"
"encoding/base64"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
config
"github.com/jbenet/go-ipfs/config"
core
"github.com/jbenet/go-ipfs/core"
ci
"github.com/jbenet/go-ipfs/crypto"
imp
"github.com/jbenet/go-ipfs/importer"
chunk
"github.com/jbenet/go-ipfs/importer/chunk"
peer
"github.com/jbenet/go-ipfs/peer"
updates
"github.com/jbenet/go-ipfs/updates"
u
"github.com/jbenet/go-ipfs/util"
)
...
...
@@ -33,6 +39,66 @@ func init() {
cmdIpfsInit
.
Flag
.
String
(
"d"
,
""
,
"Change default datastore location"
)
}
var
defaultPeers
=
[]
*
config
.
BootstrapPeer
{
&
config
.
BootstrapPeer
{
// mars.i.ipfs.io
PeerID
:
"QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
,
Address
:
"/ip4/104.131.131.82/tcp/4001"
,
},
}
func
datastoreConfig
(
dspath
string
)
(
config
.
Datastore
,
error
)
{
ds
:=
config
.
Datastore
{}
if
len
(
dspath
)
==
0
{
var
err
error
dspath
,
err
=
config
.
DataStorePath
(
""
)
if
err
!=
nil
{
return
ds
,
err
}
}
ds
.
Path
=
dspath
ds
.
Type
=
"leveldb"
// Construct the data store if missing
if
err
:=
os
.
MkdirAll
(
dspath
,
os
.
ModePerm
);
err
!=
nil
{
return
ds
,
err
}
// Check the directory is writeable
if
f
,
err
:=
os
.
Create
(
filepath
.
Join
(
dspath
,
"._check_writeable"
));
err
==
nil
{
os
.
Remove
(
f
.
Name
())
}
else
{
return
ds
,
errors
.
New
(
"Datastore '"
+
dspath
+
"' is not writeable"
)
}
return
ds
,
nil
}
func
identityConfig
(
nbits
int
)
(
config
.
Identity
,
error
)
{
ident
:=
config
.
Identity
{}
fmt
.
Println
(
"generating key pair..."
)
sk
,
pk
,
err
:=
ci
.
GenerateKeyPair
(
ci
.
RSA
,
nbits
)
if
err
!=
nil
{
return
ident
,
err
}
// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes
,
err
:=
sk
.
Bytes
()
if
err
!=
nil
{
return
ident
,
err
}
ident
.
PrivKey
=
base64
.
StdEncoding
.
EncodeToString
(
skbytes
)
id
,
err
:=
peer
.
IDFromPubKey
(
pk
)
if
err
!=
nil
{
return
ident
,
err
}
ident
.
PeerID
=
id
.
Pretty
()
return
ident
,
nil
}
func
initCmd
(
c
*
commander
.
Command
,
inp
[]
string
)
error
{
configpath
,
err
:=
getConfigDir
(
c
.
Parent
)
if
err
!=
nil
{
...
...
@@ -62,30 +128,12 @@ func initCmd(c *commander.Command, inp []string) error {
}
cfg
:=
new
(
config
.
Config
)
cfg
.
Datastore
=
config
.
Datastore
{}
if
len
(
dspath
)
==
0
{
dspath
,
err
=
config
.
DataStorePath
(
""
)
if
err
!=
nil
{
return
err
}
}
cfg
.
Datastore
.
Path
=
dspath
cfg
.
Datastore
.
Type
=
"leveldb"
// Construct the data store if missing
if
err
:=
os
.
MkdirAll
(
dspath
,
os
.
ModePerm
);
err
!=
nil
{
// setup the datastore
cfg
.
Datastore
,
err
=
datastoreConfig
(
dspath
)
if
err
!=
nil
{
return
err
}
// Check the directory is writeable
if
f
,
err
:=
os
.
Create
(
filepath
.
Join
(
dspath
,
"._check_writeable"
));
err
==
nil
{
os
.
Remove
(
f
.
Name
())
}
else
{
return
errors
.
New
(
"Datastore '"
+
dspath
+
"' is not writeable"
)
}
cfg
.
Identity
=
config
.
Identity
{}
// setup the node addresses.
cfg
.
Addresses
=
config
.
Addresses
{
Swarm
:
"/ip4/0.0.0.0/tcp/4001"
,
...
...
@@ -106,42 +154,47 @@ func initCmd(c *commander.Command, inp []string) error {
return
errors
.
New
(
"Bitsize less than 1024 is considered unsafe."
)
}
u
.
POut
(
"generating key pair
\n
"
)
sk
,
pk
,
err
:=
ci
.
GenerateKeyPair
(
ci
.
RSA
,
nbits
)
cfg
.
Identity
,
err
=
identityConfig
(
nbits
)
if
err
!=
nil
{
return
err
}
// currently storing key unencrypted. in the future we need to encrypt it.
// TODO(security)
skbytes
,
err
:=
sk
.
Bytes
()
if
err
!=
nil
{
return
err
// Use these hardcoded bootstrap peers for now.
cfg
.
Bootstrap
=
defaultPeers
// tracking ipfs version used to generate the init folder
// and adding update checker default setting.
cfg
.
Version
=
config
.
Version
{
Check
:
"error"
,
Current
:
updates
.
Version
,
}
cfg
.
Identity
.
PrivKey
=
base64
.
StdEncoding
.
EncodeToString
(
skbytes
)
id
,
err
:=
peer
.
IDFromPubKey
(
pk
)
err
=
config
.
WriteConfigFile
(
filename
,
cfg
)
if
err
!=
nil
{
return
err
}
cfg
.
Identity
.
PeerID
=
id
.
Pretty
()
u
.
POut
(
"peer identity: %s
\n
"
,
id
.
Pretty
())
// Use these hardcoded bootstrap peers for now.
cfg
.
Bootstrap
=
[]
*
config
.
BootstrapPeer
{
&
config
.
BootstrapPeer
{
// mars.i.ipfs.io
PeerID
:
"QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ"
,
Address
:
"/ip4/104.131.131.82/tcp/4001"
,
},
nd
,
err
:=
core
.
NewIpfsNode
(
cfg
,
false
)
if
err
!=
nil
{
return
err
}
defer
nd
.
Close
()
// tracking ipfs version used to generate the init folder and adding update checker default setting.
cfg
.
Version
=
config
.
VersionDefaultValue
()
// Set up default file
msg
:=
`Hello and Welcome to IPFS!
If you're seeing this, that means that you have successfully
installed ipfs and are now interfacing with the wonderful
world of DAGs and hashes!
`
reader
:=
bytes
.
NewBufferString
(
msg
)
err
=
config
.
WriteConfigFile
(
filename
,
cfg
)
defnd
,
err
:=
imp
.
BuildDagFromReader
(
reader
,
nd
.
DAG
,
nd
.
Pinning
.
GetManual
(),
chunk
.
DefaultSplitter
)
if
err
!=
nil
{
return
err
}
k
,
_
:=
defnd
.
Key
()
fmt
.
Printf
(
"Default file key: %s
\n
"
,
k
)
return
nil
}
cmd/ipfs2/init.go
浏览文件 @
61c1e39a
package
main
import
(
"bytes"
"encoding/base64"
"errors"
"fmt"
...
...
@@ -9,7 +10,10 @@ import (
cmds
"github.com/jbenet/go-ipfs/commands"
config
"github.com/jbenet/go-ipfs/config"
core
"github.com/jbenet/go-ipfs/core"
ci
"github.com/jbenet/go-ipfs/crypto"
imp
"github.com/jbenet/go-ipfs/importer"
chunk
"github.com/jbenet/go-ipfs/importer/chunk"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
)
...
...
@@ -46,40 +50,114 @@ var initCmd = &cmds.Command{
nBitsForKeypair
=
4096
}
return
nil
,
doInit
(
req
.
Context
()
.
ConfigRoot
,
dspathOverride
,
force
,
nBitsForKeypair
)
return
doInit
(
req
.
Context
()
.
ConfigRoot
,
dspathOverride
,
force
,
nBitsForKeypair
)
},
}
// TODO add default welcome hash: eaa68bedae247ed1e5bd0eb4385a3c0959b976e4
var
errCannotInitConfigExists
=
errors
.
New
(
`ipfs configuration file already exists!
Reinitializing would overwrite your keys.
(use -f to force overwrite)
`
)
var
welcomeMsg
=
`Hello and Welcome to IPFS!
██╗██████╗ ███████╗███████╗
██║██╔══██╗██╔════╝██╔════╝
██║██████╔╝█████╗ ███████╗
██║██╔═══╝ ██╔══╝ ╚════██║
██║██║ ██║ ███████║
╚═╝╚═╝ ╚═╝ ╚══════╝
If you're seeing this, you have successfully installed
IPFS and are now interfacing with the ipfs merkledag!
For a short demo of what you can do, enter 'ipfs tour'
`
// NB: if dspath is not provided, it will be retrieved from the config
func
doInit
(
configRoot
string
,
dspathOverride
string
,
force
bool
,
nBitsForKeypair
int
)
error
{
func
doInit
(
configRoot
string
,
dspathOverride
string
,
force
bool
,
nBitsForKeypair
int
)
(
interface
{},
error
)
{
u
.
POut
(
"initializing ipfs node at %s
\n
"
,
configRoot
)
configFilename
,
err
:=
config
.
Filename
(
configRoot
)
if
err
!=
nil
{
return
errors
.
New
(
"Couldn't get home directory path"
)
return
nil
,
errors
.
New
(
"Couldn't get home directory path"
)
}
fi
,
err
:=
os
.
Lstat
(
configFilename
)
if
fi
!=
nil
||
(
err
!=
nil
&&
!
os
.
IsNotExist
(
err
))
{
if
!
force
{
// TODO multi-line string
return
errors
.
New
(
"ipfs configuration file already exists!
\n
Reinitializing would overwrite your keys.
\n
(use -f to force overwrite)"
)
return
nil
,
errCannotInitConfigExists
}
}
conf
,
err
:=
initConfig
(
configFilename
,
dspathOverride
,
nBitsForKeypair
)
if
err
!=
nil
{
return
nil
,
err
}
nd
,
err
:=
core
.
NewIpfsNode
(
conf
,
false
)
if
err
!=
nil
{
return
nil
,
err
}
defer
nd
.
Close
()
// Set up default file
reader
:=
bytes
.
NewBufferString
(
welcomeMsg
)
defnd
,
err
:=
imp
.
BuildDagFromReader
(
reader
,
nd
.
DAG
,
nd
.
Pinning
.
GetManual
(),
chunk
.
DefaultSplitter
)
if
err
!=
nil
{
return
nil
,
err
}
k
,
err
:=
defnd
.
Key
()
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to write test file: %s"
,
err
)
}
fmt
.
Printf
(
"done.
\n
to test, enter: ipfs cat %s
\n
"
,
k
)
return
nil
,
nil
}
func
datastoreConfig
(
dspath
string
)
(
config
.
Datastore
,
error
)
{
ds
:=
config
.
Datastore
{}
if
len
(
dspath
)
==
0
{
var
err
error
dspath
,
err
=
config
.
DataStorePath
(
""
)
if
err
!=
nil
{
return
ds
,
err
}
}
ds
.
Path
=
dspath
ds
.
Type
=
"leveldb"
// Construct the data store if missing
if
err
:=
os
.
MkdirAll
(
dspath
,
os
.
ModePerm
);
err
!=
nil
{
return
ds
,
err
}
// Check the directory is writeable
if
f
,
err
:=
os
.
Create
(
filepath
.
Join
(
dspath
,
"._check_writeable"
));
err
==
nil
{
os
.
Remove
(
f
.
Name
())
}
else
{
return
ds
,
errors
.
New
(
"Datastore '"
+
dspath
+
"' is not writeable"
)
}
return
ds
,
nil
}
func
initConfig
(
configFilename
string
,
dspathOverride
string
,
nBitsForKeypair
int
)
(
*
config
.
Config
,
error
)
{
ds
,
err
:=
datastoreConfig
(
dspathOverride
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
identity
,
err
:=
identityConfig
(
nBitsForKeypair
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
conf
:=
config
.
Config
{
conf
:=
&
config
.
Config
{
// setup the node addresses.
Addresses
:
config
.
Addresses
{
...
...
@@ -110,38 +188,11 @@ func doInit(configRoot string, dspathOverride string, force bool, nBitsForKeypai
Version
:
config
.
VersionDefaultValue
(),
}
err
=
config
.
WriteConfigFile
(
configFilename
,
conf
)
if
err
!=
nil
{
return
err
}
return
nil
}
func
datastoreConfig
(
dspath
string
)
(
config
.
Datastore
,
error
)
{
ds
:=
config
.
Datastore
{}
if
len
(
dspath
)
==
0
{
var
err
error
dspath
,
err
=
config
.
DataStorePath
(
""
)
if
err
!=
nil
{
return
ds
,
err
}
}
ds
.
Path
=
dspath
ds
.
Type
=
"leveldb"
// Construct the data store if missing
if
err
:=
os
.
MkdirAll
(
dspath
,
os
.
ModePerm
);
err
!=
nil
{
return
ds
,
err
}
// Check the directory is writeable
if
f
,
err
:=
os
.
Create
(
filepath
.
Join
(
dspath
,
"._check_writeable"
));
err
==
nil
{
os
.
Remove
(
f
.
Name
())
}
else
{
return
ds
,
errors
.
New
(
"Datastore '"
+
dspath
+
"' is not writeable"
)
if
err
:=
config
.
WriteConfigFile
(
configFilename
,
conf
);
err
!=
nil
{
return
nil
,
err
}
return
ds
,
nil
return
conf
,
nil
}
func
identityConfig
(
nbits
int
)
(
config
.
Identity
,
error
)
{
...
...
@@ -151,7 +202,7 @@ func identityConfig(nbits int) (config.Identity, error) {
return
ident
,
errors
.
New
(
"Bitsize less than 1024 is considered unsafe."
)
}
fmt
.
Print
ln
(
"generating key pair..."
)
fmt
.
Print
f
(
"generating key pair..."
)
sk
,
pk
,
err
:=
ci
.
GenerateKeyPair
(
ci
.
RSA
,
nbits
)
if
err
!=
nil
{
return
ident
,
err
...
...
pin/pin.go
浏览文件 @
61c1e39a
...
...
@@ -32,6 +32,7 @@ type Pinner interface {
Pin
(
*
mdag
.
Node
,
bool
)
error
Unpin
(
util
.
Key
,
bool
)
error
Flush
()
error
GetManual
()
ManualPinner
}
// ManualPinner is for manually editing the pin structure
...
...
@@ -263,3 +264,7 @@ func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
p
.
indirPin
.
Increment
(
k
)
}
}
func
(
p
*
pinner
)
GetManual
()
ManualPinner
{
return
p
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论