Skip to content

Commit

Permalink
Merge pull request ipfs#102 from ipfs/feat/transports
Browse files Browse the repository at this point in the history
feat: add a transports section for enabling/disabling transports
  • Loading branch information
Stebalien committed Jun 16, 2020
2 parents d78334d + 520c711 commit 88d5a72
Show file tree
Hide file tree
Showing 4 changed files with 370 additions and 13 deletions.
7 changes: 0 additions & 7 deletions config/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,4 @@ type Experiments struct {
Libp2pStreamMounting bool
P2pHttpProxy bool
StrategicProviding bool

// OverrideSecurityTransports overrides the set of available security
// transports when non-empty. This option should eventually migrate some
// place more stable.
//
// Default: ["tls", "secio", "noise"].
OverrideSecurityTransports []string `json:",omitempty"`
}
68 changes: 62 additions & 6 deletions config/swarm.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,75 @@
package config

type SwarmConfig struct {
AddrFilters []string
// AddrFilters specifies a set libp2p addresses that we should never
// dial or receive connections from.
AddrFilters []string

// DisableBandwidthMetrics disables recording of bandwidth metrics for a
// slight reduction in memory usage. You probably don't need to set this
// flag.
DisableBandwidthMetrics bool
DisableNatPortMap bool
DisableRelay bool
EnableRelayHop bool

// autorelay functionality
// if true, then the libp2p host will be constructed with autorelay functionality.
// DisableNatPortMap turns off NAT port mapping (UPnP, etc.).
DisableNatPortMap bool

// DisableRelay explicitly disables the relay transport.
//
// Deprecated: This flag is deprecated and is overridden by
// `Transports.Relay` if specified.
DisableRelay bool `json:",omitempty"`

// EnableRelayHop makes this node act as a public relay, relaying
// traffic between other nodes.
EnableRelayHop bool

// EnableAutoRelay enables the "auto relay" feature.
//
// When both EnableAutoRelay and EnableRelayHop are set, this go-ipfs node
// will advertise itself as a public relay. Otherwise it will find and use
// advertised public relays when it determines that it's not reachable
// from the public internet.
EnableAutoRelay bool

// Transports contains flags to enable/disable libp2p transports.
Transports Transports

// ConnMgr configures the connection manager.
ConnMgr ConnMgr
}

type Transports struct {
// Network specifies the base transports we'll use for dialing. To
// listen on a transport, add the transport to your Addresses.Swarm.
Network struct {
// All default to on.
QUIC Flag `json:",omitempty"`
TCP Flag `json:",omitempty"`
Websocket Flag `json:",omitempty"`
Relay Flag `json:",omitempty"`
}

// Security specifies the transports used to encrypt insecure network
// transports.
Security struct {
// Defaults to 100.
TLS Priority `json:",omitempty"`
// Defaults to 200.
SECIO Priority `json:",omitempty"`
// Defaults to 300.
Noise Priority `json:",omitempty"`
}

// Multiplexers specifies the transports used to multiplex multiple
// connections over a single duplex connection.
Multiplexers struct {
// Defaults to 100.
Yamux Priority `json:",omitempty"`
// Defaults to 200.
Mplex Priority `json:",omitempty"`
}
}

