2022-08-13 09:41:01 -05:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-10-15 03:40:35 -05:00
|
|
|
"gitea.com/gitea/act_runner/core"
|
2022-08-17 01:25:14 -05:00
|
|
|
"gitea.com/gitea/proto-go/ping/v1/pingv1connect"
|
|
|
|
"gitea.com/gitea/proto-go/runner/v1/runnerv1connect"
|
2022-08-13 09:41:01 -05:00
|
|
|
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new runner client.
|
2022-09-25 05:54:00 -05:00
|
|
|
func New(endpoint, secret string, opts ...Option) *HTTPClient {
|
|
|
|
cfg := &config{}
|
2022-08-13 09:41:01 -05:00
|
|
|
|
2022-08-13 21:59:09 -05:00
|
|
|
// Loop through each option
|
|
|
|
for _, opt := range opts {
|
|
|
|
// Call the option giving the instantiated
|
2022-09-25 05:54:00 -05:00
|
|
|
opt.apply(cfg)
|
2022-08-13 21:59:09 -05:00
|
|
|
}
|
|
|
|
|
2022-09-25 05:54:00 -05:00
|
|
|
interceptor := connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
|
|
|
|
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
|
2022-10-15 03:40:35 -05:00
|
|
|
req.Header().Set(core.UUIDHeader, secret)
|
2022-09-25 05:54:00 -05:00
|
|
|
return next(ctx, req)
|
|
|
|
}
|
|
|
|
})
|
2022-10-13 21:06:52 -05:00
|
|
|
|
2022-09-25 05:54:00 -05:00
|
|
|
cfg.opts = append(cfg.opts, connect.WithInterceptors(interceptor))
|
|
|
|
|
|
|
|
if cfg.httpClient == nil {
|
|
|
|
cfg.httpClient = &http.Client{
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
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)
|
|
|
|
},
|
2022-08-13 09:41:01 -05:00
|
|
|
},
|
2022-09-25 05:54:00 -05:00
|
|
|
}
|
2022-08-13 09:41:01 -05:00
|
|
|
}
|
|
|
|
|
2022-09-25 05:54:00 -05:00
|
|
|
if cfg.skipVerify {
|
|
|
|
cfg.httpClient = &http.Client{
|
2022-08-13 09:41:01 -05:00
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 05:54:00 -05:00
|
|
|
|
|
|
|
return &HTTPClient{
|
|
|
|
PingServiceClient: pingv1connect.NewPingServiceClient(
|
|
|
|
cfg.httpClient,
|
|
|
|
endpoint,
|
|
|
|
cfg.opts...,
|
|
|
|
),
|
|
|
|
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
|
|
|
|
cfg.httpClient,
|
|
|
|
endpoint,
|
|
|
|
cfg.opts...,
|
|
|
|
),
|
|
|
|
}
|
2022-08-13 09:41:01 -05:00
|
|
|
}
|
|
|
|
|
2022-08-28 01:05:56 -05:00
|
|
|
var _ Client = (*HTTPClient)(nil)
|
|
|
|
|
2022-08-13 09:41:01 -05:00
|
|
|
// An HTTPClient manages communication with the runner API.
|
|
|
|
type HTTPClient struct {
|
2022-09-25 05:54:00 -05:00
|
|
|
pingv1connect.PingServiceClient
|
|
|
|
runnerv1connect.RunnerServiceClient
|
2022-09-03 07:57:32 -05:00
|
|
|
}
|