Skip to content

Commit

Permalink
apply styles
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokangwang committed May 30, 2023
1 parent b7e8554 commit 1f8e71f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
3 changes: 2 additions & 1 deletion transport/internet/request/assembler/simple/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func (s *simpleAssemblerClient) NewSession(ctx context.Context, opts ...request.
sessionContext, finish := context.WithCancel(ctx)
session := &simpleAssemblerClientSession{
sessionID: sessionID, tripper: s.assembly.Tripper(), readBuffer: bytes.NewBuffer(nil),
ctx: sessionContext, finish: finish, writerChan: make(chan []byte), readerChan: make(chan []byte, 16), assembler: s}
ctx: sessionContext, finish: finish, writerChan: make(chan []byte), readerChan: make(chan []byte, 16), assembler: s,
}
go session.keepRunning()
return session, nil
}
Expand Down
4 changes: 2 additions & 2 deletions transport/internet/request/roundtripper/httprt/httprt.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

func newHTTPRoundTripperClient(ctx context.Context, config *ClientConfig) request.RoundTripperClient {
_ = ctx
return &httpTripperClient{config: config}
}

Expand All @@ -27,8 +28,7 @@ type httpTripperClient struct {
assembly request.TransportClientAssembly
}

type unimplementedBackDrop struct {
}
type unimplementedBackDrop struct{}

func (u unimplementedBackDrop) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, newError("unimplemented")
Expand Down
7 changes: 4 additions & 3 deletions transport/internet/request/stereotype/meek/meek.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func meekDial(ctx context.Context, dest net.Destination, streamSettings *interne
BackoffFactor: 1.5,
FailedRetryIntervalMs: 1000,
}
httprtSetting := &httprt.ClientConfig{Http: &httprt.HTTPConfig{
UrlPrefix: meekSetting.Url,
},
httprtSetting := &httprt.ClientConfig{
Http: &httprt.HTTPConfig{
UrlPrefix: meekSetting.Url,
},
}
request := &assembly.Config{
Assembler: serial.ToTypedMessage(simpleAssembler),
Expand Down
29 changes: 18 additions & 11 deletions transport/internet/transportcommon/httpDialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import (
"sync"
"time"

"github.com/v2fly/v2ray-core/v5/transport/internet/security"
"golang.org/x/net/http2"

"github.com/v2fly/v2ray-core/v5/transport/internet/security"
)

type DialerFunc func(ctx context.Context, addr string) (net.Conn, error)

// NewALPNAwareHTTPRoundTripper creates an instance of RoundTripper that dial to remote HTTPS endpoint with
// an alternative version of TLS implementation.
func NewALPNAwareHTTPRoundTripper(ctx context.Context, dialer DialerFunc,
backdropTransport http.RoundTripper) http.RoundTripper {
backdropTransport http.RoundTripper,
) http.RoundTripper {
rtImpl := &alpnAwareHTTPRoundTripperImpl{
connectWithH1: map[string]bool{},
backdropTransport: backdropTransport,
Expand Down Expand Up @@ -51,9 +53,11 @@ type pendingConnKey struct {
dest string
}

var errEAGAIN = errors.New("incorrect ALPN negotiated, try again with another ALPN")
var errEAGAINTooMany = errors.New("incorrect ALPN negotiated")
var errExpired = errors.New("connection have expired")
var (
errEAGAIN = errors.New("incorrect ALPN negotiated, try again with another ALPN")
errEAGAINTooMany = errors.New("incorrect ALPN negotiated")
errExpired = errors.New("connection have expired")
)

func (r *alpnAwareHTTPRoundTripperImpl) RoundTrip(req *http.Request) (*http.Response, error) {
if req.URL.Scheme != "https" {
Expand Down Expand Up @@ -106,19 +110,21 @@ func getPendingConnectionID(dest string, alpnIsH2 bool) pendingConnKey {
}

func (r *alpnAwareHTTPRoundTripperImpl) putConn(addr string, alpnIsH2 bool, conn net.Conn) {
connId := getPendingConnectionID(addr, alpnIsH2)
r.pendingConn[connId] = NewUnclaimedConnection(conn, time.Minute)
connID := getPendingConnectionID(addr, alpnIsH2)
r.pendingConn[connID] = NewUnclaimedConnection(conn, time.Minute)
}

func (r *alpnAwareHTTPRoundTripperImpl) getConn(addr string, alpnIsH2 bool) net.Conn {
connId := getPendingConnectionID(addr, alpnIsH2)
if conn, ok := r.pendingConn[connId]; ok {
delete(r.pendingConn, connId)
connID := getPendingConnectionID(addr, alpnIsH2)
if conn, ok := r.pendingConn[connID]; ok {
delete(r.pendingConn, connID)
if claimedConnection, err := conn.claimConnection(); err == nil {
return claimedConnection
}
}
return nil
}

func (r *alpnAwareHTTPRoundTripperImpl) dialOrGetTLSWithExpectedALPN(ctx context.Context, addr string, expectedH2 bool) (net.Conn, error) {
r.accessDialingConnection.Lock()
defer r.accessDialingConnection.Unlock()
Expand All @@ -127,7 +133,7 @@ func (r *alpnAwareHTTPRoundTripperImpl) dialOrGetTLSWithExpectedALPN(ctx context
return nil, errEAGAIN
}

//Get a cached connection if possible to reduce preflight connection closed without sending data
// Get a cached connection if possible to reduce preflight connection closed without sending data
if gconn := r.getConn(addr, expectedH2); gconn != nil {
return gconn, nil
}
Expand Down Expand Up @@ -164,6 +170,7 @@ func (r *alpnAwareHTTPRoundTripperImpl) dialOrGetTLSWithExpectedALPN(ctx context
}

func (r *alpnAwareHTTPRoundTripperImpl) dialTLS(ctx context.Context, addr string) (net.Conn, error) {
_ = ctx
return r.dialer(r.ctx, addr)
}

Expand Down
3 changes: 2 additions & 1 deletion transport/internet/transportcommon/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func (l *combinedListener) Close() error {
}

func ListenWithSecuritySettings(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig) (
net.Listener, error) {
net.Listener, error,
) {
var l combinedListener

transportEnvironment := envctx.EnvironmentFromContext(ctx).(environment.TransportEnvironment)
Expand Down

0 comments on commit 1f8e71f

Please sign in to comment.