2018-03-01 07:16:52 -05:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2018-03-01 08:22:33 -05:00
|
|
|
"strings"
|
2019-01-10 04:40:51 -05:00
|
|
|
"time"
|
2018-03-01 07:16:52 -05:00
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/net"
|
|
|
|
"v2ray.com/core/common/serial"
|
2018-09-10 07:23:27 -04:00
|
|
|
"v2ray.com/core/common/session"
|
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}),
|
2019-01-01 14:16:04 -05:00
|
|
|
net.ConnectionOnClose(common.ChainedClosable{done, request.Body}),
|
2018-04-20 15:30:58 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
|
|
|
|
httpSettings := streamSettings.ProtocolSettings.(*Config)
|
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
|
|
|
}
|
|
|
|
|
2018-11-21 08:54:40 -05:00
|
|
|
config := tls.ConfigFromStreamSettings(streamSettings)
|
2018-03-01 07:16:52 -05:00
|
|
|
if config == nil {
|
|
|
|
return nil, newError("TLS must be enabled for http transport.").AtWarning()
|
|
|
|
}
|
|
|
|
|
|
|
|
server := &http.Server{
|
2019-01-10 04:40:51 -05:00
|
|
|
Addr: serial.Concat(address, ":", port),
|
|
|
|
TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")),
|
|
|
|
Handler: listener,
|
|
|
|
ReadHeaderTimeout: time.Second * 4,
|
2018-03-01 07:16:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
listener.server = server
|
2018-03-21 18:38:49 -04:00
|
|
|
go func() {
|
2018-09-15 15:35:32 -04:00
|
|
|
tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{
|
2018-09-10 07:23:27 -04:00
|
|
|
IP: address.IP(),
|
|
|
|
Port: int(port),
|
2018-11-21 08:54:40 -05:00
|
|
|
}, streamSettings.SocketSettings)
|
2018-09-10 07:23:27 -04:00
|
|
|
if err != nil {
|
|
|
|
newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = server.ServeTLS(tcpListener, "", "")
|
2018-03-21 18:38:49 -04:00
|
|
|
if err != nil {
|
2018-09-10 07:23:27 -04:00
|
|
|
newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
2018-03-21 18:38:49 -04:00
|
|
|
}
|
|
|
|
}()
|
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
|
|
|
}
|