- Notifications
You must be signed in to change notification settings - Fork99
A Go implementation of STUN
License
pion/stun
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Packagestun
implements Session Traversal Utilities for NAT (STUN) (RFC 5389)protocol andclient with no external dependencies and zero allocations in hot paths.Clientsupports automatic request retransmissions.
You can get your current IP address from any STUN server by sendingbinding request. See more idiomatic example atcmd/stun-client
.
package mainimport ("fmt""github.com/pion/stun")funcmain() {// Parse a STUN URIu,err:=stun.ParseURI("stun:stun.l.google.com:19302")iferr!=nil {panic(err)}// Creating a "connection" to STUN server.c,err:=stun.DialURI(u,&stun.DialConfig{})iferr!=nil {panic(err)}// Building binding request with random transaction id.message:=stun.MustBuild(stun.TransactionID,stun.BindingRequest)// Sending request to STUN server, waiting for response message.iferr:=c.Do(message,func(res stun.Event) {ifres.Error!=nil {panic(res.Error)}// Decoding XOR-MAPPED-ADDRESS attribute from message.varxorAddr stun.XORMappedAddressiferr:=xorAddr.GetFrom(res.Message);err!=nil {panic(err)}fmt.Println("your IP is",xorAddr.IP)});err!=nil {panic(err)}}
- RFC 5389:Session Traversal Utilities for NAT (STUN)
- RFC 5769:Test Vectors for Session Traversal Utilities for NAT (STUN)
- RFC 6062:Traversal Using Relays around NAT (TURN) Extensions for TCP Allocations
- RFC 7064:URI Scheme for the Session Traversal Utilities for NAT (STUN) Protocol
- RFC 7065:Traversal Using Relays around NAT (TURN) Uniform Resource Identifiers
- RFC 5780:NAT Behavior Discovery Using Session Traversal Utilities for NAT (STUN) viacmd/stun-nat-behaviour
- (TLS-over-)TCP client support
- RFC 5389:ALTERNATE-SERVER support#48
RFC 5389 obsoletesRFC 3489, so implementation was ignored by purpose, however,RFC 3489 can be easily implemented as separate package.
Package is currently stable, no backward incompatible changes are expectedwith exception of critical bugs or security fixes.
Additional attributes are unlikely to be implemented in scope of stun package,the only exception is constants for attribute or message types.
Go 1.12 is currently supported and tested in CI.
Client behavior is tested and verified in many ways:
- End-To-End with long-term credentials
- coturn: The coturnserver (linux)
- Bunch of code static checkers (linters)
- Standard unit-tests with coverage reporting (linux {amd64,arm64}, windows and darwin)
- Explicit API backward compatibilitycheck, see
api
directory
SeeTeamCity project ande2e
directoryfor more information. Also the Wireshark.pcap
files are available for e2e test inartifacts for build.
Intel(R) Core(TM) i7-8700K:
version: 1.22.2goos: linuxgoarch: amd64pkg: github.com/pion/stunPASSbenchmark iter time/iter throughput bytes alloc allocs--------- ---- --------- ---------- ----------- ------BenchmarkMappedAddress_AddTo-12 32489450 38.30 ns/op 0 B/op 0 allocs/opBenchmarkAlternateServer_AddTo-12 31230991 39.00 ns/op 0 B/op 0 allocs/opBenchmarkAgent_GC-12 431390 2918.00 ns/op 0 B/op 0 allocs/opBenchmarkAgent_Process-12 35901940 36.20 ns/op 0 B/op 0 allocs/opBenchmarkMessage_GetNotFound-12 242004358 5.19 ns/op 0 B/op 0 allocs/opBenchmarkMessage_Get-12 230520343 5.21 ns/op 0 B/op 0 allocs/opBenchmarkClient_Do-12 1282231 943.00 ns/op 0 B/op 0 allocs/opBenchmarkErrorCode_AddTo-12 16318916 75.50 ns/op 0 B/op 0 allocs/opBenchmarkErrorCodeAttribute_AddTo-12 21584140 54.80 ns/op 0 B/op 0 allocs/opBenchmarkErrorCodeAttribute_GetFrom-12 100000000 11.10 ns/op 0 B/op 0 allocs/opBenchmarkFingerprint_AddTo-12 19368768 64.00 ns/op 687.81 MB/s 0 B/op 0 allocs/opBenchmarkFingerprint_Check-12 24167007 49.10 ns/op 1057.99 MB/s 0 B/op 0 allocs/opBenchmarkBuildOverhead/Build-12 5486252 224.00 ns/op 0 B/op 0 allocs/opBenchmarkBuildOverhead/BuildNonPointer-12 2496544 517.00 ns/op 100 B/op 4 allocs/opBenchmarkBuildOverhead/Raw-12 6652118 181.00 ns/op 0 B/op 0 allocs/opBenchmarkMessage_ForEach-12 28254212 35.90 ns/op 0 B/op 0 allocs/opBenchmarkMessageIntegrity_AddTo-12 1000000 1179.00 ns/op 16.96 MB/s 0 B/op 0 allocs/opBenchmarkMessageIntegrity_Check-12 975954 1219.00 ns/op 26.24 MB/s 0 B/op 0 allocs/opBenchmarkMessage_Write-12 41040598 30.40 ns/op 922.13 MB/s 0 B/op 0 allocs/opBenchmarkMessageType_Value-12 1000000000 0.53 ns/op 0 B/op 0 allocs/opBenchmarkMessage_WriteTo-12 94942935 11.30 ns/op 0 B/op 0 allocs/opBenchmarkMessage_ReadFrom-12 43437718 29.30 ns/op 682.87 MB/s 0 B/op 0 allocs/opBenchmarkMessage_ReadBytes-12 74693397 15.90 ns/op 1257.42 MB/s 0 B/op 0 allocs/opBenchmarkIsMessage-12 1000000000 1.20 ns/op 16653.64 MB/s 0 B/op 0 allocs/opBenchmarkMessage_NewTransactionID-12 521121 2450.00 ns/op 0 B/op 0 allocs/opBenchmarkMessageFull-12 5389495 221.00 ns/op 0 B/op 0 allocs/opBenchmarkMessageFullHardcore-12 12715876 94.40 ns/op 0 B/op 0 allocs/opBenchmarkMessage_WriteHeader-12 100000000 11.60 ns/op 0 B/op 0 allocs/opBenchmarkMessage_CloneTo-12 30199020 41.80 ns/op 1626.66 MB/s 0 B/op 0 allocs/opBenchmarkMessage_AddTo-12 415257625 2.97 ns/op 0 B/op 0 allocs/opBenchmarkDecode-12 49573747 23.60 ns/op 0 B/op 0 allocs/opBenchmarkUsername_AddTo-12 56282674 22.50 ns/op 0 B/op 0 allocs/opBenchmarkUsername_GetFrom-12 100000000 10.10 ns/op 0 B/op 0 allocs/opBenchmarkNonce_AddTo-12 39419097 35.80 ns/op 0 B/op 0 allocs/opBenchmarkNonce_AddTo_BadLength-12 196291666 6.04 ns/op 0 B/op 0 allocs/opBenchmarkNonce_GetFrom-12 120857732 9.93 ns/op 0 B/op 0 allocs/opBenchmarkUnknownAttributes/AddTo-12 28881430 37.20 ns/op 0 B/op 0 allocs/opBenchmarkUnknownAttributes/GetFrom-12 64907534 19.80 ns/op 0 B/op 0 allocs/opBenchmarkXOR-12 32868506 32.20 ns/op 31836.66 MB/sBenchmarkXORSafe-12 5185776 234.00 ns/op 4378.74 MB/sBenchmarkXORFast-12 30975679 32.50 ns/op 31525.28 MB/sBenchmarkXORMappedAddress_AddTo-12 21518028 54.50 ns/op 0 B/op 0 allocs/opBenchmarkXORMappedAddress_GetFrom-12 35597667 34.40 ns/op 0 B/op 0 allocs/opok github.com/pion/stun 60.973s
The library is used as a part of our WebRTC implementation. Please refer to thatroadmap to track our major milestones.
Pion has an active community on theSlack.
Follow thePion Twitter for project updates and important WebRTC news.
We are always looking to supportyour projects. Please reach out if you have something to build!If you need commercial support or don't want to use public methods you can contact us atteam@pion.ly
Check out thecontributing wiki to join the group of amazing people making this project possible
MIT License - seeLICENSE for full text
About
A Go implementation of STUN