A Go implementation of the Session Traversal Utilities for NAT (STUN) protocol as defined in RFC 5389. This library provides both client and server implementations for NAT traversal, which is essential for peer-to-peer networking applications.
- Full STUN Protocol Support: Implements RFC 5389 specifications
- Client & Server: Both client and server implementations included
- XOR-MAPPED-ADDRESS: Support for the XOR-MAPPED-ADDRESS attribute
- Structured Logging: Comprehensive logging with configurable levels
- Error Handling: Robust error handling throughout the codebase
- Easy API: Simple and intuitive API design
- Production Ready: Suitable for production use with proper logging
go get github.com/lai0xn/stunpackage main
import (
"fmt"
"github.com/lai0xn/stun"
)
func main() {
// Create a STUN client
client := stun.NewClient("stun.l.google.com:19302")
// Send a binding request
msg, err := client.Dial(&stun.Message{
Header: stun.Header{
Type: stun.BindingRequest,
},
})
if err != nil {
log.Fatal(err)
}
// Get the XOR mapped address
xorAddr, err := msg.GetXorAddr()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Public IP: %s:%d\n", xorAddr.IP, xorAddr.Port)
}package main
import (
"github.com/lai0xn/stun"
)
func main() {
// Create a STUN server
server := stun.NewServer(stun.ServerConfig{
Addr: "127.0.0.1",
Port: "3478",
Logger: stun.NewDefaultLogger(),
})
// Start listening
if err := server.Listen(); err != nil {
log.Fatal(err)
}
}The library includes a comprehensive logging system that can be configured for different environments:
logger := stun.NewLogger(stun.LoggerConfig{
Level: stun.DebugLevel,
Format: "text",
ShowCaller: true,
})logger := stun.NewLogger(stun.LoggerConfig{
Level: stun.InfoLevel,
Format: "json",
ShowCaller: false,
})DebugLevel: Detailed debug informationInfoLevel: General information messagesWarnLevel: Warning messagesErrorLevel: Error messagesFatalLevel: Fatal errors (exits program)
Creates a new STUN client with the specified server address.
Creates a new STUN client with a custom logger.
Sends a STUN binding request and returns the response.
Creates a new STUN server with the specified configuration.
Starts the server and begins listening for connections.
Gracefully shuts down the server.
Creates a new Message by parsing the provided byte buffer.
Searches for a specific attribute type in the message.
Extracts the XOR-MAPPED-ADDRESS attribute from the message.
Converts the Message to its binary representation.
See the examples/ directory for complete working examples:
examples/client/client.go: Basic client usageexamples/server/server.go: Basic server usage
This implementation supports the core STUN protocol features:
- Binding Request/Response: Core message types for NAT discovery
- XOR-MAPPED-ADDRESS: Attribute containing the client's public IP
- Transaction ID: Unique identifier for each STUN transaction
- Magic Cookie: Protocol identifier (0x2112A442)
- IPv4 Support: Full IPv4 address handling
The library provides comprehensive error handling with specific error types:
ErrAttrNotFound: Attribute not found in messageErrShortBuffer: Buffer too short for readingErrInvalidCookie: Invalid magic cookieErrShortWrite: Incomplete write operation
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- WebRTC: Web Real-Time Communication
- TURN Protocol: Traversal Using Relays around NAT
- ICE Protocol: Interactive Connectivity Establishment