-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
96 lines (78 loc) · 2.37 KB
/
main.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
libp2p "github.com/libp2p/go-libp2p"
crypto "github.com/libp2p/go-libp2p-crypto"
peerstore "github.com/libp2p/go-libp2p-peerstore"
multiaddr "github.com/multiformats/go-multiaddr"
)
func makeHost(config Config) *Node {
ctx := context.Background()
// libp2p.New constructs a new libp2p Host. Other options can be added
// here.
priv, _, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
host, err := libp2p.New(
ctx,
libp2p.ListenAddrs([]multiaddr.Multiaddr(config.ListenAddresses)...),
libp2p.Identity(priv),
libp2p.DisableRelay(),
)
if err != nil {
panic(err)
}
return NewNode(host)
}
func startMultilogue(config Config) {
client := makeHost(config)
clientPeer := &Peer{
peerID: client.ID(),
username: config.Username}
var chatUI *ChatUI
if config.HostMultiAddress != "" {
maddr, err := multiaddr.NewMultiaddr(config.HostMultiAddress)
if err != nil {
client.debugPrintln(err)
panic(err)
}
info, err := peerstore.InfoFromP2pAddr(maddr)
if err != nil {
client.debugPrintln(err)
panic(err)
}
client.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.PermanentAddrTTL)
client.JoinChannel(clientPeer, info.ID, config.ChannelName)
chatUI = NewChatUI(client, config.ChannelName, clientPeer, info.ID, config.HostMultiAddress)
} else {
host := makeHost(config)
hostID := host.ID()
host.Peerstore().AddAddrs(client.ID(), client.Addrs(), peerstore.PermanentAddrTTL)
client.Peerstore().AddAddrs(host.ID(), host.Addrs(), peerstore.PermanentAddrTTL)
var channelConfig *ChannelConfig
if config.ChannelConfigFile == "" {
channelConfig = DefaultChannelConfig()
} else {
ReadChannelConfigs(config.ChannelConfigFile, channelConfig)
}
host.CreateChannel(clientPeer, config.ChannelName, channelConfig)
client.JoinChannel(clientPeer, hostID, config.ChannelName)
hostIDStr := host.ID().Pretty()
addresses := ""
for _, element := range host.Addrs() {
// index is the index where we are
// element is the element from someSlice for where we are
addresses = addresses + element.String() + "/p2p/" + hostIDStr + " "
}
chatUI = NewChatUI(client, config.ChannelName, clientPeer, hostID, addresses)
}
chatUI.StartUI()
// there was a way to enter peer id lol
select {}
}
func main() {
// // Parse flags
config, err := ParseFlags()
if err != nil {
panic(err)
}
startMultilogue(config)
}