summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-05-19 00:38:36 +0300
committerPaul Buetow <paul@buetow.org>2023-05-19 00:38:36 +0300
commite881e64406bc8f2d0d5d3099a48d3cf4774e7bd8 (patch)
tree587c6e5d91ba73b9a6f56ac8c2dfa1711b9d5b79
parentdde3bed148ea90cd505bf6fb67b08719ebc8933a (diff)
can reject non-participants
-rw-r--r--gorum.json1
-rw-r--r--internal/config.go29
-rw-r--r--internal/quorum.go9
-rw-r--r--internal/run.go6
-rw-r--r--internal/tcpserver.go16
-rw-r--r--internal/vote.go2
6 files changed, 51 insertions, 12 deletions
diff --git a/gorum.json b/gorum.json
index a2560d5..0e28cfa 100644
--- a/gorum.json
+++ b/gorum.json
@@ -1,4 +1,5 @@
{
+ "StateDir": "./",
"Address": "earth:1234",
"Participants": [
"earth:1234",
diff --git a/internal/config.go b/internal/config.go
index 1ed0bd8..f8f6b10 100644
--- a/internal/config.go
+++ b/internal/config.go
@@ -3,10 +3,14 @@ package internal
import (
"encoding/json"
"io/ioutil"
+ "log"
+ "net"
"os"
+ "strings"
)
type config struct {
+ StateDir string
Address string
Participants []string
}
@@ -32,3 +36,28 @@ func newConfig(configFile string) (config, error) {
return config, nil
}
+
+func (c config) isParticipant(remoteAddr string) bool {
+ remoteIP := stripPort(remoteAddr)
+
+ for _, participant := range c.Participants {
+ ips, err := net.LookupIP(stripPort(participant))
+ if err != nil {
+ log.Println(err)
+ continue
+ }
+
+ for _, ip := range ips {
+ if remoteIP == ip.String() {
+ return true
+ }
+ }
+ }
+
+ return false
+}
+
+func stripPort(addr string) string {
+ parts := strings.Split(addr, ":")
+ return parts[0]
+}
diff --git a/internal/quorum.go b/internal/quorum.go
index 528f460..1307f20 100644
--- a/internal/quorum.go
+++ b/internal/quorum.go
@@ -1,6 +1,9 @@
package internal
-import "time"
+import (
+ "log"
+ "time"
+)
type quorum struct {
ID string
@@ -9,3 +12,7 @@ type quorum struct {
}
type quorumMap map[string]quorum
+
+func (q quorumMap) vote(v vote) {
+ log.Printf("Adding vote %v", v)
+}
diff --git a/internal/run.go b/internal/run.go
index 4165f50..8b7faa9 100644
--- a/internal/run.go
+++ b/internal/run.go
@@ -2,7 +2,6 @@ package internal
import (
"context"
- "log"
)
func Run(ctx context.Context, configFile string) {
@@ -12,19 +11,20 @@ func Run(ctx context.Context, configFile string) {
}
ch := make(chan vote)
+ quorum := make(quorumMap)
go func() {
for {
select {
case vote := <-ch:
- log.Println(vote)
+ quorum.vote(vote)
case <-ctx.Done():
return
}
}
}()
- if err := startTcpServer(ctx, config.Address, ch); err != nil {
+ if err := startTcpServer(ctx, config, ch); err != nil {
panic(err)
}
}
diff --git a/internal/tcpserver.go b/internal/tcpserver.go
index c65fb0b..a7153f8 100644
--- a/internal/tcpserver.go
+++ b/internal/tcpserver.go
@@ -9,25 +9,29 @@ import (
"time"
)
-func startTcpServer(ctx context.Context, address string, ch chan<- vote) error {
- listener, err := net.Listen("tcp", address)
+func startTcpServer(ctx context.Context, config config, ch chan<- vote) error {
+ listener, err := net.Listen("tcp", config.Address)
if err != nil {
return fmt.Errorf("Error starting TCP server: %s", err.Error())
}
defer listener.Close()
- log.Printf("TCP server started on %s\n", address)
+ log.Printf("TCP server started on %s\n", config.Address)
for {
- // Accept incoming client connections
conn, err := listener.Accept()
if err != nil {
log.Printf("Error accepting connection: %s\n", err.Error())
continue
}
- log.Printf("Client connected: %s\n", conn.RemoteAddr().String())
+ if !config.isParticipant(conn.RemoteAddr().String()) {
+ log.Printf("Denying connection, peer not a participant: %v\n", conn.RemoteAddr().String())
+ conn.Close()
+ continue
+ }
+ log.Printf("Client connected: %s\n", conn.RemoteAddr().String())
go handleConnection(ctx, conn, ch)
}
}
@@ -35,8 +39,6 @@ func startTcpServer(ctx context.Context, address string, ch chan<- vote) error {
func handleConnection(ctx context.Context, conn net.Conn, ch chan<- vote) {
defer conn.Close()
remoteAddr := conn.RemoteAddr().String()
- // TODO: Reject remote connection when not in participant list!
- log.Printf("Client %s connected\n", remoteAddr)
reader := bufio.NewReader(conn)
for {
diff --git a/internal/vote.go b/internal/vote.go
index 7c7268a..322c620 100644
--- a/internal/vote.go
+++ b/internal/vote.go
@@ -4,6 +4,6 @@ import "time"
type vote struct {
From string
- //IDs []string
+ IDs []string
time time.Time
}