ssl_server_socket.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 TSSLServerSocket struct {
  26. listener net.Listener
  27. addr net.Addr
  28. clientTimeout time.Duration
  29. interrupted bool
  30. cfg *tls.Config
  31. }
  32. func NewTSSLServerSocket(listenAddr string, cfg *tls.Config) (*TSSLServerSocket, error) {
  33. return NewTSSLServerSocketTimeout(listenAddr, cfg, 0)
  34. }
  35. func NewTSSLServerSocketTimeout(listenAddr string, cfg *tls.Config, clientTimeout time.Duration) (*TSSLServerSocket, error) {
  36. addr, err := net.ResolveTCPAddr("tcp", listenAddr)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return &TSSLServerSocket{addr: addr, clientTimeout: clientTimeout, cfg: cfg}, nil
  41. }
  42. func (p *TSSLServerSocket) Listen() error {
  43. if p.IsListening() {
  44. return nil
  45. }
  46. l, err := tls.Listen(p.addr.Network(), p.addr.String(), p.cfg)
  47. if err != nil {
  48. return err
  49. }
  50. p.listener = l
  51. return nil
  52. }
  53. func (p *TSSLServerSocket) Accept() (TTransport, error) {
  54. if p.interrupted {
  55. return nil, errTransportInterrupted
  56. }
  57. if p.listener == nil {
  58. return nil, NewTTransportException(NOT_OPEN, "No underlying server socket")
  59. }
  60. conn, err := p.listener.Accept()
  61. if err != nil {
  62. return nil, NewTTransportExceptionFromError(err)
  63. }
  64. return NewTSSLSocketFromConnTimeout(conn, p.cfg, p.clientTimeout), nil
  65. }
  66. // Checks whether the socket is listening.
  67. func (p *TSSLServerSocket) IsListening() bool {
  68. return p.listener != nil
  69. }
  70. // Connects the socket, creating a new socket object if necessary.
  71. func (p *TSSLServerSocket) Open() error {
  72. if p.IsListening() {
  73. return NewTTransportException(ALREADY_OPEN, "Server socket already open")
  74. }
  75. if l, err := tls.Listen(p.addr.Network(), p.addr.String(), p.cfg); err != nil {
  76. return err
  77. } else {
  78. p.listener = l
  79. }
  80. return nil
  81. }
  82. func (p *TSSLServerSocket) Addr() net.Addr {
  83. return p.addr
  84. }
  85. func (p *TSSLServerSocket) Close() error {
  86. defer func() {
  87. p.listener = nil
  88. }()
  89. if p.IsListening() {
  90. return p.listener.Close()
  91. }
  92. return nil
  93. }
  94. func (p *TSSLServerSocket) Interrupt() error {
  95. p.interrupted = true
  96. return nil
  97. }