@cryptotaxi247 / kubo / commits / 2eac5aeec

begin code for NAT detection

Jeromy committed Nov 2, 2014 at 21:19 UTC 2eac5aeec608825d44a9fbbdbb901c2583217bea
4 files changed +77 -9
net/conn/handshake.go
+21
@@ -6,9 +6,11 @@ import (
6
7 handshake "github.com/jbenet/go-ipfs/net/handshake"
8 hspb "github.com/jbenet/go-ipfs/net/handshake/pb"
9 + u "github.com/jbenet/go-ipfs/util"
10
11 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
12 proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
13 + ma "github.com/jbenet/go-multiaddr"
14 )
15
16 // Handshake1 exchanges local and remote versions and compares them
@@ -65,6 +67,10 @@ func Handshake3(ctx context.Context, c Conn) error {
67
68 var remoteH, localH *hspb.Handshake3
69 localH = handshake.Handshake3Msg(lpeer)
70 +
71 + rma := c.RemoteMultiaddr()
72 + localH.ObservedAddr = proto.String(rma.String())
73 +
74 localB, err := proto.Marshal(localH)
75 if err != nil {
76 return err
@@ -101,3 +107,18 @@ func Handshake3(ctx context.Context, c Conn) error {
107
108 return nil
109 }
110 +
111 +func CheckNAT(obsaddr string) (bool, error) {
112 + oma, err := ma.NewMultiaddr(obsaddr)
113 + if err != nil {
114 + return false, err
115 + }
116 + addrs, err := u.GetLocalAddresses()
117 + if err != nil {
118 + return false, err
119 + }
120 + _ = oma
121 + _ = addrs
122 +
123 + panic("not yet implemented!")
124 +}
net/handshake/pb/handshake.pb.go
+13 -8
@@ -14,15 +14,11 @@ It has these top-level messages:
14 */
15 package handshake_pb
16
17 -import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto"
18 -import json "encoding/json"
17 +import proto "code.google.com/p/gogoprotobuf/proto"
18 import math "math"
19
21 -// discarding unused import mux "github.com/jbenet/go-ipfs/net/mux/mux.pb"
22 -
23 -// Reference proto, json, and math imports to suppress error if they are not otherwise used.
20 +// Reference imports to suppress errors if they are not otherwise used.
21 var _ = proto.Marshal
25 -var _ = &json.SyntaxError{}
22 var _ = math.Inf
23
24 // Handshake1 is delivered _before_ the secure channel is initialized
@@ -56,8 +52,10 @@ func (m *Handshake1) GetAgentVersion() string {
52 // Handshake3 is delivered _after_ the secure channel is initialized
53 type Handshake3 struct {
54 // listenAddrs are the multiaddrs this node listens for open connections on
59 - ListenAddrs [][]byte `protobuf:"bytes,2,rep,name=listenAddrs" json:"listenAddrs,omitempty"`
60 - XXX_unrecognized []byte `json:"-"`
55 + ListenAddrs [][]byte `protobuf:"bytes,2,rep,name=listenAddrs" json:"listenAddrs,omitempty"`
56 + // we'll have more fields here later.
57 + ObservedAddr *string `protobuf:"bytes,4,opt,name=observedAddr" json:"observedAddr,omitempty"`
58 + XXX_unrecognized []byte `json:"-"`
59 }
60
61 func (m *Handshake3) Reset() { *m = Handshake3{} }
@@ -71,5 +69,12 @@ func (m *Handshake3) GetListenAddrs() [][]byte {
69 return nil
70 }
71
72 +func (m *Handshake3) GetObservedAddr() string {
73 + if m != nil && m.ObservedAddr != nil {
74 + return *m.ObservedAddr
75 + }
76 + return ""
77 +}
78 +
79 func init() {
80 }
net/handshake/pb/handshake.proto
+2 -1
@@ -1,6 +1,6 @@
1 package handshake.pb;
2
3 -import "github.com/jbenet/go-ipfs/net/mux/mux.proto";
3 +//import "github.com/jbenet/go-ipfs/net/mux/mux.proto";
4
5 // Handshake1 is delivered _before_ the secure channel is initialized
6 message Handshake1 {
@@ -30,4 +30,5 @@ message Handshake3 {
30 // repeated mux.ProtocolID services = 3;
31
32 // we'll have more fields here later.
33 + optional string observedAddr = 4;
34 }
util/util.go
+41
@@ -4,12 +4,16 @@ import (
4 "errors"
5 "io"
6 "math/rand"
7 + "net"
8 "os"
9 "path/filepath"
10 + "reflect"
11 "strings"
12 "time"
13
14 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
15 + ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
16 + manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
17 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/mitchellh/go-homedir"
18 )
19
@@ -107,3 +111,40 @@ func GetenvBool(name string) bool {
111 v := strings.ToLower(os.Getenv(name))
112 return v == "true" || v == "t" || v == "1"
113 }
114 +
115 +func IsLoopbackAddr(addr string) bool {
116 + return addr == "/ip4/127.0.0.1" || addr == "/ip6/::1"
117 +}
118 +
119 +func GetLocalAddresses() ([]ma.Multiaddr, error) {
120 + ifaces, err := net.Interfaces()
121 + if err != nil {
122 + return nil, err
123 + }
124 +
125 + var maddrs []ma.Multiaddr
126 + for _, i := range ifaces {
127 + addrs, err := i.Addrs()
128 + if err != nil {
129 + log.Warningf("Skipping addr: %s", err)
130 + continue
131 + }
132 + for _, addr := range addrs {
133 + switch v := addr.(type) {
134 + case *net.IPNet:
135 + maddr, err := manet.FromIP(v.IP)
136 + if err != nil {
137 + log.Errorf("maddr parsing error: %s", err)
138 + continue
139 + }
140 + if IsLoopbackAddr(maddr.String()) {
141 + continue
142 + }
143 + maddrs = append(maddrs, maddr)
144 + default:
145 + log.Errorf("Got '%s' type = '%s'", v, reflect.TypeOf(v))
146 + }
147 + }
148 + }
149 + return maddrs, nil
150 +}