|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "dbm-services/common/reverse-api/config" |
| 6 | + "encoding/json" |
| 7 | + errs "errors" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "strconv" |
| 14 | + |
| 15 | + "github.com/pkg/errors" |
| 16 | +) |
| 17 | + |
| 18 | +func ReverseCall(api config.ReverseApiName, bkCloudId int, ports ...int) (data []byte, err error) { |
| 19 | + addrs, err := readNginxProxyAddrs() |
| 20 | + if err != nil { |
| 21 | + return nil, errors.Wrap(err, "failed to read nginx proxy addresses") |
| 22 | + } |
| 23 | + |
| 24 | + var errCollect []error |
| 25 | + for _, addr := range addrs { |
| 26 | + apiPath, _ := url.JoinPath(config.ReverseApiBase, api.String(), "/") |
| 27 | + ep := url.URL{ |
| 28 | + Scheme: "http", |
| 29 | + Host: addr, |
| 30 | + Path: apiPath, |
| 31 | + } |
| 32 | + |
| 33 | + req, err := http.NewRequest(http.MethodGet, ep.String(), nil) |
| 34 | + if err != nil { |
| 35 | + return nil, errors.Wrap(err, "failed to create request") |
| 36 | + } |
| 37 | + |
| 38 | + q := req.URL.Query() |
| 39 | + q.Add("bk_cloud_id", strconv.Itoa(bkCloudId)) |
| 40 | + for _, port := range ports { |
| 41 | + q.Add("port", strconv.Itoa(port)) |
| 42 | + } |
| 43 | + req.URL.RawQuery = q.Encode() |
| 44 | + |
| 45 | + data, err = do(req) |
| 46 | + if err == nil { |
| 47 | + return data, nil |
| 48 | + } |
| 49 | + errCollect = append(errCollect, err) |
| 50 | + } |
| 51 | + |
| 52 | + return nil, errs.Join(errCollect...) |
| 53 | +} |
| 54 | + |
| 55 | +func do(request *http.Request) (data []byte, err error) { |
| 56 | + resp, err := http.DefaultClient.Do(request) |
| 57 | + if err != nil { |
| 58 | + return nil, errors.Wrap(err, "failed to send request") |
| 59 | + } |
| 60 | + defer func() { |
| 61 | + _ = resp.Body.Close() |
| 62 | + }() |
| 63 | + |
| 64 | + b, err := io.ReadAll(resp.Body) |
| 65 | + if err != nil { |
| 66 | + return nil, errors.Wrap(err, "failed to read response body") |
| 67 | + } |
| 68 | + |
| 69 | + if resp.StatusCode != http.StatusOK { |
| 70 | + return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(b)) |
| 71 | + } |
| 72 | + |
| 73 | + var r apiResponse |
| 74 | + err = json.Unmarshal(b, &r) |
| 75 | + if err != nil { |
| 76 | + return nil, errors.Wrap(err, "failed to unmarshal response body") |
| 77 | + } |
| 78 | + |
| 79 | + if !r.Result { |
| 80 | + return nil, errors.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, r.Errors) |
| 81 | + } |
| 82 | + |
| 83 | + return r.Data, nil |
| 84 | +} |
| 85 | + |
| 86 | +func readNginxProxyAddrs() (addrs []string, err error) { |
| 87 | + f, err := os.Open(filepath.Join(config.CommonConfigDir, config.NginxProxyAddrsFileName)) |
| 88 | + if err != nil { |
| 89 | + return nil, errors.Wrap(err, "failed to open nginx proxy addrs") |
| 90 | + } |
| 91 | + defer func() { |
| 92 | + _ = f.Close() |
| 93 | + }() |
| 94 | + |
| 95 | + scanner := bufio.NewScanner(f) |
| 96 | + for scanner.Scan() { |
| 97 | + addrs = append(addrs, scanner.Text()) |
| 98 | + } |
| 99 | + if err := scanner.Err(); err != nil { |
| 100 | + return nil, errors.Wrap(err, "failed to read nginx proxy addrs") |
| 101 | + } |
| 102 | + return addrs, nil |
| 103 | +} |
0 commit comments