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

Add tls.handshake_match.version matcher #155

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Current matchers:
- **layer4.matchers.proxy_protocol** - matches connections that start with [HAPROXY proxy protocol](https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt).
- **layer4.matchers.socks4** - matches connections that look like [SOCKSv4](https://www.openssh.com/txt/socks4.protocol).
- **layer4.matchers.socks5** - matches connections that look like [SOCKSv5](https://www.rfc-editor.org/rfc/rfc1928.html).
- **tls.handshake_match.version** - matches TLS Version from ClientHello, using strings from [VersionName](https://go.dev/src/crypto/tls/common.go).

Current handlers:

Expand Down
55 changes: 55 additions & 0 deletions modules/l4tls/version_matcher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020 Matthew Holt
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package l4tls

import (
"crypto/tls"

"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddytls"
)

func init() {
caddy.RegisterModule(MatchVersion{})
}

type MatchVersion []string

// CaddyModule returns the Caddy module information.
func (MatchVersion) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "tls.handshake_match.version",
New: func() caddy.Module { return new(MatchVersion) },
}
}

// Match against connection TLS "Version" by name. Eg: "TLS 1.0"
// See https://go.dev/src/crypto/tls/common.go
func (m MatchVersion) Match(hello *tls.ClientHelloInfo) bool {
clientVersions := hello.SupportedVersions
for _, mVersion := range m {
for _, clientVersion := range clientVersions {
if mVersion == tls.VersionName(clientVersion) {
return true
}
}
}
return false
}

// Interface guards
var (
_ caddytls.ConnectionMatcher = (*MatchVersion)(nil)
)