Skip to content

Commit ed4bdac

Browse files
committed
fix: add check listen address
Signed-off-by: Zixuan Liu <nodeces@gmail.com>
1 parent 5bab3ba commit ed4bdac

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ type Config struct {
1616
JoinAddress string
1717
// DataDir holds raft data.
1818
DataDir string
19-
// ListenAddress is a network address for raft server and HTTP server.
19+
// ListenAddress is a network address for raft server and HTTP(S) server,
20+
// the address is a specified address, such as 10.1.1.19:6780.
2021
ListenAddress string
2122
// TLSConfig is used to configure a TLS server and client.
22-
// You have to provide a peer certificate.
23-
// We recommend using cfssl tool to create this certificates.
23+
// If TLSConfig is not nil, we will set TLSConfig to the raft server and the HTTPS server,
24+
// otherwise we will start a server without any security.
25+
//
26+
// Note:
27+
// You have to provide a peer certificate when TLSConfig is not nil,
28+
// we recommend using cfssl tool to create this certificates.
2429
TLSConfig *tls.Config
2530
// RaftConfig provides any necessary configuration for the Raft server.
2631
RaftConfig *raft.Config

dispatcher.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hraftdispatcher
33
import (
44
"context"
55
"crypto/tls"
6+
"fmt"
67
"github.com/soheilhy/cmux"
78
"net"
89

@@ -53,8 +54,20 @@ func NewHRaftDispatcher(config *Config) (*HRaftDispatcher, error) {
5354

5455
logger := zap.NewExample()
5556

57+
// check ListenAddress is network address
58+
listenAddress, err := net.ResolveTCPAddr("tcp", config.ListenAddress)
59+
if err != nil {
60+
return nil, err
61+
}
62+
if listenAddress.IP == nil {
63+
return nil, errors.New("host is omitted in ListenAddress")
64+
}
65+
ip := net.ParseIP(listenAddress.IP.String())
66+
if ip != nil && ip.IsUnspecified() {
67+
return nil, fmt.Errorf("cannot use unspecified IP %s", ip)
68+
}
69+
5670
var ln net.Listener
57-
var err error
5871
if config.TLSConfig == nil {
5972
ln, err = net.Listen("tcp", config.ListenAddress)
6073
} else {
@@ -71,7 +84,6 @@ func NewHRaftDispatcher(config *Config) (*HRaftDispatcher, error) {
7184

7285
streamLayer, err := store.NewTCPStreamLayer(raftLn, config.TLSConfig)
7386
if err != nil {
74-
logger.Error(err.Error())
7587
return nil, err
7688
}
7789

dispatcher_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package hraftdispatcher
33
import (
44
"crypto/tls"
55
"crypto/x509"
6+
"github.com/casbin/hraft-dispatcher/store/mocks"
67
"io/ioutil"
78
"os"
89
"testing"
@@ -529,3 +530,27 @@ m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
529530

530531
return e, dispatcher, nil
531532
}
533+
534+
func TestNewHRaftDispatcher(t *testing.T) {
535+
_, err := NewHRaftDispatcher(&Config{
536+
Enforcer: &mocks.MockIDistributedEnforcer{},
537+
ServerID: "test",
538+
JoinAddress: "",
539+
DataDir: "/tmp/hraft-dispatcher",
540+
ListenAddress: ":6780",
541+
TLSConfig: nil,
542+
RaftConfig: nil,
543+
})
544+
assert.EqualError(t, err, "host is omitted in ListenAddress")
545+
546+
_, err = NewHRaftDispatcher(&Config{
547+
Enforcer: &mocks.MockIDistributedEnforcer{},
548+
ServerID: "test",
549+
JoinAddress: "",
550+
DataDir: "/tmp/hraft-dispatcher",
551+
ListenAddress: "0.0.0.0:6780",
552+
TLSConfig: nil,
553+
RaftConfig: nil,
554+
})
555+
assert.EqualError(t, err, "cannot use unspecified IP 0.0.0.0")
556+
}

0 commit comments

Comments
 (0)