@@ -51,6 +51,7 @@ ipfs peers in the internet.
51
51
"disconnect" : swarmDisconnectCmd ,
52
52
"filters" : swarmFiltersCmd ,
53
53
"peers" : swarmPeersCmd ,
54
+ "peering" : swarmPeeringCmd ,
54
55
},
55
56
}
56
57
@@ -61,6 +62,149 @@ const (
61
62
swarmDirectionOptionName = "direction"
62
63
)
63
64
65
+ type peeringResult struct {
66
+ ID peer.ID
67
+ Status string
68
+ }
69
+
70
+ var swarmPeeringCmd = & cmds.Command {
71
+ Helptext : cmds.HelpText {
72
+ Tagline : "Modify the peering subsystem." ,
73
+ ShortDescription : `
74
+ 'ipfs swarm peering' manages the peering subsystem.
75
+ Peers in the peering subsystem is maintained to be connected, reconnected
76
+ on disconnect with a back-off.
77
+ The changes are not saved to the config.
78
+ ` ,
79
+ },
80
+ Subcommands : map [string ]* cmds.Command {
81
+ "add" : swarmPeeringAddCmd ,
82
+ "ls" : swarmPeeringLsCmd ,
83
+ "rm" : swarmPeeringRmCmd ,
84
+ },
85
+ }
86
+
87
+ var swarmPeeringAddCmd = & cmds.Command {
88
+ Helptext : cmds.HelpText {
89
+ Tagline : "Add peers into the peering subsystem." ,
90
+ ShortDescription : `
91
+ 'ipfs swarm peering add' will add the new address to the peering subsystem as one that should always be connected to.
92
+ ` ,
93
+ },
94
+ Arguments : []cmds.Argument {
95
+ cmds .StringArg ("address" , true , true , "address of peer to add into the peering subsystem" ),
96
+ },
97
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
98
+ addrs := make ([]ma.Multiaddr , len (req .Arguments ))
99
+
100
+ for i , arg := range req .Arguments {
101
+ addr , err := ma .NewMultiaddr (arg )
102
+ if err != nil {
103
+ return err
104
+ }
105
+
106
+ addrs [i ] = addr
107
+ }
108
+
109
+ addInfos , err := peer .AddrInfosFromP2pAddrs (addrs ... )
110
+ if err != nil {
111
+ return err
112
+ }
113
+
114
+ node , err := cmdenv .GetNode (env )
115
+ if err != nil {
116
+ return err
117
+ }
118
+
119
+ for _ , addrinfo := range addInfos {
120
+ node .Peering .AddPeer (addrinfo )
121
+ err = res .Emit (peeringResult {addrinfo .ID , "success" })
122
+ if err != nil {
123
+ return err
124
+ }
125
+ }
126
+ return nil
127
+ },
128
+ Encoders : cmds.EncoderMap {
129
+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , pr * peeringResult ) error {
130
+ fmt .Fprintf (w , "add %s %s\n " , pr .ID .String (), pr .Status )
131
+ return nil
132
+ }),
133
+ },
134
+ Type : peeringResult {},
135
+ }
136
+
137
+ var swarmPeeringLsCmd = & cmds.Command {
138
+ Helptext : cmds.HelpText {
139
+ Tagline : "List peers registered in the peering subsystem." ,
140
+ ShortDescription : `
141
+ 'ipfs swarm peering ls' lists the peers that are registered in the peering subsystem and to which the daemon is always connected.
142
+ ` ,
143
+ },
144
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
145
+ node , err := cmdenv .GetNode (env )
146
+ if err != nil {
147
+ return err
148
+ }
149
+ peers := node .Peering .ListPeers ()
150
+ return cmds .EmitOnce (res , addrInfos {Peers : peers })
151
+ },
152
+ Type : addrInfos {},
153
+ Encoders : cmds.EncoderMap {
154
+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , ai * addrInfos ) error {
155
+ for _ , info := range ai .Peers {
156
+ fmt .Fprintf (w , "%s\n " , info .ID )
157
+ for _ , addr := range info .Addrs {
158
+ fmt .Fprintf (w , "\t %s\n " , addr )
159
+ }
160
+ }
161
+ return nil
162
+ }),
163
+ },
164
+ }
165
+
166
+ type addrInfos struct {
167
+ Peers []peer.AddrInfo
168
+ }
169
+
170
+ var swarmPeeringRmCmd = & cmds.Command {
171
+ Helptext : cmds.HelpText {
172
+ Tagline : "Remove a peer from the peering subsystem." ,
173
+ ShortDescription : `
174
+ 'ipfs swarm peering rm' will remove the given ID from the peering subsystem and remove it from the always-on connection.
175
+ ` ,
176
+ },
177
+ Arguments : []cmds.Argument {
178
+ cmds .StringArg ("ID" , true , true , "ID of peer to remove from the peering subsystem" ),
179
+ },
180
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
181
+ node , err := cmdenv .GetNode (env )
182
+ if err != nil {
183
+ return err
184
+ }
185
+
186
+ for _ , arg := range req .Arguments {
187
+ id , err := peer .Decode (arg )
188
+ if err != nil {
189
+ return err
190
+ }
191
+
192
+ node .Peering .RemovePeer (id )
193
+ if err = res .Emit (peeringResult {id , "success" }); err != nil {
194
+ return err
195
+ }
196
+ }
197
+ return nil
198
+ },
199
+ Type : peeringResult {},
200
+ Encoders : cmds.EncoderMap {
201
+ cmds .Text : cmds .MakeTypedEncoder (func (req * cmds.Request , w io.Writer , pr * peeringResult ) error {
202
+ fmt .Fprintf (w , "add %s %s\n " , pr .ID .String (), pr .Status )
203
+ return nil
204
+ }),
205
+ },
206
+ }
207
+
64
208
var swarmPeersCmd = & cmds.Command {
65
209
Helptext : cmds.HelpText {
66
210
Tagline : "List peers with open connections." ,
0 commit comments