@cryptotaxi247 / kubo / commits / b83ea099f

go-ipfs-config: change randomports Description

tarekbadr committed Nov 3, 2018 at 15:41 UTC b83ea099ff15ff9952d0f078302025838bfa926d
1 file changed +30 -1
config/profile.go
+30 -1
@@ -1,6 +1,10 @@
1 package config
2
3 -import "time"
3 +import (
4 + "fmt"
5 + "net"
6 + "time"
7 +)
8
9 // Transformer is a function which takes configuration and applies some filter to it
10 type Transformer func(c *Config) error
@@ -159,6 +163,31 @@ fetching may be degraded.
163 return nil
164 },
165 },
166 + "randomports": {
167 + Description: `Use a random port number for swarm.`,
168 +
169 + Transform: func(c *Config) error {
170 + port, err := getAvailablePort()
171 + if err != nil {
172 + return err
173 + }
174 + c.Addresses.Swarm = []string{
175 + fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port),
176 + fmt.Sprintf("/ip6/::/tcp/%d", port),
177 + }
178 + return nil
179 + },
180 + },
181 +}
182 +
183 +func getAvailablePort() (port int, err error) {
184 + ln, err := net.Listen("tcp", "[::]:0")
185 + if err != nil {
186 + return 0, err
187 + }
188 + defer ln.Close()
189 + port = ln.Addr().(*net.TCPAddr).Port
190 + return port, nil
191 }
192
193 func appendSingle(a []string, b []string) []string {