rootRoute.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package dynamicproxy
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "log"
  6. "os"
  7. "imuslab.com/zoraxy/mod/utils"
  8. )
  9. /*
  10. rootRoute.go
  11. This script handle special case in routing where the root proxy
  12. entity is involved. This also include its setting object
  13. RootRoutingOptions
  14. */
  15. var rootConfigFilepath string = "conf/root_config.json"
  16. func loadRootRoutingOptionsFromFile() (*RootRoutingOptions, error) {
  17. if !utils.FileExists(rootConfigFilepath) {
  18. //Not found. Create a root option
  19. js, _ := json.MarshalIndent(RootRoutingOptions{}, "", " ")
  20. err := os.WriteFile(rootConfigFilepath, js, 0775)
  21. if err != nil {
  22. return nil, errors.New("Unable to write root config to file: " + err.Error())
  23. }
  24. }
  25. newRootOption := RootRoutingOptions{}
  26. rootOptionsBytes, err := os.ReadFile(rootConfigFilepath)
  27. if err != nil {
  28. log.Println("[Error] Unable to read root config file at " + rootConfigFilepath + ": " + err.Error())
  29. return nil, err
  30. }
  31. err = json.Unmarshal(rootOptionsBytes, &newRootOption)
  32. if err != nil {
  33. log.Println("[Error] Unable to parse root config file: " + err.Error())
  34. return nil, err
  35. }
  36. return &newRootOption, nil
  37. }
  38. // Save the new config to file. Note that this will not overwrite the runtime one
  39. func (opt *RootRoutingOptions) SaveToFile() error {
  40. js, _ := json.MarshalIndent(opt, "", " ")
  41. err := os.WriteFile(rootConfigFilepath, js, 0775)
  42. return err
  43. }