-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
757 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,5 @@ | ||
package host | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// @TODO: add custom mobile option here | ||
type MobileConfig struct { | ||
} | ||
|
||
func FromMap(v map[string]interface{}) (*Config, error) { | ||
buf := new(bytes.Buffer) | ||
if err := json.NewEncoder(buf).Encode(v); err != nil { | ||
return nil, err | ||
} | ||
var conf MobileConfig | ||
if err := json.NewDecoder(buf).Decode(&conf); err != nil { | ||
return nil, fmt.Errorf("failure to decode config: %s", err) | ||
} | ||
return &conf, nil | ||
} | ||
|
||
func ToMap(conf *MobileConfig) (map[string]interface{}, error) { | ||
buf := new(bytes.Buffer) | ||
if err := json.NewEncoder(buf).Encode(conf); err != nil { | ||
return nil, err | ||
} | ||
var m map[string]interface{} | ||
if err := json.NewDecoder(buf).Decode(&m); err != nil { | ||
return nil, fmt.Errorf("failure to decode config: %s", err) | ||
} | ||
return m, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
package mobile | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"time" | ||
|
||
ipfs_config "github.com/ipfs/go-ipfs-config" | ||
libp2p_ci "github.com/libp2p/go-libp2p-core/crypto" | ||
libp2p_peer "github.com/libp2p/go-libp2p-core/peer" | ||
) | ||
|
||
func Init(out io.Writer, nBitsForKeypair int) (*ipfs_config.Config, error) { | ||
identity, err := identityConfig(out, nBitsForKeypair) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bootstrapPeers, err := ipfs_config.DefaultBootstrapPeers() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
datastore := ipfs_config.DefaultDatastoreConfig() | ||
|
||
conf := &ipfs_config.Config{ | ||
API: ipfs_config.API{ | ||
HTTPHeaders: map[string][]string{ | ||
"Access-Control-Allow-Credentials": []string{"true"}, | ||
"Access-Control-Allow-Origin": []string{"*"}, | ||
"Access-Control-Allow-Methods": []string{"GET", "PUT", "POST"}, | ||
}, | ||
}, | ||
|
||
// setup the node's default addresses. | ||
// NOTE: two swarm listen addrs, one tcp, one utp. | ||
Addresses: addressesConfig(), | ||
|
||
Datastore: datastore, | ||
Bootstrap: ipfs_config.BootstrapPeerStrings(bootstrapPeers), | ||
Identity: identity, | ||
Discovery: ipfs_config.Discovery{ | ||
MDNS: ipfs_config.MDNS{ | ||
Enabled: true, | ||
Interval: 10, | ||
}, | ||
}, | ||
|
||
Routing: ipfs_config.Routing{ | ||
Type: "dhtclient", | ||
}, | ||
|
||
// setup the node mount points. | ||
Mounts: ipfs_config.Mounts{ | ||
IPFS: "/ipfs", | ||
IPNS: "/ipns", | ||
}, | ||
|
||
Ipns: ipfs_config.Ipns{ | ||
ResolveCacheSize: 128, | ||
}, | ||
|
||
Gateway: ipfs_config.Gateway{ | ||
RootRedirect: "", | ||
Writable: false, | ||
NoFetch: false, | ||
PathPrefixes: []string{}, | ||
HTTPHeaders: map[string][]string{ | ||
"Access-Control-Allow-Origin": []string{"*"}, | ||
"Access-Control-Allow-Methods": []string{"GET"}, | ||
"Access-Control-Allow-Headers": []string{"X-Requested-With", "Range", "User-Agent"}, | ||
}, | ||
APICommands: []string{}, | ||
}, | ||
Reprovider: ipfs_config.Reprovider{ | ||
Interval: "12h", | ||
Strategy: "all", | ||
}, | ||
Swarm: ipfs_config.SwarmConfig{ | ||
ConnMgr: ipfs_config.ConnMgr{ | ||
LowWater: DefaultConnMgrLowWater, | ||
HighWater: DefaultConnMgrHighWater, | ||
GracePeriod: DefaultConnMgrGracePeriod.String(), | ||
Type: "basic", | ||
}, | ||
}, | ||
Experimental: ipfs_config.Experiments{ | ||
FilestoreEnabled: false, | ||
Libp2pStreamMounting: false, | ||
P2pHttpProxy: false, | ||
QUIC: true, | ||
ShardingEnabled: false, | ||
UrlstoreEnabled: false, | ||
PreferTLS: true, | ||
}, | ||
} | ||
|
||
return conf, nil | ||
} | ||
|
||
// DefaultConnMgrHighWater is the default value for the connection managers | ||
// 'high water' mark | ||
const DefaultConnMgrHighWater = 200 | ||
|
||
// DefaultConnMgrLowWater is the default value for the connection managers 'low | ||
// water' mark | ||
const DefaultConnMgrLowWater = 100 | ||
|
||
// DefaultConnMgrGracePeriod is the default value for the connection managers | ||
// grace period | ||
const DefaultConnMgrGracePeriod = time.Second * 20 | ||
|
||
func addressesConfig() ipfs_config.Addresses { | ||
return ipfs_config.Addresses{ | ||
Swarm: []string{ | ||
"/ip4/0.0.0.0/tcp/0", | ||
"/ip6/::/tcp/0", | ||
|
||
"/ip4/0.0.0.0/udp/0/quic", | ||
"/ip6/::/udp/0/quic", | ||
}, | ||
Announce: []string{}, | ||
NoAnnounce: []string{}, | ||
API: ipfs_config.Strings{"/ip4/127.0.0.1/tcp/5001"}, | ||
Gateway: ipfs_config.Strings{"/ip4/127.0.0.1/tcp/8080"}, | ||
} | ||
} | ||
|
||
// DefaultDatastoreConfig is an internal function exported to aid in testing. | ||
func DefaultDatastoreConfig() ipfs_config.Datastore { | ||
return ipfs_config.Datastore{ | ||
StorageMax: "10GB", | ||
StorageGCWatermark: 90, // 90% | ||
GCPeriod: "1h", | ||
BloomFilterSize: 0, | ||
Spec: map[string]interface{}{ | ||
"type": "mount", | ||
"mounts": []interface{}{ | ||
map[string]interface{}{ | ||
"mountpoint": "/blocks", | ||
"type": "measure", | ||
"prefix": "flatfs.datastore", | ||
"child": map[string]interface{}{ | ||
"type": "flatfs", | ||
"path": "blocks", | ||
"sync": true, | ||
"shardFunc": "/repo/flatfs/shard/v1/next-to-last/2", | ||
}, | ||
}, | ||
map[string]interface{}{ | ||
"mountpoint": "/", | ||
"type": "measure", | ||
"prefix": "leveldb.datastore", | ||
"child": map[string]interface{}{ | ||
"type": "levelds", | ||
"path": "datastore", | ||
"compression": "none", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// identityConfig initializes a new identity. | ||
func identityConfig(out io.Writer, nbits int) (ipfs_config.Identity, error) { | ||
// TODO guard higher up | ||
ident := ipfs_config.Identity{} | ||
if nbits < 1024 { | ||
return ident, errors.New("bitsize less than 1024 is considered unsafe") | ||
} | ||
|
||
fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits) | ||
sk, pk, err := libp2p_ci.GenerateKeyPair(libp2p_ci.RSA, nbits) | ||
if err != nil { | ||
return ident, err | ||
} | ||
fmt.Fprintf(out, "done\n") | ||
|
||
// 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 := libp2p_peer.IDFromPublicKey(pk) | ||
if err != nil { | ||
return ident, err | ||
} | ||
ident.PeerID = id.Pretty() | ||
fmt.Fprintf(out, "libp2p_peer identity: %s\n", ident.PeerID) | ||
return ident, nil | ||
} |
Oops, something went wrong.