-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from systemli/Implement-Domain-Lookup
✨ Implement Domain Lookup
- Loading branch information
Showing
10 changed files
with
342 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
type Domain struct { | ||
userli UserliService | ||
} | ||
|
||
func NewDomain(userli UserliService) *Domain { | ||
return &Domain{userli: userli} | ||
} | ||
|
||
func (d *Domain) Handle(conn net.Conn) { | ||
defer conn.Close() | ||
command := make([]byte, 4096) | ||
_, err := conn.Read(command) | ||
if err != nil { | ||
fmt.Println("Error reading:", err.Error()) | ||
} | ||
|
||
command = bytes.Trim(command, "\x00") | ||
parts := strings.Split(string(command), " ") | ||
if len(parts) < 2 || parts[0] != "get" { | ||
fmt.Println("Invalid command") | ||
_, _ = conn.Write([]byte("400 Bad Request\n")) | ||
return | ||
} | ||
|
||
domain := strings.TrimSuffix(parts[1], "\n") | ||
exists, err := d.userli.GetDomain(string(domain)) | ||
if err != nil { | ||
fmt.Println("Error fetching domain:", err.Error()) | ||
_, _ = conn.Write([]byte("400 Error fetching domain\n")) | ||
return | ||
} | ||
|
||
if !exists { | ||
_, _ = conn.Write([]byte("500 NO%20RESULT\n")) | ||
return | ||
} | ||
|
||
_, _ = conn.Write([]byte("200 1\n")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/rand" | ||
"errors" | ||
"math/big" | ||
"net" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type DomainTestSuite struct { | ||
suite.Suite | ||
|
||
wg *sync.WaitGroup | ||
ctx context.Context | ||
} | ||
|
||
func (s *DomainTestSuite) SetupTest() { | ||
s.wg = &sync.WaitGroup{} | ||
s.ctx = context.Background() | ||
} | ||
|
||
func (s *DomainTestSuite) AfterTest(_, _ string) { | ||
s.ctx.Done() | ||
} | ||
|
||
func (s *DomainTestSuite) TestDomain() { | ||
userli := new(MockUserliService) | ||
userli.On("GetDomain", "example.com").Return(true, nil) | ||
userli.On("GetDomain", "notfound.com").Return(false, nil) | ||
userli.On("GetDomain", "error.com").Return(false, errors.New("error")) | ||
|
||
portNumber, _ := rand.Int(rand.Reader, big.NewInt(65535-20000)) | ||
listen := ":" + portNumber.String() | ||
|
||
domain := NewDomain(userli) | ||
|
||
go StartTCPServer(s.ctx, s.wg, listen, domain.Handle) | ||
|
||
// wait until the server is ready | ||
for { | ||
conn, err := net.Dial("tcp", listen) | ||
if err == nil { | ||
conn.Close() | ||
break | ||
} | ||
} | ||
|
||
s.Run("success", func() { | ||
conn, err := net.Dial("tcp", listen) | ||
s.NoError(err) | ||
|
||
_, err = conn.Write([]byte("get example.com")) | ||
s.NoError(err) | ||
|
||
response := make([]byte, 4096) | ||
_, err = conn.Read(response) | ||
s.NoError(err) | ||
|
||
s.Equal("200 1\n", string(bytes.Trim(response, "\x00"))) | ||
|
||
conn.Close() | ||
}) | ||
|
||
s.Run("not found", func() { | ||
conn, err := net.Dial("tcp", listen) | ||
s.NoError(err) | ||
|
||
_, err = conn.Write([]byte("get notfound.com")) | ||
s.NoError(err) | ||
|
||
response := make([]byte, 4096) | ||
_, err = conn.Read(response) | ||
s.NoError(err) | ||
|
||
s.Equal("500 NO%20RESULT\n", string(bytes.Trim(response, "\x00"))) | ||
|
||
conn.Close() | ||
}) | ||
|
||
s.Run("error", func() { | ||
conn, err := net.Dial("tcp", listen) | ||
s.NoError(err) | ||
|
||
_, err = conn.Write([]byte("get error.com")) | ||
s.NoError(err) | ||
|
||
response := make([]byte, 4096) | ||
_, err = conn.Read(response) | ||
s.NoError(err) | ||
|
||
s.Equal("400 Error fetching domain\n", string(bytes.Trim(response, "\x00"))) | ||
|
||
conn.Close() | ||
}) | ||
} | ||
|
||
func TestDomain(t *testing.T) { | ||
suite.Run(t, new(DomainTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package main | ||
|
||
import "net" | ||
|
||
// PostfixHandler is an interface that defines the Handle method | ||
// for handling postfix postmap requests. | ||
// See https://www.postfix.org/postmap.1.html | ||
type PostfixHandler interface { | ||
Handle(conn net.Conn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"sync" | ||
) | ||
|
||
func StartTCPServer(ctx context.Context, wg *sync.WaitGroup, addr string, handler func(net.Conn)) { | ||
defer wg.Done() | ||
|
||
listener, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
fmt.Println("Error creating listener:", err.Error()) | ||
return | ||
} | ||
defer listener.Close() | ||
|
||
go func() { | ||
<-ctx.Done() | ||
listener.Close() | ||
}() | ||
|
||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
if ctx.Err() != nil { | ||
fmt.Println("Server stopped on port ", addr) | ||
return | ||
} | ||
fmt.Println("Error accepting connection:", err.Error()) | ||
continue | ||
} | ||
|
||
go handler(conn) | ||
} | ||
} |
Oops, something went wrong.