Optimize HTTP tunnel setup in TFO environment

pull/2531/head
Anonymous-Someneese 5 years ago committed by kslr
parent 197fe15d5a
commit a5caa01cb6

@ -4,9 +4,9 @@ package http
import ( import (
"bufio" "bufio"
"io"
"context" "context"
"encoding/base64" "encoding/base64"
"io"
"net/http" "net/http"
"strings" "strings"
@ -93,9 +93,7 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
p = c.policyManager.ForLevel(user.Level) p = c.policyManager.ForLevel(user.Level)
} }
if err := setUpHTTPTunnel(conn, &destination, user); err != nil { conn = setUpHTTPTunnel(conn, &destination, user)
return err
}
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle) timer := signal.CancelAfterInactivity(ctx, cancel, p.Timeouts.ConnectionIdle)
@ -125,8 +123,13 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
return nil return nil
} }
type tunnelConn struct {
internet.Connection
header *buf.Buffer
}
// setUpHTTPTunnel will create a socket tunnel via HTTP CONNECT method // setUpHTTPTunnel will create a socket tunnel via HTTP CONNECT method
func setUpHTTPTunnel(writer io.Writer, destination *net.Destination, user *protocol.MemoryUser) error { func setUpHTTPTunnel(conn internet.Connection, destination *net.Destination, user *protocol.MemoryUser) *tunnelConn {
var headers []string var headers []string
destNetAddr := destination.NetAddr() destNetAddr := destination.NetAddr()
headers = append(headers, "CONNECT "+destNetAddr+" HTTP/1.1") headers = append(headers, "CONNECT "+destNetAddr+" HTTP/1.1")
@ -140,11 +143,43 @@ func setUpHTTPTunnel(writer io.Writer, destination *net.Destination, user *proto
b := buf.New() b := buf.New()
b.WriteString(strings.Join(headers, "\r\n") + "\r\n\r\n") b.WriteString(strings.Join(headers, "\r\n") + "\r\n\r\n")
if err := buf.WriteAllBytes(writer, b.Bytes()); err != nil { return &tunnelConn {
return err Connection: conn,
header: b,
} }
}
return nil func (c *tunnelConn) Write(b []byte) (n int, err error) {
if c.header == nil {
return c.Connection.Write(b)
}
buffer := c.header
lenheader := c.header.Len()
// Concate header and b
_, err = buffer.Write(b)
if err != nil {
c.header.Resize(0, lenheader)
return 0, err
}
// Write buffer
nc, err := io.Copy(c.Connection, buffer)
if int32(nc) < lenheader {
c.header.Resize(int32(nc), lenheader)
return 0, err
}
c.header.Release()
c.header = nil
n = int(nc) - int(lenheader)
if err != nil {
return n, err
}
// Write trailing bytes
if n < len(b) {
var nw int
nw, err = c.Connection.Write(b[:n])
n += nw
}
return n, err
} }
func init() { func init() {

Loading…
Cancel
Save