customHeader.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dynamicproxy
  2. import "strconv"
  3. /*
  4. CustomHeader.go
  5. This script handle parsing and injecting custom headers
  6. into the dpcore routing logic
  7. */
  8. //SplitInboundOutboundHeaders split user defined headers into upstream and downstream headers
  9. //return upstream header and downstream header key-value pairs
  10. //if the header is expected to be deleted, the value will be set to empty string
  11. func (ept *ProxyEndpoint) SplitInboundOutboundHeaders() ([][]string, [][]string) {
  12. if len(ept.UserDefinedHeaders) == 0 {
  13. //Early return if there are no defined headers
  14. return [][]string{}, [][]string{}
  15. }
  16. //Use pre-allocation for faster performance
  17. //Downstream +2 for Permission Policy and HSTS
  18. upstreamHeaders := make([][]string, len(ept.UserDefinedHeaders))
  19. downstreamHeaders := make([][]string, len(ept.UserDefinedHeaders)+2)
  20. upstreamHeaderCounter := 0
  21. downstreamHeaderCounter := 0
  22. //Sort the headers into upstream or downstream
  23. for _, customHeader := range ept.UserDefinedHeaders {
  24. thisHeaderSet := make([]string, 2)
  25. thisHeaderSet[0] = customHeader.Key
  26. thisHeaderSet[1] = customHeader.Value
  27. if customHeader.IsRemove {
  28. //Prevent invalid config
  29. thisHeaderSet[1] = ""
  30. }
  31. //Assign to slice
  32. if customHeader.Direction == HeaderDirection_ZoraxyToUpstream {
  33. upstreamHeaders[upstreamHeaderCounter] = thisHeaderSet
  34. upstreamHeaderCounter++
  35. } else if customHeader.Direction == HeaderDirection_ZoraxyToDownstream {
  36. downstreamHeaders[downstreamHeaderCounter] = thisHeaderSet
  37. downstreamHeaderCounter++
  38. }
  39. }
  40. //Check if the endpoint require HSTS headers
  41. if ept.HSTSMaxAge > 0 {
  42. downstreamHeaders[downstreamHeaderCounter] = []string{"Strict-Transport-Security", "max-age=" + strconv.Itoa(int(ept.HSTSMaxAge))}
  43. downstreamHeaderCounter++
  44. }
  45. //Check if the endpoint require Permission Policy
  46. if ept.EnablePermissionPolicyHeader && ept.PermissionPolicy != nil {
  47. downstreamHeaders[downstreamHeaderCounter] = ept.PermissionPolicy.ToKeyValueHeader()
  48. downstreamHeaderCounter++
  49. }
  50. return upstreamHeaders, downstreamHeaders
  51. }