Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #17 from tarekbadrshalaan/feat/profile/randomports
Browse files Browse the repository at this point in the history
randomports: give user ability to init ipfs using random port for swarm.
  • Loading branch information
Stebalien authored Dec 11, 2018
2 parents cf7c0c8 + e5e6814 commit 1382b4e
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion 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 1382b4e

Please sign in to comment.