Skip to content

Commit 7e23c56

Browse files
committed
cli: add peers update_ip command
1 parent 0fea1a2 commit 7e23c56

4 files changed

Lines changed: 50 additions & 4 deletions

File tree

application_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func TestUpdatePeerSettingsIPAddr(t *testing.T) {
426426
AllowUsingAsExitNode: peer2Config.WeAllowUsingAsExitNode,
427427
})
428428
ts.Error(err)
429-
ts.ErrorContains(err, "IP "+tc.ip+" not found in")
429+
ts.ErrorContains(err, "IP "+tc.ip+" does not belong to subnet 10.66.0.0/24")
430430
})
431431
}
432432
})

cli/cli.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,31 @@ func (a *Application) init() {
270270
return changePeerDomain(a.api, c.String("pid"), c.String("domain"))
271271
},
272272
},
273+
{
274+
Name: "update_ip",
275+
Usage: "Change known peer IP address",
276+
Flags: []cli.Flag{
277+
&cli.StringFlag{
278+
Name: "pid",
279+
Usage: "peer id",
280+
Required: false,
281+
},
282+
&cli.StringFlag{
283+
Name: "name",
284+
Usage: "peer name",
285+
Required: false,
286+
},
287+
&cli.StringFlag{
288+
Name: "ip",
289+
Usage: "peer IP address",
290+
Required: true,
291+
},
292+
},
293+
Before: a.initApiAndPeerIdRequired,
294+
Action: func(c *cli.Context) error {
295+
return changePeerIP(a.api, c.String("pid"), c.String("ip"))
296+
},
297+
},
273298
{
274299
Name: "allow_exit_node",
275300
Usage: "Allow known peer to use this device as exit node (as socks5 proxy)",

cli/peers.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,27 @@ func changePeerDomain(api *apiclient.Client, peerID, newDomain string) error {
250250
return nil
251251
}
252252

253+
func changePeerIP(api *apiclient.Client, peerID, newIP string) error {
254+
pcfg, err := api.KnownPeerConfig(peerID)
255+
if err != nil {
256+
return err
257+
}
258+
259+
err = api.UpdatePeerSettings(entity.UpdatePeerSettingsRequest{
260+
PeerID: peerID,
261+
Alias: pcfg.Alias,
262+
DomainName: pcfg.DomainName,
263+
IPAddr: newIP,
264+
AllowUsingAsExitNode: pcfg.WeAllowUsingAsExitNode,
265+
})
266+
if err != nil {
267+
return err
268+
}
269+
270+
fmt.Println("peer IP address updated successfully")
271+
return nil
272+
}
273+
253274
func setAllowUsingAsExitNode(api *apiclient.Client, peerID string, allow bool) error {
254275
pcfg, err := api.KnownPeerConfig(peerID)
255276
if err != nil {

config/network_addr.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *Config) VPNLocalIPMaskUnlocked() (net.IP, net.IPMask) {
3232
// GenerateNextIpAddr is not thread safe.
3333
func (c *Config) GenerateNextIpAddr() string {
3434
localIP, netMask := c.VPNLocalIPMaskUnlocked()
35-
ipNet := net.IPNet{
35+
ipNet := &net.IPNet{
3636
IP: localIP.Mask(netMask),
3737
Mask: netMask,
3838
}
@@ -59,7 +59,7 @@ func (c *Config) GenerateNextIpAddr() string {
5959
// Checks IP for: valid ip, unique across peers, in vpn net mask
6060
func (c *Config) CheckIPUnique(checkIP string, exceptPeerID string) error {
6161
localIP, netMask := c.VPNLocalIPMaskUnlocked()
62-
ipNet := net.IPNet{
62+
ipNet := &net.IPNet{
6363
IP: localIP.Mask(netMask),
6464
Mask: netMask,
6565
}
@@ -74,7 +74,7 @@ func (c *Config) CheckIPUnique(checkIP string, exceptPeerID string) error {
7474

7575
contains := ipNet.Contains(ip)
7676
if !contains {
77-
return fmt.Errorf("IP %s not found in %s", checkIP, ipNet)
77+
return fmt.Errorf("IP %s does not belong to subnet %s", checkIP, ipNet)
7878
}
7979

8080
for _, peer := range c.KnownPeers {

0 commit comments

Comments
 (0)