aboutsummaryrefslogtreecommitdiff
path: root/route.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-04-04 01:33:02 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-04-04 01:33:02 +0200
commite4324fd473bf878306b3df387bd1bea08cdd604c (patch)
treef3005edbe48d10ed1958d8eef399cc51a0f70a7f /route.go
parentb9b8a680bae0590bfaeb954aee56b2057db41b1a (diff)
kiss
Diffstat (limited to 'route.go')
-rw-r--r--route.go66
1 files changed, 36 insertions, 30 deletions
diff --git a/route.go b/route.go
index 7ef8645..c6794cb 100644
--- a/route.go
+++ b/route.go
@@ -2,35 +2,42 @@ package goxy
import (
"crypto/tls"
+ "encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
+ "os"
)
// Routes defines a set of routes including correspondent TLS certificates
type Routes map[string]Route
type Route struct {
- ServerName *url.URL
- Upstream *url.URL
- Certificate *tls.Certificate
+ Host, Upstream string
+ Cert, Key []byte
+ serverName *url.URL
+ upstream *url.URL
+ certificate *tls.Certificate
}
func (r Route) String() string {
- return fmt.Sprintf("%v → %v", r.ServerName, r.Upstream)
+ if r.certificate != nil {
+ return fmt.Sprintf("%v → %v with TLS", r.serverName, r.upstream)
+ }
+ return fmt.Sprintf("%v → %v", r.serverName, r.upstream)
}
// GetCertificate returns certificate for SNI negotiation
func (r Routes) GetCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error) {
host := h.ServerName
- if v, ok := r[host]; ok && v.Certificate != nil {
- return v.Certificate, nil
+ if v, ok := r[host]; ok && v.certificate != nil {
+ return v.certificate, nil
}
- // HACK search for certs with port speciefied
+ // HACK search for certs without port
for k, v := range r {
if k[:len(host)] == host {
- return v.Certificate, nil
+ return v.certificate, nil
}
}
return nil, errors.New("no cert for " + host)
@@ -42,32 +49,31 @@ func (r Routes) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
}
}
-func NewRoute(e Entry) (Route, error) {
- fail := func(err error) (Route, error) { return Route{}, err }
- host, err := url.Parse(e.Host)
+func (r Routes) Save(fname string) error {
+ fd, err := os.Create(fname)
if err != nil {
- return fail(err)
+ return err
}
- up, err := url.Parse(e.Upstream)
+ defer fd.Close()
+ return json.NewEncoder(fd).Encode(r)
+}
+
+func (r *Routes) Load(fname string) error {
+ fd, err := os.Open(fname)
if err != nil {
- return fail(err)
+ return err
}
- if host.Host == "" || up.Host == "" {
- return fail(ErrNoHost)
- }
- if host.Path == "" {
- host.Path = "/"
+ defer fd.Close()
+ return json.NewDecoder(fd).Decode(r)
+}
+
+func Slug(host string) (string, bool, error) {
+ h, err := url.Parse(host)
+ if err != nil {
+ return "", false, err
}
- r := Route{ServerName: host, Upstream: up}
- if host.Scheme == "https" {
- if e.Cert == nil || e.Key == nil {
- return fail(ErrNoCert)
- }
- cert, err := tls.X509KeyPair(e.Cert, e.Key)
- if err != nil {
- return fail(err)
- }
- r.Certificate = &cert
+ if h.Path == "" {
+ h.Path = "/"
}
- return r, nil
+ return h.Host + h.Path, h.Scheme == "https", nil
}