https.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2016 fatedier, fatedier@gmail.com
  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 vhost
  15. import (
  16. "crypto/tls"
  17. "io"
  18. "net"
  19. "time"
  20. libnet "github.com/fatedier/golib/net"
  21. )
  22. type HTTPSMuxer struct {
  23. *Muxer
  24. }
  25. func NewHTTPSMuxer(listener net.Listener, timeout time.Duration) (*HTTPSMuxer, error) {
  26. mux, err := NewMuxer(listener, GetHTTPSHostname, timeout)
  27. mux.SetFailHookFunc(vhostFailed)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &HTTPSMuxer{mux}, err
  32. }
  33. func GetHTTPSHostname(c net.Conn) (_ net.Conn, _ map[string]string, err error) {
  34. reqInfoMap := make(map[string]string, 0)
  35. sc, rd := libnet.NewSharedConn(c)
  36. clientHello, err := readClientHello(rd)
  37. if err != nil {
  38. return nil, reqInfoMap, err
  39. }
  40. reqInfoMap["Host"] = clientHello.ServerName
  41. reqInfoMap["Scheme"] = "https"
  42. return sc, reqInfoMap, nil
  43. }
  44. func readClientHello(reader io.Reader) (*tls.ClientHelloInfo, error) {
  45. var hello *tls.ClientHelloInfo
  46. // Note that Handshake always fails because the readOnlyConn is not a real connection.
  47. // As long as the Client Hello is successfully read, the failure should only happen after GetConfigForClient is called,
  48. // so we only care about the error if hello was never set.
  49. err := tls.Server(readOnlyConn{reader: reader}, &tls.Config{
  50. GetConfigForClient: func(argHello *tls.ClientHelloInfo) (*tls.Config, error) {
  51. hello = &tls.ClientHelloInfo{}
  52. *hello = *argHello
  53. return nil, nil
  54. },
  55. }).Handshake()
  56. if hello == nil {
  57. return nil, err
  58. }
  59. return hello, nil
  60. }
  61. func vhostFailed(c net.Conn) {
  62. // Alert with alertUnrecognizedName
  63. _ = tls.Server(c, &tls.Config{}).Handshake()
  64. c.Close()
  65. }
  66. type readOnlyConn struct {
  67. reader io.Reader
  68. }
  69. func (conn readOnlyConn) Read(p []byte) (int, error) { return conn.reader.Read(p) }
  70. func (conn readOnlyConn) Write(_ []byte) (int, error) { return 0, io.ErrClosedPipe }
  71. func (conn readOnlyConn) Close() error { return nil }
  72. func (conn readOnlyConn) LocalAddr() net.Addr { return nil }
  73. func (conn readOnlyConn) RemoteAddr() net.Addr { return nil }
  74. func (conn readOnlyConn) SetDeadline(_ time.Time) error { return nil }
  75. func (conn readOnlyConn) SetReadDeadline(_ time.Time) error { return nil }
  76. func (conn readOnlyConn) SetWriteDeadline(_ time.Time) error { return nil }