ipfs 搭建私有网络 作为服务运行
主节点:whx: 192.168.33.10
worker: 192.168.33.21
下载:
1 wget https://github.com/ipfs/go-ipfs/releases/download/v0.7.0/go-ipfs_v0.7.0_linux-amd64.tar.gz
解压缩 tar xzf go-ipfs_v0.7.0_linux-amd64.tar.gz
切换root,两个节点都执行
初始化:
1 2 3 4 5 cd go-ipfs sudo bash install.sh cp /usr/local/bin/ipfs /usr/bin/ipfs ipfs --version /usr/local/bin/ipfs init
主节点:
安装golang
1 2 3 yum install epel-release yum install go
创建共享key
1 2 3 4 5 6 7 8 9 10 11 12 # go get github.com/Kubuxu/go-ipfs-swarm-key-gen/ipfs-swarm-key-gen # go/bin/ipfs-swarm-key-gen > /root/.ipfs/swarm.key # ipfs bootstrap rm all # ipfs id { "ID": "12D3KooWNvmkBW5noeQLzEpSpktYSZjq69727Z9e9F1sEJtuwJEb", "PublicKey": "CAESIMLMEqwfQn4BGZEcmH9ch+Oz93YYWqg//+i5/2dG26nI", "Addresses": null, "AgentVersion": "go-ipfs/0.7.0/", "ProtocolVersion": "ipfs/0.1.0", "Protocols": null }
worker节点复制swarm.key ,然后
1 2 3 ipfs bootstrap rm all ipfs bootstrap add /ip4/192.168.33.10/tcp/4001/ipfs/12D3KooWNvmkBW5noeQLzEpSpktYSZjq69727Z9e9F1sEJtuwJEb
两台服务器都把ipfs作为服务
1 2 cd /lib/systemd/system vi ipfs.service
1 2 3 4 5 6 7 8 9 [Unit] Description=IPFS [Service] ExecStart=/usr/local/bin/ipfs daemon Restart=always User=root Group=root [Install] WantedBy=multi-user.target
启用服务:
systemctl enable ipfs.service
systemctl start ipfs.service
systemctl status ipfs.service
测试
1 2 3 4 5 6 7 8 9 10 11 echo "from whx "> whx.txt # ipfs add whx.txt added QmYkeyhAYTaWdizfFEno2EtBTRdjazcSwYnhTWt4q7L5zU whx.txt # ipfs cat /ipfs/QmYkeyhAYTaWdizfFEno2EtBTRdjazcSwYnhTWt4q7L5zU from whx # echo "from test3">test.txt # ipfs add test.txt added QmXazQTUuAoiXEDqoCd4eF9g5okoMHWE2KMtAviS3dQ7h2 test.txt
添加一个目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 test ├── index.html ├── sub │ └── sub.html └── test.html added QmQV4SEvUf8UhLmf7bsjx97jtY4Pw1XBk2be2GUdyxRTMx test /index.html added QmRxqKo5fUnpNPzvWcPnBZfkV9533bqZLBzRWFeWtkjbME test /sub/sub.html added QmbNyHGiz83bNbUnyNjXTjr1pm8AhD5XHyhQfS6iLiwBT1 test /test.html added QmZicCMMw7xfcbMHbZ1hNJ1n9om1jDFywdjQFXEkGwGQMW test /sub added QmWBEkwUxHL81GScHYcKdMio7PBzJDNRHZxUpc8uRFdyQT test QmQV4SEvUf8UhLmf7bsjx97jtY4Pw1XBk2be2GUdyxRTMx 20 index.html QmZicCMMw7xfcbMHbZ1hNJ1n9om1jDFywdjQFXEkGwGQMW - sub/ QmbNyHGiz83bNbUnyNjXTjr1pm8AhD5XHyhQfS6iLiwBT1 20 test.html added QmQV4SEvUf8UhLmf7bsjx97jtY4Pw1XBk2be2GUdyxRTMx test /index.html added QmRxqKo5fUnpNPzvWcPnBZfkV9533bqZLBzRWFeWtkjbME test /sub/sub.html added QmbxCEbzgQZmy38pew5Wy6cbfiefW8z3vGbepZuZzcgchP test /sub/test1.txt added QmbNyHGiz83bNbUnyNjXTjr1pm8AhD5XHyhQfS6iLiwBT1 test /test.html added QmWR5PNWprgGfnYS9t3MUZGV1YtYifWkebgrh7UB6Zi3xD test /sub added QmeoLK9ZsQK9zroLjb7HNxhyqE37ieL66uFnLRJoWD4bxL test QmQV4SEvUf8UhLmf7bsjx97jtY4Pw1XBk2be2GUdyxRTMx 20 index.html QmWR5PNWprgGfnYS9t3MUZGV1YtYifWkebgrh7UB6Zi3xD - sub/ QmbNyHGiz83bNbUnyNjXTjr1pm8AhD5XHyhQfS6iLiwBT1 20 test.html
注意到两次的不同
1 2 3 4 5 6 7 8 9 10 QmZicCMMw7xfcbMHbZ1hNJ1n9om1jDFywdjQFXEkGwGQMW - sub/ QmWR5PNWprgGfnYS9t3MUZGV1YtYifWkebgrh7UB6Zi3xD - sub/ QmRxqKo5fUnpNPzvWcPnBZfkV9533bqZLBzRWFeWtkjbME 21 sub.html QmRxqKo5fUnpNPzvWcPnBZfkV9533bqZLBzRWFeWtkjbME 21 sub.html QmbxCEbzgQZmy38pew5Wy6cbfiefW8z3vGbepZuZzcgchP 10 test1.txt
绑定节点名 publish 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Published to k51qzi5uqu5dk53t2hi2f8dhupwl2kkxypggp4dd7svxkxirwg411zfthqn08e: /ipfs/QmeoLK9ZsQK9zroLjb7HNxhyqE37ieL66uFnLRJoWD4bxL index.html from test added QmNZt9aFJHtggpXupw2VjsvZ1RksV7AfjhLgBSSN5q9a51 test /index.html added QmRxqKo5fUnpNPzvWcPnBZfkV9533bqZLBzRWFeWtkjbME test /sub/sub.html added QmbxCEbzgQZmy38pew5Wy6cbfiefW8z3vGbepZuZzcgchP test /sub/test1.txt added QmbNyHGiz83bNbUnyNjXTjr1pm8AhD5XHyhQfS6iLiwBT1 test /test.html added QmWR5PNWprgGfnYS9t3MUZGV1YtYifWkebgrh7UB6Zi3xD test /sub added QmbwhfHfarbU3X9wa8ovtK7Q4fGkd8R3B8biH4kUD7CmuU test Published to k51qzi5uqu5dk53t2hi2f8dhupwl2kkxypggp4dd7svxkxirwg411zfthqn08e: /ipfs/QmbwhfHfarbU3X9wa8ovtK7Q4fGkd8R3B8biH4kUD7CmuU index.html version new
节点直接不能互相发现,这是因为swarm announce的ip 不固定,多网卡的情况下回在虚拟机里绑定到docker网卡,修改方法是修改.ipfs/config
1 2 3 4 5 6 7 { "Addresses" : { "Announce" : [ "/ip4/1.2.3.4/tcp/4001" , ] } }
下载webui
https://github.com/ipfs-shipyard/ipfs-webui/releases/download/v2.11.1/ipfs-webui.tar.gz
1 2 3 4 added QmaFK9e6DjMuqnsi9hDJoJb6E1iFxJaHHWiKUoUpVjeg7h build/static added QmZtzPm6EgQToncp6RuHdaTtyPpWQy2gvTrXuMsYQxHV5k build
1 2 3 4 5 6 7 # ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://bafybeiflxftai7lnntipulvvmn3vfcs3ktig4kgzxollh276zqmchxz6cm.ipfs.localhost:8080", "http://localhost:3000", "http://127.0.0.1:5001", "https://webui.ipfs.io"]' # ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST"]' # curl -v http://localhost:8080/ipfs/QmZtzPm6EgQToncp6RuHdaTtyPpWQy2gvTrXuMsYQxHV5k < Location: http://bafybeiflxftai7lnntipulvvmn3vfcs3ktig4kgzxollh276zqmchxz6cm.ipfs.localhost:8080/ echo "127.0.0.1 bafybeiflxftai7lnntipulvvmn3vfcs3ktig4kgzxollh276zqmchxz6cm.ipfs.localhost">>/etc/hosts
访问如下地址
http://bafybeiflxftai7lnntipulvvmn3vfcs3ktig4kgzxollh276zqmchxz6cm.ipfs.localhost:8080/#/welcome
ipfs key
1 2 k51qzi5uqu5dk53t2hi2f8dhupwl2kkxypggp4dd7svxkxirwg411zfthqn08e self
云端服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # ipfs id { "ID": "12D3KooWDiAhxybZwdsdHrnjogpbdnKuBPDSFn883cwyDpJb5hiZ", "PublicKey": "CAESIDnYTZXIdT2889xegXXVgnDQwTx9GuoY/FKXqOYBGU8q", "Addresses": [ "/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWDiAhxybZwdsdHrnjogpbdnKuBPDSFn883cwyDpJb5hiZ", "/ip4/192.168.1.16/tcp/4001/p2p/12D3KooWDiAhxybZwdsdHrnjogpbdnKuBPDSFn883cwyDpJb5hiZ", "/ip6/::1/tcp/4001/p2p/12D3KooWDiAhxybZwdsdHrnjogpbdnKuBPDSFn883cwyDpJb5hiZ" ], "AgentVersion": "go-ipfs/0.7.0/", "ProtocolVersion": "ipfs/0.1.0", "Protocols": [ "/ipfs/bitswap", "/ipfs/bitswap/1.0.0", "/ipfs/bitswap/1.1.0", "/ipfs/bitswap/1.2.0", "/ipfs/id/1.0.0", "/ipfs/id/push/1.0.0", "/ipfs/lan/kad/1.0.0", "/ipfs/ping/1.0.0", "/libp2p/autonat/1.0.0", "/libp2p/circuit/relay/0.1.0", "/p2p/id/delta/1.0.0", "/x/" ] }
在服务器安装节点
1 2 3 114.115.210.207 114.115.212.160 119.3.165.66
然后再所有内网节点加入前两个节点,不加入第三个节点
1 2 3 4 ipfs bootstrap add /ip4/114.115.210.207/tcp/4001/ipfs/12D3KooWDiAhxybZwdsdHrnjogpbdnKuBPDSFn883cwyDpJb5hiZ ipfs bootstrap add /ip4/114.115.212.160/tcp/4001/ipfs/12D3KooWART3DbX2qT93YjPzBxyEVLqaFSoH9rm6Wt9ZZE2VwErh
然后在内网节点添加文件,不在两台bootstrap中查看,在第三台上不能看到文件。
在两台bootstrap中的任意一台查看过文件后,可以在第三台上看到。
结论:内网的文件需要在外网服务器上pin后,才能在别的节点查看。
todo:在一台nat后的服务器上添加文件,研究其它节点的情况。
研究nodejs版本的各个例子
browser-http-client-upload-file:
启动后点击查看的时候会把http://localhost:8080/ipfs/Qmb3b88paN4AocnjyTNCpRqh2CsbskhvoLbE7o6BhHmD85
重定向到
http://Qmb3b88paN4AocnjyTNCpRqh2CsbskhvoLbE7o6BhHmD85.ipfs .localhost:8080/ipfs/
修改配置文件后不跳转了,
1 2 3 4 5 6 ipfs config --json Gateway.PublicGateways '{ "localhost:8080": { "UseSubdomains": false, "Paths": ["/ipfs", "/ipns", "/api"] } }'
浏览器直接访问ipfs api 的时候报access-control-allow 错,添加配置后重启ipfs即可
1 2 3 ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["*"]' ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "GET", "POST"]' ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials '["true"]'