1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// Copyright 2024 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package api
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
type Client struct {
baseURL string
}
func NewClient(url string) *Client {
return &Client{baseURL: strings.TrimRight(url, "/")}
}
func (client Client) GetSessionSeries(ctx context.Context, sessionID string) (*Series, error) {
return getJSON[Series](ctx, client.baseURL+"/sessions/"+sessionID+"/series")
}
func (client Client) GetSeries(ctx context.Context, seriesID string) (*Series, error) {
return getJSON[Series](ctx, client.baseURL+"/series/"+seriesID)
}
type UploadTriageResultReq struct {
SkipReason string `json:"skip_reason"`
Log []byte `json:"log"`
}
func (client Client) UploadTriageResult(ctx context.Context, sessionID string, req *UploadTriageResultReq) error {
_, err := postJSON[UploadTriageResultReq, any](ctx, client.baseURL+"/sessions/"+sessionID+"/triage_result", req)
return err
}
type TreesResp struct {
Trees []*Tree `json:"trees"`
FuzzConfigs []*FuzzConfig `json:"fuzz_configs"`
}
func (client Client) GetTrees(ctx context.Context) (*TreesResp, error) {
return getJSON[TreesResp](ctx, client.baseURL+"/trees")
}
type LastBuildReq struct {
Arch string
ConfigName string
TreeName string
Commit string
Status string
}
const BuildSuccess = "success"
func (client Client) LastBuild(ctx context.Context, req *LastBuildReq) (*Build, error) {
return postJSON[LastBuildReq, Build](ctx, client.baseURL+"/builds/last", req)
}
type UploadBuildReq struct {
Build
Config []byte `json:"config"`
Log []byte `json:"log"`
}
type UploadBuildResp struct {
ID string
}
func (client Client) UploadBuild(ctx context.Context, req *UploadBuildReq) (*UploadBuildResp, error) {
return postJSON[UploadBuildReq, UploadBuildResp](ctx, client.baseURL+"/builds/upload", req)
}
func (client Client) UploadTestResult(ctx context.Context, req *TestResult) error {
_, err := postJSON[TestResult, any](ctx, client.baseURL+"/tests/upload", req)
return err
}
func (client Client) UploadTestArtifacts(ctx context.Context, sessionID, testName string,
tarGzContent io.Reader) error {
v := url.Values{}
v.Add("session", sessionID)
v.Add("test", testName)
url := client.baseURL + "/tests/upload_artifacts?" + v.Encode()
_, err := postMultiPartFile[any](ctx, url, tarGzContent)
return err
}
func (client Client) UploadFinding(ctx context.Context, req *NewFinding) error {
_, err := postJSON[NewFinding, any](ctx, client.baseURL+"/findings/upload", req)
return err
}
type UploadSeriesResp struct {
ID string `json:"id"`
Saved bool `json:"saved"`
}
func (client Client) UploadSeries(ctx context.Context, req *Series) (*UploadSeriesResp, error) {
return postJSON[Series, UploadSeriesResp](ctx, client.baseURL+"/series/upload", req)
}
type UploadSessionResp struct {
ID string `json:"id"`
}
func (client Client) UploadSession(ctx context.Context, req *NewSession) (*UploadSessionResp, error) {
return postJSON[NewSession, UploadSessionResp](ctx, client.baseURL+"/sessions/upload", req)
}
const requestTimeout = time.Minute
func finishRequest[Resp any](httpReq *http.Request) (*Resp, error) {
client := &http.Client{
Timeout: requestTimeout,
}
resp, err := client.Do(httpReq)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := ensure200(resp); err != nil {
return nil, err
}
var data Resp
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}
return &data, nil
}
func ensure200(resp *http.Response) error {
if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body)
if len(bodyBytes) > 128 {
bodyBytes = bodyBytes[:128]
}
return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, bodyBytes)
}
return nil
}
|