client.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 stun
  17. import (
  18. "errors"
  19. "net"
  20. "strconv"
  21. )
  22. // Client is a STUN client, which can be set STUN server address and is used
  23. // to discover NAT type.
  24. type Client struct {
  25. serverAddr string
  26. softwareName string
  27. conn net.PacketConn
  28. logger *Logger
  29. }
  30. // NewClient returns a client without network connection. The network
  31. // connection will be build when calling Discover function.
  32. func NewClient() *Client {
  33. c := new(Client)
  34. c.SetSoftwareName(DefaultSoftwareName)
  35. c.logger = NewLogger()
  36. return c
  37. }
  38. // NewClientWithConnection returns a client which uses the given connection.
  39. // Please note the connection should be acquired via net.Listen* method.
  40. func NewClientWithConnection(conn net.PacketConn) *Client {
  41. c := new(Client)
  42. c.conn = conn
  43. c.SetSoftwareName(DefaultSoftwareName)
  44. c.logger = NewLogger()
  45. return c
  46. }
  47. // SetVerbose sets the client to be in the verbose mode, which prints
  48. // information in the discover process.
  49. func (c *Client) SetVerbose(v bool) {
  50. c.logger.SetDebug(v)
  51. }
  52. // SetVVerbose sets the client to be in the double verbose mode, which prints
  53. // information and packet in the discover process.
  54. func (c *Client) SetVVerbose(v bool) {
  55. c.logger.SetInfo(v)
  56. }
  57. // SetServerHost allows user to set the STUN hostname and port.
  58. func (c *Client) SetServerHost(host string, port int) {
  59. c.serverAddr = net.JoinHostPort(host, strconv.Itoa(port))
  60. }
  61. // SetServerAddr allows user to set the transport layer STUN server address.
  62. func (c *Client) SetServerAddr(address string) {
  63. c.serverAddr = address
  64. }
  65. // SetSoftwareName allows user to set the name of the software, which is used
  66. // for logging purpose (NOT used in the current implementation).
  67. func (c *Client) SetSoftwareName(name string) {
  68. c.softwareName = name
  69. }
  70. // Discover contacts the STUN server and gets the response of NAT type, host
  71. // for UDP punching.
  72. func (c *Client) Discover() (NATType, *Host, error) {
  73. if c.serverAddr == "" {
  74. c.SetServerAddr(DefaultServerAddr)
  75. }
  76. serverUDPAddr, err := net.ResolveUDPAddr("udp", c.serverAddr)
  77. if err != nil {
  78. return NATError, nil, err
  79. }
  80. // Use the connection passed to the client if it is not nil, otherwise
  81. // create a connection and close it at the end.
  82. conn := c.conn
  83. if conn == nil {
  84. conn, err = net.ListenUDP("udp", nil)
  85. if err != nil {
  86. return NATError, nil, err
  87. }
  88. defer conn.Close()
  89. }
  90. return c.discover(conn, serverUDPAddr)
  91. }
  92. // Keepalive sends and receives a bind request, which ensures the mapping stays open
  93. // Only applicable when client was created with a connection.
  94. func (c *Client) Keepalive() (*Host, error) {
  95. if c.conn == nil {
  96. return nil, errors.New("no connection available")
  97. }
  98. if c.serverAddr == "" {
  99. c.SetServerAddr(DefaultServerAddr)
  100. }
  101. serverUDPAddr, err := net.ResolveUDPAddr("udp", c.serverAddr)
  102. if err != nil {
  103. return nil, err
  104. }
  105. resp, err := c.test1(c.conn, serverUDPAddr)
  106. if err != nil {
  107. return nil, err
  108. }
  109. if resp == nil || resp.packet == nil {
  110. return nil, errors.New("failed to contact")
  111. }
  112. return resp.mappedAddr, nil
  113. }