-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
154 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package httpproxy | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
// aLongTimeAgo is a non-zero time, far in the past, used for | ||
// immediate cancellation of network operations. | ||
// copies from http | ||
var aLongTimeAgo = time.Unix(1, 0) | ||
|
||
// NewListenerCompatibilityReadDeadline this is a wrapper used to be compatible with | ||
// the contents of ServerConn after wrapping it so that it can be hijacked properly. | ||
// there is no effect if the content is not manipulated. | ||
func NewListenerCompatibilityReadDeadline(listener net.Listener) net.Listener { | ||
return listenerCompatibilityReadDeadline{listener} | ||
} | ||
|
||
type listenerCompatibilityReadDeadline struct { | ||
net.Listener | ||
} | ||
|
||
func (w listenerCompatibilityReadDeadline) Accept() (net.Conn, error) { | ||
c, err := w.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return connCompatibilityReadDeadline{c}, nil | ||
} | ||
|
||
type connCompatibilityReadDeadline struct { | ||
net.Conn | ||
} | ||
|
||
func (d connCompatibilityReadDeadline) SetReadDeadline(t time.Time) error { | ||
if aLongTimeAgo == t { | ||
t = time.Now().Add(time.Second) | ||
} | ||
return d.Conn.SetReadDeadline(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package httpproxy | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestNewListenerCompatibilityReadDeadline(t *testing.T) { | ||
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(w, "check", r.RequestURI) | ||
})) | ||
|
||
listener, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
listener = newHexListener(listener) | ||
listener = NewListenerCompatibilityReadDeadline(listener) | ||
|
||
s, err := NewSimpleServer("http://u:p@:0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
s.Listener = listener | ||
s.Start(context.Background()) | ||
defer s.Close() | ||
|
||
dial, err := NewDialer(s.ProxyURL()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
dial.ProxyDial = func(ctx context.Context, network, address string) (net.Conn, error) { | ||
conn, err := net.Dial(network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
conn = newHexConn(conn) | ||
return conn, nil | ||
} | ||
cli := testServer.Client() | ||
cli.Transport = &http.Transport{ | ||
DialContext: dial.DialContext, | ||
} | ||
|
||
resp, err := cli.Get(testServer.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
resp.Body.Close() | ||
} | ||
|
||
func newHexListener(listener net.Listener) net.Listener { | ||
return hexListener{ | ||
Listener: listener, | ||
} | ||
} | ||
|
||
type hexListener struct { | ||
net.Listener | ||
} | ||
|
||
func (h hexListener) Accept() (net.Conn, error) { | ||
conn, err := h.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newHexConn(conn), nil | ||
} | ||
|
||
func newHexConn(conn net.Conn) net.Conn { | ||
return hexConn{ | ||
Conn: conn, | ||
r: hex.NewDecoder(conn), | ||
w: hex.NewEncoder(conn), | ||
} | ||
} | ||
|
||
type hexConn struct { | ||
net.Conn | ||
r io.Reader | ||
w io.Writer | ||
} | ||
|
||
func (h hexConn) Read(p []byte) (n int, err error) { | ||
return h.r.Read(p) | ||
} | ||
|
||
func (h hexConn) Write(p []byte) (n int, err error) { | ||
return h.w.Write(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters