Skip to content

Commit

Permalink
Merge pull request ipfs#17 from tarekbadrshalaan/feat/profile/randomp…
Browse files Browse the repository at this point in the history
…orts

randomports: give user ability to init ipfs using random port for swarm.
  • Loading branch information
Stebalien authored Dec 11, 2018
2 parents 0c7d4bf + 4c7c3ba commit 5e1211d
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion config/profile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "time"
import (
"fmt"
"net"
"time"
)

// Transformer is a function which takes configuration and applies some filter to it
type Transformer func(c *Config) error
Expand Down Expand Up @@ -160,6 +164,31 @@ fetching may be degraded.
return nil
},
},
"randomports": {
Description: `Use a random port number for swarm.`,

Transform: func(c *Config) error {
port, err := getAvailablePort()
if err != nil {
return err
}
c.Addresses.Swarm = []string{
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port),
fmt.Sprintf("/ip6/::/tcp/%d", port),
}
return nil
},
},
}

func getAvailablePort() (port int, err error) {
ln, err := net.Listen("tcp", "[::]:0")
if err != nil {
return 0, err
}
defer ln.Close()
port = ln.Addr().(*net.TCPAddr).Port
return port, nil
}

func appendSingle(a []string, b []string) []string {
Expand Down

0 comments on commit 5e1211d

Please sign in to comment.