Skip to content

Commit

Permalink
expose Access-Control-Allow-Private-Network
Browse files Browse the repository at this point in the history
  • Loading branch information
lqs committed Nov 19, 2024
1 parent 4e16a77 commit 8027d0e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
29 changes: 17 additions & 12 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
)

type mixHandler struct {
writer http.ResponseWriter
request *http.Request
grpcServer *grpc.Server
grpcWeb *grpcweb.WrappedGrpcServer
httpHandler http.Handler
writer http.ResponseWriter
request *http.Request
grpcServer *grpc.Server
grpcWeb *grpcweb.WrappedGrpcServer
httpHandler http.Handler
allowPrivateNetwork bool
}

func (h mixHandler) isGrpc() bool {
Expand Down Expand Up @@ -67,6 +68,9 @@ func (h mixHandler) handleGrpcWeb() bool {
if (h.grpcWeb.IsGrpcWebRequest(h.request) || h.grpcWeb.IsAcceptableGrpcCorsRequest(h.request)) && h.isRegisteredGrpcPath() {
wrapper := wrapBrotli(h.writer, h.request)
defer wrapper.Close()
if h.allowPrivateNetwork {
wrapper.Header().Set("Access-Control-Allow-Private-Network", "true")
}
h.grpcWeb.ServeHTTP(wrapper, h.request)
return true
}
Expand All @@ -91,8 +95,8 @@ func (h mixHandler) handle() {
}
}

func newHandler(grpcServer *grpc.Server, http2Server *http2.Server, httpHandler http.Handler) (http.Handler, func()) {
grpcWeb := grpcweb.WrapServer(grpcServer, grpcweb.WithOriginFunc(func(origin string) bool {
func (s *server) newHandler(http2Server *http2.Server, httpHandler http.Handler) (http.Handler, func()) {
grpcWeb := grpcweb.WrapServer(s.grpcServer, grpcweb.WithOriginFunc(func(origin string) bool {
return true
}))
var wg sync.WaitGroup
Expand All @@ -105,11 +109,12 @@ func newHandler(grpcServer *grpc.Server, http2Server *http2.Server, httpHandler
wg.Add(1)
defer wg.Done()
mixHandler{
writer: writer,
request: request,
grpcServer: grpcServer,
grpcWeb: grpcWeb,
httpHandler: httpHandler,
writer: writer,
request: request,
grpcServer: s.grpcServer,
grpcWeb: grpcWeb,
httpHandler: httpHandler,
allowPrivateNetwork: s.config.AllowPrivateNetwork,
}.handle()
}), http2Server), wg.Wait
}
13 changes: 7 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ type Server interface {
}

type Config struct {
Port int
ShutdownDelay time.Duration
MaxHeaderBytes int
GrpcServerOptions []grpc.ServerOption
OnStarted func()
Port int
ShutdownDelay time.Duration
MaxHeaderBytes int
GrpcServerOptions []grpc.ServerOption
OnStarted func()
AllowPrivateNetwork bool
}

type server struct {
Expand Down Expand Up @@ -58,7 +59,7 @@ func (s *server) StartAndWait(ctx context.Context) error {
var connectionClose atomic.Bool

http2Server := &http2.Server{}
handler, wait := newHandler(s.grpcServer, http2Server, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler, wait := s.newHandler(http2Server, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if connectionClose.Load() {
w.Header().Set("Connection", "close")
}
Expand Down

0 comments on commit 8027d0e

Please sign in to comment.