Skip to content

Commit b2a65c8

Browse files
refactor: server
1 parent 6629cd3 commit b2a65c8

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

proxy/proxy.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"net/url"
1313
"strings"
1414
"sync"
15-
"time"
1615

1716
"github.com/coder/boundary/audit"
1817
"github.com/coder/boundary/rules"
@@ -26,7 +25,7 @@ type Server struct {
2625
tlsConfig *tls.Config
2726
httpPort int
2827

29-
httpServer *http.Server
28+
listener net.Listener
3029
}
3130

3231
// Config holds configuration for the proxy server
@@ -51,27 +50,22 @@ func NewProxyServer(config Config) *Server {
5150

5251
// Start starts the HTTP proxy server with TLS termination capability
5352
func (p *Server) Start(ctx context.Context) error {
54-
// Create HTTP server with TLS termination capability
55-
p.httpServer = &http.Server{
56-
Addr: fmt.Sprintf(":%d", p.httpPort),
57-
Handler: http.HandlerFunc(p.handleHTTPWithTLSTermination),
58-
}
59-
6053
// Start HTTP server with custom listener for TLS detection
6154
go func() {
6255
p.logger.Info("Starting HTTP proxy with TLS termination", "port", p.httpPort)
63-
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", p.httpPort))
56+
var err error
57+
p.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", p.httpPort))
6458
if err != nil {
6559
p.logger.Error("Failed to create HTTP listener", "error", err)
6660
return
6761
}
6862

6963
for {
70-
conn, err := listener.Accept()
64+
conn, err := p.listener.Accept()
7165
if err != nil {
7266
select {
7367
case <-ctx.Done():
74-
err = listener.Close()
68+
err = p.listener.Close()
7569
if err != nil {
7670
p.logger.Error("Failed to close listener", "error", err)
7771
}
@@ -94,17 +88,17 @@ func (p *Server) Start(ctx context.Context) error {
9488

9589
// Stops proxy server
9690
func (p *Server) Stop() error {
97-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
98-
defer cancel()
99-
100-
var httpErr error
101-
if p.httpServer != nil {
102-
httpErr = p.httpServer.Shutdown(ctx)
91+
if p.listener == nil {
92+
return nil
10393
}
10494

105-
if httpErr != nil {
106-
return httpErr
95+
err := p.listener.Close()
96+
if err != nil {
97+
p.logger.Error("Failed to close listener", "error", err)
10798
}
99+
100+
fmt.Printf("STOP is finished\n")
101+
108102
return nil
109103
}
110104

@@ -479,13 +473,6 @@ func (p *Server) handleConnectionWithTLSDetection(conn net.Conn) {
479473
}
480474
}
481475

482-
// handleHTTPWithTLSTermination is the main handler (currently just delegates to regular HTTP)
483-
func (p *Server) handleHTTPWithTLSTermination(w http.ResponseWriter, r *http.Request) {
484-
// This handler is not used when we do custom connection handling
485-
// All traffic goes through handleConnectionWithTLSDetection
486-
p.handleHTTP(w, r)
487-
}
488-
489476
// connectionWrapper lets us "unread" the peeked byte
490477
type connectionWrapper struct {
491478
net.Conn

proxy/proxy_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ func TestProxyServerBasicHTTP(t *testing.T) {
109109
}`
110110
require.Equal(t, expectedResponse, string(body))
111111
})
112+
113+
err = server.Stop()
114+
require.NoError(t, err)
115+
cancel()
116+
err = <-serverDone
117+
require.NoError(t, err)
112118
}
113119

114120
// TestProxyServerBasicHTTPS tests basic HTTPS request handling
@@ -211,4 +217,10 @@ func TestProxyServerBasicHTTPS(t *testing.T) {
211217
`
212218
require.Equal(t, expectedResponse, string(body))
213219
})
220+
221+
err = server.Stop()
222+
require.NoError(t, err)
223+
cancel()
224+
err = <-serverDone
225+
require.NoError(t, err)
214226
}

0 commit comments

Comments
 (0)