|
| 1 | +package buildout |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/murphysecurity/murphysec/infra/logctx" |
| 6 | + "go.uber.org/zap" |
| 7 | + "gopkg.in/ini.v1" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +func base(ctx context.Context, path string, result map[string]string) error { |
| 16 | + var log = logctx.Use(ctx).Sugar() |
| 17 | + e := findVersionsFile(ctx, path, result) |
| 18 | + if e == nil { |
| 19 | + return nil |
| 20 | + } |
| 21 | + pattern := filepath.Join(filepath.Dir(path), "*.cfg") |
| 22 | + files, err := filepath.Glob(pattern) |
| 23 | + if err != nil { |
| 24 | + log.Error("glob failed", zap.Error(e)) |
| 25 | + return err |
| 26 | + } |
| 27 | + for _, j := range files { |
| 28 | + if j == "buildout.cfg" { |
| 29 | + continue |
| 30 | + } |
| 31 | + if err := NoCurrentDirectoryCfg(ctx, filepath.Dir(j), j, result); err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + } |
| 35 | + return nil |
| 36 | +} |
| 37 | +func NoCurrentDirectoryCfg(ctx context.Context, NowPath string, path string, result map[string]string) error { |
| 38 | + var log = logctx.Use(ctx).Sugar() |
| 39 | + var e error |
| 40 | + var extends = "" |
| 41 | + if path == "" { |
| 42 | + return nil |
| 43 | + } |
| 44 | + if filepath.Dir(path) == NowPath { |
| 45 | + return nil |
| 46 | + } |
| 47 | + if strings.Contains(path, "http") { |
| 48 | + resp, err := http.Get(path) |
| 49 | + if err != nil { |
| 50 | + log.Error("http get failed", zap.Error(err)) |
| 51 | + return err |
| 52 | + } |
| 53 | + defer resp.Body.Close() |
| 54 | + by, err := io.ReadAll(resp.Body) |
| 55 | + if err != nil { |
| 56 | + log.Error("read body failed", zap.Error(err)) |
| 57 | + return err |
| 58 | + } |
| 59 | + extends, e = parseBuildoutBytes(ctx, by, result) |
| 60 | + if e != nil { |
| 61 | + return e |
| 62 | + } |
| 63 | + } else { |
| 64 | + // 如果不是远程连接 则尝试打开读取 |
| 65 | + extends, e = parseBuildoutCfgFile(ctx, path, result) |
| 66 | + if e != nil { |
| 67 | + return e |
| 68 | + } |
| 69 | + } |
| 70 | + if extends != "" { |
| 71 | + log.Debug("find extends", zap.String("path", extends)) |
| 72 | + return findVersionsFile(ctx, extends, result) |
| 73 | + } |
| 74 | + return NoCurrentDirectoryCfg(ctx, NowPath, extends, result) |
| 75 | +} |
| 76 | +func findVersionsFile(ctx context.Context, path string, result map[string]string) error { |
| 77 | + var log = logctx.Use(ctx).Sugar() |
| 78 | + var extends string |
| 79 | + var e error |
| 80 | + // 如果事远程链接 则读取 |
| 81 | + if strings.Contains(path, "http") { |
| 82 | + resp, err := http.Get(path) |
| 83 | + if err != nil { |
| 84 | + log.Error("http get failed", zap.Error(err)) |
| 85 | + return err |
| 86 | + } |
| 87 | + defer resp.Body.Close() |
| 88 | + by, err := io.ReadAll(resp.Body) |
| 89 | + if err != nil { |
| 90 | + log.Error("read body failed", zap.Error(err)) |
| 91 | + return err |
| 92 | + } |
| 93 | + extends, e = parseBuildoutBytes(ctx, by, result) |
| 94 | + if e != nil { |
| 95 | + return e |
| 96 | + } |
| 97 | + } else { |
| 98 | + // 如果不是远程连接 则尝试打开读取 |
| 99 | + extends, e = parseBuildoutCfgFile(ctx, path, result) |
| 100 | + if e != nil { |
| 101 | + return e |
| 102 | + } |
| 103 | + } |
| 104 | + if extends != "" { |
| 105 | + log.Debug("find extends", zap.String("path", extends)) |
| 106 | + return findVersionsFile(ctx, extends, result) |
| 107 | + } |
| 108 | + |
| 109 | + return nil |
| 110 | +} |
| 111 | +func parseBuildoutBytes(ctx context.Context, by []byte, result map[string]string) (string, error) { |
| 112 | + var log = logctx.Use(ctx).Sugar() |
| 113 | + cfg, err := ini.Load(by) |
| 114 | + if err != nil { |
| 115 | + log.Error("Fail to read file: ", zap.Error(err)) |
| 116 | + return "", err |
| 117 | + } |
| 118 | + for _, section := range cfg.Sections() { |
| 119 | + if section.Name() == "version" || section.Name() == "dependencies" || section.Name() == "versions" { |
| 120 | + for _, key := range section.Keys() { |
| 121 | + if key.Name() != "" && key.Value() != "" { |
| 122 | + result[key.Name()] = key.Value() |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + extends := cfg.Section("buildout").Key("extends").String() |
| 128 | + return extends, nil |
| 129 | +} |
| 130 | +func parseBuildoutCfgFile(ctx context.Context, path string, result map[string]string) (string, error) { |
| 131 | + var log = logctx.Use(ctx).Sugar() |
| 132 | + by, err := os.ReadFile(path) |
| 133 | + if err != nil { |
| 134 | + log.Error("read file failed", zap.Error(err)) |
| 135 | + return "", err |
| 136 | + } |
| 137 | + cfg, err := ini.Load(by) |
| 138 | + if err != nil { |
| 139 | + log.Error("Fail to read file: ", zap.Error(err)) |
| 140 | + return "", err |
| 141 | + } |
| 142 | + for _, section := range cfg.Sections() { |
| 143 | + if section.Name() == "version" || section.Name() == "dependencies" || section.Name() == "versions" { |
| 144 | + for _, key := range section.Keys() { |
| 145 | + if key.Name() != "" && key.Value() != "" { |
| 146 | + result[key.Name()] = key.Value() |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + extends := cfg.Section("buildout").Key("extends").String() |
| 152 | + if extends == "" { |
| 153 | + return "", nil |
| 154 | + } |
| 155 | + if filepath.IsAbs(extends) { |
| 156 | + return extends, nil |
| 157 | + } else { |
| 158 | + return filepath.Join(filepath.Dir(path), extends), nil |
| 159 | + } |
| 160 | +} |
0 commit comments