Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
d303ff45
提交
d303ff45
authored
11月 03, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #253 from jbenet/net-detect
NAT detect
上级
60ef8e5e
3e620427
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
133 行增加
和
9 行删除
+133
-9
handshake.go
net/conn/handshake.go
+45
-0
handshake.pb.go
net/handshake/pb/handshake.pb.go
+13
-8
handshake.proto
net/handshake/pb/handshake.proto
+6
-1
swarm.go
net/swarm/swarm.go
+11
-0
util.go
util/util.go
+58
-0
没有找到文件。
net/conn/handshake.go
浏览文件 @
d303ff45
...
...
@@ -3,12 +3,15 @@ package conn
import
(
"errors"
"fmt"
"strings"
handshake
"github.com/jbenet/go-ipfs/net/handshake"
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"
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
...
...
@@ -65,6 +68,10 @@ func Handshake3(ctx context.Context, c Conn) error {
var
remoteH
,
localH
*
hspb
.
Handshake3
localH
=
handshake
.
Handshake3Msg
(
lpeer
)
rma
:=
c
.
RemoteMultiaddr
()
localH
.
ObservedAddr
=
proto
.
String
(
rma
.
String
())
localB
,
err
:=
proto
.
Marshal
(
localH
)
if
err
!=
nil
{
return
err
...
...
@@ -99,5 +106,43 @@ func Handshake3(ctx context.Context, c Conn) error {
return
err
}
// If we are behind a NAT, inform the user that certain things might not work yet
nat
,
err
:=
checkNAT
(
remoteH
.
GetObservedAddr
())
if
err
!=
nil
{
log
.
Errorf
(
"Error in NAT detection: %s"
,
err
)
}
if
nat
{
msg
:=
`Remote peer observed our address to be: %s
The local addresses are: %s
Thus, connection is going through NAT, and other connections may fail.
IPFS NAT traversal is still under development. Please bug us on github or irc to fix this.
Baby steps: http://jbenet.static.s3.amazonaws.com/271dfcf/baby-steps.gif
`
addrs
,
_
:=
u
.
GetLocalAddresses
()
log
.
Warning
(
fmt
.
Sprintf
(
msg
,
remoteH
.
GetObservedAddr
(),
addrs
))
}
return
nil
}
// checkNAT returns whether or not we might be behind a NAT
func
checkNAT
(
observedaddr
string
)
(
bool
,
error
)
{
observedma
,
err
:=
ma
.
NewMultiaddr
(
observedaddr
)
if
err
!=
nil
{
return
false
,
err
}
addrs
,
err
:=
u
.
GetLocalAddresses
()
if
err
!=
nil
{
return
false
,
err
}
omastr
:=
observedma
.
String
()
for
_
,
addr
:=
range
addrs
{
if
strings
.
HasPrefix
(
omastr
,
addr
.
String
())
{
return
false
,
nil
}
}
return
true
,
nil
}
net/handshake/pb/handshake.pb.go
浏览文件 @
d303ff45
...
...
@@ -14,15 +14,11 @@ It has these top-level messages:
*/
package
handshake_pb
import
proto
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto"
import
json
"encoding/json"
import
proto
"code.google.com/p/gogoprotobuf/proto"
import
math
"math"
// discarding unused import mux "github.com/jbenet/go-ipfs/net/mux/mux.pb"
// Reference proto, json, and math imports to suppress error if they are not otherwise used.
// Reference imports to suppress errors if they are not otherwise used.
var
_
=
proto
.
Marshal
var
_
=
&
json
.
SyntaxError
{}
var
_
=
math
.
Inf
// Handshake1 is delivered _before_ the secure channel is initialized
...
...
@@ -56,8 +52,10 @@ func (m *Handshake1) GetAgentVersion() string {
// Handshake3 is delivered _after_ the secure channel is initialized
type
Handshake3
struct
{
// listenAddrs are the multiaddrs this node listens for open connections on
ListenAddrs
[][]
byte
`protobuf:"bytes,2,rep,name=listenAddrs" json:"listenAddrs,omitempty"`
XXX_unrecognized
[]
byte
`json:"-"`
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:"-"`
}
func
(
m
*
Handshake3
)
Reset
()
{
*
m
=
Handshake3
{}
}
...
...
@@ -71,5 +69,12 @@ func (m *Handshake3) GetListenAddrs() [][]byte {
return
nil
}
func
(
m
*
Handshake3
)
GetObservedAddr
()
string
{
if
m
!=
nil
&&
m
.
ObservedAddr
!=
nil
{
return
*
m
.
ObservedAddr
}
return
""
}
func
init
()
{
}
net/handshake/pb/handshake.proto
浏览文件 @
d303ff45
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
message
Handshake1
{
...
...
@@ -30,4 +30,9 @@ message Handshake3 {
// repeated mux.ProtocolID services = 3;
// we'll have more fields here later.
// oservedAddr is the multiaddr of the remote endpoint that the local node perceives
// this is useful information to convey to the other side, as it helps the remote endpoint
// determine whether its connection to the local peer goes through NAT.
optional
string
observedAddr
=
4
;
}
net/swarm/swarm.go
浏览文件 @
d303ff45
...
...
@@ -3,6 +3,7 @@ package swarm
import
(
"errors"
"fmt"
"strings"
"sync"
conn
"github.com/jbenet/go-ipfs/net/conn"
...
...
@@ -127,6 +128,16 @@ func (s *Swarm) Dial(peer peer.Peer) (conn.Conn, error) {
Peerstore
:
s
.
peers
,
}
// If we are attempting to connect to the zero addr, fail out early
raddr
:=
peer
.
NetAddress
(
"tcp"
)
if
raddr
==
nil
{
return
nil
,
fmt
.
Errorf
(
"No remote address for network tcp"
)
}
if
strings
.
HasPrefix
(
raddr
.
String
(),
"/ip4/0.0.0.0"
)
{
return
nil
,
fmt
.
Errorf
(
"Attempted to connect to loopback address: %s"
,
raddr
)
}
c
,
err
=
d
.
Dial
(
s
.
Context
(),
"tcp"
,
peer
)
if
err
!=
nil
{
return
nil
,
err
...
...
util/util.go
浏览文件 @
d303ff45
...
...
@@ -4,12 +4,16 @@ import (
"errors"
"io"
"math/rand"
"net"
"os"
"path/filepath"
"reflect"
"strings"
"time"
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"
)
...
...
@@ -107,3 +111,57 @@ func GetenvBool(name string) bool {
v
:=
strings
.
ToLower
(
os
.
Getenv
(
name
))
return
v
==
"true"
||
v
==
"t"
||
v
==
"1"
}
// IsLoopbackAddr returns whether or not the ip portion of the passed in multiaddr
// string is a loopback address
func
IsLoopbackAddr
(
addr
string
)
bool
{
loops
:=
[]
string
{
"/ip4/127.0.0.1"
,
"/ip6/::1"
}
for
_
,
loop
:=
range
loops
{
if
strings
.
HasPrefix
(
addr
,
loop
)
{
return
true
}
}
return
false
}
// GetLocalAddresses returns a list of ip addresses associated with
// the local machine
func
GetLocalAddresses
()
([]
ma
.
Multiaddr
,
error
)
{
// Enumerate interfaces on this machine
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
}
// Check each address and convert to a multiaddr
for
_
,
addr
:=
range
addrs
{
switch
v
:=
addr
.
(
type
)
{
case
*
net
.
IPNet
:
// Build multiaddr
maddr
,
err
:=
manet
.
FromIP
(
v
.
IP
)
if
err
!=
nil
{
log
.
Errorf
(
"maddr parsing error: %s"
,
err
)
continue
}
// Dont list loopback addresses
if
IsLoopbackAddr
(
maddr
.
String
())
{
continue
}
maddrs
=
append
(
maddrs
,
maddr
)
default
:
// Not sure if any other types will show up here
log
.
Errorf
(
"Got '%s' type = '%s'"
,
v
,
reflect
.
TypeOf
(
v
))
}
}
}
return
maddrs
,
nil
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论