1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 15:26:29 -04:00

consume config in dialer and hub

This commit is contained in:
Darien Raymond 2018-03-01 14:22:33 +01:00
parent d207d953bd
commit e5d3783ed7
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 73 additions and 6 deletions

View File

@ -2,9 +2,42 @@ package http
import ( import (
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/dice"
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
) )
func (c *Config) getHosts() []string {
if len(c.Host) == 0 {
return []string{"www.example.com"}
}
return c.Host
}
func (c *Config) isValidHost(host string) bool {
hosts := c.getHosts()
for _, h := range hosts {
if h == host {
return true
}
}
return false
}
func (c *Config) getRandomHost() string {
hosts := c.getHosts()
return hosts[dice.Roll(len(hosts))]
}
func (c *Config) getNormalizedPath() string {
if len(c.Path) == 0 {
return "/"
}
if c.Path[0] != '/' {
return "/" + c.Path
}
return c.Path
}
func init() { func init() {
common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_HTTP, func() interface{} { common.Must(internet.RegisterProtocolConfigCreator(internet.TransportProtocol_HTTP, func() interface{} {
return new(Config) return new(Config)

View File

@ -68,6 +68,12 @@ func getHTTPClient(ctx context.Context, dest net.Destination) (*http.Client, err
// Dial dials a new TCP connection to the given destination. // Dial dials a new TCP connection to the given destination.
func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) { func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
rawSettings := internet.TransportSettingsFromContext(ctx)
httpSettings, ok := rawSettings.(*Config)
if !ok {
return nil, newError("HTTP config is not set.").AtError()
}
client, err := getHTTPClient(ctx, dest) client, err := getHTTPClient(ctx, dest)
if err != nil { if err != nil {
return nil, err return nil, err
@ -76,12 +82,12 @@ func Dial(ctx context.Context, dest net.Destination) (internet.Connection, error
preader, pwriter := io.Pipe() preader, pwriter := io.Pipe()
request := &http.Request{ request := &http.Request{
Method: "PUT", Method: "PUT",
Host: "www.v2ray.com", Host: httpSettings.getRandomHost(),
Body: preader, Body: preader,
URL: &url.URL{ URL: &url.URL{
Scheme: "https", Scheme: "https",
Host: dest.NetAddr(), Host: dest.NetAddr(),
Path: "/", Path: httpSettings.getNormalizedPath(),
}, },
Proto: "HTTP/2", Proto: "HTTP/2",
ProtoMajor: 2, ProtoMajor: 2,

View File

@ -22,9 +22,13 @@ func TestHTTPConnection(t *testing.T) {
port := tcp.PickPort() port := tcp.PickPort()
listener, err := Listen(internet.ContextWithSecuritySettings(context.Background(), &tls.Config{ lctx := context.Background()
lctx = internet.ContextWithSecuritySettings(lctx, &tls.Config{
Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()}, Certificate: []*tls.Certificate{tlsgen.GenerateCertificateForTest()},
}), net.LocalHostIP, port, func(conn internet.Connection) { })
lctx = internet.ContextWithTransportSettings(lctx, &Config{})
listener, err := Listen(lctx, net.LocalHostIP, port, func(conn internet.Connection) {
go func() { go func() {
defer conn.Close() defer conn.Close()
@ -47,10 +51,13 @@ func TestHTTPConnection(t *testing.T) {
time.Sleep(time.Second) time.Sleep(time.Second)
conn, err := Dial(internet.ContextWithSecuritySettings(context.Background(), &tls.Config{ dctx := context.Background()
dctx = internet.ContextWithSecuritySettings(dctx, &tls.Config{
ServerName: "www.v2ray.com", ServerName: "www.v2ray.com",
AllowInsecure: true, AllowInsecure: true,
}), net.TCPDestination(net.LocalHostIP, port)) })
dctx = internet.ContextWithTransportSettings(dctx, &Config{})
conn, err := Dial(dctx, net.TCPDestination(net.LocalHostIP, port))
assert(err, IsNil) assert(err, IsNil)
defer conn.Close() defer conn.Close()

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"io" "io"
"net/http" "net/http"
"strings"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/net" "v2ray.com/core/common/net"
@ -17,6 +18,7 @@ type Listener struct {
server *http.Server server *http.Server
handler internet.ConnHandler handler internet.ConnHandler
local net.Addr local net.Addr
config Config
} }
func (l *Listener) Addr() net.Addr { func (l *Listener) Addr() net.Addr {
@ -40,6 +42,18 @@ func (fw flushWriter) Write(p []byte) (n int, err error) {
} }
func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) { func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
host := request.Host
if !l.config.isValidHost(host) {
writer.WriteHeader(404)
return
}
path := l.config.getNormalizedPath()
if !strings.HasPrefix(request.URL.Path, path) {
writer.WriteHeader(404)
return
}
writer.Header().Set("Cache-Control", "no-store")
writer.WriteHeader(200) writer.WriteHeader(200)
if f, ok := writer.(http.Flusher); ok { if f, ok := writer.(http.Flusher); ok {
f.Flush() f.Flush()
@ -56,12 +70,19 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request)
} }
func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) { func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {
rawSettings := internet.TransportSettingsFromContext(ctx)
httpSettings, ok := rawSettings.(*Config)
if !ok {
return nil, newError("HTTP config is not set.").AtError()
}
listener := &Listener{ listener := &Listener{
handler: handler, handler: handler,
local: &net.TCPAddr{ local: &net.TCPAddr{
IP: address.IP(), IP: address.IP(),
Port: int(port), Port: int(port),
}, },
config: *httpSettings,
} }
config := tls.ConfigFromContext(ctx) config := tls.ConfigFromContext(ctx)