@@ -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,129 @@ const (
61
62
swarmDirectionOptionName = "direction"
62
63
)
63
64
65
+ var swarmPeeringCmd = & cmds.Command {
66
+ Helptext : cmds.HelpText {
67
+ Tagline : "modify the peering service." ,
68
+ ShortDescription : `
69
+ 'ipfs swarm peering' is a tool to manupulate the peering service.
70
+ Peers in the peering service is maintained to be connected, reconnected
71
+ on disconnect with a back-off.
72
+ ` ,
73
+ },
74
+ Subcommands : map [string ]* cmds.Command {
75
+ "add" : swarmPeeringAddCmd ,
76
+ "ls" : swarmPeeringLsCmd ,
77
+ "rm" : swarmPeeringRmCmd ,
78
+ },
79
+ }
80
+
81
+ var swarmPeeringAddCmd = & cmds.Command {
82
+ Helptext : cmds.HelpText {
83
+ Tagline : "add peers into the peering service." ,
84
+ ShortDescription : `
85
+ 'ipfs swarm peering add' adds peers into the peering service.
86
+ ` ,
87
+ },
88
+ Arguments : []cmds.Argument {
89
+ cmds .StringArg ("address" , true , true , "address of peer to add into the PeeringService" ),
90
+ },
91
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
92
+ addrs := make ([]ma.Multiaddr , len (req .Arguments ))
93
+
94
+ for i , arg := range req .Arguments {
95
+ addr , err := ma .NewMultiaddr (arg )
96
+ if err != nil {
97
+ return err
98
+ }
99
+
100
+ addrs [i ] = addr
101
+ }
102
+
103
+ addInfos , err := peer .AddrInfosFromP2pAddrs (addrs ... )
104
+ if err != nil {
105
+ return err
106
+ }
107
+
108
+ node , err := cmdenv .GetNode (env )
109
+ if err != nil {
110
+ return err
111
+ }
112
+
113
+ output := make ([]string , len (addInfos ))
114
+ for i , id := range addInfos {
115
+ output [i ] = "add " + id .ID .Pretty ()
116
+ node .Peering .AddPeer (id )
117
+
118
+ output [i ] += " success"
119
+ }
120
+
121
+ return cmds .EmitOnce (res , & stringList {output })
122
+ },
123
+ Encoders : cmds.EncoderMap {
124
+ cmds .Text : cmds .MakeTypedEncoder (stringListEncoder ),
125
+ },
126
+ Type : stringList {},
127
+ }
128
+
129
+ var swarmPeeringLsCmd = & cmds.Command {
130
+ Helptext : cmds.HelpText {
131
+ Tagline : "list peers registered in the peering service." ,
132
+ ShortDescription : `
133
+ 'ipfs swarm peering ls' lists peers registered in the peering service.` ,
134
+ },
135
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
136
+ node , err := cmdenv .GetNode (env )
137
+ if err != nil {
138
+ return err
139
+ }
140
+ peers := node .Peering .ListPeer ()
141
+ return cmds .EmitOnce (res , addrInfos {Peers : peers })
142
+ },
143
+ Type : addrInfos {},
144
+ }
145
+
146
+ type addrInfos struct {
147
+ Peers []peer.AddrInfo
148
+ }
149
+
150
+ var swarmPeeringRmCmd = & cmds.Command {
151
+ Helptext : cmds.HelpText {
152
+ Tagline : "remove a peer from the peering service." ,
153
+ ShortDescription : `
154
+ 'ipfs swarm peering rm' removes peers from the peering service.
155
+ ` ,
156
+ },
157
+ Arguments : []cmds.Argument {
158
+ cmds .StringArg ("ID" , true , true , "ID of peer to remove from PeeringService" ),
159
+ },
160
+ Run : func (req * cmds.Request , res cmds.ResponseEmitter , env cmds.Environment ) error {
161
+ node , err := cmdenv .GetNode (env )
162
+ if err != nil {
163
+ return err
164
+ }
165
+
166
+ output := make ([]string , 0 , len (req .Arguments ))
167
+ for _ , arg := range req .Arguments {
168
+ id , err := peer .Decode (arg )
169
+ if err != nil {
170
+ return err
171
+ }
172
+
173
+ msg := "remove " + id .Pretty ()
174
+
175
+ node .Peering .RemovePeer (id )
176
+
177
+ msg += " success"
178
+ output = append (output , msg )
179
+ }
180
+ return cmds .EmitOnce (res , & stringList {output })
181
+ },
182
+ Type : stringList {},
183
+ Encoders : cmds.EncoderMap {
184
+ cmds .Text : cmds .MakeTypedEncoder (stringListEncoder ),
185
+ },
186
+ }
187
+
64
188
var swarmPeersCmd = & cmds.Command {
65
189
Helptext : cmds.HelpText {
66
190
Tagline : "List peers with open connections." ,
0 commit comments