Compare commits
1 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
06ba2dfdc1 |
|
|
@ -106,3 +106,13 @@ func GetRewrites(hostMatch string) map[string]string {
|
||||||
}
|
}
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetProxyMode() string {
|
||||||
|
v := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagMode, v)
|
||||||
|
s := v.String()
|
||||||
|
if s == "" {
|
||||||
|
return "domain"
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
const nsConf = "configuration"
|
const nsConf = "configuration"
|
||||||
const flagPort = "p"
|
const flagPort = "p"
|
||||||
|
const flagMode = "mode"
|
||||||
const flagRoutes = "r"
|
const flagRoutes = "r"
|
||||||
const flagConf = "c"
|
const flagConf = "c"
|
||||||
const flagCert = "crt"
|
const flagCert = "crt"
|
||||||
|
|
@ -34,6 +35,7 @@ type toBind struct {
|
||||||
|
|
||||||
type fileConf struct {
|
type fileConf struct {
|
||||||
Port string `yaml:"p"`
|
Port string `yaml:"p"`
|
||||||
|
Mode string `yaml:"mode"`
|
||||||
Routes []string `yaml:"r"`
|
Routes []string `yaml:"r"`
|
||||||
CertPath string `yaml:"crt"`
|
CertPath string `yaml:"crt"`
|
||||||
KeyPath string `yaml:"key"`
|
KeyPath string `yaml:"key"`
|
||||||
|
|
@ -79,6 +81,9 @@ func fromFile() error {
|
||||||
if err := conf.Set(nsConf, flagPort, packable.NewString(c.Port)); err != nil {
|
if err := conf.Set(nsConf, flagPort, packable.NewString(c.Port)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if err := conf.Set(nsConf, flagMode, packable.NewString(c.Mode)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := conf.Set(nsConf, flagRoutes, packable.NewString(strings.Join(c.Routes, ","))); err != nil {
|
if err := conf.Set(nsConf, flagRoutes, packable.NewString(strings.Join(c.Routes, ","))); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -112,6 +117,7 @@ func fromFile() error {
|
||||||
func fromFlags() error {
|
func fromFlags() error {
|
||||||
binds := make([]toBind, 0)
|
binds := make([]toBind, 0)
|
||||||
binds = append(binds, addFlag(flagPort, "51555", "port to bind to"))
|
binds = append(binds, addFlag(flagPort, "51555", "port to bind to"))
|
||||||
|
binds = append(binds, addFlag(flagMode, "domain", "[domain] or [path] to match"))
|
||||||
binds = append(binds, addFlag(flagConf, "", "configuration file path"))
|
binds = append(binds, addFlag(flagConf, "", "configuration file path"))
|
||||||
binds = append(binds, addFlag(flagRoutes, "", "comma-separated routes to map, each as from:scheme://to.tld:port"))
|
binds = append(binds, addFlag(flagRoutes, "", "comma-separated routes to map, each as from:scheme://to.tld:port"))
|
||||||
binds = append(binds, addFlag(flagCert, "", "path to .crt"))
|
binds = append(binds, addFlag(flagCert, "", "path to .crt"))
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ type rewrite struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||||
newURL, err := s.lookup(mapKey(r.Host))
|
newURL, err := s.lookup(mapKey(r, config.GetProxyMode()))
|
||||||
var transport http.RoundTripper
|
var transport http.RoundTripper
|
||||||
transport = &redirPurge{
|
transport = &redirPurge{
|
||||||
proxyHost: r.Host,
|
proxyHost: r.Host,
|
||||||
|
|
@ -32,7 +32,7 @@ func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||||
baseTransport: http.DefaultTransport,
|
baseTransport: http.DefaultTransport,
|
||||||
}
|
}
|
||||||
transport = &rewrite{
|
transport = &rewrite{
|
||||||
rewrites: config.GetRewrites(mapKey(r.Host)),
|
rewrites: config.GetRewrites(mapKey(r, config.GetProxyMode())),
|
||||||
baseTransport: transport,
|
baseTransport: transport,
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -52,10 +52,20 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
||||||
return v.URL(), err
|
return v.URL(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapKey(host string) string {
|
func mapKey(r *http.Request, proxyMode string) string {
|
||||||
host = strings.Split(host, ".")[0]
|
switch proxyMode {
|
||||||
host = strings.Split(host, ":")[0]
|
case "domain":
|
||||||
return host
|
host := strings.Split(r.Host, ".")[0]
|
||||||
|
host = strings.Split(host, ":")[0]
|
||||||
|
return host
|
||||||
|
case "path":
|
||||||
|
paths := strings.Split(r.URL.Path, "/")
|
||||||
|
if len(paths) < 2 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return paths[1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) {
|
func (rp *redirPurge) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package server
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
@ -40,3 +41,35 @@ func TestRewrite(t *testing.T) {
|
||||||
t.Errorf("failed to replace: got %q, want \"b\"", b)
|
t.Errorf("failed to replace: got %q, want \"b\"", b)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMapKey(t *testing.T) {
|
||||||
|
r := &http.Request{
|
||||||
|
Host: "a.b.c:123",
|
||||||
|
URL: &url.URL{
|
||||||
|
Path: "/c/d/e",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
if v := mapKey(r, "domain"); v != "a" {
|
||||||
|
t.Errorf("failed to get domain: got %v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
if v := mapKey(r, "path"); v != "c" {
|
||||||
|
t.Errorf("failed to get domain: got %v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.Host = "a:123"
|
||||||
|
if v := mapKey(r, "domain"); v != "a" {
|
||||||
|
t.Errorf("failed to get domain: got %v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.URL.Path = ""
|
||||||
|
if v := mapKey(r, "path"); v != "" {
|
||||||
|
t.Errorf("failed to get domain: got %v", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
r.URL.Path = "/"
|
||||||
|
if v := mapKey(r, "path"); v != "" {
|
||||||
|
t.Errorf("failed to get domain: got %v", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue