2018-03-01 07:16:52 -05:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2018-03-01 08:22:33 -05:00
|
|
|
"strings"
|
2018-03-01 07:16:52 -05:00
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/serial"
|
2018-05-27 08:42:53 -04:00
|
|
|
"v2ray.com/core/common/signal/done"
|
2018-03-01 07:16:52 -05:00
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/internet/tls"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Listener struct {
|
|
|
|
server *http.Server
|
|
|
|
handler internet.ConnHandler
|
|
|
|
local net.Addr
|
2018-03-01 08:22:33 -05:00
|
|
|
config Config
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) Addr() net.Addr {
|
|
|
|
return l.local
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Listener) Close() error {
|
2018-04-14 17:07:47 -04:00
|
|
|
return l.server.Close()
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type flushWriter struct {
|
|
|
|
w io.Writer
|
2018-05-27 08:42:53 -04:00
|
|
|
d *done.Instance
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fw flushWriter) Write(p []byte) (n int, err error) {
|
2018-03-21 07:53:57 -04:00
|
|
|
if fw.d.Done() {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
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) {
|
2018-03-01 08:22:33 -05:00
|
|
|
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")
|
2018-03-01 07:16:52 -05:00
|
|
|
writer.WriteHeader(200)
|
|
|
|
if f, ok := writer.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
2018-04-21 05:00:40 -04:00
|
|
|
|
|
|
|
remoteAddr := l.Addr()
|
|
|
|
dest, err := net.ParseDestination(request.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
newError("failed to parse request remote addr: ", request.RemoteAddr).Base(err).WriteToLog()
|
|
|
|
} else {
|
|
|
|
remoteAddr = &net.TCPAddr{
|
|
|
|
IP: dest.Address.IP(),
|
|
|
|
Port: int(dest.Port),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 08:42:53 -04:00
|
|
|
done := done.New()
|
2018-04-20 15:30:58 -04:00
|
|
|
conn := net.NewConnection(
|
|
|
|
net.ConnectionOutput(request.Body),
|
|
|
|
net.ConnectionInput(flushWriter{w: writer, d: done}),
|
|
|
|
net.ConnectionOnClose(common.NewChainedClosable(done, request.Body)),
|
|
|
|
net.ConnectionLocalAddr(l.Addr()),
|
2018-04-21 05:00:40 -04:00
|
|
|
net.ConnectionRemoteAddr(remoteAddr),
|
2018-04-20 15:30:58 -04:00
|
|
|
)
|
|
|
|
l.handler(conn)
|
2018-04-15 14:40:47 -04:00
|
|
|
<-done.Wait()
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func Listen(ctx context.Context, address net.Address, port net.Port, handler internet.ConnHandler) (internet.Listener, error) {
|
2018-03-01 08:22:33 -05:00
|
|
|
rawSettings := internet.TransportSettingsFromContext(ctx)
|
|
|
|
httpSettings, ok := rawSettings.(*Config)
|
|
|
|
if !ok {
|
|
|
|
return nil, newError("HTTP config is not set.").AtError()
|
|
|
|
}
|
|
|
|
|
2018-03-01 07:16:52 -05:00
|
|
|
listener := &Listener{
|
|
|
|
handler: handler,
|
|
|
|
local: &net.TCPAddr{
|
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
|
|
|
},
|
2018-03-01 08:22:33 -05:00
|
|
|
config: *httpSettings,
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-03-21 18:38:49 -04:00
|
|
|
go func() {
|
|
|
|
err := server.ListenAndServeTLS("", "")
|
|
|
|
if err != nil {
|
|
|
|
newError("stoping serving TLS").Base(err).WriteToLog()
|
|
|
|
}
|
|
|
|
}()
|
2018-03-01 07:16:52 -05:00
|
|
|
|
|
|
|
return listener, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-08-06 07:48:35 -04:00
|
|
|
common.Must(internet.RegisterTransportListener(protocolName, Listen))
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|