| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | ) |
| 6 | |
| 7 | // AutoNATServiceMode configures the ipfs node's AutoNAT service. |
| 8 | type AutoNATServiceMode int |
| 9 | |
| 10 | const ( |
| 11 | // AutoNATServiceUnset indicates that the user has not set the |
| 12 | // AutoNATService mode. |
| 13 | // |
| 14 | // When unset, nodes configured to be public DHT nodes will _also_ |
| 15 | // perform limited AutoNAT dialbacks. |
| 16 | AutoNATServiceUnset AutoNATServiceMode = iota |
| 17 | // AutoNATServiceEnabled indicates that the user has enabled the |
| 18 | // AutoNATService. |
| 19 | AutoNATServiceEnabled |
| 20 | // AutoNATServiceDisabled indicates that the user has disabled the |
| 21 | // AutoNATService. |
| 22 | AutoNATServiceDisabled |
| 23 | // AutoNATServiceEnabledV1Only forces use of V1 and disables V2 |
| 24 | // (used for testing) |
| 25 | AutoNATServiceEnabledV1Only |
| 26 | ) |
| 27 | |
| 28 | func (m *AutoNATServiceMode) UnmarshalText(text []byte) error { |
| 29 | switch string(text) { |
| 30 | case "": |
| 31 | *m = AutoNATServiceUnset |
| 32 | case "enabled": |
| 33 | *m = AutoNATServiceEnabled |
| 34 | case "disabled": |
| 35 | *m = AutoNATServiceDisabled |
| 36 | case "legacy-v1": |
| 37 | *m = AutoNATServiceEnabledV1Only |
| 38 | default: |
| 39 | return fmt.Errorf("unknown autonat mode: %s", string(text)) |
| 40 | } |
| 41 | return nil |
| 42 | } |
| 43 | |
| 44 | func (m AutoNATServiceMode) MarshalText() ([]byte, error) { |
| 45 | switch m { |
| 46 | case AutoNATServiceUnset: |
| 47 | return nil, nil |
| 48 | case AutoNATServiceEnabled: |
| 49 | return []byte("enabled"), nil |
| 50 | case AutoNATServiceDisabled: |
| 51 | return []byte("disabled"), nil |
| 52 | case AutoNATServiceEnabledV1Only: |
| 53 | return []byte("legacy-v1"), nil |
| 54 | default: |
| 55 | return nil, fmt.Errorf("unknown autonat mode: %d", m) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // AutoNATConfig configures the node's AutoNAT subsystem. |
| 60 | type AutoNATConfig struct { |
| 61 | // ServiceMode configures the node's AutoNAT service mode. |
| 62 | ServiceMode AutoNATServiceMode `json:",omitempty"` |
| 63 | |
| 64 | // Throttle configures AutoNAT dialback throttling. |
| 65 | // |
| 66 | // If unset, the conservative libp2p defaults will be unset. To help the |
| 67 | // network, please consider setting this and increasing the limits. |
| 68 | // |
| 69 | // By default, the limits will be a total of 30 dialbacks, with a |
| 70 | // per-peer max of 3 peer, resetting every minute. |
| 71 | Throttle *AutoNATThrottleConfig `json:",omitempty"` |
| 72 | } |
| 73 | |
| 74 | // AutoNATThrottleConfig configures the throttle limites. |
| 75 | type AutoNATThrottleConfig struct { |
| 76 | // GlobalLimit and PeerLimit sets the global and per-peer dialback |
| 77 | // limits. The AutoNAT service will only perform the specified number of |
| 78 | // dialbacks per interval. |
| 79 | // |
| 80 | // Setting either to 0 will disable the appropriate limit. |
| 81 | GlobalLimit, PeerLimit int |
| 82 | |
| 83 | // Interval specifies how frequently this node should reset the |
| 84 | // global/peer dialback limits. |
| 85 | // |
| 86 | // When unset, this defaults to 1 minute. |
| 87 | Interval OptionalDuration |
| 88 | } |