rewrite.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package rewrite
  2. /*
  3. rewrite.go
  4. This script handle the rewrite logic for custom headers
  5. */
  6. import (
  7. "strconv"
  8. "imuslab.com/zoraxy/mod/dynamicproxy/permissionpolicy"
  9. )
  10. // SplitInboundOutboundHeaders split user defined headers into upstream and downstream headers
  11. // return upstream header and downstream header key-value pairs
  12. // if the header is expected to be deleted, the value will be set to empty string
  13. func SplitUpDownStreamHeaders(rewriteOptions *HeaderRewriteOptions) ([][]string, [][]string) {
  14. if len(rewriteOptions.UserDefinedHeaders) == 0 && rewriteOptions.HSTSMaxAge == 0 && !rewriteOptions.EnablePermissionPolicyHeader {
  15. //Early return if there are no defined headers
  16. return [][]string{}, [][]string{}
  17. }
  18. //Use pre-allocation for faster performance
  19. //Downstream +2 for Permission Policy and HSTS
  20. upstreamHeaders := make([][]string, len(rewriteOptions.UserDefinedHeaders))
  21. downstreamHeaders := make([][]string, len(rewriteOptions.UserDefinedHeaders)+2)
  22. upstreamHeaderCounter := 0
  23. downstreamHeaderCounter := 0
  24. //Sort the headers into upstream or downstream
  25. for _, customHeader := range rewriteOptions.UserDefinedHeaders {
  26. thisHeaderSet := make([]string, 2)
  27. thisHeaderSet[0] = customHeader.Key
  28. thisHeaderSet[1] = customHeader.Value
  29. if customHeader.IsRemove {
  30. //Prevent invalid config
  31. thisHeaderSet[1] = ""
  32. }
  33. //Assign to slice
  34. if customHeader.Direction == HeaderDirection_ZoraxyToUpstream {
  35. upstreamHeaders[upstreamHeaderCounter] = thisHeaderSet
  36. upstreamHeaderCounter++
  37. } else if customHeader.Direction == HeaderDirection_ZoraxyToDownstream {
  38. downstreamHeaders[downstreamHeaderCounter] = thisHeaderSet
  39. downstreamHeaderCounter++
  40. }
  41. }
  42. //Check if the endpoint require HSTS headers
  43. if rewriteOptions.HSTSMaxAge > 0 {
  44. if rewriteOptions.HSTSIncludeSubdomains {
  45. //Endpoint listening domain includes wildcards.
  46. downstreamHeaders[downstreamHeaderCounter] = []string{"Strict-Transport-Security", "max-age=" + strconv.Itoa(int(rewriteOptions.HSTSMaxAge)) + "; includeSubdomains"}
  47. } else {
  48. downstreamHeaders[downstreamHeaderCounter] = []string{"Strict-Transport-Security", "max-age=" + strconv.Itoa(int(rewriteOptions.HSTSMaxAge))}
  49. }
  50. downstreamHeaderCounter++
  51. }
  52. //Check if the endpoint require Permission Policy
  53. if rewriteOptions.EnablePermissionPolicyHeader {
  54. var usingPermissionPolicy *permissionpolicy.PermissionsPolicy
  55. if rewriteOptions.PermissionPolicy != nil {
  56. //Custom permission policy
  57. usingPermissionPolicy = rewriteOptions.PermissionPolicy
  58. } else {
  59. //Permission policy is enabled but not customized. Use default
  60. usingPermissionPolicy = permissionpolicy.GetDefaultPermissionPolicy()
  61. }
  62. downstreamHeaders[downstreamHeaderCounter] = usingPermissionPolicy.ToKeyValueHeader()
  63. downstreamHeaderCounter++
  64. }
  65. return upstreamHeaders, downstreamHeaders
  66. }