Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support TLS server #565

Open
wants to merge 27 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0be1a63
feat: support TLS server
Apr 2, 2024
32ec507
chore: remove unused code, update testcase
Apr 2, 2024
545325a
Merge branch 'panjf2000:dev' into dev
leslie-fei Apr 7, 2024
86e717e
chore: optimizing error handling in the OnTraffic code.
Apr 7, 2024
a31400b
fix: remove tls/internal/boring package, remove the test code section…
Apr 7, 2024
2933c91
chore: tlsEventHandler OnTraffic use cached buffer
Apr 7, 2024
cb3879b
chore: tlsEventHandler OnTraffic use bbPool Buffer
Apr 7, 2024
584e828
Merge branch 'panjf2000:dev' into dev
leslie-fei Apr 17, 2024
4bcf522
fix: tls event handler the conditional statement issue.
Apr 22, 2024
ef12c18
Merge remote-tracking branch 'origin/dev' into dev
Apr 22, 2024
5025980
feat: support edge-triggered I/O (#576)
panjf2000 Apr 19, 2024
d040b2e
doc: update READMEs
panjf2000 Apr 19, 2024
e526169
feat: support multiple network addresses binding (#578)
panjf2000 Apr 21, 2024
7717171
opt: only enable SO_REUSEPORT on Linux and FreeBSD (#580)
panjf2000 Apr 21, 2024
0e982e4
opt: don't disable SO_REUSEPORT on DragonFlyBSD (#583)
panjf2000 Apr 21, 2024
cb4650c
opt: disable SO_REUSEPORT on Unix domain sockets (#584)
panjf2000 Apr 21, 2024
8ec5de2
opt: enable ET mode on listener event-loop by default (#585)
panjf2000 Apr 21, 2024
98d55a3
chore: don't print warning log in eventloop.close
panjf2000 Apr 21, 2024
dae569d
opt: refine the code of I/O handlers (#586)
panjf2000 Apr 21, 2024
68f2f01
opt: reduce duplicate code of I/O processing (#587)
panjf2000 Apr 22, 2024
46521e5
chore: update READMEs
panjf2000 Apr 22, 2024
53a05cf
chore: update READMEs
panjf2000 Apr 22, 2024
dfd7e00
feat: support TLS server
Apr 2, 2024
0cb6673
chore: move the TLS test code from gnet_test to tls_test in order to …
Apr 22, 2024
2900b77
Merge remote-tracking branch 'gnet-origin/dev' into dev
Apr 22, 2024
79b2a67
fix: TLS1.2 non-block read
Apr 23, 2024
c4221b5
chore: remove debug code
Apr 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ func Run(eventHandler EventHandler, protoAddr string, opts ...Option) error {
}
logging.Cleanup()
}()

// upgrade to TLS EventHandler
if options.TLSConfig != nil {
eventHandler = &tlsEventHandler{EventHandler: eventHandler, tlsConfig: options.TLSConfig}
}

return run(eventHandler, listeners, options, []string{protoAddr})
}

Expand All @@ -560,6 +566,12 @@ func Rotate(eventHandler EventHandler, addrs []string, opts ...Option) error {
}
logging.Cleanup()
}()

// upgrade to TLS EventHandler
if options.TLSConfig != nil {
eventHandler = &tlsEventHandler{EventHandler: eventHandler, tlsConfig: options.TLSConfig}
}

return run(eventHandler, listeners, options, addrs)
}

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0
go.uber.org/zap v1.21.0 // don't upgrade this one
golang.org/x/sync v0.6.0
golang.org/x/sys v0.16.0
golang.org/x/sys v0.18.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

Expand All @@ -15,7 +15,8 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.17
go 1.21
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -55,6 +57,8 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
11 changes: 11 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/panjf2000/gnet/v2/pkg/tls"
)

// Option is a function that will set up option.
Expand Down Expand Up @@ -127,6 +128,9 @@ type Options struct {
// Don't enable it unless you are 100% sure what you are doing.
// Note that this option is only available for stream-oriented protocol.
EdgeTriggeredIO bool

// TLSConfig support TLS
TLSConfig *tls.Config
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this an interface instead of a pointer of a specific struct, that way we can implement TLS outside the gnet and pass the interface to the gnet. Decoupling the TLS from gnet also enables the users to use other TLS implementations that implement the interface of gnet TLS.

}

// WithOptions sets up all options.
Expand Down Expand Up @@ -261,3 +265,10 @@ func WithEdgeTriggeredIO(et bool) Option {
opts.EdgeTriggeredIO = et
}
}

// WithTLSConfig sets support TLS
func WithTLSConfig(tlsConfig *tls.Config) Option {
return func(opts *Options) {
opts.TLSConfig = tlsConfig
}
}
109 changes: 109 additions & 0 deletions pkg/tls/alert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tls

import "strconv"

// An AlertError is a TLS alert.
//
// When using a QUIC transport, QUICConn methods will return an error
// which wraps AlertError rather than sending a TLS alert.
type AlertError uint8

func (e AlertError) Error() string {
return alert(e).String()
}

type alert uint8

const (
// alert level
alertLevelWarning = 1
alertLevelError = 2
)

const (
alertCloseNotify alert = 0
alertUnexpectedMessage alert = 10
alertBadRecordMAC alert = 20
alertDecryptionFailed alert = 21
alertRecordOverflow alert = 22
alertDecompressionFailure alert = 30
alertHandshakeFailure alert = 40
alertBadCertificate alert = 42
alertUnsupportedCertificate alert = 43
alertCertificateRevoked alert = 44
alertCertificateExpired alert = 45
alertCertificateUnknown alert = 46
alertIllegalParameter alert = 47
alertUnknownCA alert = 48
alertAccessDenied alert = 49
alertDecodeError alert = 50
alertDecryptError alert = 51
alertExportRestriction alert = 60
alertProtocolVersion alert = 70
alertInsufficientSecurity alert = 71
alertInternalError alert = 80
alertInappropriateFallback alert = 86
alertUserCanceled alert = 90
alertNoRenegotiation alert = 100
alertMissingExtension alert = 109
alertUnsupportedExtension alert = 110
alertCertificateUnobtainable alert = 111
alertUnrecognizedName alert = 112
alertBadCertificateStatusResponse alert = 113
alertBadCertificateHashValue alert = 114
alertUnknownPSKIdentity alert = 115
alertCertificateRequired alert = 116
alertNoApplicationProtocol alert = 120
)

var alertText = map[alert]string{
alertCloseNotify: "close notify",
alertUnexpectedMessage: "unexpected message",
alertBadRecordMAC: "bad record MAC",
alertDecryptionFailed: "decryption failed",
alertRecordOverflow: "record overflow",
alertDecompressionFailure: "decompression failure",
alertHandshakeFailure: "handshake failure",
alertBadCertificate: "bad certificate",
alertUnsupportedCertificate: "unsupported certificate",
alertCertificateRevoked: "revoked certificate",
alertCertificateExpired: "expired certificate",
alertCertificateUnknown: "unknown certificate",
alertIllegalParameter: "illegal parameter",
alertUnknownCA: "unknown certificate authority",
alertAccessDenied: "access denied",
alertDecodeError: "error decoding message",
alertDecryptError: "error decrypting message",
alertExportRestriction: "export restriction",
alertProtocolVersion: "protocol version not supported",
alertInsufficientSecurity: "insufficient security level",
alertInternalError: "internal error",
alertInappropriateFallback: "inappropriate fallback",
alertUserCanceled: "user canceled",
alertNoRenegotiation: "no renegotiation",
alertMissingExtension: "missing extension",
alertUnsupportedExtension: "unsupported extension",
alertCertificateUnobtainable: "certificate unobtainable",
alertUnrecognizedName: "unrecognized name",
alertBadCertificateStatusResponse: "bad certificate status response",
alertBadCertificateHashValue: "bad certificate hash value",
alertUnknownPSKIdentity: "unknown PSK identity",
alertCertificateRequired: "certificate required",
alertNoApplicationProtocol: "no application protocol",
}

func (e alert) String() string {
s, ok := alertText[e]
if ok {
return "tls: " + s
}
return "tls: alert(" + strconv.Itoa(int(e)) + ")"
}

func (e alert) Error() string {
return e.String()
}