https_serve.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package process
  2. import (
  3. "ehang.io/nps/core/action"
  4. "ehang.io/nps/lib/cert"
  5. "ehang.io/nps/lib/common"
  6. "ehang.io/nps/lib/enet"
  7. "ehang.io/nps/lib/logger"
  8. "github.com/pkg/errors"
  9. "go.uber.org/zap"
  10. )
  11. type HttpsServeProcess struct {
  12. CertFile string `json:"cert_file" required:"true" placeholder:"/var/cert/cert.pem" zh_name:"cert文件路径"`
  13. KeyFile string `json:"key_file" required:"true" placeholder:"/var/cert/key.pem" zh_name:"key文件路径"`
  14. HttpServeProcess
  15. }
  16. func (hsp *HttpsServeProcess) GetName() string {
  17. return "https_serve"
  18. }
  19. func (hsp *HttpsServeProcess) GetZhName() string {
  20. return "https服务"
  21. }
  22. func (hsp *HttpsServeProcess) Init(ac action.Action) error {
  23. hsp.tls = true
  24. err := hsp.HttpServeProcess.Init(ac)
  25. go hsp.httpServe.ServeTLS(hsp.CertFile, hsp.KeyFile)
  26. return err
  27. }
  28. func (hsp *HttpsServeProcess) ProcessConn(c enet.Conn) (bool, error) {
  29. clientMsg := cert.ClientHelloMsg{}
  30. b, err := c.AllBytes()
  31. if err != nil {
  32. return false, errors.Wrap(err, "get bytes")
  33. }
  34. if !clientMsg.Unmarshal(b[5:]) {
  35. return false, errors.New("can not unmarshal client hello message")
  36. }
  37. if common.HostContains(hsp.Host, clientMsg.GetServerName()) {
  38. logger.Debug("do https serve failed", zap.String("host", clientMsg.GetServerName()), zap.String("url", hsp.RouteUrl))
  39. if err := c.Reset(0); err != nil {
  40. return true, errors.Wrap(err, "reset reader connection")
  41. }
  42. return true, hsp.HttpServeProcess.ln.SendConn(c)
  43. }
  44. return false, nil
  45. }