Skip to content

Commit

Permalink
bump utls and minor dependency fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Jan 4, 2025
1 parent 4db2538 commit b06365f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
40 changes: 20 additions & 20 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"time"
)

func (d *fronted) initCaching(cacheFile string) {
d.prepopulateFronts(cacheFile)
go d.maintainCache(cacheFile)
func (f *fronted) initCaching(cacheFile string) {
f.prepopulateFronts(cacheFile)
go f.maintainCache(cacheFile)
}

func (d *fronted) prepopulateFronts(cacheFile string) {
func (f *fronted) prepopulateFronts(cacheFile string) {
bytes, err := os.ReadFile(cacheFile)
if os.IsNotExist(err) {
if err = os.MkdirAll(filepath.Dir(cacheFile), 0755); err != nil {
Expand Down Expand Up @@ -42,48 +42,48 @@ func (d *fronted) prepopulateFronts(cacheFile string) {
now := time.Now()

// update last succeeded status of masquerades based on cached values
for _, f := range d.fronts {
for _, fr := range f.fronts {
for _, cf := range cachedFronts {
sameFront := cf.ProviderID == f.getProviderID() && cf.Domain == f.getDomain() && cf.IpAddress == f.getIpAddress()
cachedValueFresh := now.Sub(f.lastSucceeded()) < d.maxAllowedCachedAge
sameFront := cf.ProviderID == fr.getProviderID() && cf.Domain == fr.getDomain() && cf.IpAddress == fr.getIpAddress()
cachedValueFresh := now.Sub(fr.lastSucceeded()) < f.maxAllowedCachedAge
if sameFront && cachedValueFresh {
f.setLastSucceeded(cf.LastSucceeded)
fr.setLastSucceeded(cf.LastSucceeded)
}
}
}
}

func (d *fronted) markCacheDirty() {
func (f *fronted) markCacheDirty() {
select {
case d.cacheDirty <- nil:
case f.cacheDirty <- nil:
// okay
default:
// already dirty
}
}

func (d *fronted) maintainCache(cacheFile string) {
func (f *fronted) maintainCache(cacheFile string) {
for {
select {
case <-d.cacheClosed:
case <-f.cacheClosed:
return
case <-time.After(d.cacheSaveInterval):
case <-time.After(f.cacheSaveInterval):
select {
case <-d.cacheClosed:
case <-f.cacheClosed:
return
case <-d.cacheDirty:
d.updateCache(cacheFile)
case <-f.cacheDirty:
f.updateCache(cacheFile)
}
}
}
}

func (d *fronted) updateCache(cacheFile string) {
func (f *fronted) updateCache(cacheFile string) {
log.Debugf("Updating cache at %v", cacheFile)
cache := d.fronts.sortedCopy()
cache := f.fronts.sortedCopy()
sizeToSave := len(cache)
if d.maxCacheSize < sizeToSave {
sizeToSave = d.maxCacheSize
if f.maxCacheSize < sizeToSave {
sizeToSave = f.maxCacheSize
}
b, err := json.Marshal(cache[:sizeToSave])
if err != nil {
Expand Down
13 changes: 9 additions & 4 deletions fronted.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ func (f *fronted) tryAllFronts() {
for i := 0; i < f.frontSize(); i++ {
m := f.frontAt(i)
pool.Submit(func() {
//log.Debugf("Running task #%d with front %v", i, m.getIpAddress())
// log.Debugf("Running task #%d with front %v", i, m.getIpAddress())
if f.isStopped() {
return
}
if f.hasEnoughWorkingFronts() {
// We have enough working fronts, so no need to continue.
//log.Debug("Enough working fronts...ignoring task")
// log.Debug("Enough working fronts...ignoring task")
return
}
working := f.vetFront(m)
Expand Down Expand Up @@ -258,7 +258,7 @@ func (f *fronted) RoundTripHijack(req *http.Request) (*http.Response, net.Conn,
// store body in-memory to be able to replay it if necessary
body, err = io.ReadAll(req.Body)
if err != nil {
err := fmt.Errorf("unable to read request body: %v", err)
err = fmt.Errorf("unable to read request body: %w", err)
op.FailIf(err)
return nil, nil, err
}
Expand Down Expand Up @@ -394,7 +394,12 @@ func (f *fronted) doDial(m Front) (net.Conn, bool, error) {
var conn net.Conn
var err error
retriable := false
conn, err = m.dial(f.certPool.Load().(*x509.CertPool), f.clientHelloID)
pool, ok := f.certPool.Load().(*x509.CertPool)
if !ok {
pool = nil
}
// A nil cert pool will just use the system's root CAs.
conn, err = m.dial(pool, f.clientHelloID)
if err != nil {
if !isNetworkUnreachable(err) {
op.FailIf(err)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ require (
github.com/getlantern/ops v0.0.0-20231025133620-f368ab734534
github.com/getlantern/tlsdialer/v3 v3.0.3
github.com/getlantern/waitforserver v1.0.1
github.com/refraction-networking/utls v1.3.3
github.com/refraction-networking/utls v1.6.7
github.com/stretchr/testify v1.8.4
)

require (
github.com/andybalholm/brotli v1.0.6 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gaukas/godicttls v0.0.3 // indirect
github.com/getlantern/byteexec v0.0.0-20170405023437-4cfb26ec74f4 // indirect
github.com/getlantern/context v0.0.0-20220418194847-3d5e7a086201 // indirect
github.com/getlantern/elevate v0.0.0-20200430163644-2881a121236d // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ github.com/alitto/pond/v2 v2.1.5/go.mod h1:xkjYEgQ05RSpWdfSd1nM3OVv7TBhLdy7rMp3+
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gaukas/godicttls v0.0.3 h1:YNDIf0d9adcxOijiLrEzpfZGAkNwLRzPaG6OjU7EITk=
github.com/gaukas/godicttls v0.0.3/go.mod h1:l6EenT4TLWgTdwslVb4sEMOCf7Bv0JAK67deKr9/NCI=
github.com/getlantern/byteexec v0.0.0-20170405023437-4cfb26ec74f4 h1:Nqmy8i81dzokjNHpyOg24gnQBeGRF7D51m8HmBRNn0Y=
github.com/getlantern/byteexec v0.0.0-20170405023437-4cfb26ec74f4/go.mod h1:4WCQkaCIwta0KlF9bQZA1jYqp8bzIS2PeCqjnef8nZ8=
github.com/getlantern/context v0.0.0-20190109183933-c447772a6520/go.mod h1:L+mq6/vvYHKjCX2oez0CgEAJmbq1fbb/oNJIWQkBybY=
Expand Down Expand Up @@ -81,8 +81,8 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/refraction-networking/utls v0.0.0-20190909200633-43c36d3c1f57/go.mod h1:tz9gX959MEFfFN5whTIocCLUG57WiILqtdVxI8c6Wj0=
github.com/refraction-networking/utls v1.3.3 h1:f/TBLX7KBciRyFH3bwupp+CE4fzoYKCirhdRcC490sw=
github.com/refraction-networking/utls v1.3.3/go.mod h1:DlecWW1LMlMJu+9qpzzQqdHDT/C2LAe03EdpLUz/RL8=
github.com/refraction-networking/utls v1.6.7 h1:zVJ7sP1dJx/WtVuITug3qYUq034cDq9B2MR1K67ULZM=
github.com/refraction-networking/utls v1.6.7/go.mod h1:BC3O4vQzye5hqpmDTWUqi4P5DDhzJfkV1tdqtawQIH0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down

0 comments on commit b06365f

Please sign in to comment.