Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
698edf7e45 | ||
|
|
48e0048216 |
@@ -106,13 +106,3 @@ 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,7 +14,6 @@ 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"
|
||||||
@@ -35,7 +34,6 @@ 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"`
|
||||||
@@ -81,9 +79,6 @@ 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
|
||||||
}
|
}
|
||||||
@@ -117,7 +112,6 @@ 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"))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"io"
|
"io"
|
||||||
"local/rproxy3/config"
|
"local/rproxy3/config"
|
||||||
"local/rproxy3/storage/packable"
|
"local/rproxy3/storage/packable"
|
||||||
@@ -24,15 +25,16 @@ 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, config.GetProxyMode()))
|
newURL, err := s.lookup(mapKey(r.Host))
|
||||||
var transport http.RoundTripper
|
var transport http.RoundTripper
|
||||||
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
transport = &redirPurge{
|
transport = &redirPurge{
|
||||||
proxyHost: r.Host,
|
proxyHost: r.Host,
|
||||||
targetHost: newURL.Host,
|
targetHost: newURL.Host,
|
||||||
baseTransport: http.DefaultTransport,
|
baseTransport: http.DefaultTransport,
|
||||||
}
|
}
|
||||||
transport = &rewrite{
|
transport = &rewrite{
|
||||||
rewrites: config.GetRewrites(mapKey(r, config.GetProxyMode())),
|
rewrites: config.GetRewrites(mapKey(r.Host)),
|
||||||
baseTransport: transport,
|
baseTransport: transport,
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -52,20 +54,10 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
|||||||
return v.URL(), err
|
return v.URL(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapKey(r *http.Request, proxyMode string) string {
|
func mapKey(host string) string {
|
||||||
switch proxyMode {
|
host = strings.Split(host, ".")[0]
|
||||||
case "domain":
|
host = strings.Split(host, ":")[0]
|
||||||
host := strings.Split(r.Host, ".")[0]
|
return host
|
||||||
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,7 +3,6 @@ package server
|
|||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -41,35 +40,3 @@ 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
25
vendor/vendor.json
vendored
25
vendor/vendor.json
vendored
@@ -1,25 +0,0 @@
|
|||||||
{
|
|
||||||
"comment": "",
|
|
||||||
"ignore": "test",
|
|
||||||
"package": [
|
|
||||||
{
|
|
||||||
"checksumSHA1": "GtamqiJoL7PGHsN454AoffBFMa8=",
|
|
||||||
"path": "golang.org/x/net/context",
|
|
||||||
"revision": "65e2d4e15006aab9813ff8769e768bbf4bb667a0",
|
|
||||||
"revisionTime": "2019-02-01T23:59:58Z"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"checksumSHA1": "HoCvrd3hEhsFeBOdEw7cbcfyk50=",
|
|
||||||
"path": "golang.org/x/time/rate",
|
|
||||||
"revision": "fbb02b2291d28baffd63558aa44b4b56f178d650",
|
|
||||||
"revisionTime": "2018-04-12T16:56:04Z"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"checksumSHA1": "QqDq2x8XOU7IoOR98Cx1eiV5QY8=",
|
|
||||||
"path": "gopkg.in/yaml.v2",
|
|
||||||
"revision": "51d6538a90f86fe93ac480b35f37b2be17fef232",
|
|
||||||
"revisionTime": "2018-11-15T11:05:04Z"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"rootPath": "local/rproxy3"
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user