1
0

https.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2025 The frp 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 group
  15. import (
  16. "context"
  17. "net"
  18. "sync"
  19. gerr "github.com/fatedier/golib/errors"
  20. "github.com/fatedier/frp/pkg/util/vhost"
  21. )
  22. type HTTPSGroupController struct {
  23. groups map[string]*HTTPSGroup
  24. httpsMuxer *vhost.HTTPSMuxer
  25. mu sync.Mutex
  26. }
  27. func NewHTTPSGroupController(httpsMuxer *vhost.HTTPSMuxer) *HTTPSGroupController {
  28. return &HTTPSGroupController{
  29. groups: make(map[string]*HTTPSGroup),
  30. httpsMuxer: httpsMuxer,
  31. }
  32. }
  33. func (ctl *HTTPSGroupController) Listen(
  34. ctx context.Context,
  35. group, groupKey string,
  36. routeConfig vhost.RouteConfig,
  37. ) (l net.Listener, err error) {
  38. indexKey := group
  39. ctl.mu.Lock()
  40. g, ok := ctl.groups[indexKey]
  41. if !ok {
  42. g = NewHTTPSGroup(ctl)
  43. ctl.groups[indexKey] = g
  44. }
  45. ctl.mu.Unlock()
  46. return g.Listen(ctx, group, groupKey, routeConfig)
  47. }
  48. func (ctl *HTTPSGroupController) RemoveGroup(group string) {
  49. ctl.mu.Lock()
  50. defer ctl.mu.Unlock()
  51. delete(ctl.groups, group)
  52. }
  53. type HTTPSGroup struct {
  54. group string
  55. groupKey string
  56. domain string
  57. acceptCh chan net.Conn
  58. httpsLn *vhost.Listener
  59. lns []*HTTPSGroupListener
  60. ctl *HTTPSGroupController
  61. mu sync.Mutex
  62. }
  63. func NewHTTPSGroup(ctl *HTTPSGroupController) *HTTPSGroup {
  64. return &HTTPSGroup{
  65. lns: make([]*HTTPSGroupListener, 0),
  66. ctl: ctl,
  67. acceptCh: make(chan net.Conn),
  68. }
  69. }
  70. func (g *HTTPSGroup) Listen(
  71. ctx context.Context,
  72. group, groupKey string,
  73. routeConfig vhost.RouteConfig,
  74. ) (ln *HTTPSGroupListener, err error) {
  75. g.mu.Lock()
  76. defer g.mu.Unlock()
  77. if len(g.lns) == 0 {
  78. // the first listener, listen on the real address
  79. httpsLn, errRet := g.ctl.httpsMuxer.Listen(ctx, &routeConfig)
  80. if errRet != nil {
  81. return nil, errRet
  82. }
  83. ln = newHTTPSGroupListener(group, g, httpsLn.Addr())
  84. g.group = group
  85. g.groupKey = groupKey
  86. g.domain = routeConfig.Domain
  87. g.httpsLn = httpsLn
  88. g.lns = append(g.lns, ln)
  89. go g.worker()
  90. } else {
  91. // route config in the same group must be equal
  92. if g.group != group || g.domain != routeConfig.Domain {
  93. return nil, ErrGroupParamsInvalid
  94. }
  95. if g.groupKey != groupKey {
  96. return nil, ErrGroupAuthFailed
  97. }
  98. ln = newHTTPSGroupListener(group, g, g.lns[0].Addr())
  99. g.lns = append(g.lns, ln)
  100. }
  101. return
  102. }
  103. func (g *HTTPSGroup) worker() {
  104. for {
  105. c, err := g.httpsLn.Accept()
  106. if err != nil {
  107. return
  108. }
  109. err = gerr.PanicToError(func() {
  110. g.acceptCh <- c
  111. })
  112. if err != nil {
  113. return
  114. }
  115. }
  116. }
  117. func (g *HTTPSGroup) Accept() <-chan net.Conn {
  118. return g.acceptCh
  119. }
  120. func (g *HTTPSGroup) CloseListener(ln *HTTPSGroupListener) {
  121. g.mu.Lock()
  122. defer g.mu.Unlock()
  123. for i, tmpLn := range g.lns {
  124. if tmpLn == ln {
  125. g.lns = append(g.lns[:i], g.lns[i+1:]...)
  126. break
  127. }
  128. }
  129. if len(g.lns) == 0 {
  130. close(g.acceptCh)
  131. if g.httpsLn != nil {
  132. g.httpsLn.Close()
  133. }
  134. g.ctl.RemoveGroup(g.group)
  135. }
  136. }
  137. type HTTPSGroupListener struct {
  138. groupName string
  139. group *HTTPSGroup
  140. addr net.Addr
  141. closeCh chan struct{}
  142. }
  143. func newHTTPSGroupListener(name string, group *HTTPSGroup, addr net.Addr) *HTTPSGroupListener {
  144. return &HTTPSGroupListener{
  145. groupName: name,
  146. group: group,
  147. addr: addr,
  148. closeCh: make(chan struct{}),
  149. }
  150. }
  151. func (ln *HTTPSGroupListener) Accept() (c net.Conn, err error) {
  152. var ok bool
  153. select {
  154. case <-ln.closeCh:
  155. return nil, ErrListenerClosed
  156. case c, ok = <-ln.group.Accept():
  157. if !ok {
  158. return nil, ErrListenerClosed
  159. }
  160. return c, nil
  161. }
  162. }
  163. func (ln *HTTPSGroupListener) Addr() net.Addr {
  164. return ln.addr
  165. }
  166. func (ln *HTTPSGroupListener) Close() (err error) {
  167. close(ln.closeCh)
  168. // remove self from HTTPSGroup
  169. ln.group.CloseListener(ln)
  170. return
  171. }