Skip to content

Commit

Permalink
update http.RoundTripper used by Auth client (#1251)
Browse files Browse the repository at this point in the history
* update http.RoundTripper used by Auth client

* remove unused code

* clean-ups

* use custom http.RoundTripper for auth client

* update createProClient

* Add comment

* use ChainedThenFronted
  • Loading branch information
atavism authored Dec 5, 2024
1 parent c63862b commit fc3a90b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 272 deletions.
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 @@ -164,9 +163,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
27 changes: 6 additions & 21 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 @@ -53,33 +51,20 @@ type AuthClient interface {

// 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

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),
}
} else {
log.Debug("using proxied.ChainedNonPersistent")
rt, _ := proxied.ChainedNonPersistent("")
httpClient = pro.NewHTTPClient(rt, 30*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,
// The Auth client uses an http.Client that first attempts to connect via chained proxies
// and then falls back to using domain fronting with the custom op name above
HttpClient: &http.Client{
Transport: proxied.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: 15 additions & 16 deletions internalsdk/pro/pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,23 @@ 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,
userConfig: userConfig,
backoffRunner: &backoffRunner{},
RESTClient: webclient.NewRESTClient(opts),
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.

45 changes: 18 additions & 27 deletions internalsdk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"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"
"github.com/getlantern/pathdb"
"golang.org/x/crypto/pbkdf2"
"google.golang.org/protobuf/proto"
Expand All @@ -27,32 +26,24 @@ import (

// createProClient creates a new instance of ProClient with the given client session information
func createProClient(session Session, platform string) pro.ProClient {
dialTimeout := 30 * time.Second
if platform == "ios" {
dialTimeout = 20 * time.Second
}
webclientOpts := &webclient.Opts{
Timeout: dialTimeout,
UserConfig: func() common.UserConfig {
internalHeaders := map[string]string{
common.PlatformHeader: platform,
common.AppVersionHeader: common.ApplicationVersion,
}
deviceID, _ := session.GetDeviceID()
userID, _ := session.GetUserID()
token, _ := session.GetToken()
lang, _ := session.Locale()
return common.NewUserConfig(
common.DefaultAppName,
deviceID,
userID,
token,
internalHeaders,
lang,
)
},
}
return pro.NewClient(fmt.Sprintf("https://%s", common.ProAPIHost), webclientOpts)
return pro.NewClient(fmt.Sprintf("https://%s", common.ProAPIHost), func() common.UserConfig {
internalHeaders := map[string]string{
common.PlatformHeader: platform,
common.AppVersionHeader: common.ApplicationVersion,
}
deviceID, _ := session.GetDeviceID()
userID, _ := session.GetUserID()
token, _ := session.GetToken()
lang, _ := session.Locale()
return common.NewUserConfig(
common.DefaultAppName,
deviceID,
userID,
token,
internalHeaders,
lang,
)
})
}

func BytesToFloat64LittleEndian(b []byte) (float64, error) {
Expand Down

0 comments on commit fc3a90b

Please sign in to comment.