Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update http.RoundTripper used by Auth client #1251

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions desktop/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/getlantern/lantern-client/internalsdk/common"
proclient "github.com/getlantern/lantern-client/internalsdk/pro"
"github.com/getlantern/lantern-client/internalsdk/protos"
"github.com/getlantern/lantern-client/internalsdk/webclient"
)

var (
Expand Down Expand Up @@ -165,9 +164,7 @@ func (app *App) Run(ctx context.Context) {
userConfig := func() common.UserConfig {
return settings.UserConfig(app.Settings())
}
proClient := proclient.NewClient(fmt.Sprintf("https://%s", common.ProAPIHost), &webclient.Opts{
UserConfig: userConfig,
})
proClient := proclient.NewClient(fmt.Sprintf("https://%s", common.ProAPIHost), userConfig)
authClient := auth.NewClient(fmt.Sprintf("https://%s", common.DFBaseUrl), userConfig)

app.mu.Lock()
Expand Down
40 changes: 21 additions & 19 deletions internalsdk/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package auth
import (
"context"

"fmt"
"net/http"
"strings"
"time"
Expand All @@ -12,7 +11,6 @@ import (
"github.com/getlantern/golog"

"github.com/getlantern/lantern-client/internalsdk/common"
"github.com/getlantern/lantern-client/internalsdk/pro"
"github.com/getlantern/lantern-client/internalsdk/protos"
"github.com/getlantern/lantern-client/internalsdk/webclient"

Expand Down Expand Up @@ -51,35 +49,39 @@ type AuthClient interface {
Healthz(ctx context.Context) (bool, error)
}

// NewClient creates a new instance of AuthClient
func NewClient(baseURL string, userConfig func() common.UserConfig) AuthClient {
// The default http.RoundTripper is ChainedNonPersistent which proxies requests through chained servers
// and does not use keep alive connections. Since no root CA is specified, we do not need to check for an error.

var httpClient *http.Client
type serialTransport []http.RoundTripper

if baseURL == fmt.Sprintf("https://%s", common.APIBaseUrl) {
log.Debug("using proxied.Fronted")
//this is ios version
httpClient = &http.Client{
Transport: proxied.Fronted("auth_fronted_roundtrip", 30*time.Second),
func (tr serialTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
for _, rt := range tr {
resp, err = rt.RoundTrip(req)
if err == nil {
return
}
} else {
log.Debug("using proxied.ChainedNonPersistent")
rt, _ := proxied.ChainedNonPersistent("")
httpClient = pro.NewHTTPClient(rt, 30*time.Second)
log.Debugf("Error roundtripping request to %v, continuing to next transport", req.URL)
}
log.Errorf("Unable to roundtrip request to %v, out of transports", req.URL)
return
}

// NewClient creates a new instance of AuthClient
func NewClient(baseURL string, userConfig func() common.UserConfig) AuthClient {
chained, err := proxied.ChainedNonPersistent("")
if err != nil {
log.Fatal(err)
}
frt := proxied.Fronted("auth_fronted_roundtrip", 10*time.Second)
rc := webclient.NewRESTClient(&webclient.Opts{
BaseURL: baseURL,
OnBeforeRequest: func(client *resty.Client, req *http.Request) error {
prepareUserRequest(req, userConfig())
return nil
},
HttpClient: httpClient,
HttpClient: &http.Client{
Transport: serialTransport{chained, frt},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This updates the auth client to use an http.Client that first attempts to connect via chained proxies and then falls back to using domain fronting with the custom name above (@jigar-f @hwh33 I'm not sure if we can just use proxied.ChainedThenFronted() here?).
Please let me know if this is correct otherwise feel free to change it to the one we should be using

Copy link
Contributor

@jigar-f jigar-f Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember some work needs to be done on auth API access not sure if that was done or not, Let's wait for @hwh33 on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I would just use ChainedThenFronted

Timeout: 30 * time.Second,
},
UserConfig: userConfig,
})

return &authClient{rc}
}

Expand Down
25 changes: 0 additions & 25 deletions internalsdk/pro/http.go

This file was deleted.

31 changes: 16 additions & 15 deletions internalsdk/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/getlantern/errors"
"github.com/getlantern/flashlight/v7/proxied"
Expand Down Expand Up @@ -55,22 +56,22 @@ type ProClient interface {
}

// NewClient creates a new instance of ProClient
func NewClient(baseURL string, opts *webclient.Opts) ProClient {
if opts.HttpClient == nil {
// The default http.RoundTripper used by the ProClient is ParallelForIdempotent which
// attempts to send requests through both chained and direct fronted routes in parallel
// for HEAD and GET requests and ChainedThenFronted for all others.
opts.HttpClient = NewHTTPClient(proxied.ParallelForIdempotent(), opts.Timeout)
}
if opts.OnBeforeRequest == nil {
opts.OnBeforeRequest = func(client *resty.Client, req *http.Request) error {
prepareProRequest(req, common.ProAPIHost, opts.UserConfig())
return nil
}
}
func NewClient(baseURL string, userConfig func() common.UserConfig) ProClient {
return &proClient{
userConfig: opts.UserConfig,
RESTClient: webclient.NewRESTClient(opts),
userConfig: userConfig,
RESTClient: webclient.NewRESTClient(&webclient.Opts{
// The default http.RoundTripper used by the ProClient is ParallelForIdempotent which
// attempts to send requests through both chained and direct fronted routes in parallel
// for HEAD and GET requests and ChainedThenFronted for all others.
HttpClient: &http.Client{
Transport: proxied.ParallelForIdempotent(),
Timeout: 30 * time.Second,
},
OnBeforeRequest: func(client *resty.Client, req *http.Request) error {
prepareProRequest(req, common.ProAPIHost, userConfig())
return nil
},
}),
}
}

Expand Down
115 changes: 0 additions & 115 deletions internalsdk/pro/proxy.go

This file was deleted.

64 changes: 0 additions & 64 deletions internalsdk/pro/proxy_test.go

This file was deleted.

Loading
Loading