v315.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package v315
  2. import (
  3. "encoding/json"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "imuslab.com/zoraxy/mod/update/updateutil"
  8. )
  9. func UpdateFrom314To315() error {
  10. //Load the configs
  11. oldConfigFiles, err := filepath.Glob("./conf/proxy/*.config")
  12. if err != nil {
  13. return err
  14. }
  15. //Backup all the files
  16. err = os.MkdirAll("./conf/proxy-314.old/", 0775)
  17. if err != nil {
  18. return err
  19. }
  20. for _, oldConfigFile := range oldConfigFiles {
  21. // Extract the file name from the path
  22. fileName := filepath.Base(oldConfigFile)
  23. // Construct the backup file path
  24. backupFile := filepath.Join("./conf/proxy-314.old/", fileName)
  25. // Copy the file to the backup directory
  26. err := updateutil.CopyFile(oldConfigFile, backupFile)
  27. if err != nil {
  28. return err
  29. }
  30. }
  31. //read the config into the old struct
  32. for _, oldConfigFile := range oldConfigFiles {
  33. configContent, err := os.ReadFile(oldConfigFile)
  34. if err != nil {
  35. log.Println("Unable to read config file "+filepath.Base(oldConfigFile), err.Error())
  36. continue
  37. }
  38. thisOldConfigStruct := v314ProxyEndpoint{}
  39. err = json.Unmarshal(configContent, &thisOldConfigStruct)
  40. if err != nil {
  41. log.Println("Unable to parse file "+filepath.Base(oldConfigFile), err.Error())
  42. continue
  43. }
  44. //Convert the old struct to the new struct
  45. thisNewConfigStruct := convertV314ToV315(thisOldConfigStruct)
  46. //Write the new config to file
  47. newConfigContent, err := json.MarshalIndent(thisNewConfigStruct, "", " ")
  48. if err != nil {
  49. log.Println("Unable to marshal new config "+filepath.Base(oldConfigFile), err.Error())
  50. continue
  51. }
  52. err = os.WriteFile(oldConfigFile, newConfigContent, 0664)
  53. if err != nil {
  54. log.Println("Unable to write new config "+filepath.Base(oldConfigFile), err.Error())
  55. continue
  56. }
  57. }
  58. return nil
  59. }
  60. func convertV314ToV315(thisOldConfigStruct v314ProxyEndpoint) v315ProxyEndpoint {
  61. //Move old header and auth configs into struct
  62. newHeaderRewriteRules := HeaderRewriteRules{
  63. UserDefinedHeaders: thisOldConfigStruct.UserDefinedHeaders,
  64. RequestHostOverwrite: thisOldConfigStruct.RequestHostOverwrite,
  65. HSTSMaxAge: thisOldConfigStruct.HSTSMaxAge,
  66. EnablePermissionPolicyHeader: thisOldConfigStruct.EnablePermissionPolicyHeader,
  67. PermissionPolicy: thisOldConfigStruct.PermissionPolicy,
  68. DisableHopByHopHeaderRemoval: thisOldConfigStruct.DisableHopByHopHeaderRemoval,
  69. }
  70. newAuthenticationProvider := AuthenticationProvider{
  71. RequireBasicAuth: thisOldConfigStruct.RequireBasicAuth,
  72. BasicAuthCredentials: thisOldConfigStruct.BasicAuthCredentials,
  73. BasicAuthExceptionRules: thisOldConfigStruct.BasicAuthExceptionRules,
  74. }
  75. //Convert proxy type int to enum
  76. var newConfigProxyType ProxyType
  77. if thisOldConfigStruct.ProxyType == 0 {
  78. newConfigProxyType = ProxyTypeRoot
  79. } else if thisOldConfigStruct.ProxyType == 1 {
  80. newConfigProxyType = ProxyTypeHost
  81. } else if thisOldConfigStruct.ProxyType == 2 {
  82. newConfigProxyType = ProxyTypeVdir
  83. }
  84. //Update the config struct
  85. thisNewConfigStruct := v315ProxyEndpoint{
  86. ProxyType: newConfigProxyType,
  87. RootOrMatchingDomain: thisOldConfigStruct.RootOrMatchingDomain,
  88. MatchingDomainAlias: thisOldConfigStruct.MatchingDomainAlias,
  89. ActiveOrigins: thisOldConfigStruct.ActiveOrigins,
  90. InactiveOrigins: thisOldConfigStruct.InactiveOrigins,
  91. UseStickySession: thisOldConfigStruct.UseStickySession,
  92. UseActiveLoadBalance: thisOldConfigStruct.UseActiveLoadBalance,
  93. Disabled: thisOldConfigStruct.Disabled,
  94. BypassGlobalTLS: thisOldConfigStruct.BypassGlobalTLS,
  95. VirtualDirectories: thisOldConfigStruct.VirtualDirectories,
  96. RequireRateLimit: thisOldConfigStruct.RequireRateLimit,
  97. RateLimit: thisOldConfigStruct.RateLimit,
  98. AccessFilterUUID: thisOldConfigStruct.AccessFilterUUID,
  99. DefaultSiteOption: thisOldConfigStruct.DefaultSiteOption,
  100. DefaultSiteValue: thisOldConfigStruct.DefaultSiteValue,
  101. //Append the new struct into the new config
  102. HeaderRewriteRules: &newHeaderRewriteRules,
  103. AuthenticationProvider: &newAuthenticationProvider,
  104. }
  105. return thisNewConfigStruct
  106. }