main.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2013, Cong Ding. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // author: Cong Ding <dinggnu@gmail.com>
  16. package main
  17. import (
  18. "flag"
  19. "fmt"
  20. "github.com/ccding/go-stun/stun"
  21. )
  22. func main() {
  23. var serverAddr = flag.String("s", stun.DefaultServerAddr, "STUN server address")
  24. var v = flag.Bool("v", false, "verbose mode")
  25. var vv = flag.Bool("vv", false, "double verbose mode (includes -v)")
  26. var vvv = flag.Bool("vvv", false, "triple verbose mode (includes -v and -vv)")
  27. flag.Parse()
  28. // Creates a STUN client. NewClientWithConnection can also be used if
  29. // you want to handle the UDP listener by yourself.
  30. client := stun.NewClient()
  31. // The default addr (stun.DefaultServerAddr) will be used unless we
  32. // call SetServerAddr.
  33. client.SetServerAddr(*serverAddr)
  34. // Non verbose mode will be used by default unless we call
  35. // SetVerbose(true) or SetVVerbose(true).
  36. client.SetVerbose(*v || *vv || *vvv)
  37. client.SetVVerbose(*vv || *vvv)
  38. // Discover the NAT and return the result.
  39. nat, host, err := client.Discover()
  40. if err != nil {
  41. fmt.Println(err)
  42. return
  43. }
  44. fmt.Println("NAT Type:", nat)
  45. if host != nil {
  46. fmt.Println("External IP Family:", host.Family())
  47. fmt.Println("External IP:", host.IP())
  48. fmt.Println("External Port:", host.Port())
  49. }
  50. }