提交 2eac5aee 作者: Jeromy

begin code for NAT detection

上级 03ca7fdf
...@@ -6,9 +6,11 @@ import ( ...@@ -6,9 +6,11 @@ import (
handshake "github.com/jbenet/go-ipfs/net/handshake" handshake "github.com/jbenet/go-ipfs/net/handshake"
hspb "github.com/jbenet/go-ipfs/net/handshake/pb" hspb "github.com/jbenet/go-ipfs/net/handshake/pb"
u "github.com/jbenet/go-ipfs/util"
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto" proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
ma "github.com/jbenet/go-multiaddr"
) )
// Handshake1 exchanges local and remote versions and compares them // Handshake1 exchanges local and remote versions and compares them
...@@ -65,6 +67,10 @@ func Handshake3(ctx context.Context, c Conn) error { ...@@ -65,6 +67,10 @@ func Handshake3(ctx context.Context, c Conn) error {
var remoteH, localH *hspb.Handshake3 var remoteH, localH *hspb.Handshake3
localH = handshake.Handshake3Msg(lpeer) localH = handshake.Handshake3Msg(lpeer)
rma := c.RemoteMultiaddr()
localH.ObservedAddr = proto.String(rma.String())
localB, err := proto.Marshal(localH) localB, err := proto.Marshal(localH)
if err != nil { if err != nil {
return err return err
...@@ -101,3 +107,18 @@ func Handshake3(ctx context.Context, c Conn) error { ...@@ -101,3 +107,18 @@ func Handshake3(ctx context.Context, c Conn) error {
return nil return nil
} }
func CheckNAT(obsaddr string) (bool, error) {
oma, err := ma.NewMultiaddr(obsaddr)
if err != nil {
return false, err
}
addrs, err := u.GetLocalAddresses()
if err != nil {
return false, err
}
_ = oma
_ = addrs
panic("not yet implemented!")
}
...@@ -14,15 +14,11 @@ It has these top-level messages: ...@@ -14,15 +14,11 @@ It has these top-level messages:
*/ */
package handshake_pb package handshake_pb
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto" import proto "code.google.com/p/gogoprotobuf/proto"
import json "encoding/json"
import math "math" import math "math"
// discarding unused import mux "github.com/jbenet/go-ipfs/net/mux/mux.pb" // Reference imports to suppress errors if they are not otherwise used.
// Reference proto, json, and math imports to suppress error if they are not otherwise used.
var _ = proto.Marshal var _ = proto.Marshal
var _ = &json.SyntaxError{}
var _ = math.Inf var _ = math.Inf
// Handshake1 is delivered _before_ the secure channel is initialized // Handshake1 is delivered _before_ the secure channel is initialized
...@@ -57,6 +53,8 @@ func (m *Handshake1) GetAgentVersion() string { ...@@ -57,6 +53,8 @@ func (m *Handshake1) GetAgentVersion() string {
type Handshake3 struct { type Handshake3 struct {
// listenAddrs are the multiaddrs this node listens for open connections on // listenAddrs are the multiaddrs this node listens for open connections on
ListenAddrs [][]byte `protobuf:"bytes,2,rep,name=listenAddrs" json:"listenAddrs,omitempty"` ListenAddrs [][]byte `protobuf:"bytes,2,rep,name=listenAddrs" json:"listenAddrs,omitempty"`
// we'll have more fields here later.
ObservedAddr *string `protobuf:"bytes,4,opt,name=observedAddr" json:"observedAddr,omitempty"`
XXX_unrecognized []byte `json:"-"` XXX_unrecognized []byte `json:"-"`
} }
...@@ -71,5 +69,12 @@ func (m *Handshake3) GetListenAddrs() [][]byte { ...@@ -71,5 +69,12 @@ func (m *Handshake3) GetListenAddrs() [][]byte {
return nil return nil
} }
func (m *Handshake3) GetObservedAddr() string {
if m != nil && m.ObservedAddr != nil {
return *m.ObservedAddr
}
return ""
}
func init() { func init() {
} }
package handshake.pb; package handshake.pb;
import "github.com/jbenet/go-ipfs/net/mux/mux.proto"; //import "github.com/jbenet/go-ipfs/net/mux/mux.proto";
// Handshake1 is delivered _before_ the secure channel is initialized // Handshake1 is delivered _before_ the secure channel is initialized
message Handshake1 { message Handshake1 {
...@@ -30,4 +30,5 @@ message Handshake3 { ...@@ -30,4 +30,5 @@ message Handshake3 {
// repeated mux.ProtocolID services = 3; // repeated mux.ProtocolID services = 3;
// we'll have more fields here later. // we'll have more fields here later.
optional string observedAddr = 4;
} }
...@@ -4,12 +4,16 @@ import ( ...@@ -4,12 +4,16 @@ import (
"errors" "errors"
"io" "io"
"math/rand" "math/rand"
"net"
"os" "os"
"path/filepath" "path/filepath"
"reflect"
"strings" "strings"
"time" "time"
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore" ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir" "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
) )
...@@ -107,3 +111,40 @@ func GetenvBool(name string) bool { ...@@ -107,3 +111,40 @@ func GetenvBool(name string) bool {
v := strings.ToLower(os.Getenv(name)) v := strings.ToLower(os.Getenv(name))
return v == "true" || v == "t" || v == "1" return v == "true" || v == "t" || v == "1"
} }
func IsLoopbackAddr(addr string) bool {
return addr == "/ip4/127.0.0.1" || addr == "/ip6/::1"
}
func GetLocalAddresses() ([]ma.Multiaddr, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var maddrs []ma.Multiaddr
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
log.Warningf("Skipping addr: %s", err)
continue
}
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
maddr, err := manet.FromIP(v.IP)
if err != nil {
log.Errorf("maddr parsing error: %s", err)
continue
}
if IsLoopbackAddr(maddr.String()) {
continue
}
maddrs = append(maddrs, maddr)
default:
log.Errorf("Got '%s' type = '%s'", v, reflect.TypeOf(v))
}
}
}
return maddrs, nil
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论