2022-08-13 17:41:01 +03:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
v1 "gitea.com/gitea/proto/gen/proto/v1"
|
|
|
|
"gitea.com/gitea/proto/gen/proto/v1/v1connect"
|
|
|
|
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new runner client.
|
2022-08-14 05:59:09 +03:00
|
|
|
func New(endpoint, secret string, skipverify bool, opts ...Option) *HTTPClient {
|
2022-08-13 17:41:01 +03:00
|
|
|
client := &HTTPClient{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
Secret: secret,
|
|
|
|
SkipVerify: skipverify,
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:59:09 +03:00
|
|
|
// Loop through each option
|
|
|
|
for _, opt := range opts {
|
|
|
|
// Call the option giving the instantiated
|
|
|
|
opt.Apply(client)
|
|
|
|
}
|
|
|
|
|
2022-08-13 17:41:01 +03:00
|
|
|
client.Client = &http.Client{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Transport: &http2.Transport{
|
|
|
|
AllowHTTP: true,
|
|
|
|
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
|
|
|
|
return net.Dial(netw, addr)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if skipverify {
|
|
|
|
client.Client = &http.Client{
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
// An HTTPClient manages communication with the runner API.
|
|
|
|
type HTTPClient struct {
|
|
|
|
Client *http.Client
|
|
|
|
Endpoint string
|
|
|
|
Secret string
|
|
|
|
SkipVerify bool
|
2022-08-14 05:59:09 +03:00
|
|
|
|
|
|
|
opts []connect.ClientOption
|
2022-08-13 17:41:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ping sends a ping message to the server to test connectivity.
|
|
|
|
func (p *HTTPClient) Ping(ctx context.Context, machine string) error {
|
|
|
|
client := v1connect.NewPingServiceClient(
|
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
2022-08-14 05:59:09 +03:00
|
|
|
p.opts...,
|
2022-08-13 17:41:01 +03:00
|
|
|
)
|
|
|
|
req := connect.NewRequest(&v1.PingRequest{
|
|
|
|
Data: machine,
|
|
|
|
})
|
|
|
|
|
|
|
|
req.Header().Set("X-Gitea-Token", p.Secret)
|
|
|
|
|
|
|
|
_, err := client.Ping(ctx, req)
|
|
|
|
return err
|
|
|
|
}
|
2022-08-14 08:29:00 +03:00
|
|
|
|
|
|
|
// Ping sends a ping message to the server to test connectivity.
|
|
|
|
func (p *HTTPClient) Request(ctx context.Context) (*v1.Stage, error) {
|
|
|
|
client := v1connect.NewRunnerServiceClient(
|
|
|
|
p.Client,
|
|
|
|
p.Endpoint,
|
|
|
|
p.opts...,
|
|
|
|
)
|
|
|
|
req := connect.NewRequest(&v1.ConnectRequest{})
|
|
|
|
|
|
|
|
req.Header().Set("X-Gitea-Token", p.Secret)
|
|
|
|
|
|
|
|
res, err := client.Connect(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.Msg.Stage, err
|
|
|
|
}
|