-
Notifications
You must be signed in to change notification settings - Fork 20.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
p2p/nat: add stun protocol #30882
Open
fearlessfe
wants to merge
6
commits into
ethereum:master
Choose a base branch
from
optimism-java:nat-stun
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+258
−2
Open
p2p/nat: add stun protocol #30882
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b86371
p2p/nat:add stun protocol
fearlessfe 04087bb
p2p/nat: fix lint error
fearlessfe b9c6120
p2p/nat: do some optimization
fearlessfe 8873290
feat: make stun server private
a176db2
feat: handler ip in the closure
5765be1
feat: add stun server list
fearlessfe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
// Copyright 2024 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package nat | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
stunV2 "github.com/pion/stun/v2" | ||
) | ||
|
||
var stunDefaultServerList = []string{ | ||
"159.223.0.83:3478", | ||
"stun.l.google.com:19302", | ||
"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 { | ||
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) { | ||
serverList := make([]string, 0) | ||
if serverAddr == "default" { | ||
serverList = stunDefaultServerList | ||
} else { | ||
_, err := net.ResolveUDPAddr("udp4", serverAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
serverList = append(serverList, serverAddr) | ||
} | ||
|
||
return &stun{ | ||
serverList: serverList, | ||
}, nil | ||
} | ||
|
||
func (s stun) String() string { | ||
return fmt.Sprintf("STUN(%s)", s.serverList[s.activeIndex]) | ||
} | ||
|
||
func (stun) SupportsMapping() bool { | ||
return false | ||
} | ||
|
||
func (stun) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) (uint16, error) { | ||
return uint16(extport), nil | ||
} | ||
|
||
func (stun) DeleteMapping(string, int, int) error { | ||
return nil | ||
} | ||
|
||
type stunResponse struct { | ||
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 { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
|
||
message, err := stunV2.Build(stunV2.TransactionID, stunV2.BindingRequest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var responseError error | ||
var mappedAddr stunV2.XORMappedAddress | ||
err = conn.Do(message, func(event stunV2.Event) { | ||
if event.Error != nil { | ||
responseError = event.Error | ||
return | ||
} | ||
if err := mappedAddr.GetFrom(event.Message); err != nil { | ||
responseError = err | ||
} | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if responseError != nil { | ||
return nil, responseError | ||
} | ||
|
||
return mappedAddr.IP, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another way to do it would be to mimic
pmp
Don't really know if it's a good idea to have a default. For now, some devops team maintain a stun on a server, but is that a promise for the future? What happens when DO recycles it and someone else is given the ip address?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like it either. We try to avoid hard-coding network endpoints in the client, and when we do, it should be a list of multiple ones. There are lists of public STUN server endpoints floating around that we could use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about server list here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah we can integrate that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fjl I make the stun addr to stun server list. When request to stun server, it's up to three requests. f the response is error, it will request to the next server until a success response or all stun server are requested. The code are inspired in
p2p/discover/lookup.go