p2p/nat: add MarshalText to instances
This commit is contained in:
parent
663de35163
commit
0fa76086eb
|
@ -133,8 +133,9 @@ func Map(m Interface, c <-chan struct{}, protocol string, extport, intport int,
|
||||||
// Mapping operations will not return an error but won't actually do anything.
|
// Mapping operations will not return an error but won't actually do anything.
|
||||||
type ExtIP net.IP
|
type ExtIP net.IP
|
||||||
|
|
||||||
func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil }
|
func (n ExtIP) ExternalIP() (net.IP, error) { return net.IP(n), nil }
|
||||||
func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) }
|
func (n ExtIP) String() string { return fmt.Sprintf("ExtIP(%v)", net.IP(n)) }
|
||||||
|
func (n ExtIP) MarshalText() ([]byte, error) { return []byte(fmt.Sprintf("extip:%v", net.IP(n))), nil }
|
||||||
|
|
||||||
// These do nothing.
|
// These do nothing.
|
||||||
|
|
||||||
|
@ -148,7 +149,7 @@ func (ExtIP) DeleteMapping(string, int, int) error { return nil }
|
||||||
func Any() Interface {
|
func Any() Interface {
|
||||||
// TODO: attempt to discover whether the local machine has an
|
// TODO: attempt to discover whether the local machine has an
|
||||||
// Internet-class address. Return ExtIP in this case.
|
// Internet-class address. Return ExtIP in this case.
|
||||||
return startautodisc("UPnP or NAT-PMP", func() Interface {
|
return startautodisc("any", func() Interface {
|
||||||
found := make(chan Interface, 2)
|
found := make(chan Interface, 2)
|
||||||
go func() { found <- discoverUPnP() }()
|
go func() { found <- discoverUPnP() }()
|
||||||
go func() { found <- discoverPMP() }()
|
go func() { found <- discoverPMP() }()
|
||||||
|
@ -164,7 +165,7 @@ func Any() Interface {
|
||||||
// UPnP returns a port mapper that uses UPnP. It will attempt to
|
// UPnP returns a port mapper that uses UPnP. It will attempt to
|
||||||
// discover the address of your router using UDP broadcasts.
|
// discover the address of your router using UDP broadcasts.
|
||||||
func UPnP() Interface {
|
func UPnP() Interface {
|
||||||
return startautodisc("UPnP", discoverUPnP)
|
return startautodisc("upnp", discoverUPnP)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PMP returns a port mapper that uses NAT-PMP. The provided gateway
|
// PMP returns a port mapper that uses NAT-PMP. The provided gateway
|
||||||
|
@ -174,7 +175,7 @@ func PMP(gateway net.IP) Interface {
|
||||||
if gateway != nil {
|
if gateway != nil {
|
||||||
return &pmp{gw: gateway, c: natpmp.NewClient(gateway)}
|
return &pmp{gw: gateway, c: natpmp.NewClient(gateway)}
|
||||||
}
|
}
|
||||||
return startautodisc("NAT-PMP", discoverPMP)
|
return startautodisc("natpmp", discoverPMP)
|
||||||
}
|
}
|
||||||
|
|
||||||
// autodisc represents a port mapping mechanism that is still being
|
// autodisc represents a port mapping mechanism that is still being
|
||||||
|
@ -228,6 +229,10 @@ func (n *autodisc) String() string {
|
||||||
return n.found.String()
|
return n.found.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *autodisc) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(n.what), nil
|
||||||
|
}
|
||||||
|
|
||||||
// wait blocks until auto-discovery has been performed.
|
// wait blocks until auto-discovery has been performed.
|
||||||
func (n *autodisc) wait() error {
|
func (n *autodisc) wait() error {
|
||||||
n.once.Do(func() {
|
n.once.Do(func() {
|
||||||
|
|
|
@ -70,6 +70,10 @@ func (n *pmp) DeleteMapping(protocol string, extport, intport int) (err error) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *pmp) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(fmt.Sprintf("natpmp:%v", n.gw)), nil
|
||||||
|
}
|
||||||
|
|
||||||
func discoverPMP() Interface {
|
func discoverPMP() Interface {
|
||||||
// run external address lookups on all potential gateways
|
// run external address lookups on all potential gateways
|
||||||
gws := potentialGateways()
|
gws := potentialGateways()
|
||||||
|
|
Loading…
Reference in New Issue