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

randomports: give user ability to init ipfs using random port for swarm. #17

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -159,6 +163,31 @@ fetching may be degraded.
return nil
},
},
"randomports": {
Description: `Use a random port number for swarm.`,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @djdv
as you can see here I updated the discretion as you said.

again sorry be late and please feel free to ask about more update.


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