1
0

v308.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package v308
  2. import (
  3. "encoding/json"
  4. "io"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. )
  9. /*
  10. v3.0.7 update to v3.0.8
  11. This update function
  12. */
  13. // Update proxy config files from v3.0.7 to v3.0.8
  14. func UpdateFrom307To308() error {
  15. //Load the configs
  16. oldConfigFiles, err := filepath.Glob("./conf/proxy/*.config")
  17. if err != nil {
  18. return err
  19. }
  20. //Backup all the files
  21. err = os.MkdirAll("./conf/proxy.old/", 0775)
  22. if err != nil {
  23. return err
  24. }
  25. for _, oldConfigFile := range oldConfigFiles {
  26. // Extract the file name from the path
  27. fileName := filepath.Base(oldConfigFile)
  28. // Construct the backup file path
  29. backupFile := filepath.Join("./conf/proxy.old/", fileName)
  30. // Copy the file to the backup directory
  31. err := copyFile(oldConfigFile, backupFile)
  32. if err != nil {
  33. return err
  34. }
  35. }
  36. //read the config into the old struct
  37. for _, oldConfigFile := range oldConfigFiles {
  38. configContent, err := os.ReadFile(oldConfigFile)
  39. if err != nil {
  40. log.Println("Unable to read config file "+filepath.Base(oldConfigFile), err.Error())
  41. continue
  42. }
  43. thisOldConfigStruct := v307ProxyEndpoint{}
  44. err = json.Unmarshal(configContent, &thisOldConfigStruct)
  45. if err != nil {
  46. log.Println("Unable to parse file "+filepath.Base(oldConfigFile), err.Error())
  47. continue
  48. }
  49. //Convert the old config to new config
  50. newProxyStructure := convertV307ToV308(thisOldConfigStruct)
  51. js, _ := json.MarshalIndent(newProxyStructure, "", " ")
  52. err = os.WriteFile(oldConfigFile, js, 0775)
  53. if err != nil {
  54. log.Println(err.Error())
  55. continue
  56. }
  57. }
  58. return nil
  59. }
  60. func convertV307ToV308(old v307ProxyEndpoint) v308ProxyEndpoint {
  61. // Create a new v308ProxyEndpoint instance
  62. matchingDomainsSlice := old.MatchingDomainAlias
  63. if matchingDomainsSlice == nil {
  64. matchingDomainsSlice = []string{}
  65. }
  66. newEndpoint := v308ProxyEndpoint{
  67. ProxyType: old.ProxyType,
  68. RootOrMatchingDomain: old.RootOrMatchingDomain,
  69. MatchingDomainAlias: matchingDomainsSlice,
  70. ActiveOrigins: []*v308Upstream{{ // Mapping Domain field to v308Upstream struct
  71. OriginIpOrDomain: old.Domain,
  72. RequireTLS: old.RequireTLS,
  73. SkipCertValidations: old.SkipCertValidations,
  74. SkipWebSocketOriginCheck: old.SkipWebSocketOriginCheck,
  75. Weight: 1,
  76. MaxConn: 0,
  77. }},
  78. InactiveOrigins: []*v308Upstream{},
  79. UseStickySession: false,
  80. Disabled: old.Disabled,
  81. BypassGlobalTLS: old.BypassGlobalTLS,
  82. VirtualDirectories: old.VirtualDirectories,
  83. UserDefinedHeaders: old.UserDefinedHeaders,
  84. HSTSMaxAge: old.HSTSMaxAge,
  85. EnablePermissionPolicyHeader: old.EnablePermissionPolicyHeader,
  86. PermissionPolicy: old.PermissionPolicy,
  87. RequireBasicAuth: old.RequireBasicAuth,
  88. BasicAuthCredentials: old.BasicAuthCredentials,
  89. BasicAuthExceptionRules: old.BasicAuthExceptionRules,
  90. RequireRateLimit: old.RequireRateLimit,
  91. RateLimit: old.RateLimit,
  92. AccessFilterUUID: old.AccessFilterUUID,
  93. DefaultSiteOption: old.DefaultSiteOption,
  94. DefaultSiteValue: old.DefaultSiteValue,
  95. }
  96. return newEndpoint
  97. }
  98. // Helper function to copy files
  99. func copyFile(src, dst string) error {
  100. sourceFile, err := os.Open(src)
  101. if err != nil {
  102. return err
  103. }
  104. defer sourceFile.Close()
  105. destinationFile, err := os.Create(dst)
  106. if err != nil {
  107. return err
  108. }
  109. defer destinationFile.Close()
  110. _, err = io.Copy(destinationFile, sourceFile)
  111. return err
  112. }