Skip to content

Commit 4ea0a5f

Browse files
committed
Add accept timeouts for public and private proxies and ensure connections that are not accepted by the private proxy are closed after some time.
1 parent 41aa921 commit 4ea0a5f

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

proxy/forms.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ var InternalEndpointForm = forms.Form{
123123
forms.IsString{},
124124
},
125125
},
126+
{
127+
Name: "timeout",
128+
Validators: []forms.Validator{
129+
forms.IsOptional{Default: 30.0},
130+
forms.IsFloat{HasMin: true, Min: 0, HasMax: true, Max: 3000},
131+
},
132+
},
126133
},
127134
}
128135

@@ -223,5 +230,12 @@ var PublicSettingsForm = forms.Form{
223230
},
224231
},
225232
},
233+
{
234+
Name: "accept_timeout",
235+
Validators: []forms.Validator{
236+
forms.IsOptional{Default: 10.0},
237+
forms.IsFloat{HasMin: true, Min: 0, HasMax: true, Max: 3000},
238+
},
239+
},
226240
},
227241
}

proxy/private_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (p *ProxyConnection) TerminateTLS(proxyConnection net.Conn) error {
278278
defer server.Stop()
279279
select {
280280
case <-done:
281-
case <-time.After(5 * time.Second):
281+
case <-time.After(time.Duration(p.settings.Timeout) * time.Second):
282282
break
283283
return fmt.Errorf("timeout handling request")
284284
}

proxy/public_server.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,23 @@ func (s *PublicServer) handleTlsConnection(conn net.Conn) {
514514
s.tlsHellos[randomStr] = buf[:reqLen]
515515
s.mutex.Unlock()
516516

517+
go func() {
518+
time.Sleep(time.Duration(s.settings.AcceptTimeout) * time.Second)
519+
s.mutex.Lock()
520+
defer s.mutex.Unlock()
521+
// connection still waiting, we close it
522+
if conn, ok := s.tlsConnections[randomStr]; ok {
523+
eps.Log.Warningf("TLS connection not accepted in time by private proxy, closing it...")
524+
if err := conn.Close(); err != nil {
525+
eps.Log.Error(err)
526+
}
527+
delete(s.tlsConnections, randomStr)
528+
delete(s.tlsHellos, randomStr)
529+
} else {
530+
eps.Log.Debugf("Connection accepted...")
531+
}
532+
}()
533+
517534
// we tell the internal proxy about an incoming connection
518535
request := jsonrpc.MakeRequest(fmt.Sprintf("%s.incomingConnection", announcement.Operator), "", map[string]interface{}{
519536
"domain": hostName,

proxy/settings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type PublicServerSettings struct {
4949
JSONRPCClient *jsonrpc.JSONRPCClientSettings `json:"jsonrpc_client"`
5050
JSONRPCServer *jsonrpc.JSONRPCServerSettings `json:"jsonrpc_server`
5151
TCPRateLimits []*net.RateLimit `json:"tcp_rate_limits"`
52+
AcceptTimeout float64 `json:"accept_timeout"`
5253
}
5354

5455
type PublicAnnouncement struct {
@@ -74,6 +75,7 @@ type InternalEndpointSettings struct {
7475
TLS *tls.TLSSettings `json:"tls"`
7576
JSONRPCClient *jsonrpc.JSONRPCClientSettings `json:"jsonrpc_client"`
7677
JSONRPCPath string `json:"jsonrpc_path"`
78+
Timeout float64 `json:"timeout"`
7779
}
7880

7981
type PrivateServerSettings struct {

0 commit comments

Comments
 (0)