mirror of https://github.com/v2ray/v2ray-core
h2 transport
parent
af29e42771
commit
d207d953bd
|
@ -27,3 +27,16 @@ type HasType interface {
|
||||||
// Type returns the type of the object.
|
// Type returns the type of the object.
|
||||||
Type() interface{}
|
Type() interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ChainedClosable []Closable
|
||||||
|
|
||||||
|
func NewChainedClosable(c ...Closable) ChainedClosable {
|
||||||
|
return ChainedClosable(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cc ChainedClosable) Close() error {
|
||||||
|
for _, c := range cc {
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -29,12 +29,12 @@ func GenerateCertificateForTest() *v2tls.Certificate {
|
||||||
Subject: pkix.Name{
|
Subject: pkix.Name{
|
||||||
Organization: []string{"V2Ray Inc"},
|
Organization: []string{"V2Ray Inc"},
|
||||||
},
|
},
|
||||||
NotBefore: time.Now(),
|
NotBefore: time.Now().Add(time.Hour * -1),
|
||||||
NotAfter: time.Now().Add(time.Hour),
|
NotAfter: time.Now().Add(time.Hour),
|
||||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||||
BasicConstraintsValid: true,
|
BasicConstraintsValid: true,
|
||||||
DNSNames: []string{"www.v2ray.com"},
|
DNSNames: []string{"www.v2ray.com", "v2ray.com"},
|
||||||
}
|
}
|
||||||
|
|
||||||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
||||||
|
|
|
@ -23,6 +23,7 @@ const (
|
||||||
TransportProtocol_UDP TransportProtocol = 1
|
TransportProtocol_UDP TransportProtocol = 1
|
||||||
TransportProtocol_MKCP TransportProtocol = 2
|
TransportProtocol_MKCP TransportProtocol = 2
|
||||||
TransportProtocol_WebSocket TransportProtocol = 3
|
TransportProtocol_WebSocket TransportProtocol = 3
|
||||||
|
TransportProtocol_HTTP TransportProtocol = 4
|
||||||
)
|
)
|
||||||
|
|
||||||
var TransportProtocol_name = map[int32]string{
|
var TransportProtocol_name = map[int32]string{
|
||||||
|
@ -30,12 +31,14 @@ var TransportProtocol_name = map[int32]string{
|
||||||
1: "UDP",
|
1: "UDP",
|
||||||
2: "MKCP",
|
2: "MKCP",
|
||||||
3: "WebSocket",
|
3: "WebSocket",
|
||||||
|
4: "HTTP",
|
||||||
}
|
}
|
||||||
var TransportProtocol_value = map[string]int32{
|
var TransportProtocol_value = map[string]int32{
|
||||||
"TCP": 0,
|
"TCP": 0,
|
||||||
"UDP": 1,
|
"UDP": 1,
|
||||||
"MKCP": 2,
|
"MKCP": 2,
|
||||||
"WebSocket": 3,
|
"WebSocket": 3,
|
||||||
|
"HTTP": 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x TransportProtocol) String() string {
|
func (x TransportProtocol) String() string {
|
||||||
|
@ -138,29 +141,29 @@ func init() {
|
||||||
func init() { proto.RegisterFile("v2ray.com/core/transport/internet/config.proto", fileDescriptor0) }
|
func init() { proto.RegisterFile("v2ray.com/core/transport/internet/config.proto", fileDescriptor0) }
|
||||||
|
|
||||||
var fileDescriptor0 = []byte{
|
var fileDescriptor0 = []byte{
|
||||||
// 374 bytes of a gzipped FileDescriptorProto
|
// 379 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x91, 0x4d, 0x4b, 0xeb, 0x40,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xb4, 0x91, 0x4f, 0x4b, 0xf3, 0x40,
|
||||||
0x14, 0x86, 0x6f, 0x92, 0x72, 0x6f, 0x7a, 0xda, 0x5e, 0xd3, 0x59, 0x15, 0xa1, 0x58, 0x2b, 0x48,
|
0x10, 0x87, 0xdf, 0x24, 0xe5, 0x35, 0x9d, 0xb6, 0x9a, 0xee, 0xa9, 0x08, 0xc5, 0x5a, 0x41, 0x82,
|
||||||
0x70, 0x31, 0x29, 0x71, 0xef, 0xa2, 0x71, 0x23, 0x5a, 0x0c, 0x49, 0x55, 0x10, 0xa4, 0xa4, 0xe3,
|
0x87, 0x4d, 0x89, 0xdf, 0xa0, 0xf1, 0x50, 0xd1, 0x62, 0x48, 0xa2, 0x82, 0x20, 0x25, 0x5d, 0xd7,
|
||||||
0x18, 0x82, 0x4d, 0xa6, 0x4c, 0x46, 0x31, 0xbf, 0xc7, 0x9d, 0x7b, 0xff, 0x9f, 0xe4, 0x63, 0x86,
|
0x10, 0x6c, 0xb2, 0x65, 0xb3, 0x8a, 0xf9, 0x3c, 0xde, 0xbc, 0xfb, 0xfd, 0x24, 0xff, 0x96, 0xa2,
|
||||||
0xa2, 0x50, 0xba, 0x71, 0x37, 0x64, 0xde, 0xf3, 0x9c, 0x27, 0xef, 0x00, 0x7e, 0x75, 0x79, 0x54,
|
0x50, 0x7a, 0xf1, 0x36, 0x64, 0x7e, 0xf3, 0xcc, 0x93, 0x59, 0xc0, 0x6f, 0x36, 0x0f, 0x73, 0x4c,
|
||||||
0x60, 0xc2, 0x52, 0x87, 0x30, 0x4e, 0x1d, 0xc1, 0xa3, 0x2c, 0x5f, 0x33, 0x2e, 0x9c, 0x24, 0x13,
|
0x58, 0x62, 0x11, 0xc6, 0xa9, 0x25, 0x78, 0x98, 0x66, 0x6b, 0xc6, 0x85, 0x15, 0xa7, 0x82, 0xf2,
|
||||||
0x94, 0x67, 0x54, 0x38, 0x84, 0x65, 0x4f, 0x49, 0x8c, 0xd7, 0x9c, 0x09, 0x86, 0x86, 0x32, 0xcf,
|
0x94, 0x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
0x68, 0x51, 0x19, 0x03, 0x00, 0x00,
|
0x00, 0xff, 0xff, 0xf3, 0x7b, 0xd5, 0x57, 0x23, 0x03, 0x00, 0x00,
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,7 @@ enum TransportProtocol {
|
||||||
UDP = 1;
|
UDP = 1;
|
||||||
MKCP = 2;
|
MKCP = 2;
|
||||||
WebSocket = 3;
|
WebSocket = 3;
|
||||||
|
HTTP = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
message TransportConfig {
|
message TransportConfig {
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/transport/internet"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_HTTP, func() interface{} {
|
||||||
|
return new(Config)
|
||||||
|
}))
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import proto "github.com/golang/protobuf/proto"
|
||||||
|
import fmt "fmt"
|
||||||
|
import math "math"
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Host []string `protobuf:"bytes,1,rep,name=host" json:"host,omitempty"`
|
||||||
|
Path string `protobuf:"bytes,2,opt,name=path" json:"path,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Config) Reset() { *m = Config{} }
|
||||||
|
func (m *Config) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Config) ProtoMessage() {}
|
||||||
|
func (*Config) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
|
func (m *Config) GetHost() []string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Host
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Config) GetPath() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Path
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*Config)(nil), "v2ray.core.transport.internet.http.Config")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterFile("v2ray.com/core/transport/internet/http/config.proto", fileDescriptor0)
|
||||||
|
}
|
||||||
|
|
||||||
|
var fileDescriptor0 = []byte{
|
||||||
|
// 173 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x32, 0x2e, 0x33, 0x2a, 0x4a,
|
||||||
|
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x2f, 0x29, 0x4a, 0xcc, 0x2b,
|
||||||
|
0x2e, 0xc8, 0x2f, 0x2a, 0xd1, 0xcf, 0xcc, 0x2b, 0x49, 0x2d, 0xca, 0x4b, 0x2d, 0xd1, 0xcf, 0x28,
|
||||||
|
0x29, 0x29, 0xd0, 0x4f, 0xce, 0xcf, 0x4b, 0xcb, 0x4c, 0xd7, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17,
|
||||||
|
0x52, 0x82, 0x69, 0x2a, 0x4a, 0xd5, 0x83, 0x6b, 0xd0, 0x83, 0x69, 0xd0, 0x03, 0x69, 0x50, 0x32,
|
||||||
|
0xe0, 0x62, 0x73, 0x06, 0xeb, 0x11, 0x12, 0xe2, 0x62, 0xc9, 0xc8, 0x2f, 0x2e, 0x91, 0x60, 0x54,
|
||||||
|
0x60, 0xd6, 0xe0, 0x0c, 0x02, 0xb3, 0x41, 0x62, 0x05, 0x89, 0x25, 0x19, 0x12, 0x4c, 0x0a, 0x8c,
|
||||||
|
0x20, 0x31, 0x10, 0xdb, 0x29, 0x94, 0x4b, 0x2d, 0x39, 0x3f, 0x57, 0x8f, 0xb0, 0xd9, 0x01, 0x8c,
|
||||||
|
0x51, 0x2c, 0x20, 0x7a, 0x15, 0x93, 0x52, 0x98, 0x51, 0x50, 0x62, 0xa5, 0x9e, 0x33, 0x48, 0x71,
|
||||||
|
0x08, 0x5c, 0xb1, 0x27, 0x4c, 0xb1, 0x47, 0x49, 0x49, 0x41, 0x12, 0x1b, 0xd8, 0xcd, 0xc6, 0x80,
|
||||||
|
0x00, 0x00, 0x00, 0xff, 0xff, 0xdc, 0xf2, 0x95, 0x63, 0xea, 0x00, 0x00, 0x00,
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package v2ray.core.transport.internet.http;
|
||||||
|
option csharp_namespace = "V2Ray.Core.Transport.Internet.Http";
|
||||||
|
option go_package = "http";
|
||||||
|
option java_package = "com.v2ray.core.transport.internet.http";
|
||||||
|
option java_multiple_files = true;
|
||||||
|
|
||||||
|
message Config {
|
||||||
|
repeated string host = 1;
|
||||||
|
string path = 2;
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/net"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Connection struct {
|
||||||
|
Reader io.Reader
|
||||||
|
Writer io.Writer
|
||||||
|
Closer common.Closable
|
||||||
|
Local net.Addr
|
||||||
|
Remote net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) Read(b []byte) (int, error) {
|
||||||
|
return c.Reader.Read(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) Write(b []byte) (int, error) {
|
||||||
|
return c.Writer.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) Close() error {
|
||||||
|
return c.Closer.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) LocalAddr() net.Addr {
|
||||||
|
return c.Local
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) RemoteAddr() net.Addr {
|
||||||
|
return c.Remote
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) SetDeadline(t time.Time) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) SetReadDeadline(t time.Time) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) SetWriteDeadline(t time.Time) error {
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1,115 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
gotls "crypto/tls"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"golang.org/x/net/http2"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/net"
|
||||||
|
"v2ray.com/core/transport/internet"
|
||||||
|
"v2ray.com/core/transport/internet/tls"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
globalDialerMap = make(map[net.Destination]*http.Client)
|
||||||
|
globalDailerAccess sync.Mutex
|
||||||
|
)
|
||||||
|
|
||||||
|
func getHTTPClient(ctx context.Context, dest net.Destination) (*http.Client, error) {
|
||||||
|
globalDailerAccess.Lock()
|
||||||
|
defer globalDailerAccess.Unlock()
|
||||||
|
|
||||||
|
if client, found := globalDialerMap[dest]; found {
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
config := tls.ConfigFromContext(ctx)
|
||||||
|
if config == nil {
|
||||||
|
return nil, newError("TLS must be enabled for http transport.").AtWarning()
|
||||||
|
}
|
||||||
|
|
||||||
|
transport := &http2.Transport{
|
||||||
|
DialTLS: func(network string, addr string, tlsConfig *gotls.Config) (net.Conn, error) {
|
||||||
|
rawHost, rawPort, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(rawPort) == 0 {
|
||||||
|
rawPort = "443"
|
||||||
|
}
|
||||||
|
port, err := net.PortFromString(rawPort)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
address := net.ParseAddress(rawHost)
|
||||||
|
|
||||||
|
pconn, err := internet.DialSystem(context.Background(), nil, net.TCPDestination(address, port))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return gotls.Client(pconn, tlsConfig), nil
|
||||||
|
},
|
||||||
|
TLSClientConfig: config.GetTLSConfig(tls.WithDestination(dest), tls.WithNextProto("h2")),
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{
|
||||||
|
Transport: transport,
|
||||||
|
}
|
||||||
|
|
||||||
|
globalDialerMap[dest] = client
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial dials a new TCP connection to the given destination.
|
||||||
|
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
|
||||||
|
client, err := getHTTPClient(ctx, dest)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
preader, pwriter := io.Pipe()
|
||||||
|
request := &http.Request{
|
||||||
|
Method: "PUT",
|
||||||
|
Host: "www.v2ray.com",
|
||||||
|
Body: preader,
|
||||||
|
URL: &url.URL{
|
||||||
|
Scheme: "https",
|
||||||
|
Host: dest.NetAddr(),
|
||||||
|
Path: "/",
|
||||||
|
},
|
||||||
|
Proto: "HTTP/2",
|
||||||
|
ProtoMajor: 2,
|
||||||
|
ProtoMinor: 0,
|
||||||
|
}
|
||||||
|
response, err := client.Do(request)
|
||||||
|
if err != nil {
|
||||||
|
return nil, newError("failed to dial to ", dest).Base(err).AtWarning()
|
||||||
|
}
|
||||||
|
if response.StatusCode != 200 {
|
||||||
|
return nil, newError("unexpected status", response.StatusCode).AtWarning()
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Connection{
|
||||||
|
Reader: response.Body,
|
||||||
|
Writer: pwriter,
|
||||||
|
Closer: common.NewChainedClosable(preader, pwriter, response.Body),
|
||||||
|
Local: &net.TCPAddr{
|
||||||
|
IP: []byte{0, 0, 0, 0},
|
||||||
|
Port: 0,
|
||||||
|
},
|
||||||
|
Remote: &net.TCPAddr{
|
||||||
|
IP: []byte{0, 0, 0, 0},
|
||||||
|
Port: 0,
|
||||||
|
},
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
common.Must(internet.RegisterTransportDialer(internet.TransportProtocol_HTTP, Dial))
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import "v2ray.com/core/common/errors"
|
||||||
|
|
||||||
|
func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("Transport", "Internet", "HTTP") }
|
|
@ -0,0 +1,3 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg http -path Transport,Internet,HTTP
|
|
@ -0,0 +1,75 @@
|
||||||
|
package http_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/buf"
|
||||||
|
"v2ray.com/core/common/net"
|
||||||
|
"v2ray.com/core/testing/servers/tcp"
|
||||||
|
tlsgen "v2ray.com/core/testing/tls"
|
||||||
|
"v2ray.com/core/transport/internet"
|
||||||
|
. "v2ray.com/core/transport/internet/http"
|
||||||
|
"v2ray.com/core/transport/internet/tls"
|
||||||
|
. "v2ray.com/ext/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHTTPConnection(t *testing.T) {
|
||||||
|
assert := With(t)
|
||||||
|
|
||||||
|
port := tcp.PickPort()
|
||||||
|
|
||||||
|
listener, err := Listen(internet.ContextWithSecuritySettings(context.Background(), &tls.Config{
|
||||||
|
Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()},
|
||||||
|
}), net.LocalHostIP, port, func(conn internet.Connection) {
|
||||||
|
go func() {
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
b := buf.New()
|
||||||
|
defer b.Release()
|
||||||
|
|
||||||
|
for {
|
||||||
|
if err := b.Reset(buf.ReadFrom(conn)); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
nBytes, err := conn.Write(b.Bytes())
|
||||||
|
assert(err, IsNil)
|
||||||
|
assert(nBytes, Equals, b.Len())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
})
|
||||||
|
assert(err, IsNil)
|
||||||
|
|
||||||
|
defer listener.Close()
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
conn, err := Dial(internet.ContextWithSecuritySettings(context.Background(), &tls.Config{
|
||||||
|
ServerName: "www.v2ray.com",
|
||||||
|
AllowInsecure: true,
|
||||||
|
}), net.TCPDestination(net.LocalHostIP, port))
|
||||||
|
assert(err, IsNil)
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
const N = 1024
|
||||||
|
b1 := make([]byte, N)
|
||||||
|
common.Must2(rand.Read(b1))
|
||||||
|
b2 := buf.New()
|
||||||
|
|
||||||
|
nBytes, err := conn.Write(b1)
|
||||||
|
assert(nBytes, Equals, N)
|
||||||
|
assert(err, IsNil)
|
||||||
|
|
||||||
|
assert(b2.Reset(buf.ReadFullFrom(conn, N)), IsNil)
|
||||||
|
assert(b2.Bytes(), Equals, b1)
|
||||||
|
|
||||||
|
nBytes, err = conn.Write(b1)
|
||||||
|
assert(nBytes, Equals, N)
|
||||||
|
assert(err, IsNil)
|
||||||
|
|
||||||
|
assert(b2.Reset(buf.ReadFullFrom(conn, N)), IsNil)
|
||||||
|
assert(b2.Bytes(), Equals, b1)
|
||||||
|
}
|
|
@ -0,0 +1,86 @@
|
||||||
|
package http
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"v2ray.com/core/common"
|
||||||
|
"v2ray.com/core/common/net"
|
||||||
|
"v2ray.com/core/common/serial"
|
||||||
|
"v2ray.com/core/common/signal"
|
||||||
|
"v2ray.com/core/transport/internet"
|
||||||
|
"v2ray.com/core/transport/internet/tls"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Listener struct {
|
||||||
|
server *http.Server
|
||||||
|
handler internet.ConnHandler
|
||||||
|
local net.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Listener) Addr() net.Addr {
|
||||||
|
return l.local
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Listener) Close() error {
|
||||||
|
return l.server.Shutdown(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
type flushWriter struct {
|
||||||
|
w io.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fw flushWriter) Write(p []byte) (n int, err error) {
|
||||||
|
n, err = fw.w.Write(p)
|
||||||
|
if f, ok := fw.w.(http.Flusher); ok {
|
||||||
|
f.Flush()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
writer.WriteHeader(200)
|
||||||
|
if f, ok := writer.(http.Flusher); ok {
|
||||||
|
f.Flush()
|
||||||
|
}
|
||||||
|
done := signal.NewDone()
|
||||||
|
l.handler(&Connection{
|
||||||
|
Reader: request.Body,
|
||||||
|
Writer: flushWriter{writer},
|
||||||
|
Closer: common.NewChainedClosable(request.Body, done),
|
||||||
|
Local: l.Addr(),
|
||||||
|
Remote: l.Addr(),
|
||||||
|
})
|
||||||
|
<-done.C()
|
||||||
|
}
|
||||||
|
|
||||||
|
func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {
|
||||||
|
listener := &Listener{
|
||||||
|
handler: handler,
|
||||||
|
local: &net.TCPAddr{
|
||||||
|
IP: address.IP(),
|
||||||
|
Port: int(port),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
config := tls.ConfigFromContext(ctx)
|
||||||
|
if config == nil {
|
||||||
|
return nil, newError("TLS must be enabled for http transport.").AtWarning()
|
||||||
|
}
|
||||||
|
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: serial.Concat(address, ":", port),
|
||||||
|
TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")),
|
||||||
|
Handler: listener,
|
||||||
|
}
|
||||||
|
|
||||||
|
listener.server = server
|
||||||
|
go server.ListenAndServeTLS("", "")
|
||||||
|
|
||||||
|
return listener, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
common.Must(internet.RegisterTransportListener(internet.TransportProtocol_HTTP, Listen))
|
||||||
|
}
|
|
@ -28,7 +28,6 @@ func (c *Config) BuildCertificates() []tls.Certificate {
|
||||||
func (c *Config) GetTLSConfig(opts ...Option) *tls.Config {
|
func (c *Config) GetTLSConfig(opts ...Option) *tls.Config {
|
||||||
config := &tls.Config{
|
config := &tls.Config{
|
||||||
ClientSessionCache: globalSessionCache,
|
ClientSessionCache: globalSessionCache,
|
||||||
NextProtos: []string{"http/1.1"},
|
|
||||||
}
|
}
|
||||||
if c == nil {
|
if c == nil {
|
||||||
return config
|
return config
|
||||||
|
@ -47,6 +46,9 @@ func (c *Config) GetTLSConfig(opts ...Option) *tls.Config {
|
||||||
if len(c.NextProtocol) > 0 {
|
if len(c.NextProtocol) > 0 {
|
||||||
config.NextProtos = c.NextProtocol
|
config.NextProtos = c.NextProtocol
|
||||||
}
|
}
|
||||||
|
if len(config.NextProtos) == 0 {
|
||||||
|
config.NextProtos = []string{"http/1.1"}
|
||||||
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue