-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
l4xmpp: Add support for matching XMPP connections, match TLS-ALPN (#33)
* Add generic ALPN matcher * Add XMPP matcher * Add XMPP matcher to imports.go * Apply gofmt and remove superfluous line
- Loading branch information
Showing
3 changed files
with
108 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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(MatchALPN{}) | ||
} | ||
|
||
type MatchALPN []string | ||
|
||
// CaddyModule returns the Caddy module information. | ||
func (MatchALPN) CaddyModule() caddy.ModuleInfo { | ||
return caddy.ModuleInfo{ | ||
ID: "tls.handshake_match.alpn", | ||
New: func() caddy.Module { return new(MatchALPN) }, | ||
} | ||
} | ||
|
||
func (m MatchALPN) Match(hello *tls.ClientHelloInfo) bool { | ||
clientProtocols := hello.SupportedProtos | ||
for _, alpn := range m { | ||
for _, clientProtocol := range clientProtocols { | ||
if alpn == string(clientProtocol) { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Interface guards | ||
var ( | ||
_ caddytls.ConnectionMatcher = (*MatchALPN)(nil) | ||
) |
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,54 @@ | ||
// 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 l4xmpp | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
|
||
"github.com/caddyserver/caddy/v2" | ||
"github.com/mholt/caddy-l4/layer4" | ||
) | ||
|
||
func init() { | ||
caddy.RegisterModule(MatchXMPP{}) | ||
} | ||
|
||
// MatchXMPP is able to match XMPP connections. | ||
type MatchXMPP struct{} | ||
|
||
// CaddyModule returns the Caddy module information. | ||
func (MatchXMPP) CaddyModule() caddy.ModuleInfo { | ||
return caddy.ModuleInfo{ | ||
ID: "layer4.matchers.xmpp", | ||
New: func() caddy.Module { return new(MatchXMPP) }, | ||
} | ||
} | ||
|
||
// Match returns true if the connection looks like XMPP. | ||
func (m MatchXMPP) Match(cx *layer4.Connection) (bool, error) { | ||
p := make([]byte, minXmppLength) | ||
n, err := io.ReadFull(cx, p) | ||
if err != nil || n < minXmppLength { // needs at least 50 (fix for adium/pidgin) | ||
return false, nil | ||
} | ||
return strings.Contains(string(p), xmppWord), nil | ||
} | ||
|
||
var xmppWord = "jabber" | ||
var minXmppLength = 50 | ||
|
||
// Interface guard | ||
var _ layer4.ConnMatcher = (*MatchXMPP)(nil) |