aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-04-03 19:36:14 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-04-03 19:36:14 +0200
commitadc9524911d91c68e0b5c9b2ef0f2e0bd81e5c68 (patch)
tree4a399f3f9e8267fd07938e2b2ca5affdd3943355
parent3b6c7c42268ef6349f88f9a56c58d7b6573d4830 (diff)
Add Entries
-rw-r--r--route.go16
-rw-r--r--rpc.go30
-rw-r--r--server.go10
3 files changed, 39 insertions, 17 deletions
diff --git a/route.go b/route.go
index 47c1ebd..19e0115 100644
--- a/route.go
+++ b/route.go
@@ -10,21 +10,21 @@ import (
"os"
)
-// Route defines a set of routes including correspondent TLS certificates
-type Route map[string]route
+// Routes defines a set of routes including correspondent TLS certificates
+type Routes map[string]Route
-type route struct {
+type Route struct {
ServerName *url.URL
Upstream *url.URL
Certificate *tls.Certificate
}
-func (r route) String() string {
+func (r Route) String() string {
return fmt.Sprintf("%v → %v", r.ServerName, r.Upstream)
}
// GetCertificate returns certificate for SNI negotiation
-func (r Route) GetCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error) {
+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
@@ -39,7 +39,7 @@ func (r Route) GetCertificate(h *tls.ClientHelloInfo) (*tls.Certificate, error)
}
// Save routes to persistent file
-func (r Route) Save(fname string) error {
+func (r Routes) Save(fname string) error {
fd, err := os.Create(fname)
if err != nil {
return err
@@ -49,7 +49,7 @@ func (r Route) Save(fname string) error {
}
// Load routes from persistent file
-func (r *Route) Load(fname string) error {
+func (r *Routes) Load(fname string) error {
fd, err := os.Open(fname)
if err != nil {
return err
@@ -58,7 +58,7 @@ func (r *Route) Load(fname string) error {
return json.NewDecoder(fd).Decode(r)
}
-func (r Route) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
+func (r Routes) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
for _, v := range r {
fmt.Fprintln(w, v)
}
diff --git a/rpc.go b/rpc.go
index bf34484..a96bb61 100644
--- a/rpc.go
+++ b/rpc.go
@@ -2,10 +2,12 @@ package goxy
import (
"crypto/tls"
+ "encoding/json"
"errors"
"fmt"
"net/rpc"
"net/url"
+ "os"
)
var (
@@ -16,6 +18,8 @@ var (
const RPCPort = ":8000"
+type Entries map[string]Entry
+
// Entry holds routing settings
type Entry struct {
Host string // URL
@@ -56,7 +60,7 @@ func (s *GoXY) Add(e Entry, _ *struct{}) error {
if host.Path == "" {
host.Path = "/"
}
- r := route{
+ r := Route{
ServerName: host,
Upstream: up,
}
@@ -71,7 +75,7 @@ func (s *GoXY) Add(e Entry, _ *struct{}) error {
r.Certificate = &crt
}
defer s.server.Save(s.server.DataFile)
- s.server.Route[host.Host] = r
+ s.server.Routes[host.Host] = r
return s.server.Update()
}
@@ -84,15 +88,33 @@ func (s *GoXY) Del(host string, _ *struct{}) error {
if h.Host == "" {
return ErrEmptyHost
}
- delete(s.server.Route, h.Host)
+ delete(s.server.Routes, h.Host)
s.server.Save(s.server.DataFile)
return s.server.Update()
}
// List routes
func (s GoXY) List(_ struct{}, ret *[]string) error {
- for _, v := range s.server.Route {
+ for _, v := range s.server.Routes {
*ret = append(*ret, fmt.Sprint(v))
}
return nil
}
+
+func (e Entries) Save(fname string) error {
+ fd, err := os.Create(fname)
+ if err != nil {
+ return err
+ }
+ defer fd.Close()
+ return json.NewEncoder(fd).Encode(e)
+}
+
+func (e *Entries) Load(fname string) error {
+ fd, err := os.Open(fname)
+ if err != nil {
+ return err
+ }
+ defer fd.Close()
+ return json.NewDecoder(fd).Decode(e)
+}
diff --git a/server.go b/server.go
index 9fe2571..da541c0 100644
--- a/server.go
+++ b/server.go
@@ -8,7 +8,7 @@ import (
type Server struct {
DataFile string
- Route
+ Routes
wwwServer http.Server
tlsServer http.Server
rpcServer http.Server
@@ -18,10 +18,10 @@ func NewServer(dataFile, listenWWW, listenTLS, listenRPC string) (*Server, error
if listenRPC == "" {
listenRPC = RPCPort
}
- r := make(Route)
+ r := make(Routes)
server := &Server{
DataFile: dataFile,
- Route: r,
+ Routes: r,
wwwServer: http.Server{
Addr: listenWWW,
},
@@ -39,7 +39,7 @@ func NewServer(dataFile, listenWWW, listenTLS, listenRPC string) (*Server, error
server.Load(dataFile)
}
RegisterRPC(server)
- http.Handle("/debug/route", server.Route)
+ http.Handle("/debug/route", server.Routes)
return server, server.Update()
}
@@ -47,7 +47,7 @@ func NewServer(dataFile, listenWWW, listenTLS, listenRPC string) (*Server, error
func (s *Server) Update() error {
wwwMux := http.NewServeMux()
tlsMux := http.NewServeMux()
- for _, v := range s.Route {
+ for _, v := range s.Routes {
host := v.ServerName.Host + v.ServerName.Path
up := v.Upstream
switch v.ServerName.Scheme {