ssl_socket.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "net"
  22. "time"
  23. "crypto/tls"
  24. )
  25. type TSSLSocket struct {
  26. conn net.Conn
  27. addr net.Addr
  28. timeout time.Duration
  29. cfg *tls.Config
  30. }
  31. // NewTSSLSocket creates a net.Conn-backed TTransport, given a host and port and tls Configuration
  32. //
  33. // Example:
  34. // trans, err := thrift.NewTSocket("localhost:9090")
  35. func NewTSSLSocket(hostPort string, cfg *tls.Config) (*TSSLSocket, error) {
  36. return NewTSSLSocketTimeout(hostPort, cfg, 0)
  37. }
  38. // NewTSSLSocketTimeout creates a net.Conn-backed TTransport, given a host and port
  39. // it also accepts a tls Configuration and a timeout as a time.Duration
  40. func NewTSSLSocketTimeout(hostPort string, cfg *tls.Config, timeout time.Duration) (*TSSLSocket, error) {
  41. //conn, err := net.DialTimeout(network, address, timeout)
  42. addr, err := net.ResolveTCPAddr("tcp", hostPort)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return NewTSSLSocketFromAddrTimeout(addr, cfg, timeout), 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. if p.IsOpen() {
  77. return NewTTransportException(ALREADY_OPEN, "Socket already connected.")
  78. }
  79. if p.addr == nil {
  80. return NewTTransportException(NOT_OPEN, "Cannot open nil address.")
  81. }
  82. if len(p.addr.Network()) == 0 {
  83. return NewTTransportException(NOT_OPEN, "Cannot open bad network name.")
  84. }
  85. if len(p.addr.String()) == 0 {
  86. return NewTTransportException(NOT_OPEN, "Cannot open bad address.")
  87. }
  88. var err error
  89. if p.conn, err = tls.Dial(p.addr.Network(), p.addr.String(), p.cfg); err != nil {
  90. return NewTTransportException(NOT_OPEN, err.Error())
  91. }
  92. return nil
  93. }
  94. // Retreive the underlying net.Conn
  95. func (p *TSSLSocket) Conn() net.Conn {
  96. return p.conn
  97. }
  98. // Returns true if the connection is open
  99. func (p *TSSLSocket) IsOpen() bool {
  100. if p.conn == nil {
  101. return false
  102. }
  103. return true
  104. }
  105. // Closes the socket.
  106. func (p *TSSLSocket) Close() error {
  107. // Close the socket
  108. if p.conn != nil {
  109. err := p.conn.Close()
  110. if err != nil {
  111. return err
  112. }
  113. p.conn = nil
  114. }
  115. return nil
  116. }
  117. func (p *TSSLSocket) Read(buf []byte) (int, error) {
  118. if !p.IsOpen() {
  119. return 0, NewTTransportException(NOT_OPEN, "Connection not open")
  120. }
  121. p.pushDeadline(true, false)
  122. n, err := p.conn.Read(buf)
  123. return n, NewTTransportExceptionFromError(err)
  124. }
  125. func (p *TSSLSocket) Write(buf []byte) (int, error) {
  126. if !p.IsOpen() {
  127. return 0, NewTTransportException(NOT_OPEN, "Connection not open")
  128. }
  129. p.pushDeadline(false, true)
  130. return p.conn.Write(buf)
  131. }
  132. func (p *TSSLSocket) Peek() bool {
  133. return p.IsOpen()
  134. }
  135. func (p *TSSLSocket) Flush() error {
  136. return nil
  137. }
  138. func (p *TSSLSocket) Interrupt() error {
  139. if !p.IsOpen() {
  140. return nil
  141. }
  142. return p.conn.Close()
  143. }