From 7abeb12fc439135ae607f8f3509baffa58b6c254 Mon Sep 17 00:00:00 2001 From: Subreme <71085002+subreme@users.noreply.github.com> Date: Tue, 22 Jun 2021 13:07:43 +0200 Subject: [PATCH 1/2] Fix Race Condition Added a RWMutex in in `header.go`, used in `writeSubset()` and `SortedKeyValues()`, in order to avoid any conflicts in the `exclude` map when several tasks are run, as suggested by @ShrimpAIO. --- go.mod | 2 +- go.sum | 4 ++++ header.go | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 1c97b081..e6f0dba5 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,5 @@ go 1.16 require ( github.com/andybalholm/brotli v1.0.3 golang.org/x/net v0.0.0-20210610132358-84b48f89b13b - golang.org/x/term v0.0.0-20210503060354-a79de5458b56 + golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b ) diff --git a/go.sum b/go.sum index 46278dae..18574203 100644 --- a/go.sum +++ b/go.sum @@ -4,8 +4,12 @@ golang.org/x/net v0.0.0-20210610132358-84b48f89b13b h1:k+E048sYJHyVnsr1GDrRZWQ32 golang.org/x/net v0.0.0-20210610132358-84b48f89b13b/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY= +golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE= +golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/header.go b/header.go index e57d92ed..62fc694f 100644 --- a/header.go +++ b/header.go @@ -194,6 +194,8 @@ var headerSorterPool = sync.Pool{ New: func() interface{} { return new(headerSorter) }, } +var mutex = &sync.RWMutex{} + // SortedKeyValues returns h's keys sorted in the returned kvs // slice. The headerSorter used to sort is also returned, for possible // return to headerSorterCache. @@ -204,9 +206,11 @@ func (h Header) SortedKeyValues(exclude map[string]bool) (kvs []HeaderKeyValues, } kvs = hs.kvs[:0] for k, vv := range h { + mutex.RLock() if !exclude[k] { kvs = append(kvs, HeaderKeyValues{k, vv}) } + mutex.RUnlock() } hs.kvs = kvs sort.Sort(hs) @@ -245,6 +249,7 @@ func (h Header) writeSubset(w io.Writer, exclude map[string]bool, trace *httptra var kvs []HeaderKeyValues var sorter *headerSorter + // Check if the HeaderOrder is defined. if headerOrder, ok := h[HeaderOrderKey]; ok { order := make(map[string]int) @@ -254,7 +259,9 @@ func (h Header) writeSubset(w io.Writer, exclude map[string]bool, trace *httptra if exclude == nil { exclude = make(map[string]bool) } + mutex.Lock() exclude[HeaderOrderKey] = true + mutex.Unlock() kvs, sorter = h.SortedKeyValuesBy(order, exclude) } else { kvs, sorter = h.SortedKeyValues(exclude) From 608f2a2828a5917d6a7f7cafb2b929297464b22a Mon Sep 17 00:00:00 2001 From: Subreme <71085002+subreme@users.noreply.github.com> Date: Tue, 22 Jun 2021 14:24:20 +0200 Subject: [PATCH 2/2] Improve README.md Fixed a couple typos in `README.md`, including the organization name in the link to the new repository (as pointed out by @0xflotus). Fixed the formatting in accordance with Google's Markdown Style Guide. Commented out the note at the beginning of the document without deleting it, as it is meant for the original repository owned by @zMrKrabz. --- README.md | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 229f0bfe..fd8a7908 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,25 @@ -# fhttp +# fhttp + -# Features +## Features -## Ordered Headers -Allows for pseudo header order and normal header order. Most of the code is taken from [this pr](https://go-review.googlesource.com/c/go/+/105755/). +### Ordered Headers -## Connection settings -Has Chrome-like connection settings: -``` +The package allows for both pseudo header order and normal header order. Most of the code is taken from [this Pull Request](https://go-review.googlesource.com/c/go/+/105755/). + +### Connection settings + +fhhtp has Chrome-like connection settings, as shown below: + +```text SETTINGS_HEADER_TABLE_SIZE = 65536 (2^16) SETTINGS_ENABLE_PUSH = 1 SETTINGS_MAX_CONCURRENT_STREAMS = 1000 @@ -23,8 +28,9 @@ SETTINGS_MAX_FRAME_SIZE = 16384 (2^14) SETTINGS_MAX_HEADER_LIST_SIZE = 262144 (2^18) ``` -Default net/http settings: -``` +The default net/http settings, on the other hand, are the following: + +```text SETTINGS_HEADER_TABLE_SIZE = 4096 SETTINGS_ENABLE_PUSH = 0 SETTINGS_MAX_CONCURRENT_STREAMS = unlimited @@ -33,20 +39,23 @@ SETTINGS_MAX_FRAME_SIZE = 16384 SETTINGS_MAX_HEADER_LIST_SIZE = 10485760 ``` -ENABLE_PUSH implementation was merged from [this pull request](https://go-review.googlesource.com/c/net/+/181497/) +The ENABLE_PUSH implementation was merged from [this Pull Request](https://go-review.googlesource.com/c/net/+/181497/). + +### gzip, deflate, and br encoding + +`gzip`, `deflate`, and `br` encoding are all supported by the package. -## gzip, deflate, br encoding -Actually supports and implements encoding `gzip, deflate, br` +### Pseudo header order -## Pseudo header order -Supports pseudo header order for http2 to mitigate fingerprinting. Read more about it [here](https://www.akamai.com/uk/en/multimedia/documents/white-paper/passive-fingerprinting-of-http2-clients-white-paper.pdf) +fhttp supports pseudo header order for http2, helping mitigate fingerprinting. You can read more about how it works [here](https://www.akamai.com/uk/en/multimedia/documents/white-paper/passive-fingerprinting-of-http2-clients-white-paper.pdf). + +### Backward compatible with net/http -## Backward compatible with net/http Although this library is an extension of `net/http`, it is also meant to be backward compatible. Replacing ```go import ( - "net/http" + "net/http" ) ``` @@ -54,13 +63,14 @@ with ```go import ( - http "github.com/useflyent/fhttp" + http "github.com/useflyent/fhttp" ) ``` -SHOULD not break anything. +SHOULD not break anything. + +## Credits -# Credits Special thanks to the following people for helping me with this project. * [cc](https://github.com/x04/) for guiding me when I first started this project and inspiring me with [cclient](https://github.com/x04/cclient)