Browse Source

[p2p] refactor upnp to use new logger

pull/491/head
Anton Kaliaev 8 years ago
parent
commit
bc4e6566e7
No known key found for this signature in database GPG Key ID: 7B6881D965918214
3 changed files with 19 additions and 26 deletions
  1. +1
    -2
      cmd/tendermint/commands/probe_upnp.go
  2. +0
    -7
      p2p/upnp/log.go
  3. +18
    -17
      p2p/upnp/probe.go

+ 1
- 2
cmd/tendermint/commands/probe_upnp.go View File

@ -20,8 +20,7 @@ func init() {
}
func probeUpnp(cmd *cobra.Command, args []string) error {
capabilities, err := upnp.Probe()
capabilities, err := upnp.Probe(logger)
if err != nil {
fmt.Println("Probe failed: %v", err)
} else {


+ 0
- 7
p2p/upnp/log.go View File

@ -1,7 +0,0 @@
package upnp
import (
"github.com/tendermint/tmlibs/logger"
)
var log = logger.New("module", "upnp")

+ 18
- 17
p2p/upnp/probe.go View File

@ -7,6 +7,7 @@ import (
"time"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
)
type UPNPCapabilities struct {
@ -14,24 +15,24 @@ type UPNPCapabilities struct {
Hairpin bool
}
func makeUPNPListener(intPort int, extPort int) (NAT, net.Listener, net.IP, error) {
func makeUPNPListener(intPort int, extPort int, logger log.Logger) (NAT, net.Listener, net.IP, error) {
nat, err := Discover()
if err != nil {
return nil, nil, nil, errors.New(fmt.Sprintf("NAT upnp could not be discovered: %v", err))
}
log.Info(cmn.Fmt("ourIP: %v", nat.(*upnpNAT).ourIP))
logger.Info(cmn.Fmt("ourIP: %v", nat.(*upnpNAT).ourIP))
ext, err := nat.GetExternalAddress()
if err != nil {
return nat, nil, nil, errors.New(fmt.Sprintf("External address error: %v", err))
}
log.Info(cmn.Fmt("External address: %v", ext))
logger.Info(cmn.Fmt("External address: %v", ext))
port, err := nat.AddPortMapping("tcp", extPort, intPort, "Tendermint UPnP Probe", 0)
if err != nil {
return nat, nil, ext, errors.New(fmt.Sprintf("Port mapping error: %v", err))
}
log.Info(cmn.Fmt("Port mapping mapped: %v", port))
logger.Info(cmn.Fmt("Port mapping mapped: %v", port))
// also run the listener, open for all remote addresses.
listener, err := net.Listen("tcp", fmt.Sprintf(":%v", intPort))
@ -41,22 +42,22 @@ func makeUPNPListener(intPort int, extPort int) (NAT, net.Listener, net.IP, erro
return nat, listener, ext, nil
}
func testHairpin(listener net.Listener, extAddr string) (supportsHairpin bool) {
func testHairpin(listener net.Listener, extAddr string, logger log.Logger) (supportsHairpin bool) {
// Listener
go func() {
inConn, err := listener.Accept()
if err != nil {
log.Notice(cmn.Fmt("Listener.Accept() error: %v", err))
logger.Info(cmn.Fmt("Listener.Accept() error: %v", err))
return
}
log.Info(cmn.Fmt("Accepted incoming connection: %v -> %v", inConn.LocalAddr(), inConn.RemoteAddr()))
logger.Info(cmn.Fmt("Accepted incoming connection: %v -> %v", inConn.LocalAddr(), inConn.RemoteAddr()))
buf := make([]byte, 1024)
n, err := inConn.Read(buf)
if err != nil {
log.Notice(cmn.Fmt("Incoming connection read error: %v", err))
logger.Info(cmn.Fmt("Incoming connection read error: %v", err))
return
}
log.Info(cmn.Fmt("Incoming connection read %v bytes: %X", n, buf))
logger.Info(cmn.Fmt("Incoming connection read %v bytes: %X", n, buf))
if string(buf) == "test data" {
supportsHairpin = true
return
@ -66,28 +67,28 @@ func testHairpin(listener net.Listener, extAddr string) (supportsHairpin bool) {
// Establish outgoing
outConn, err := net.Dial("tcp", extAddr)
if err != nil {
log.Notice(cmn.Fmt("Outgoing connection dial error: %v", err))
logger.Info(cmn.Fmt("Outgoing connection dial error: %v", err))
return
}
n, err := outConn.Write([]byte("test data"))
if err != nil {
log.Notice(cmn.Fmt("Outgoing connection write error: %v", err))
logger.Info(cmn.Fmt("Outgoing connection write error: %v", err))
return
}
log.Info(cmn.Fmt("Outgoing connection wrote %v bytes", n))
logger.Info(cmn.Fmt("Outgoing connection wrote %v bytes", n))
// Wait for data receipt
time.Sleep(1 * time.Second)
return
}
func Probe() (caps UPNPCapabilities, err error) {
log.Info("Probing for UPnP!")
func Probe(logger log.Logger) (caps UPNPCapabilities, err error) {
logger.Info("Probing for UPnP!")
intPort, extPort := 8001, 8001
nat, listener, ext, err := makeUPNPListener(intPort, extPort)
nat, listener, ext, err := makeUPNPListener(intPort, extPort, logger)
if err != nil {
return
}
@ -97,12 +98,12 @@ func Probe() (caps UPNPCapabilities, err error) {
defer func() {
err = nat.DeletePortMapping("tcp", intPort, extPort)
if err != nil {
log.Warn(cmn.Fmt("Port mapping delete error: %v", err))
logger.Error(cmn.Fmt("Port mapping delete error: %v", err))
}
listener.Close()
}()
supportsHairpin := testHairpin(listener, fmt.Sprintf("%v:%v", ext, extPort))
supportsHairpin := testHairpin(listener, fmt.Sprintf("%v:%v", ext, extPort), logger)
if supportsHairpin {
caps.Hairpin = true
}


Loading…
Cancel
Save