forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from r4f4ss/stun
Add stun server for ip discovery and flag stun
- Loading branch information
Showing
8 changed files
with
203 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/p2p/enode" | ||
"github.com/ethereum/go-ethereum/p2p/nat" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func newLocalNodeForTesting() (*enode.LocalNode, *enode.DB) { | ||
db, _ := enode.OpenDB("") | ||
key, _ := crypto.GenerateKey() | ||
return enode.NewLocalNode(db, key), db | ||
} | ||
|
||
func TestDoPortMapping(t *testing.T) { | ||
nat := nat.ExtIP{33, 44, 55, 66} | ||
localNode, _ := newLocalNodeForTesting() | ||
listenerAddr := &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 1234} | ||
|
||
doPortMapping(nat, localNode, listenerAddr) | ||
|
||
assert.Equal(t, localNode.Seq(), uint64(1)) | ||
assert.Equal(t, localNode.Node().IP(), net.IP{33, 44, 55, 66}) | ||
assert.Equal(t, localNode.Node().UDP(), 1234) | ||
assert.Equal(t, localNode.Node().TCP(), 0) | ||
|
||
_ = localNode.Node().UDP() | ||
assert.Equal(t, localNode.Seq(), uint64(2)) | ||
} |
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
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,67 @@ | ||
package nat | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/pion/stun" | ||
) | ||
|
||
const STUNDefaultServerAddr = "159.223.0.83:3478" | ||
|
||
type STUN struct { | ||
serverAddr string | ||
} | ||
|
||
func NewSTUN(serverAddr string) STUN { | ||
if serverAddr == "" { | ||
serverAddr = STUNDefaultServerAddr | ||
} | ||
return STUN{serverAddr: serverAddr} | ||
} | ||
|
||
func (s STUN) String() string { | ||
return fmt.Sprintf("STUN(%s)", s.serverAddr) | ||
} | ||
|
||
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 | ||
} | ||
|
||
func (s STUN) ExternalIP() (net.IP, error) { | ||
conn, err := stun.Dial("udp4", s.serverAddr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer func() { | ||
_ = conn.Close() | ||
}() | ||
|
||
message := stun.MustBuild(stun.TransactionID, stun.BindingRequest) | ||
var response *stun.Event | ||
err = conn.Do(message, func(event stun.Event) { | ||
response = &event | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if response.Error != nil { | ||
return nil, response.Error | ||
} | ||
|
||
var mappedAddr stun.XORMappedAddress | ||
if err := mappedAddr.GetFrom(response.Message); err != nil { | ||
return nil, err | ||
} | ||
|
||
return mappedAddr.IP, nil | ||
} |