|
@@ -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)
|