Skip to content

Commit

Permalink
Merge branch 'main' into iran
Browse files Browse the repository at this point in the history
  • Loading branch information
anacrolix committed Oct 17, 2022
2 parents a3db610 + 4afea5b commit 4542761
Show file tree
Hide file tree
Showing 31 changed files with 15,478 additions and 15,916 deletions.
13 changes: 5 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ test-and-cover: $(SOURCES)
CP=$$(echo $$TP | tr ' ', ',') && \
set -x && \
for pkg in $$TP; do \
GO111MODULE=on go test -race -v -tags="headless" -covermode=atomic -coverprofile=profile_tmp.cov -coverpkg "$$CP" $$pkg || exit 1; \
go test -race -v -tags="headless" -covermode=atomic -coverprofile=profile_tmp.cov -coverpkg "$$CP" $$pkg || exit 1; \
tail -n +2 profile_tmp.cov >> profile.cov; \
done

test: $(SOURCES)
@TP=$$(go list ./...) && \
for pkg in $$TP; do \
GO111MODULE=on go test -failfast -race -v -tags="headless" $$pkg || exit 1; \
done
test:
go test -failfast -race -v -tags="headless" ./...

define prep-for-mobile
go env -w 'GOPRIVATE=github.com/getlantern/*'
Expand All @@ -34,14 +31,14 @@ endef
lanternsdk-android.aar: $(SOURCES)
$(call prep-for-mobile) && \
echo "LDFLAGS $(LDFLAGS)" && \
echo "Running gomobile with `which gomobile` version `GO111MODULE=off gomobile version` ..." && \
echo "Running gomobile with `which gomobile` version `gomobile version` ..." && \
gomobile bind -cache `pwd`/.gomobilecache -o=lanternsdk-android.aar -target=android -tags='headless publicsdk' -ldflags="$(LDFLAGS)" github.com/getlantern/flashlight/lanternsdk

# we build the LanternSDK.framework in two steps to use XCFramework
# See https://stackoverflow.com/questions/63942997/generate-xcframework-file-with-gomobile
Lanternsdk.xcframework: $(SOURCES)
@$(call prep-for-mobile) && \
echo "Running gomobile with `which gomobile` version `GO111MODULE=off gomobile version` ..." && \
echo "Running gomobile with `which gomobile` version `gomobile version` ..." && \
gomobile bind -cache `pwd`/.gomobilecache -o=Lanternsdk.xcframework -target=ios -tags='headless publicsdk' -ldflags="$(LDFLAGS)" github.com/getlantern/flashlight/lanternsdk

clean:
Expand Down
88 changes: 58 additions & 30 deletions balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ type Dialer interface {
// Stop stops background processing for this Dialer.
Stop()

// Ping performs an ICMP ping of the proxy used by this dialer
Ping()

WriteStats(w io.Writer)
}

Expand Down Expand Up @@ -318,42 +315,81 @@ func (b *Balancer) newBalancedDial(network string, addr string) (*balancedDial,
}, nil
}

