Skip to content

Commit

Permalink
Merge pull request #319 from topfreegames/fix_certs
Browse files Browse the repository at this point in the history
Fix problem with non-tls on metagame-base, improve certificate parsin…
  • Loading branch information
rsafonseca authored Jul 13, 2023
2 parents 04e8c01 + e871cc7 commit 7f66c3a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
7 changes: 4 additions & 3 deletions acceptor/tcp_acceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io"
"io/ioutil"
"net"
"fmt"

"github.com/mailgun/proxyproto"
"github.com/topfreegames/pitaya/v2/conn/codec"
Expand Down Expand Up @@ -79,11 +80,11 @@ func (t *tcpPlayerConn) GetNextMessage() (b []byte, err error) {
func NewTCPAcceptor(addr string, certs ...string) *TCPAcceptor {
certificates := []tls.Certificate{}
if len(certs) != 2 && len(certs) != 0 {
panic(constants.ErrInvalidCertificates)
} else if len(certs) == 2 {
panic(constants.ErrIncorrectNumberOfCertificates)
} else if ( len(certs) == 2 && certs[0] != "" && certs[1] != "") {
cert, err := tls.LoadX509KeyPair(certs[0], certs[1])
if err != nil {
panic(constants.ErrInvalidCertificates)
panic(fmt.Errorf("%w: %w",constants.ErrInvalidCertificates,err))
}
certificates = append(certificates, cert)
}
Expand Down
16 changes: 8 additions & 8 deletions acceptor/tcp_acceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ var tcpAcceptorTables = []struct {
name string
addr string
certs []string
panicErr error
panicErr string
}{
{"test_1", "0.0.0.0:0", []string{"./fixtures/server.crt", "./fixtures/server.key"}, nil},
{"test_2", "0.0.0.0:0", []string{}, nil},
{"test_3", "127.0.0.1:0", []string{"wqd"}, constants.ErrInvalidCertificates},
{"test_4", "127.0.0.1:0", []string{"wqd", "wqdqwd"}, constants.ErrInvalidCertificates},
{"test_5", "127.0.0.1:0", []string{"wqd", "wqdqwd", "wqdqdqwd"}, constants.ErrInvalidCertificates},
{"test_1", "0.0.0.0:0", []string{"./fixtures/server.crt", "./fixtures/server.key"}, ""},
{"test_2", "0.0.0.0:0", []string{}, ""},
{"test_3", "127.0.0.1:0", []string{"wqd"}, "certificates must be exactly two"},
{"test_4", "127.0.0.1:0", []string{"wqd", "wqdqwd"}, "invalid certificates: open wqd: no such file or directory"},
{"test_5", "127.0.0.1:0", []string{"wqd", "wqdqwd", "wqdqdqwd"}, "certificates must be exactly two"},
}

func TestNewTCPAcceptorGetConnChanAndGetAddr(t *testing.T) {
t.Parallel()
for _, table := range tcpAcceptorTables {
t.Run(table.name, func(t *testing.T) {
if table.panicErr != nil {
assert.PanicsWithValue(t, table.panicErr, func() {
if table.panicErr != "" {
assert.PanicsWithError(t, table.panicErr, func() {
NewTCPAcceptor(table.addr, table.certs...)
})
} else {
Expand Down
3 changes: 2 additions & 1 deletion constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var (
ErrGroupAlreadyExists = errors.New("group already exists")
ErrGroupNotFound = errors.New("group not found")
ErrIllegalUID = errors.New("illegal uid")
ErrInvalidCertificates = errors.New("certificates must be exactly two")
ErrIncorrectNumberOfCertificates = errors.New("certificates must be exactly two")
ErrInvalidCertificates = errors.New("invalid certificates")
ErrInvalidSpanCarrier = errors.New("tracing: invalid span carrier")
ErrKickingUsers = errors.New("failed to kick users, check array with failed uids")
ErrMemberAlreadyExists = errors.New("member already exists in group")
Expand Down

0 comments on commit 7f66c3a

Please sign in to comment.