diff --git a/client/http.go b/client/http.go index b0b1e75..6ec432f 100644 --- a/client/http.go +++ b/client/http.go @@ -1,69 +1,41 @@ package client import ( - "crypto/tls" - "net" - "net/http" - "strings" - "time" - "code.gitea.io/bots-proto-go/ping/v1/pingv1connect" "code.gitea.io/bots-proto-go/runner/v1/runnerv1connect" - - "golang.org/x/net/http2" + "context" + "gitea.com/gitea/act_runner/core" + "github.com/bufbuild/connect-go" + "net/http" + "strings" ) // New returns a new runner client. -func New(endpoint string, opts ...Option) *HTTPClient { - cfg := &config{} - - // Loop through each option - for _, opt := range opts { - // Call the option giving the instantiated - opt.apply(cfg) - } - - 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) - }, - }, - } - } - - if cfg.skipVerify { - cfg.httpClient = &http.Client{ - CheckRedirect: func(*http.Request, []*http.Request) error { - return http.ErrUseLastResponse - }, - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, - }, - }, - } - } - +func New(endpoint string, uuid, token string, opts ...connect.ClientOption) *HTTPClient { baseURL := strings.TrimRight(endpoint, "/") + "/api/bots" + opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { + return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { + if uuid != "" { + req.Header().Set(core.UUIDHeader, uuid) + } + if token != "" { + req.Header().Set(core.TokenHeader, token) + } + return next(ctx, req) + } + }))) + return &HTTPClient{ PingServiceClient: pingv1connect.NewPingServiceClient( - cfg.httpClient, + http.DefaultClient, baseURL, - cfg.opts..., + opts..., ), RunnerServiceClient: runnerv1connect.NewRunnerServiceClient( - cfg.httpClient, + http.DefaultClient, baseURL, - cfg.opts..., + opts..., ), endpoint: endpoint, } diff --git a/client/options.go b/client/options.go deleted file mode 100644 index 92773b1..0000000 --- a/client/options.go +++ /dev/null @@ -1,97 +0,0 @@ -package client - -import ( - "context" - "net/http" - - "gitea.com/gitea/act_runner/core" - - "github.com/bufbuild/connect-go" -) - -type config struct { - httpClient *http.Client - skipVerify bool - opts []connect.ClientOption -} - -// An Option configures a mutex. -type Option interface { - apply(*config) -} - -// OptionFunc is a function that configure a value. -type OptionFunc func(*config) - -// Apply calls f(option) -func (f OptionFunc) apply(cfg *config) { - f(cfg) -} - -func WithSkipVerify(c bool) Option { - return OptionFunc(func(cfg *config) { - cfg.skipVerify = c - }) -} - -func WithClientOptions(opts ...connect.ClientOption) Option { - return OptionFunc(func(cfg *config) { - cfg.opts = append(cfg.opts, opts...) - }) -} - -// WithGRPC configures clients to use the HTTP/2 gRPC protocol. -func WithGRPC(c bool) Option { - return OptionFunc(func(cfg *config) { - if !c { - return - } - cfg.opts = append(cfg.opts, connect.WithGRPC()) - }) -} - -// WithGRPCWeb configures clients to use the gRPC-Web protocol. -func WithGRPCWeb(c bool) Option { - return OptionFunc(func(cfg *config) { - if !c { - return - } - cfg.opts = append(cfg.opts, connect.WithGRPCWeb()) - }) -} - -// WithUUIDHeader add runner uuid in header -func WithUUIDHeader(uuid string) Option { - return OptionFunc(func(cfg *config) { - if uuid == "" { - return - } - cfg.opts = append( - cfg.opts, - connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { - return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { - req.Header().Set(core.UUIDHeader, uuid) - return next(ctx, req) - } - })), - ) - }) -} - -// WithTokenHeader add runner token in header -func WithTokenHeader(token string) Option { - return OptionFunc(func(cfg *config) { - if token == "" { - return - } - cfg.opts = append( - cfg.opts, - connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc { - return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) { - req.Header().Set(core.TokenHeader, token) - return next(ctx, req) - } - })), - ) - }) -} diff --git a/cmd/daemon.go b/cmd/daemon.go index 875b804..c171363 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -40,11 +40,8 @@ func runDaemon(ctx context.Context, envFile string) func(cmd *cobra.Command, arg cli := client.New( cfg.Client.Address, - client.WithSkipVerify(cfg.Client.SkipVerify), - client.WithGRPC(cfg.Client.GRPC), - client.WithGRPCWeb(cfg.Client.GRPCWeb), - client.WithUUIDHeader(cfg.Runner.UUID), - client.WithTokenHeader(cfg.Runner.Token), + cfg.Runner.UUID, + cfg.Runner.Token, ) runner := &runtime.Runner{ diff --git a/cmd/register.go b/cmd/register.go index 3f08c61..efb0109 100644 --- a/cmd/register.go +++ b/cmd/register.go @@ -264,9 +264,7 @@ func doRegister(cfg *config.Config, inputs *registerInputs) error { // initial http client cli := client.New( inputs.InstanceAddr, - client.WithSkipVerify(cfg.Client.SkipVerify), - client.WithGRPC(cfg.Client.GRPC), - client.WithGRPCWeb(cfg.Client.GRPCWeb), + "", "", ) for { diff --git a/config/config.go b/config/config.go index f5bfa8d..f0fc2eb 100644 --- a/config/config.go +++ b/config/config.go @@ -23,10 +23,7 @@ type ( } Client struct { - Address string `ignored:"true"` - SkipVerify bool `envconfig:"GITEA_RPC_SKIP_VERIFY"` - GRPC bool `envconfig:"GITEA_RPC_GRPC" default:"true"` - GRPCWeb bool `envconfig:"GITEA_RPC_GRPC_WEB"` + Address string `ignored:"true"` } Runner struct { diff --git a/go.mod b/go.mod index 208816e..4cce600 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ require ( github.com/nektos/act v0.0.0 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 - golang.org/x/net v0.0.0-20220906165146-f3363e06e74c golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde google.golang.org/protobuf v1.28.1 ) @@ -69,9 +68,9 @@ require ( github.com/xeipuuv/gojsonschema v0.0.0-20180618132009-1d523034197f // indirect go.opencensus.io v0.23.0 // indirect golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect + golang.org/x/net v0.0.0-20220906165146-f3363e06e74c // indirect golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect - golang.org/x/text v0.3.7 // indirect gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 83007f2..e11cc89 100644 --- a/go.sum +++ b/go.sum @@ -974,7 +974,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=