1
0

https.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. if err != nil {
  28. return nil, err
  29. }
  30. return &HTTPSMuxer{mux}, err
  31. }
  32. func GetHTTPSHostname(c net.Conn) (_ net.Conn, _ map[string]string, err error) {
  33. reqInfoMap := make(map[string]string, 0)
  34. sc, rd := libnet.NewSharedConn(c)
  35. clientHello, err := readClientHello(rd)
  36. if err != nil {
  37. return nil, reqInfoMap, err
  38. }
  39. reqInfoMap["Host"] = clientHello.ServerName
  40. reqInfoMap["Scheme"] = "https"
  41. return sc, reqInfoMap, nil
  42. }
  43. func readClientHello(reader io.Reader) (*tls.ClientHelloInfo, error) {
  44. var hello *tls.ClientHelloInfo
  45. // Note that Handshake always fails because the readOnlyConn is not a real connection.
  46. // As long as the Client Hello is successfully read, the failure should only happen after GetConfigForClient is called,
  47. // so we only care about the error if hello was never set.
  48. err := tls.Server(readOnlyConn{reader: reader}, &tls.Config{
  49. GetConfigForClient: func(argHello *tls.ClientHelloInfo) (*tls.Config, error) {
  50. hello = &tls.ClientHelloInfo{}
  51. *hello = *argHello
  52. return nil, nil
  53. },
  54. }).Handshake()
  55. if hello == nil {
  56. return nil, err
  57. }
  58. return hello, nil
  59. }
  60. type readOnlyConn struct {
  61. reader io.Reader
  62. }
  63. func (conn readOnlyConn) Read(p []byte) (int, error) { return conn.reader.Read(p) }
  64. func (conn readOnlyConn) Write(_ []byte) (int, error) { return 0, io.ErrClosedPipe }
  65. func (conn readOnlyConn) Close() error { return nil }
  66. func (conn readOnlyConn) LocalAddr() net.Addr { return nil }
  67. func (conn readOnlyConn) RemoteAddr() net.Addr { return nil }
  68. func (conn readOnlyConn) SetDeadline(_ time.Time) error { return nil }
  69. func (conn readOnlyConn) SetReadDeadline(_ time.Time) error { return nil }
  70. func (conn readOnlyConn) SetWriteDeadline(_ time.Time) error { return nil }