aboutsummaryrefslogtreecommitdiff
path: root/route.go
blob: c8c91918caf0566074376895247841f844888069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package goxy

import (
	"crypto/tls"
	"errors"
)

// Route defines a set of routes including correspondent TLS certificates
type Route map[string]Entry

// GetCertificate returns certificate for SNI negotiation
func (r Route) GetCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error) {
	if e, ok := r[h.ServerName]; ok && e.cert != nil {
		return e.cert, nil
	}
	return nil, errors.New("no cert for " + h.ServerName)
}

// Entry holds routing settings
type Entry struct {
	ServerName string           // HostName
	Upstream   string           // URL
	Cert       []byte           // PEM
	Key        []byte           // PEM
	cert       *tls.Certificate // Parsed
}

func (e Entry) String() string {
	ret := e.ServerName + " → " + e.Upstream
	if e.cert != nil {
		ret += " with TLS"
	}
	return ret
}