Skip to content

Commit 36c5472

Browse files
authored
feat: IPv6 support (#192)
* πŸ› fix(server.go): validate IP address before starting server ✨ feat(server.go): add support for IPv6 addresses * ✨ feat(cli): add support for IPv6 addresses in the `--listen` flag * πŸ› fix(server.go): add nolint comment to ignore magic number warning in ipv6 check * πŸ› fix(server.go): use fmt.Sprintf to format IP and port instead of strconv.Itoa and string concatenation
1 parent 717542e commit 36c5472

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

β€ŽCHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this package will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v2.24.0
8+
9+
### Added
10+
11+
- Support for IPv6 addresses in the `--listen` flag [#191]
12+
13+
[#191]:https://github.com/tarampampam/error-pages/issues/191
14+
715
## v2.23.0
816

917
### Added

β€Žinternal/cli/shared/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var ConfigFileFlag = &cli.StringFlag{ //nolint:gochecknoglobals
1717
var ListenAddrFlag = &cli.StringFlag{ //nolint:gochecknoglobals
1818
Name: "listen",
1919
Aliases: []string{"l"},
20-
Usage: "IP address to Listen on",
20+
Usage: "IP (v4 or v6) address to Listen on",
2121
Value: "0.0.0.0",
2222
EnvVars: []string{env.ListenAddr.String()},
2323
}

β€Žinternal/http/server.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package http
22

33
import (
4-
"strconv"
4+
"errors"
5+
"fmt"
6+
"net"
7+
"strings"
58
"time"
69

710
"github.com/fasthttp/router"
@@ -57,8 +60,24 @@ func NewServer(log *zap.Logger) Server {
5760
}
5861

5962
// Start server.
60-
func (s *Server) Start(ip string, port uint16) error {
61-
return s.fast.ListenAndServe(ip + ":" + strconv.Itoa(int(port)))
63+
func (s *Server) Start(ip string, port uint16) (err error) {
64+
if net.ParseIP(ip) == nil {
65+
return errors.New("invalid IP address")
66+
}
67+
68+
var ln net.Listener
69+
70+
if strings.Count(ip, ":") >= 2 { //nolint:gomnd // ipv6
71+
if ln, err = net.Listen("tcp6", fmt.Sprintf("[%s]:%d", ip, port)); err != nil {
72+
return err
73+
}
74+
} else { // ipv4
75+
if ln, err = net.Listen("tcp4", fmt.Sprintf("%s:%d", ip, port)); err != nil {
76+
return err
77+
}
78+
}
79+
80+
return s.fast.Serve(ln)
6281
}
6382

6483
type templatePicker interface {

0 commit comments

Comments
Β (0)