go-ethereum/p2p/nat/nat_stun.go

92 lines
2.3 KiB
Go
Raw Normal View History

// Copyright 2024 The go-ethereum Authors
2024-12-09 08:00:41 -06:00
// 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"
2024-12-09 20:26:56 -06:00
stunV2 "github.com/pion/stun/v2"
2024-12-09 08:00:41 -06:00
)
// The code are from erigon p2p/nat/nat_stun.go
// This stun server is part of the mainnet infrastructure.
// The addr are from https://github.com/ethereum/trin/blob/master/portalnet/src/socket.rs
2024-12-09 20:26:56 -06:00
const stunDefaultServerAddr = "159.223.0.83:3478"
2024-12-09 08:00:41 -06:00
2024-12-09 20:26:56 -06:00
type stun struct {
server *net.UDPAddr
2024-12-09 08:00:41 -06:00
}
2024-12-09 20:26:56 -06:00
func newSTUN(serverAddr string) (Interface, error) {
if serverAddr == "default" {
2024-12-09 20:26:56 -06:00
serverAddr = stunDefaultServerAddr
2024-12-09 08:00:41 -06:00
}
addr, err := net.ResolveUDPAddr("udp4", serverAddr)
if err != nil {
return nil, err
}
2024-12-09 20:26:56 -06:00
return stun{server: addr}, nil
2024-12-09 08:00:41 -06:00
}
2024-12-09 20:26:56 -06:00
func (s stun) String() string {
return fmt.Sprintf("STUN(%s)", s.server)
2024-12-09 08:00:41 -06:00
}
2024-12-09 20:26:56 -06:00
func (stun) SupportsMapping() bool {
2024-12-09 08:00:41 -06:00
return false
}
2024-12-09 20:26:56 -06:00
func (stun) AddMapping(protocol string, extport, intport int, name string, lifetime time.Duration) (uint16, error) {
2024-12-09 08:00:41 -06:00
return uint16(extport), nil
}
2024-12-09 20:26:56 -06:00
func (stun) DeleteMapping(string, int, int) error {
2024-12-09 08:00:41 -06:00
return nil
}
2024-12-09 20:26:56 -06:00
func (s stun) ExternalIP() (net.IP, error) {
conn, err := stunV2.Dial("udp4", s.server.String())
2024-12-09 08:00:41 -06:00
if err != nil {
return nil, err
}
defer conn.Close()
2024-12-09 08:00:41 -06:00
2024-12-09 20:26:56 -06:00
message, err := stunV2.Build(stunV2.TransactionID, stunV2.BindingRequest)
if err != nil {
return nil, err
}
2024-12-09 20:26:56 -06:00
var response *stunV2.Event
err = conn.Do(message, func(event stunV2.Event) {
2024-12-09 08:00:41 -06:00
response = &event
})
if err != nil {
return nil, err
}
if response.Error != nil {
return nil, response.Error
}
2024-12-09 20:26:56 -06:00
var mappedAddr stunV2.XORMappedAddress
2024-12-09 08:00:41 -06:00
if err := mappedAddr.GetFrom(response.Message); err != nil {
return nil, err
}
return mappedAddr.IP, nil
}