parse.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 Yunion
  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 dns
  15. import (
  16. "strings"
  17. "github.com/coredns/coredns/request"
  18. "github.com/miekg/dns"
  19. "yunion.io/x/pkg/tristate"
  20. "yunion.io/x/onecloud/pkg/compute/models"
  21. )
  22. type recordRequest struct {
  23. state request.Request
  24. // domainSegs []string
  25. srcProjectId string
  26. srcInCloud bool
  27. // network *models.SNetwork
  28. }
  29. func parseRequest(state request.Request) *recordRequest {
  30. // base, _ := dnsutil.TrimZone(state.Name(), state.Zone)
  31. // segs := dns.SplitDomainName(base)
  32. r := &recordRequest{
  33. state: state,
  34. // domainSegs: segs,
  35. }
  36. srcIP := r.SrcIP4()
  37. // NOTE the check on networks_tbl is a hack, we should be more specific
  38. // by querying only guest networks, host networks, and others like
  39. // loadbalancer network the to come.
  40. //
  41. // Order matters here, we want to find the srcIP project as accurately
  42. // as possible
  43. if guest := models.GuestnetworkManager.GetGuestByAddress(srcIP, ""); guest != nil {
  44. r.srcProjectId = guest.ProjectId
  45. r.srcInCloud = true
  46. } else if _, err := models.NetworkManager.GetOnPremiseNetworkOfIP(srcIP, "", tristate.None); err == nil {
  47. // r.srcProjectId = "" // no specific project
  48. r.srcInCloud = true
  49. }
  50. return r
  51. }
  52. func (r recordRequest) Name() string {
  53. //fullName, _ := dnsutil.TrimZone(r.state.Name(), "")
  54. name := r.state.Name()
  55. name = strings.TrimSuffix(name, ".")
  56. return name
  57. }
  58. func (r recordRequest) Zone() string {
  59. return r.state.Zone
  60. }
  61. func (r recordRequest) IsPlainName() bool {
  62. nl := dns.CountLabel(r.Name())
  63. return nl == 1
  64. }
  65. func (r recordRequest) QueryName() string {
  66. seps := strings.Split(r.Name(), ".")
  67. if len(seps) == 0 {
  68. return ""
  69. }
  70. return seps[0]
  71. }
  72. func (r recordRequest) Type() string {
  73. return DNSTypeMap[r.state.QType()]
  74. }
  75. func (r recordRequest) IsSRV() bool {
  76. return r.Type() == DNSTypeMap[dns.TypeSRV]
  77. }
  78. func (r recordRequest) SrcIP4() string {
  79. ip := r.state.IP()
  80. return ip
  81. }
  82. func (r recordRequest) ProjectId() string {
  83. return r.srcProjectId
  84. }
  85. func (r recordRequest) SrcInCloud() bool {
  86. return r.srcInCloud
  87. }
  88. /*type K8sQueryInfo struct {
  89. ServiceName string
  90. Namespace string
  91. }
  92. func (r recordRequest) GetK8sQueryInfo() K8sQueryInfo {
  93. parts := strings.SplitN(r.Name(), ".", 3)
  94. var svcName string
  95. var namespace string
  96. if len(parts) >= 2 {
  97. svcName = parts[0]
  98. namespace = parts[1]
  99. } else {
  100. svcName = parts[0]
  101. namespace = "default"
  102. }
  103. return K8sQueryInfo{
  104. ServiceName: svcName,
  105. Namespace: namespace,
  106. }
  107. }*/