net.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. // for HTTPSServer.ServeTLS:
  20. /*
  21. Copyright (c) 2012 The Go Authors. All rights reserved.
  22. Redistribution and use in source and binary forms, with or without
  23. modification, are permitted provided that the following conditions are
  24. met:
  25. * Redistributions of source code must retain the above copyright
  26. notice, this list of conditions and the following disclaimer.
  27. * Redistributions in binary form must reproduce the above
  28. copyright notice, this list of conditions and the following disclaimer
  29. in the documentation and/or other materials provided with the
  30. distribution.
  31. * Neither the name of Google Inc. nor the names of its
  32. contributors may be used to endorse or promote products derived from
  33. this software without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package server
  47. import (
  48. "net"
  49. "net/http"
  50. tris "github.com/Psiphon-Labs/tls-tris"
  51. )
  52. // HTTPSServer is a wrapper around http.Server which adds the
  53. // ServeTLS function.
  54. type HTTPSServer struct {
  55. *http.Server
  56. }
  57. // ServeTLS is similar to http.Serve, but uses TLS.
  58. //
  59. // The http package has both ListenAndServe and ListenAndServeTLS higher-
  60. // level interfaces, but only Serve (not TLS) offers a lower-level interface that
  61. // allows the caller to keep a refererence to the Listener, allowing for external
  62. // shutdown. ListenAndServeTLS also requires the TLS cert and key to be in files
  63. // and we avoid that here.
  64. //
  65. // Note that the http.Server.TLSConfig field is ignored and the tris.Config
  66. // parameter is used intead.
  67. func (server *HTTPSServer) ServeTLS(listener net.Listener, config *tris.Config) error {
  68. tlsListener := tris.NewListener(listener, config)
  69. return server.Serve(tlsListener)
  70. }