mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	templates: TLSVersion (#2323)
* new template action: TLS protocol version * new template action: use caddytls.GetSupportedProtocolName Avoids code duplication by reusing existing method to get TLS protocol version used on connection. Also adds tests
This commit is contained in:
		
							parent
							
								
									03b10f9c8e
								
							
						
					
					
						commit
						d3e3fc533f
					
				@ -31,6 +31,7 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/mholt/caddy/caddytls"
 | 
				
			||||||
	"github.com/russross/blackfriday"
 | 
						"github.com/russross/blackfriday"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -448,6 +449,15 @@ func (c Context) AddLink(link string) string {
 | 
				
			|||||||
	return ""
 | 
						return ""
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Returns either TLS protocol version if TLS used or empty string otherwise
 | 
				
			||||||
 | 
					func (c Context) TLSVersion() (ret string) {
 | 
				
			||||||
 | 
						if c.Req.TLS != nil {
 | 
				
			||||||
 | 
							// Safe to ignore an error
 | 
				
			||||||
 | 
							ret, _ = caddytls.GetSupportedProtocolName(c.Req.TLS.Version)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// buffer pool for .Include context actions
 | 
					// buffer pool for .Include context actions
 | 
				
			||||||
var includeBufs = sync.Pool{
 | 
					var includeBufs = sync.Pool{
 | 
				
			||||||
	New: func() interface{} {
 | 
						New: func() interface{} {
 | 
				
			||||||
 | 
				
			|||||||
@ -16,6 +16,7 @@ package httpserver
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
 | 
						"crypto/tls"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
@ -922,3 +923,40 @@ func TestAddLink(t *testing.T) {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestTlsVersion(t *testing.T) {
 | 
				
			||||||
 | 
						for _, test := range []struct {
 | 
				
			||||||
 | 
							tlsState       *tls.ConnectionState
 | 
				
			||||||
 | 
							expectedResult string
 | 
				
			||||||
 | 
						}{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								&tls.ConnectionState{Version: tls.VersionTLS10},
 | 
				
			||||||
 | 
								"tls1.0",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								&tls.ConnectionState{Version: tls.VersionTLS11},
 | 
				
			||||||
 | 
								"tls1.1",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								&tls.ConnectionState{Version: tls.VersionTLS12},
 | 
				
			||||||
 | 
								"tls1.2",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// TLS not used
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								nil,
 | 
				
			||||||
 | 
								"",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// Unsupported version
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								&tls.ConnectionState{Version: 0x0399},
 | 
				
			||||||
 | 
								"",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						} {
 | 
				
			||||||
 | 
							context := getContextOrFail(t)
 | 
				
			||||||
 | 
							context.Req.TLS = test.tlsState
 | 
				
			||||||
 | 
							result := context.TLSVersion()
 | 
				
			||||||
 | 
							if result != test.expectedResult {
 | 
				
			||||||
 | 
								t.Errorf("Expected %s got %s", test.expectedResult, result)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user