Skip to content

Commit

Permalink
Unable to set IP for http/https port (#221) (#226)
Browse files Browse the repository at this point in the history
#221: Unable to set IP for http/https port
  • Loading branch information
0xERR0R authored Jun 28, 2021
1 parent 6a4537b commit 447821f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
11 changes: 9 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"blocky/log"
"fmt"
"os"
"strconv"
"strings"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -58,8 +60,13 @@ func initConfig() {
cfg = config.NewConfig(configPath, false)
log.ConfigureLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogTimestamp)

if cfg.HTTPPort != 0 {
apiPort = cfg.HTTPPort
if cfg.HTTPPort != "" {
split := strings.Split(cfg.HTTPPort, ":")
port, err := strconv.Atoi(split[len(split)-1])

if err == nil {
apiPort = uint16(port)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ type Config struct {
LogFormat string `yaml:"logFormat"`
LogTimestamp bool `yaml:"logTimestamp"`
Port string `yaml:"port"`
HTTPPort uint16 `yaml:"httpPort"`
HTTPSPort uint16 `yaml:"httpsPort"`
HTTPPort string `yaml:"httpPort"`
HTTPSPort string `yaml:"httpsPort"`
DisableIPv6 bool `yaml:"disableIPv6"`
CertFile string `yaml:"httpsCertFile"`
KeyFile string `yaml:"httpsKeyFile"`
Expand Down
7 changes: 4 additions & 3 deletions docs/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ queryLog:

# optional: DNS listener port and bind ip address, default 53 (UDP and TCP). Example: 53, :53, 127.0.0.1:53
port: 53
# optional: HTTP listener port, default 0 = no http listener. If > 0, will be used for prometheus metrics, pprof, REST API, DoH ...
# optional: HTTP listener port and bind ip address, default empty = no http listener. If > 0, will be used for prometheus metrics, pprof, REST API, DoH ... Example: 4000, :4000, 127.0.0.1:4000
httpPort: 4000
# optional: HTTPS listener port and bind ip address, default empty = no http listener. If > 0, will be used for prometheus metrics, pprof, REST API, DoH... Example: 443, :443, 127.0.0.1:443
httpPort: 4000
# optional: HTTPS listener port, default 0 = no http listener. If > 0, will be used for prometheus metrics, pprof, REST API, DoH...
#httpsPort: 443
# mandatory, if https port > 0: path to cert and key file for SSL encryption
#httpsCertFile: server.crt
Expand All @@ -143,4 +144,4 @@ logLevel: info
# optional: Log format (text or json). Default: text
logFormat: text
# optional: log timestamps. Default: true
logTimestamp: true
logTimestamp: true
6 changes: 3 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ configuration properties as [JSON](config.yml).

| Parameter | Mandatory | Default value | Description |
| --------------- | --------- | -------------------| ------------------------------------------------- |
| port | no | 53 | IP and Port to serve DNS endpoint (TCP and UDP). If you wish to specify a specific IP, you can do so such as 192.168.0.1:53 |
| httpPort | no | 0 | HTTP listener port. If > 0, will be used for prometheus metrics, pprof, REST API, DoH ... |
| httpsPort | no | 0 | HTTPS listener port. If > 0, will be used for prometheus metrics, pprof, REST API, DoH... |
| port | no | 53 | Port and optional bind ip address to serve DNS endpoint (TCP and UDP). If you wish to specify a specific IP, you can do so such as 192.168.0.1:53. Example: 53, :53, :127.0.0.1:53 |
| httpPort | no | | HTTP listener port and optional bind ip address . If > 0, will be used for prometheus metrics, pprof, REST API, DoH ...If you wish to specify a specific IP, you can do so such as 192.168.0.1:4000. Example: 4000, :4000, 127.0.0.1:4000 |
| httpsPort | no | | HTTPS listener port and optional bind ip address . If > 0, will be used for prometheus metrics, pprof, REST API, DoH... If you wish to specify a specific IP, you can do so such as 192.168.0.1:443 |
| httpsCertFile | yes, if httpsPort > 0 | | path to cert and key file for SSL encryption |
| httpsKeyFile | yes, if httpsPort > 0 | | path to cert and key file for SSL encryption |
| bootstrapDns | no | | use this DNS server to resolve blacklist urls and upstream DNS servers (DoH). Useful if no DNS resolver is configured and blocky needs to resolve a host name. Format net:IP:port, net must be udp or tcp|
Expand Down
28 changes: 14 additions & 14 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ func logger() *logrus.Entry {
return log.PrefixedLog("server")
}

func getServerAddress(cfg *config.Config) string {
address := cfg.Port
if !strings.Contains(cfg.Port, ":") {
address = fmt.Sprintf(":%s", cfg.Port)
func getServerAddress(addr string) string {
address := addr
if !strings.Contains(addr, ":") {
address = fmt.Sprintf(":%s", addr)
}

return address
}

// NewServer creates new server instance with passed config
func NewServer(cfg *config.Config) (server *Server, err error) {
address := getServerAddress(cfg)
address := getServerAddress(cfg.Port)

log.ConfigureLogger(cfg.LogLevel, cfg.LogFormat, cfg.LogTimestamp)

Expand All @@ -58,21 +58,21 @@ func NewServer(cfg *config.Config) (server *Server, err error) {

router := createRouter(cfg)

if cfg.HTTPPort > 0 {
if httpListener, err = net.Listen("tcp", fmt.Sprintf(":%d", cfg.HTTPPort)); err != nil {
return nil, fmt.Errorf("start http listener on port %d failed: %w", cfg.HTTPPort, err)
if cfg.HTTPPort != "" {
if httpListener, err = net.Listen("tcp", getServerAddress(cfg.HTTPPort)); err != nil {
return nil, fmt.Errorf("start http listener on %s failed: %w", cfg.HTTPPort, err)
}

metrics.Start(router, cfg.Prometheus)
}

if cfg.HTTPSPort > 0 {
if cfg.HTTPSPort != "" {
if cfg.CertFile == "" || cfg.KeyFile == "" {
return nil, fmt.Errorf("httpsCertFile and httpsKeyFile parameters are mandatory for HTTPS")
}

if httpsListener, err = net.Listen("tcp", fmt.Sprintf(":%d", cfg.HTTPSPort)); err != nil {
return nil, fmt.Errorf("start https listener on port %d failed: %w", cfg.HTTPSPort, err)
if httpsListener, err = net.Listen("tcp", getServerAddress(cfg.HTTPSPort)); err != nil {
return nil, fmt.Errorf("start https listener on port %s failed: %w", cfg.HTTPSPort, err)
}

metrics.Start(router, cfg.Prometheus)
Expand Down Expand Up @@ -176,7 +176,7 @@ func (s *Server) printConfiguration() {
}

logger().Infof("- DNS listening port: '%s'", s.cfg.Port)
logger().Infof("- HTTP listening port: %d", s.cfg.HTTPPort)
logger().Infof("- HTTP listening on addr/port: %s", s.cfg.HTTPPort)

logger().Info("runtime information:")

Expand Down Expand Up @@ -219,7 +219,7 @@ func (s *Server) Start() {

go func() {
if s.httpListener != nil {
logger().Infof("http server is up and running on port %d", s.cfg.HTTPPort)
logger().Infof("http server is up and running on addr/port %s", s.cfg.HTTPPort)

err := http.Serve(s.httpListener, s.httpMux)
util.FatalOnError("start http listener failed: ", err)
Expand All @@ -228,7 +228,7 @@ func (s *Server) Start() {

go func() {
if s.httpsListener != nil {
logger().Infof("https server is up and running on port %d", s.cfg.HTTPSPort)
logger().Infof("https server is up and running on addr/port %s", s.cfg.HTTPSPort)

err := http.ServeTLS(s.httpsListener, s.httpMux, s.cfg.CertFile, s.cfg.KeyFile)
util.FatalOnError("start https listener failed: ", err)
Expand Down
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var _ = Describe("Running DNS server", func() {
},

Port: "55555",
HTTPPort: 4000,
HTTPPort: "4000",
LogLevel: "info",
Prometheus: config.PrometheusConfig{
Enable: true,
Expand Down

0 comments on commit 447821f

Please sign in to comment.