http.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2017. See AUTHORS.
  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. package openssl
  15. import (
  16. "net/http"
  17. )
  18. // ListenAndServeTLS will take an http.Handler and serve it using OpenSSL over
  19. // the given tcp address, configured to use the provided cert and key files.
  20. func ListenAndServeTLS(addr string, cert_file string, key_file string,
  21. handler http.Handler) error {
  22. return ServerListenAndServeTLS(
  23. &http.Server{Addr: addr, Handler: handler}, cert_file, key_file)
  24. }
  25. // ServerListenAndServeTLS will take an http.Server and serve it using OpenSSL
  26. // configured to use the provided cert and key files.
  27. func ServerListenAndServeTLS(srv *http.Server,
  28. cert_file, key_file string) error {
  29. addr := srv.Addr
  30. if addr == "" {
  31. addr = ":https"
  32. }
  33. ctx, err := NewCtxFromFiles(cert_file, key_file)
  34. if err != nil {
  35. return err
  36. }
  37. l, err := Listen("tcp", addr, ctx)
  38. if err != nil {
  39. return err
  40. }
  41. return srv.Serve(l)
  42. }
  43. // TODO: http client integration
  44. // holy crap, getting this integrated nicely with the Go stdlib HTTP client
  45. // stack so that it does proxying, connection pooling, and most importantly
  46. // hostname verification is really hard. So much stuff is hardcoded to just use
  47. // the built-in TLS lib. I think to get this to work either some crazy
  48. // hacktackery beyond me, an almost straight up fork of the HTTP client, or
  49. // serious stdlib internal refactoring is necessary.
  50. // even more so, good luck getting openssl to use the operating system default
  51. // root certificates if the user doesn't provide any. sadlol
  52. // NOTE: if you're going to try and write your own round tripper, at least use
  53. // openssl.Dial, or equivalent logic