ssl_socket.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "crypto/tls"
  22. "net"
  23. "time"
  24. )
  25. type TSSLSocket struct {
  26. conn net.Conn
  27. // hostPort contains host:port (e.g. "asdf.com:12345"). The field is
  28. // only valid if addr is nil.
  29. hostPort string
  30. // addr is nil when hostPort is not "", and is only used when the
  31. // TSSLSocket is constructed from a net.Addr.
  32. addr net.Addr
  33. timeout time.Duration
  34. cfg *tls.Config
  35. }
  36. // NewTSSLSocket creates a net.Conn-backed TTransport, given a host and port and tls Configuration
  37. //
  38. // Example:
  39. // trans, err := thrift.NewTSSLSocket("localhost:9090", nil)
  40. func NewTSSLSocket(hostPort string, cfg *tls.Config) (*TSSLSocket, error) {
  41. return NewTSSLSocketTimeout(hostPort, cfg, 0)
  42. }
  43. // NewTSSLSocketTimeout creates a net.Conn-backed TTransport, given a host and port
  44. // it also accepts a tls Configuration and a timeout as a time.Duration
  45. func NewTSSLSocketTimeout(hostPort string, cfg *tls.Config, timeout time.Duration) (*TSSLSocket, error) {
  46. return &TSSLSocket{hostPort: hostPort, timeout: timeout, cfg: cfg}, nil
  47. }
  48. // Creates a TSSLSocket from a net.Addr
  49. func NewTSSLSocketFromAddrTimeout(addr net.Addr, cfg *tls.Config, timeout time.Duration) *TSSLSocket {
  50. return &TSSLSocket{addr: addr, timeout: timeout, cfg: cfg}
  51. }
  52. // Creates a TSSLSocket from an existing net.Conn
  53. func NewTSSLSocketFromConnTimeout(conn net.Conn, cfg *tls.Config, timeout time.Duration) *TSSLSocket {
  54. return &TSSLSocket{conn: conn, addr: conn.RemoteAddr(), timeout: timeout, cfg: cfg}
  55. }
  56. // Sets the socket timeout
  57. func (p *TSSLSocket) SetTimeout(timeout time.Duration) error {
  58. p.timeout = timeout
  59. return nil
  60. }
  61. func (p *TSSLSocket) pushDeadline(read, write bool) {
  62. var t time.Time
  63. if p.timeout > 0 {
  64. t = time.Now().Add(time.Duration(p.timeout))
  65. }
  66. if read && write {
  67. p.conn.SetDeadline(t)
  68. } else if read {
  69. p.conn.SetReadDeadline(t)
  70. } else if write {
  71. p.conn.SetWriteDeadline(t)
  72. }
  73. }
  74. // Connects the socket, creating a new socket object if necessary.
  75. func (p *TSSLSocket) Open() error {
  76. var err error
  77. // If we have a hostname, we need to pass the hostname to tls.Dial for
  78. // certificate hostname checks.
  79. if p.hostPort != "" {
  80. if p.conn, err = tls.Dial("tcp", p.hostPort, p.cfg); err != nil {
  81. return NewTTransportException(NOT_OPEN, err.Error())
  82. }
  83. } else {
  84. if p.IsOpen() {
  85. return NewTTransportException(ALREADY_OPEN, "Socket already connected.")
  86. }
  87. if p.addr == nil {
  88. return NewTTransportException(NOT_OPEN, "Cannot open nil address.")
  89. }
  90. if len(p.addr.Network()) == 0 {
  91. return NewTTransportException(NOT_OPEN, "Cannot open bad network name.")
  92. }
  93. if len(p.addr.String()) == 0 {
  94. return NewTTransportException(NOT_OPEN, "Cannot open bad address.")
  95. }
  96. if p.conn, err = tls.Dial(p.addr.Network(), p.addr.String(), p.cfg); err != nil {
  97. return NewTTransportException(NOT_OPEN, err.Error())
  98. }
  99. }
  100. return nil
  101. }
  102. // Retrieve the underlying net.Conn
  103. func (p *TSSLSocket) Conn() net.Conn {
  104. return p.conn
  105. }
  106. // Returns true if the connection is open
  107. func (p *TSSLSocket) IsOpen() bool {
  108. if p.conn == nil {
  109. return false
  110. }
  111. return true
  112. }
  113. // Closes the socket.
  114. func (p *TSSLSocket) Close() error {
  115. // Close the socket
  116. if p.conn != nil {
  117. err := p.conn.Close()
  118. if err != nil {
  119. return err
  120. }
  121. p.conn = nil
  122. }
  123. return nil
  124. }
  125. func (p *TSSLSocket) Read(buf []byte) (int, error) {
  126. if !p.IsOpen() {
  127. return 0, NewTTransportException(NOT_OPEN, "Connection not open")
  128. }
  129. p.pushDeadline(true, false)
  130. n, err := p.conn.Read(buf)
  131. return n, NewTTransportExceptionFromError(err)
  132. }
  133. func (p *TSSLSocket) Write(buf []byte) (int, error) {
  134. if !p.IsOpen() {
  135. return 0, NewTTransportException(NOT_OPEN, "Connection not open")
  136. }
  137. p.pushDeadline(false, true)
  138. return p.conn.Write(buf)
  139. }
  140. func (p *TSSLSocket) Flush() error {
  141. return nil
  142. }
  143. func (p *TSSLSocket) Interrupt() error {
  144. if !p.IsOpen() {
  145. return nil
  146. }
  147. return p.conn.Close()
  148. }
  149. func (p *TSSLSocket) RemainingBytes() (num_bytes uint64) {
  150. const maxSize = ^uint64(0)
  151. return maxSize // the thruth is, we just don't know unless framed is used
  152. }