From a0aa7b737446f6557424d1160389a50b06c2b1c1 Mon Sep 17 00:00:00 2001 From: lucifer9 Date: Wed, 26 Jun 2019 13:51:33 +0800 Subject: [PATCH 1/6] fix incompatible errors when building with bazel 0.26+ --- infra/bazel/zip.bzl | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/infra/bazel/zip.bzl b/infra/bazel/zip.bzl index ac6b12a3..fc3ae2f5 100644 --- a/infra/bazel/zip.bzl +++ b/infra/bazel/zip.bzl @@ -47,10 +47,10 @@ def _zip_file(ctx): if (s.startswith("/") or s.endswith("/") or d.startswith("/") or d.endswith("/")): fail("mappings should not begin or end with slash") - srcs = depset() - srcs += ctx.files.srcs - srcs += ctx.files.data - srcs += collect_runfiles(ctx.attr.data) + srcs = depset(transitive = [depset(ctx.files.srcs),depset(ctx.files.data),depset(collect_runfiles(ctx.attr.data))]) + # srcs += ctx.files.srcs + # srcs += ctx.files.data + # srcs += collect_runfiles(ctx.attr.data) mapped = _map_sources(ctx, srcs, ctx.attr.mappings) cmd = [ "#!/bin/sh", @@ -74,7 +74,7 @@ def _zip_file(ctx): for _, zip_path in mapped if "/" in zip_path ], - ) + ).to_list() ] cmd += [ 'ln -sf "${repo}/%s" "${tmp}/%s"' % (path, zip_path) @@ -86,12 +86,12 @@ def _zip_file(ctx): 'cd "${repo}"', 'rm -rf "${tmp}"', ] - script = ctx.new_file(ctx.bin_dir, "%s.sh" % ctx.label.name) - ctx.file_action(output = script, content = "\n".join(cmd), executable = True) + script = ctx.actions.declare_file("%s/%s.sh" % (ctx.bin_dir, ctx.label.name)) + ctx.actions.write(output = script, content = "\n".join(cmd), is_executable = True) inputs = [ctx.file._zipper] inputs += [dep.zip_file for dep in ctx.attr.deps] - inputs += list(srcs) - ctx.action( + inputs += list(srcs.to_list()) + ctx.actions.run( inputs = inputs, outputs = [ctx.outputs.out], executable = script, @@ -117,7 +117,7 @@ def _map_sources(ctx, srcs, mappings): mappings_indexes = range(len(mappings)) used = {i: False for i in mappings_indexes} mapped = [] - for file_ in srcs: + for file_ in srcs.to_list(): run_path = long_path(ctx, file_) zip_path = None for i in mappings_indexes: @@ -159,6 +159,6 @@ pkg_zip = rule( "deps": attr.label_list(providers = ["zip_file"]), "exclude": attr.string_list(), "mappings": attr.string_dict(), - "_zipper": attr.label(default = Label(ZIPPER), single_file = True), + "_zipper": attr.label(default = Label(ZIPPER), allow_single_file = True), }, ) From e744537b8074b6e1a40bd5e1b007b4093d7e0650 Mon Sep 17 00:00:00 2001 From: lucifer9 Date: Thu, 27 Jun 2019 14:21:40 +0800 Subject: [PATCH 2/6] add server-side http2 over tcp (h2c) support --- transport/internet/http/hub.go | 78 +++++++++++++++++++++++----------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index 1ec53c5d..f2d9fdbf 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -16,6 +16,9 @@ import ( "v2ray.com/core/common/signal/done" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/tls" + + "golang.org/x/net/http2" + "golang.org/x/net/http2/h2c" ) type Listener struct { @@ -104,34 +107,61 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti config := tls.ConfigFromStreamSettings(streamSettings) 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, - ReadHeaderTimeout: time.Second * 4, - } - - listener.server = server - go func() { - tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ - IP: address.IP(), - Port: int(port), - }, streamSettings.SocketSettings) - if err != nil { - newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) - return + // return nil, newError("TLS must be enabled for http transport.").AtWarning() + h2s:=&http2.Server{} + + server := &http.Server{ + Addr: serial.Concat(address, ":", port), + // TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), + Handler: h2c.NewHandler(listener,h2s), + ReadHeaderTimeout: time.Second * 4, } - err = server.ServeTLS(tcpListener, "", "") - if err != nil { - newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) + listener.server = server + go func() { + tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ + IP: address.IP(), + Port: int(port), + }, streamSettings.SocketSettings) + if err != nil { + newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) + return + } + + err = server.Serve(tcpListener) + if err != nil { + newError("stoping serving H2C").Base(err).WriteToLog(session.ExportIDToError(ctx)) + } + }() + + return listener, nil + } else { + server := &http.Server{ + Addr: serial.Concat(address, ":", port), + TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), + Handler: listener, + ReadHeaderTimeout: time.Second * 4, } - }() - return listener, nil + listener.server = server + go func() { + tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ + IP: address.IP(), + Port: int(port), + }, streamSettings.SocketSettings) + if err != nil { + newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) + return + } + + err = server.ServeTLS(tcpListener, "", "") + if err != nil { + newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) + } + }() + + return listener, nil + } } func init() { From 1955d72e3e5ceb8a6ca72b705c05094240e81a74 Mon Sep 17 00:00:00 2001 From: lucifer9 Date: Thu, 27 Jun 2019 14:27:47 +0800 Subject: [PATCH 3/6] fix some lint warnings. --- transport/internet/http/hub.go | 54 ++++++++++++++++------------------ 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index f2d9fdbf..721e0c7a 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -108,12 +108,12 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti config := tls.ConfigFromStreamSettings(streamSettings) if config == nil { // return nil, newError("TLS must be enabled for http transport.").AtWarning() - h2s:=&http2.Server{} + h2s := &http2.Server{} server := &http.Server{ - Addr: serial.Concat(address, ":", port), + Addr: serial.Concat(address, ":", port), // TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), - Handler: h2c.NewHandler(listener,h2s), + Handler: h2c.NewHandler(listener, h2s), ReadHeaderTimeout: time.Second * 4, } @@ -135,33 +135,31 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti }() return listener, nil - } else { - server := &http.Server{ - Addr: serial.Concat(address, ":", port), - TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), - Handler: listener, - ReadHeaderTimeout: time.Second * 4, - } - - listener.server = server - go func() { - tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ - IP: address.IP(), - Port: int(port), - }, streamSettings.SocketSettings) - if err != nil { - newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) - return - } + } + server := &http.Server{ + Addr: serial.Concat(address, ":", port), + TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), + Handler: listener, + ReadHeaderTimeout: time.Second * 4, + } - err = server.ServeTLS(tcpListener, "", "") - if err != nil { - newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) - } - }() + listener.server = server + go func() { + tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ + IP: address.IP(), + Port: int(port), + }, streamSettings.SocketSettings) + if err != nil { + newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) + return + } - return listener, nil - } + err = server.ServeTLS(tcpListener, "", "") + if err != nil { + newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) + } + }() + return listener, nil } func init() { From f59c0542c92701804b8fe1a96bb06ed168ed98a0 Mon Sep 17 00:00:00 2001 From: lucifer9 Date: Sat, 29 Jun 2019 10:21:34 +0800 Subject: [PATCH 4/6] Revert "fix some lint warnings." This reverts commit 1955d72e3e5ceb8a6ca72b705c05094240e81a74. --- transport/internet/http/hub.go | 54 ++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index 721e0c7a..f2d9fdbf 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -108,12 +108,12 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti config := tls.ConfigFromStreamSettings(streamSettings) if config == nil { // return nil, newError("TLS must be enabled for http transport.").AtWarning() - h2s := &http2.Server{} + h2s:=&http2.Server{} server := &http.Server{ - Addr: serial.Concat(address, ":", port), + Addr: serial.Concat(address, ":", port), // TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), - Handler: h2c.NewHandler(listener, h2s), + Handler: h2c.NewHandler(listener,h2s), ReadHeaderTimeout: time.Second * 4, } @@ -135,31 +135,33 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti }() return listener, nil - } - server := &http.Server{ - Addr: serial.Concat(address, ":", port), - TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), - Handler: listener, - ReadHeaderTimeout: time.Second * 4, - } - - listener.server = server - go func() { - tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ - IP: address.IP(), - Port: int(port), - }, streamSettings.SocketSettings) - if err != nil { - newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) - return + } else { + server := &http.Server{ + Addr: serial.Concat(address, ":", port), + TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), + Handler: listener, + ReadHeaderTimeout: time.Second * 4, } - err = server.ServeTLS(tcpListener, "", "") - if err != nil { - newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) - } - }() - return listener, nil + listener.server = server + go func() { + tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ + IP: address.IP(), + Port: int(port), + }, streamSettings.SocketSettings) + if err != nil { + newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) + return + } + + err = server.ServeTLS(tcpListener, "", "") + if err != nil { + newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) + } + }() + + return listener, nil + } } func init() { From a28fabacefafd1ab49cea46546f3f11080a27cd0 Mon Sep 17 00:00:00 2001 From: lucifer9 Date: Sat, 29 Jun 2019 10:21:42 +0800 Subject: [PATCH 5/6] Revert "add server-side http2 over tcp (h2c) support" This reverts commit e744537b8074b6e1a40bd5e1b007b4093d7e0650. --- transport/internet/http/hub.go | 78 +++++++++++----------------------- 1 file changed, 24 insertions(+), 54 deletions(-) diff --git a/transport/internet/http/hub.go b/transport/internet/http/hub.go index f2d9fdbf..1ec53c5d 100644 --- a/transport/internet/http/hub.go +++ b/transport/internet/http/hub.go @@ -16,9 +16,6 @@ import ( "v2ray.com/core/common/signal/done" "v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet/tls" - - "golang.org/x/net/http2" - "golang.org/x/net/http2/h2c" ) type Listener struct { @@ -107,61 +104,34 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti config := tls.ConfigFromStreamSettings(streamSettings) if config == nil { - // return nil, newError("TLS must be enabled for http transport.").AtWarning() - h2s:=&http2.Server{} - - server := &http.Server{ - Addr: serial.Concat(address, ":", port), - // TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), - Handler: h2c.NewHandler(listener,h2s), - ReadHeaderTimeout: time.Second * 4, + 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, + ReadHeaderTimeout: time.Second * 4, + } + + listener.server = server + go func() { + tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ + IP: address.IP(), + Port: int(port), + }, streamSettings.SocketSettings) + if err != nil { + newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) + return } - listener.server = server - go func() { - tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ - IP: address.IP(), - Port: int(port), - }, streamSettings.SocketSettings) - if err != nil { - newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) - return - } - - err = server.Serve(tcpListener) - if err != nil { - newError("stoping serving H2C").Base(err).WriteToLog(session.ExportIDToError(ctx)) - } - }() - - return listener, nil - } else { - server := &http.Server{ - Addr: serial.Concat(address, ":", port), - TLSConfig: config.GetTLSConfig(tls.WithNextProto("h2")), - Handler: listener, - ReadHeaderTimeout: time.Second * 4, + err = server.ServeTLS(tcpListener, "", "") + if err != nil { + newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) } + }() - listener.server = server - go func() { - tcpListener, err := internet.ListenSystem(ctx, &net.TCPAddr{ - IP: address.IP(), - Port: int(port), - }, streamSettings.SocketSettings) - if err != nil { - newError("failed to listen on", address, ":", port).Base(err).WriteToLog(session.ExportIDToError(ctx)) - return - } - - err = server.ServeTLS(tcpListener, "", "") - if err != nil { - newError("stoping serving TLS").Base(err).WriteToLog(session.ExportIDToError(ctx)) - } - }() - - return listener, nil - } + return listener, nil } func init() { From 4cc5558a4ebccea61af86840b821108f3ce05ae9 Mon Sep 17 00:00:00 2001 From: lucifer9 Date: Sat, 29 Jun 2019 10:22:22 +0800 Subject: [PATCH 6/6] delete some unnecessary comments --- infra/bazel/zip.bzl | 3 --- 1 file changed, 3 deletions(-) diff --git a/infra/bazel/zip.bzl b/infra/bazel/zip.bzl index fc3ae2f5..40f89b6d 100644 --- a/infra/bazel/zip.bzl +++ b/infra/bazel/zip.bzl @@ -48,9 +48,6 @@ def _zip_file(ctx): d.startswith("/") or d.endswith("/")): fail("mappings should not begin or end with slash") srcs = depset(transitive = [depset(ctx.files.srcs),depset(ctx.files.data),depset(collect_runfiles(ctx.attr.data))]) - # srcs += ctx.files.srcs - # srcs += ctx.files.data - # srcs += collect_runfiles(ctx.attr.data) mapped = _map_sources(ctx, srcs, ctx.attr.mappings) cmd = [ "#!/bin/sh",