Skip to content
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
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

fearlessfe
Copy link

@fearlessfe fearlessfe commented Dec 9, 2024

fixed #30881

This PR introduces STUN (Session Traversal Utilities for NAT) support to the nat package. The STUN protocol is useful for determining the public IP address of a NAT gateway and enables applications to work effectively in NAT environments.

usage

--nat stun 
--nat stun:<stun server addr>

Copy link
Contributor

@holiman holiman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do a go mod tidy also

}

func NewSTUN(serverAddr string) STUN {
if serverAddr == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if serverAddr == "" {
if serverAddr == "default" {

Imo better if using the default is an explicit opt-in from the user

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also verify that serverAddr can be converted into a valid udp4 addr, and error otherwise.
And why not let serverAddr be and *UDPAddr instead?

	addr, err := net.ResolveUDPAddr("udp4", serverAddr)
        if err != nil{
              return nil, err
        }
        return STUN{ serverAddr: addr}, nil

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that a good idea, I will use *UDPAddr instead and return err if addr are not valid

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@holiman
for this oneImo better if using the default is an explicit opt-in from the user

you mean use should use like this --nat stun: default

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, something like that. --nat=stun:default without space, probably

p2p/nat/nat.go Outdated Show resolved Hide resolved
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
1. upgrade to github.com/pion/stun/v2
2. check the server addr
@fjl fjl changed the title p2p/nat:add stun protocol p2p/nat: add stun protocol Dec 9, 2024
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
p2p/nat/nat_stun.go Outdated Show resolved Hide resolved
@fjl fjl self-assigned this Dec 9, 2024
@fearlessfe fearlessfe requested a review from holiman December 12, 2024 02:04
Comment on lines +62 to +63
// "stun:default" uses stun protocol with default stun server
// "stun:192.168.0.1:1234" uses stun protocol with stun server address 192.168.0.1:1234
Copy link
Contributor

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

// "stun" uses stun protocol with default stun server
// "stun:192.168.0.1:1234" uses stun protocol with stun server address 192.168.0.1:1234

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?

Copy link
Contributor

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.

Copy link
Author

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

Copy link
Contributor

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

Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add stun protocol for p2p/nat
3 participants