// ConnMgr defines configuration options for the libp2p connection manager
type ConnMgr struct {
Type string
Expand Down
173 changes: 173 additions & 0 deletions config/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package config

import (
"encoding"
"encoding/json"
"fmt"
"time"
)

Expand Down Expand Up @@ -41,6 +43,174 @@ func (o Strings) MarshalJSON() ([]byte, error) {
var _ json.Unmarshaler = (*Strings)(nil)
var _ json.Marshaler = (*Strings)(nil)

// Flag represents a ternary value: false (-1), default (0), or true (+1).
//
// When encoded in json, False is "false", Default is "null" (or empty), and True
// is "true".
type Flag int8

const (
False Flag = -1
Default Flag = 0
True Flag = 1
)

// WithDefault resolves the value of the flag given the provided default value.
//
// Panics if Flag is an invalid value.
func (f Flag) WithDefault(defaultValue bool) bool {
switch f {
case False:
return false
case Default:
return defaultValue
case True:
return true
default:
panic(fmt.Sprintf("invalid flag value %d", f))
}
}

func (f Flag) MarshalJSON() ([]byte, error) {
switch f {
case Default:
return json.Marshal(nil)
case True:
return json.Marshal(true)
case False:
return json.Marshal(false)
default:
return nil, fmt.Errorf("invalid flag value: %d", f)
}
}

func (f *Flag) UnmarshalJSON(input []byte) error {
switch string(input) {
case "null":
*f = Default
case "false":
*f = False
case "true":
*f = True
default:
return fmt.Errorf("failed to unmarshal %q into a flag: must be null/undefined, true, or false", string(input))
}
return nil
}

func (f Flag) String() string {
switch f {
case Default:
return "default"
case True:
return "true"
case False:
return "false"
default:
return fmt.Sprintf("<invalid flag value %d>", f)
}
}

var _ json.Unmarshaler = (*Flag)(nil)
var _ json.Marshaler = (*Flag)(nil)

// Priority represents a value with a priority where 0 means "default" and -1
// means "disabled".
//
// When encoded in json, Default is encoded as "null" and Disabled is encoded as
// "false".
type Priority int64

const (
DefaultPriority Priority = 0
Disabled Priority = -1
)

// WithDefault resolves the priority with the given default.
//
// If defaultPriority is Default/0, this function will return 0.
//
// Panics if the priority has an invalid value (e.g., not DefaultPriority,
// Disabled, or > 0).
func (p Priority) WithDefault(defaultPriority Priority) (priority int64, enabled bool) {
switch p {
case Disabled:
return 0, false
case DefaultPriority:
switch defaultPriority {
case Disabled:
return 0, false
case DefaultPriority:
return 0, true
default:
if defaultPriority <= 0 {
panic(fmt.Sprintf("invalid priority %d < 0", int64(defaultPriority)))
}
return int64(defaultPriority), true
}
default:
if p <= 0 {
panic(fmt.Sprintf("invalid priority %d < 0", int64(p)))
}
return int64(p), true
}
}

func (p Priority) MarshalJSON() ([]byte, error) {
// > 0 == Priority
if p > 0 {
return json.Marshal(int64(p))
}
// <= 0 == special
switch p {
case DefaultPriority:
return json.Marshal(nil)
case Disabled:
return json.Marshal(false)
default:
return nil, fmt.Errorf("invalid priority value: %d", p)
}
}

func (p *Priority) UnmarshalJSON(input []byte) error {
switch string(input) {
case "null", "undefined":
*p = DefaultPriority
case "false":
*p = Disabled
case "true":
return fmt.Errorf("'true' is not a valid priority")
default:
var priority int64
err := json.Unmarshal(input, &priority)
if err != nil {
return err
}
if priority <= 0 {
return fmt.Errorf("priority must be positive: %d <= 0", priority)
}
*p = Priority(priority)
}
return nil
}

func (p Priority) String() string {
if p > 0 {
return fmt.Sprintf("%d", p)
}
switch p {
case DefaultPriority:
return "default"
case Disabled:
return "false"
default:
return fmt.Sprintf("<invalid priority %d>", p)
}
}

var _ json.Unmarshaler = (*Flag)(nil)
var _ json.Marshaler = (*Flag)(nil)

// Duration wraps time.Duration to provide json serialization and deserialization.
//
// NOTE: the zero value encodes to an empty string.
Expand All @@ -59,3 +229,6 @@ func (d Duration) MarshalText() ([]byte, error) {
func (d Duration) String() string {
return time.Duration(d).String()
}

var _ encoding.TextUnmarshaler = (*Duration)(nil)
var _ encoding.TextMarshaler = (*Duration)(nil)
Loading

0 comments on commit 88d5a72

Please sign in to comment.