Skip to content

Commit 38d61a5

Browse files
Moved timeouts to constants
1 parent 1375833 commit 38d61a5

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

gohpts.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
"golang.org/x/net/proxy"
1616
)
1717

18+
const (
19+
readTimeout time.Duration = 10 * time.Second
20+
writeTimeout time.Duration = 10 * time.Second
21+
timeout time.Duration = 10 * time.Second
22+
kbSize int64 = 1000
23+
)
24+
1825
// Hop-by-hop headers
1926
// https://datatracker.ietf.org/doc/html/rfc2616#section-13.5.1
2027
var hopHeaders = []string{
@@ -137,10 +144,10 @@ func (app *app) handleForward(w http.ResponseWriter, r *http.Request) {
137144
return
138145
}
139146
var written string
140-
if n < 1000 {
147+
if n < kbSize {
141148
written = fmt.Sprintf("%dB", n)
142149
} else {
143-
written = fmt.Sprintf("%dKB", n/1000)
150+
written = fmt.Sprintf("%dKB", n/kbSize)
144151
}
145152
app.logger.Debug().Msgf("%s - %s - %s - %d - %s", r.Proto, r.Method, r.Host, resp.StatusCode, written)
146153
}
@@ -149,7 +156,7 @@ func (app *app) handleTunnel(w http.ResponseWriter, r *http.Request) {
149156
var dstConn net.Conn
150157
var err error
151158
if isLocalAddress(r.Host) {
152-
dstConn, err = net.DialTimeout("tcp", r.Host, 10*time.Second)
159+
dstConn, err = net.DialTimeout("tcp", r.Host, timeout)
153160
if err != nil {
154161
http.Error(w, err.Error(), http.StatusServiceUnavailable)
155162
return
@@ -196,10 +203,10 @@ func (app *app) transfer(wg *sync.WaitGroup, destination io.Writer, source io.Re
196203
app.logger.Error().Err(err).Msgf("Error during copy from %s to %s: %v", srcName, destName, err)
197204
}
198205
var written string
199-
if n < 1000 {
206+
if n < kbSize {
200207
written = fmt.Sprintf("%dB", n)
201208
} else {
202-
written = fmt.Sprintf("%dKB", n/1000)
209+
written = fmt.Sprintf("%dKB", n/kbSize)
203210
}
204211
app.logger.Debug().Msgf("copied %s from %s to %s", written, srcName, destName)
205212
}
@@ -250,9 +257,9 @@ func New(conf *Config) *app {
250257
User: conf.User,
251258
Password: conf.Pass,
252259
}
253-
dialer, err := proxy.SOCKS5("tcp", conf.AddrSOCKS, &auth, nil)
260+
dialer, err := proxy.SOCKS5("tcp", conf.AddrSOCKS, &auth, &net.Dialer{Timeout: timeout})
254261
if err != nil {
255-
logger.Fatal().Err(err).Msg("Unable to create SOCKS5 client")
262+
logger.Fatal().Err(err).Msg("Unable to create SOCKS5 dialer")
256263
}
257264
socks := &http.Client{
258265
Transport: &http.Transport{
@@ -261,12 +268,12 @@ func New(conf *Config) *app {
261268
CheckRedirect: func(req *http.Request, via []*http.Request) error {
262269
return http.ErrUseLastResponse
263270
},
264-
Timeout: 10 * time.Second,
271+
Timeout: timeout,
265272
}
266273
hs := &http.Server{
267274
Addr: conf.AddrHTTP,
268-
ReadTimeout: 10 * time.Second,
269-
WriteTimeout: 10 * time.Second,
275+
ReadTimeout: readTimeout,
276+
WriteTimeout: writeTimeout,
270277
MaxHeaderBytes: 1 << 20,
271278
}
272279
hc := &http.Client{
@@ -276,7 +283,7 @@ func New(conf *Config) *app {
276283
CheckRedirect: func(req *http.Request, via []*http.Request) error {
277284
return http.ErrUseLastResponse
278285
},
279-
Timeout: 10 * time.Second,
286+
Timeout: timeout,
280287
}
281288
logger.Info().Msgf("SOCKS5 Proxy: %s", conf.AddrSOCKS)
282289
logger.Info().Msgf("HTTP Proxy: %s", conf.AddrHTTP)

0 commit comments

Comments
 (0)