func (bd *balancedDial) dial(ctx context.Context, start time.Time) (conn net.Conn, err error) {
newCTX, cancel := context.WithTimeout(ctx, bd.Balancer.overallDialTimeout)
func (bd *balancedDial) dial(parentCtx context.Context, start time.Time) (net.Conn, error) {
childCtx, cancel := context.WithTimeout(parentCtx, bd.Balancer.overallDialTimeout)
defer cancel()
deadline, _ := newCTX.Deadline()
attempts := 0
for {
conn := bd.dialWithDialer(newCTX, bd.dialers[bd.idx], start, attempts)
if conn != nil {
conn, err := bd.dialWithDialer(childCtx, bd.dialers[bd.idx], start, attempts)
if err != nil {
// Log the dial error here as a trace since it's not fatal. If we
// have another dialer, we'll try it.
log.Tracef(
"Dialing %s://%s with %s failed: %v. You can ignore this since we'll try with other dialers.",
bd.network, bd.addr, bd.dialers[bd.idx].Label(), err)
} else {
return conn, nil
}
if time.Now().After(deadline) {
break

// Check context: if it dies, we can't continue.
select {
case <-childCtx.Done():
err = fmt.Errorf(
"Dialing %s://%s with %s on attempt %d context died (1st codepath): dial duration (%s): Context duration (%s). Context error: %w",
bd.network, bd.addr,
bd.dialers[bd.idx].Label(),
attempts, time.Since(start),
bd.Balancer.overallDialTimeout,
childCtx.Err())
return nil, err
default:
}

attempts++
if !bd.advanceToNextDialer(attempts) {
break
}
// check deadline again after advancing, as we may have slept for a while
if time.Now().After(deadline) {
break
if !bd.advanceToNextDialer(childCtx, attempts) {
// If we reached here, we have no more dialers and the context
// might've died. Determine the error and die
if childCtx.Err() != nil {
err = fmt.Errorf(
"Dialing %s://%s with %s on attempt %d context died (2nd codepath): Context duration (%s). Context error: %w",
bd.network, bd.addr,
bd.dialers[bd.idx].Label(),
attempts,
bd.Balancer.overallDialTimeout,
childCtx.Err())
} else {
err = fmt.Errorf("No more dialers for %s://%s with %s on attempt %d",
bd.network, bd.addr,
bd.dialers[bd.idx].Label(),
attempts)
}
return nil, err
}
// If we reach here, we failed with this dialer, but there are still
// other dialers in the pipeline. Advance to the next one.
}

return nil, fmt.Errorf("Still unable to dial %s://%s after %d attempts", bd.network, bd.addr, attempts)
// UNREACHABLE CODE
}

// advanceToNextDialer advances this balancedDial to the next dialer, cycling
// back to the beginning if necessary. If all dialers have failed upstream, this
// method returns false.
func (bd *balancedDial) advanceToNextDialer(attempts int) bool {
func (bd *balancedDial) advanceToNextDialer(
ctx context.Context,
attempts int) bool {
if len(bd.failedUpstream) == len(bd.dialers) {
// all dialers failed upstream, give up
return false
}

for {
select {
case <-ctx.Done():
return false
default:
}

bd.idx++
if bd.idx >= len(bd.dialers) {
bd.idx = 0
Expand All @@ -368,20 +404,20 @@ func (bd *balancedDial) advanceToNextDialer(attempts int) bool {
}
}

func (bd *balancedDial) dialWithDialer(ctx context.Context, dialer Dialer, start time.Time, attempts int) net.Conn {
func (bd *balancedDial) dialWithDialer(ctx context.Context, dialer Dialer, start time.Time, attempts int) (net.Conn, error) {
deadline, _ := ctx.Deadline()
log.Tracef("Dialing %s://%s with %s on pass %v with timeout %v", bd.network, bd.addr, dialer.Label(), attempts, deadline.Sub(time.Now()))
oldRTT, oldBW := dialer.EstRTT(), dialer.EstBandwidth()
conn, failedUpstream, err := dialer.DialContext(ctx, bd.network, bd.addr)
if err != nil {
bd.onFailure(dialer, failedUpstream, err, attempts)
return nil
return nil, err
}
// Multiplexed dialers don't wait for anything from the proxy when dialing
// "persistent" connections, so we can't blindly trust such connections as
// signals of success dialers.
if bd.network == NetworkPersistent {
return conn
return conn, nil
}
// Please leave this at Debug level, as it helps us understand
// performance issues caused by a poor proxy being selected.
Expand All @@ -401,7 +437,7 @@ func (bd *balancedDial) dialWithDialer(ctx context.Context, dialer Dialer, start
default:
}
}
return conn
return conn, nil
}

func (bd *balancedDial) onSuccess(dialer Dialer) {
Expand Down Expand Up @@ -712,14 +748,6 @@ func (b *Balancer) KeepLookingForSucceedingDialer() {
}
}

// PingProxies pings the client's proxies.
func (bal *Balancer) PingProxies() {
dialers := bal.copyOfDialers()
for _, dialer := range dialers {
go dialer.Ping()
}
}

func recordTopDialer(sortedDialers []Dialer) {
ops.SetGlobal("num_proxies", len(sortedDialers))

Expand Down
3 changes: 2 additions & 1 deletion balancer/balancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestGoodSlowDialer(t *testing.T) {

func TestAllFailingUpstream(t *testing.T) {
addr, l := echoServer()
defer func() { _ = l.Close() }()
defer func() { require.NoError(t, l.Close()) }()

dialer1 := &testDialer{
name: "dialer1",
Expand Down
1 change: 0 additions & 1 deletion balancer/test_dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,4 @@ func (d *testDialer) Stop() {
atomic.StoreInt32(&d.stopped, 1)
}

func (d *testDialer) Ping() {}
func (d *testDialer) WriteStats(w io.Writer) {}
8 changes: 7 additions & 1 deletion chained/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package chained
import (
"strconv"

"github.com/getlantern/lantern-cloud/cmd/api/apipb"
"github.com/getlantern/golog"
"github.com/getlantern/lantern-cloud/cmd/api/apipb"
tls "github.com/refraction-networking/utls"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -182,8 +182,14 @@ var availableClientHelloIDs = map[string]tls.ClientHelloID{
"HelloFirefox_Auto": tls.HelloFirefox_Auto,
"HelloFirefox_55": tls.HelloFirefox_55,
"HelloFirefox_56": tls.HelloFirefox_56,
"HelloFirefox_105": tls.HelloFirefox_105,
"HelloChrome_Auto": tls.HelloChrome_Auto,
"HelloChrome_58": tls.HelloChrome_58,
"HelloChrome_62": tls.HelloChrome_62,
"HelloChrome_106": tls.HelloChrome_106,
"HelloEdge_Auto": tls.HelloEdge_Auto,
"Hello360_Auto": tls.Hello360_Auto,
"HelloQQ_Auto": tls.HelloQQ_Auto,
"HelloQQ_11": tls.HelloQQ_11_1,
"HelloBrowser": helloBrowser,
}
33 changes: 0 additions & 33 deletions chained/ping.go

This file was deleted.

2 changes: 2 additions & 0 deletions chained/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ func createImpl(configDir, name, addr, transport string, s *apipb.ProxyConfig, u
impl, err = newWSSImpl(addr, s, reportDialCore)
case "tlsmasq":
impl, err = newTLSMasqImpl(configDir, name, addr, s, uc, reportDialCore)
case "starbridge":
impl, err = newStarbridgeImpl(name, addr, s, reportDialCore)
default:
err = errors.New("Unknown transport: %v", transport).With("addr", addr).With("plugabble-transport", transport)
}
Expand Down
3 changes: 1 addition & 2 deletions chained/shadowsocks_impl.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !iosapp
// +build !iosapp

package chained

Expand All @@ -18,8 +17,8 @@ import (
"github.com/getlantern/errors"
shadowsocks "github.com/getlantern/lantern-shadowsocks/client"

"github.com/getlantern/lantern-cloud/cmd/api/apipb"
"github.com/getlantern/flashlight/ops"
"github.com/getlantern/lantern-cloud/cmd/api/apipb"
)

const (
Expand Down
44 changes: 44 additions & 0 deletions chained/starbridge_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package chained

import (
"context"
"net"

"github.com/OperatorFoundation/Starbridge-go/Starbridge/v3"
"github.com/getlantern/flashlight/ops"
"github.com/getlantern/lantern-cloud/cmd/api/apipb"
)

type starbridge struct {
reportDialCore reportDialCoreFn
config Starbridge.ClientConfig
conn net.Conn
}

func (s *starbridge) dialServer(op *ops.Op, ctx context.Context) (net.Conn, error) {
return s.reportDialCore(op, func() (net.Conn, error) {
conn, err := s.config.Dial(s.config.Address)
if err != nil {
return nil, err
}
s.conn = conn
return conn, nil
})
}

func (s *starbridge) close() {
if s.conn != nil {
s.conn.Close()
}
}

func newStarbridgeImpl(name, addr string, pc *apipb.ProxyConfig, reportDialCore reportDialCoreFn) (proxyImpl, error) {
config := Starbridge.ClientConfig{
Address: addr,
ServerPersistentPublicKey: pc.Cert,
}
return &starbridge{
config: config,
reportDialCore: reportDialCore,
}, nil
}
2 changes: 1 addition & 1 deletion chained/tls_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

tls "github.com/refraction-networking/utls"

"github.com/getlantern/lantern-cloud/cmd/api/apipb"
"github.com/getlantern/flashlight/browsers/simbrowser"
"github.com/getlantern/flashlight/common"
"github.com/getlantern/flashlight/ops"
"github.com/getlantern/lantern-cloud/cmd/api/apipb"
"github.com/getlantern/tlsresumption"
)

Expand Down
Loading

0 comments on commit 4542761

Please sign in to comment.