mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 09:53:48 +00:00
feat: add stun server list
This commit is contained in:
parent
3e1ceea6d9
commit
876ff08944
3 changed files with 127 additions and 25 deletions
|
|
@ -24,28 +24,58 @@ import (
|
||||||
stunV2 "github.com/pion/stun/v2"
|
stunV2 "github.com/pion/stun/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The code are from erigon p2p/nat/nat_stun.go
|
var stunDefaultServerList = []string{
|
||||||
// This stun server is part of the mainnet infrastructure.
|
"159.223.0.83:3478",
|
||||||
// The addr are from https://github.com/ethereum/trin/blob/master/portalnet/src/socket.rs
|
"stun.l.google.com:19302",
|
||||||
const stunDefaultServerAddr = "159.223.0.83:3478"
|
"stun1.l.google.com:19302",
|
||||||
|
"stun2.l.google.com:19302",
|
||||||
|
"stun3.l.google.com:19302",
|
||||||
|
"stun4.l.google.com:19302",
|
||||||
|
"stun01.sipphone.com",
|
||||||
|
"stun.ekiga.net",
|
||||||
|
"stun.fwdnet.net",
|
||||||
|
"stun.ideasip.com",
|
||||||
|
"stun.iptel.org",
|
||||||
|
"stun.rixtelecom.se",
|
||||||
|
"stun.schlund.de",
|
||||||
|
"stunserver.org",
|
||||||
|
"stun.softjoys.com",
|
||||||
|
"stun.voiparound.com",
|
||||||
|
"stun.voipbuster.com",
|
||||||
|
"stun.voipstunt.com",
|
||||||
|
"stun.voxgratia.org",
|
||||||
|
"stun.xten.com",
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestLimit = 3
|
||||||
|
|
||||||
type stun struct {
|
type stun struct {
|
||||||
server *net.UDPAddr
|
serverList []string
|
||||||
|
activeIndex int // the server index which return the IP
|
||||||
|
pendingRequests int // request in flight
|
||||||
|
askedIndex map[int]struct{}
|
||||||
|
replyCh chan stunResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSTUN(serverAddr string) (Interface, error) {
|
func newSTUN(serverAddr string) (Interface, error) {
|
||||||
|
serverList := make([]string, 0)
|
||||||
if serverAddr == "default" {
|
if serverAddr == "default" {
|
||||||
serverAddr = stunDefaultServerAddr
|
serverList = stunDefaultServerList
|
||||||
}
|
} else {
|
||||||
addr, err := net.ResolveUDPAddr("udp4", serverAddr)
|
_, err := net.ResolveUDPAddr("udp4", serverAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return stun{server: addr}, nil
|
serverList = append(serverList, serverAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &stun{
|
||||||
|
serverList: serverList,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s stun) String() string {
|
func (s stun) String() string {
|
||||||
return fmt.Sprintf("STUN(%s)", s.server)
|
return fmt.Sprintf("STUN(%s)", s.serverList[s.activeIndex])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stun) SupportsMapping() bool {
|
func (stun) SupportsMapping() bool {
|
||||||
|
|
@ -60,8 +90,51 @@ func (stun) DeleteMapping(string, int, int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s stun) ExternalIP() (net.IP, error) {
|
type stunResponse struct {
|
||||||
conn, err := stunV2.Dial("udp4", s.server.String())
|
ip net.IP
|
||||||
|
err error
|
||||||
|
index int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stun) ExternalIP() (net.IP, error) {
|
||||||
|
var err error
|
||||||
|
s.replyCh = make(chan stunResponse, requestLimit)
|
||||||
|
s.askedIndex = make(map[int]struct{})
|
||||||
|
for s.startQueries() {
|
||||||
|
response := <-s.replyCh
|
||||||
|
s.pendingRequests--
|
||||||
|
if response.err != nil {
|
||||||
|
err = response.err
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s.activeIndex = response.index
|
||||||
|
return response.ip, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stun) startQueries() bool {
|
||||||
|
for i := 0; s.pendingRequests < requestLimit && i < len(s.serverList); i++ {
|
||||||
|
_, exist := s.askedIndex[i]
|
||||||
|
if exist {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s.pendingRequests++
|
||||||
|
s.askedIndex[i] = struct{}{}
|
||||||
|
go func(index int, server string) {
|
||||||
|
ip, err := externalIP(server)
|
||||||
|
s.replyCh <- stunResponse{
|
||||||
|
ip: ip,
|
||||||
|
index: index,
|
||||||
|
err: err,
|
||||||
|
}
|
||||||
|
}(i, s.serverList[i])
|
||||||
|
}
|
||||||
|
return s.pendingRequests > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func externalIP(server string) (net.IP, error) {
|
||||||
|
conn, err := stunV2.Dial("udp4", server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
23
p2p/nat/nat_stun_test.go
Normal file
23
p2p/nat/nat_stun_test.go
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
package nat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNatStun(t *testing.T) {
|
||||||
|
nat, err := newSTUN("default")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
_, err = nat.ExternalIP()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnreachedNatServer(t *testing.T) {
|
||||||
|
stun := &stun{
|
||||||
|
serverList: []string{"1.2.3.4:1234", "1.2.3.4:1234", "1.2.3.4:1234"},
|
||||||
|
}
|
||||||
|
stun.serverList = append(stun.serverList, stunDefaultServerList...)
|
||||||
|
_, err := stun.ExternalIP()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
@ -20,6 +20,8 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This test checks that autodisc doesn't hang and returns
|
// This test checks that autodisc doesn't hang and returns
|
||||||
|
|
@ -63,17 +65,21 @@ func TestAutoDiscRace(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// stun:default should work well
|
// stun:default should work well
|
||||||
func TestStunDefault(t *testing.T) {
|
func TestParseStun(t *testing.T) {
|
||||||
nat, err := Parse("stun:default")
|
testcases := []struct {
|
||||||
|
natStr string
|
||||||
|
want *stun
|
||||||
|
}{
|
||||||
|
{"stun:default", &stun{serverList: stunDefaultServerList}},
|
||||||
|
{"stun:1.2.3.4:1234", &stun{serverList: []string{"1.2.3.4:1234"}}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testcases {
|
||||||
|
nat, err := Parse(tc.natStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("should no err, but get %v", err)
|
t.Errorf("should no err, but get %v", err)
|
||||||
}
|
}
|
||||||
stun := nat.(stun)
|
stun := nat.(*stun)
|
||||||
if stun.server.String() != stunDefaultServerAddr {
|
assert.Equal(t, stun.serverList, tc.want.serverList)
|
||||||
t.Errorf("want addr %s, got addr %s", stunDefaultServerAddr, stun.server.String())
|
|
||||||
}
|
|
||||||
_, err = stun.ExternalIP()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("get err: %v", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue