| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package confignet |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"net" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
type TransportType string |
| 15 |
|
| 16 |
const ( |
| 17 |
TransportTypeTCP TransportType = "tcp" |
| 18 |
TransportTypeTCP4 TransportType = "tcp4" |
| 19 |
TransportTypeTCP6 TransportType = "tcp6" |
| 20 |
TransportTypeUDP TransportType = "udp" |
| 21 |
TransportTypeUDP4 TransportType = "udp4" |
| 22 |
TransportTypeUDP6 TransportType = "udp6" |
| 23 |
TransportTypeIP TransportType = "ip" |
| 24 |
TransportTypeIP4 TransportType = "ip4" |
| 25 |
TransportTypeIP6 TransportType = "ip6" |
| 26 |
TransportTypeUnix TransportType = "unix" |
| 27 |
TransportTypeUnixgram TransportType = "unixgram" |
| 28 |
TransportTypeUnixPacket TransportType = "unixpacket" |
| 29 |
transportTypeEmpty TransportType = "" |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func (tt *TransportType) UnmarshalText(in []byte) error { |
| 36 |
typ := TransportType(in) |
| 37 |
switch typ { |
| 38 |
case TransportTypeTCP, |
| 39 |
TransportTypeTCP4, |
| 40 |
TransportTypeTCP6, |
| 41 |
TransportTypeUDP, |
| 42 |
TransportTypeUDP4, |
| 43 |
TransportTypeUDP6, |
| 44 |
TransportTypeIP, |
| 45 |
TransportTypeIP4, |
| 46 |
TransportTypeIP6, |
| 47 |
TransportTypeUnix, |
| 48 |
TransportTypeUnixgram, |
| 49 |
TransportTypeUnixPacket, |
| 50 |
transportTypeEmpty: |
| 51 |
*tt = typ |
| 52 |
return nil |
| 53 |
default: |
| 54 |
return fmt.Errorf("unsupported transport type %q", typ) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
type DialerConfig struct { |
| 60 |
|
| 61 |
|
| 62 |
Timeout time.Duration `mapstructure:"timeout,omitempty"` |
| 63 |
|
| 64 |
_ struct{} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func NewDefaultDialerConfig() DialerConfig { |
| 69 |
return DialerConfig{} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
type AddrConfig struct { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
Endpoint string `mapstructure:"endpoint,omitempty"` |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
Transport TransportType `mapstructure:"transport,omitempty"` |
| 84 |
|
| 85 |
|
| 86 |
DialerConfig DialerConfig `mapstructure:"dialer,omitempty"` |
| 87 |
|
| 88 |
_ struct{} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func NewDefaultAddrConfig() AddrConfig { |
| 93 |
return AddrConfig{ |
| 94 |
DialerConfig: NewDefaultDialerConfig(), |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func (na *AddrConfig) Dial(ctx context.Context) (net.Conn, error) { |
| 100 |
d := net.Dialer{Timeout: na.DialerConfig.Timeout} |
| 101 |
return d.DialContext(ctx, string(na.Transport), na.Endpoint) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
func (na *AddrConfig) Listen(ctx context.Context) (net.Listener, error) { |
| 106 |
lc := net.ListenConfig{} |
| 107 |
return lc.Listen(ctx, string(na.Transport), na.Endpoint) |
| 108 |
} |
| 109 |
|
| 110 |
func (na *AddrConfig) Validate() error { |
| 111 |
switch na.Transport { |
| 112 |
case TransportTypeTCP, |
| 113 |
TransportTypeTCP4, |
| 114 |
TransportTypeTCP6, |
| 115 |
TransportTypeUDP, |
| 116 |
TransportTypeUDP4, |
| 117 |
TransportTypeUDP6, |
| 118 |
TransportTypeIP, |
| 119 |
TransportTypeIP4, |
| 120 |
TransportTypeIP6, |
| 121 |
TransportTypeUnix, |
| 122 |
TransportTypeUnixgram, |
| 123 |
TransportTypeUnixPacket: |
| 124 |
return nil |
| 125 |
default: |
| 126 |
return fmt.Errorf("invalid transport type %q", na.Transport) |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
type TCPAddrConfig struct { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
Endpoint string `mapstructure:"endpoint,omitempty"` |
| 138 |
|
| 139 |
|
| 140 |
DialerConfig DialerConfig `mapstructure:"dialer,omitempty"` |
| 141 |
|
| 142 |
_ struct{} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
func NewDefaultTCPAddrConfig() TCPAddrConfig { |
| 147 |
return TCPAddrConfig{ |
| 148 |
DialerConfig: NewDefaultDialerConfig(), |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
func (na *TCPAddrConfig) Dial(ctx context.Context) (net.Conn, error) { |
| 154 |
d := net.Dialer{Timeout: na.DialerConfig.Timeout} |
| 155 |
return d.DialContext(ctx, string(TransportTypeTCP), na.Endpoint) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (na *TCPAddrConfig) Listen(ctx context.Context) (net.Listener, error) { |
| 160 |
lc := net.ListenConfig{} |
| 161 |
return lc.Listen(ctx, string(TransportTypeTCP), na.Endpoint) |
| 162 |
} |