parent
e20ba5361d
commit
bc11dd7f82
|
|
@ -0,0 +1,115 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"local/rproxy3/storage/packable"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetPort() string {
|
||||||
|
v := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagPort, v)
|
||||||
|
return ":" + strings.TrimPrefix(v.String(), ":")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRoutes() map[string]string {
|
||||||
|
v := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagRoutes, v)
|
||||||
|
m := make(map[string]string)
|
||||||
|
for _, v := range strings.Split(v.String(), ",") {
|
||||||
|
if len(v) == 0 {
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
from := v[:strings.Index(v, ":")]
|
||||||
|
to := v[strings.Index(v, ":")+1:]
|
||||||
|
m[from] = to
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTCP() (string, bool) {
|
||||||
|
v := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagTCP, v)
|
||||||
|
tcpAddr := v.String()
|
||||||
|
return tcpAddr, notEmpty(tcpAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSSL() (string, string, bool) {
|
||||||
|
v := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagCert, v)
|
||||||
|
certPath := v.String()
|
||||||
|
conf.Get(nsConf, flagKey, v)
|
||||||
|
keyPath := v.String()
|
||||||
|
return certPath, keyPath, notEmpty(certPath, keyPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAuth() (string, string, bool) {
|
||||||
|
v := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagUser, v)
|
||||||
|
user := v.String()
|
||||||
|
conf.Get(nsConf, flagPass, v)
|
||||||
|
pass := v.String()
|
||||||
|
return user, pass, notEmpty(user, pass)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notEmpty(s ...string) bool {
|
||||||
|
for i := range s {
|
||||||
|
if s[i] == "" || s[i] == "/dev/null" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRate() (int, int) {
|
||||||
|
r := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagRate, r)
|
||||||
|
b := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagBurst, b)
|
||||||
|
|
||||||
|
rate, err := strconv.Atoi(r.String())
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("illegal rate: %v", err)
|
||||||
|
rate = 5
|
||||||
|
}
|
||||||
|
burst, _ := strconv.Atoi(b.String())
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("illegal burst: %v", err)
|
||||||
|
burst = 5
|
||||||
|
}
|
||||||
|
|
||||||
|
return rate, burst
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTimeout() int {
|
||||||
|
t := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagTimeout, t)
|
||||||
|
|
||||||
|
timeout, err := strconv.Atoi(t.String())
|
||||||
|
if err != nil || timeout == 5 {
|
||||||
|
return 5
|
||||||
|
}
|
||||||
|
|
||||||
|
return timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRewrites(hostMatch string) map[string]string {
|
||||||
|
v := packable.NewString()
|
||||||
|
conf.Get(nsConf, flagRewrites, v)
|
||||||
|
m := make(map[string]string)
|
||||||
|
for _, v := range strings.Split(v.String(), ",") {
|
||||||
|
vs := strings.Split(v, ":")
|
||||||
|
if len(v) < 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
host := vs[0]
|
||||||
|
if host != hostMatch {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
from := vs[1]
|
||||||
|
to := strings.Join(vs[2:], ":")
|
||||||
|
m[from] = to
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,161 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"io/ioutil"
|
||||||
|
"local/rproxy3/storage"
|
||||||
|
"local/rproxy3/storage/packable"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
const nsConf = "configuration"
|
||||||
|
const flagPort = "p"
|
||||||
|
const flagRoutes = "r"
|
||||||
|
const flagConf = "c"
|
||||||
|
const flagCert = "crt"
|
||||||
|
const flagTCP = "tcp"
|
||||||
|
const flagKey = "key"
|
||||||
|
const flagUser = "user"
|
||||||
|
const flagPass = "pass"
|
||||||
|
const flagRate = "rate"
|
||||||
|
const flagBurst = "burst"
|
||||||
|
const flagTimeout = "timeout"
|
||||||
|
const flagRewrites = "rw"
|
||||||
|
|
||||||
|
var conf = storage.NewMap()
|
||||||
|
|
||||||
|
type toBind struct {
|
||||||
|
flag string
|
||||||
|
value *string
|
||||||
|
}
|
||||||
|
|
||||||
|
type fileConf struct {
|
||||||
|
Port string `yaml:"p"`
|
||||||
|
Routes []string `yaml:"r"`
|
||||||
|
CertPath string `yaml:"crt"`
|
||||||
|
TCPPath string `yaml:"tcp"`
|
||||||
|
KeyPath string `yaml:"key"`
|
||||||
|
Username string `yaml:"user"`
|
||||||
|
Password string `yaml:"pass"`
|
||||||
|
Rate string `yaml:"rate"`
|
||||||
|
Burst string `yaml:"burst"`
|
||||||
|
Timeout string `yaml:"timeout"`
|
||||||
|
Rewrites []string `yaml:"rw"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init() error {
|
||||||
|
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
|
||||||
|
log.SetFlags(log.Ltime | log.Lshortfile)
|
||||||
|
if err := fromFile(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := fromFlags(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromFile() error {
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||||
|
defer func() {
|
||||||
|
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
||||||
|
}()
|
||||||
|
flag.String(flagConf, "/dev/null", "yaml config file path")
|
||||||
|
flag.Parse()
|
||||||
|
confFlag := flag.Lookup(flagConf)
|
||||||
|
if confFlag == nil || confFlag.Value.String() == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
confBytes, err := ioutil.ReadFile(confFlag.Value.String())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var c fileConf
|
||||||
|
if err := yaml.Unmarshal(confBytes, &c); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagPort, packable.NewString(c.Port)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagRoutes, packable.NewString(strings.Join(c.Routes, ","))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagCert, packable.NewString(c.CertPath)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagTCP, packable.NewString(c.TCPPath)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagKey, packable.NewString(c.KeyPath)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagUser, packable.NewString(c.Username)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagPass, packable.NewString(c.Password)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagRate, packable.NewString(c.Rate)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagBurst, packable.NewString(c.Burst)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagTimeout, packable.NewString(c.Timeout)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, flagRewrites, packable.NewString(strings.Join(c.Rewrites, ","))); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func fromFlags() error {
|
||||||
|
binds := make([]toBind, 0)
|
||||||
|
binds = append(binds, addFlag(flagPort, "51555", "port to bind to"))
|
||||||
|
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(flagCert, "", "path to .crt"))
|
||||||
|
binds = append(binds, addFlag(flagTCP, "", "tcp addr"))
|
||||||
|
binds = append(binds, addFlag(flagKey, "", "path to .key"))
|
||||||
|
binds = append(binds, addFlag(flagUser, "", "basic auth username"))
|
||||||
|
binds = append(binds, addFlag(flagPass, "", "basic auth password"))
|
||||||
|
binds = append(binds, addFlag(flagRate, "100", "rate limit per second"))
|
||||||
|
binds = append(binds, addFlag(flagBurst, "100", "rate limit burst"))
|
||||||
|
binds = append(binds, addFlag(flagTimeout, "30", "seconds to wait for limiter"))
|
||||||
|
binds = append(binds, addFlag(flagRewrites, "", "comma-separated from:replace:replacement:oauth to rewrite in response bodies"))
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
for _, bind := range binds {
|
||||||
|
confFlag := flag.Lookup(bind.flag)
|
||||||
|
if confFlag == nil || confFlag.Value.String() == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := conf.Set(nsConf, bind.flag, packable.NewString(*bind.value)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addFlag(key, def, help string) toBind {
|
||||||
|
def = getFlagOrDefault(key, def)
|
||||||
|
v := flag.String(key, def, help)
|
||||||
|
return toBind{
|
||||||
|
flag: key,
|
||||||
|
value: v,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getFlagOrDefault(key, def string) string {
|
||||||
|
v := packable.NewString()
|
||||||
|
if err := conf.Get(nsConf, key, v); err != nil {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return v.String()
|
||||||
|
}
|
||||||
|
|
@ -1,115 +1,77 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"local/rproxy3/storage/packable"
|
"fmt"
|
||||||
"log"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetPort() string {
|
type Proxy struct {
|
||||||
v := packable.NewString()
|
To string
|
||||||
conf.Get(nsConf, flagPort, v)
|
BOAuthZ bool
|
||||||
return ":" + strings.TrimPrefix(v.String(), ":")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRoutes() map[string]string {
|
func parseProxy(s string) (string, Proxy) {
|
||||||
v := packable.NewString()
|
p := Proxy{}
|
||||||
conf.Get(nsConf, flagRoutes, v)
|
key := ""
|
||||||
m := make(map[string]string)
|
l := strings.Split(s, ",")
|
||||||
for _, v := range strings.Split(v.String(), ",") {
|
if len(l) > 0 {
|
||||||
if len(v) == 0 {
|
key = l[0]
|
||||||
return m
|
|
||||||
}
|
|
||||||
from := v[:strings.Index(v, ":")]
|
|
||||||
to := v[strings.Index(v, ":")+1:]
|
|
||||||
m[from] = to
|
|
||||||
}
|
}
|
||||||
return m
|
if len(l) > 1 {
|
||||||
|
p.To = l[1]
|
||||||
|
}
|
||||||
|
if len(l) > 2 {
|
||||||
|
p.BOAuthZ = l[2] == "true"
|
||||||
|
}
|
||||||
|
return key, p
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTCP() (string, bool) {
|
func GetBOAuthZ() (string, bool) {
|
||||||
v := packable.NewString()
|
boauthz := conf.Get("oauth").GetString()
|
||||||
conf.Get(nsConf, flagTCP, v)
|
return boauthz, boauthz != ""
|
||||||
tcpAddr := v.String()
|
|
||||||
return tcpAddr, notEmpty(tcpAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetSSL() (string, string, bool) {
|
|
||||||
v := packable.NewString()
|
|
||||||
conf.Get(nsConf, flagCert, v)
|
|
||||||
certPath := v.String()
|
|
||||||
conf.Get(nsConf, flagKey, v)
|
|
||||||
keyPath := v.String()
|
|
||||||
return certPath, keyPath, notEmpty(certPath, keyPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAuth() (string, string, bool) {
|
func GetAuth() (string, string, bool) {
|
||||||
v := packable.NewString()
|
user := conf.Get("user").GetString()
|
||||||
conf.Get(nsConf, flagUser, v)
|
pass := conf.Get("pass").GetString()
|
||||||
user := v.String()
|
return user, pass, user != "" && pass != ""
|
||||||
conf.Get(nsConf, flagPass, v)
|
|
||||||
pass := v.String()
|
|
||||||
return user, pass, notEmpty(user, pass)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func notEmpty(s ...string) bool {
|
func GetPort() string {
|
||||||
for i := range s {
|
port := conf.Get("p").GetInt()
|
||||||
if s[i] == "" || s[i] == "/dev/null" {
|
return ":" + fmt.Sprint(port)
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRate() (int, int) {
|
func GetRate() (int, int) {
|
||||||
r := packable.NewString()
|
rate := conf.Get("r").GetInt()
|
||||||
conf.Get(nsConf, flagRate, r)
|
burst := conf.Get("b").GetInt()
|
||||||
b := packable.NewString()
|
|
||||||
conf.Get(nsConf, flagBurst, b)
|
|
||||||
|
|
||||||
rate, err := strconv.Atoi(r.String())
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("illegal rate: %v", err)
|
|
||||||
rate = 5
|
|
||||||
}
|
|
||||||
burst, _ := strconv.Atoi(b.String())
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("illegal burst: %v", err)
|
|
||||||
burst = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
return rate, burst
|
return rate, burst
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTimeout() int {
|
func GetRoutes() map[string]Proxy {
|
||||||
t := packable.NewString()
|
list := conf.Get("proxy").GetString()
|
||||||
conf.Get(nsConf, flagTimeout, t)
|
definitions := strings.Split(list, ",,")
|
||||||
|
routes := make(map[string]Proxy)
|
||||||
timeout, err := strconv.Atoi(t.String())
|
for _, definition := range definitions {
|
||||||
if err != nil || timeout == 5 {
|
k, v := parseProxy(definition)
|
||||||
return 5
|
routes[k] = v
|
||||||
}
|
}
|
||||||
|
return routes
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSSL() (string, string, bool) {
|
||||||
|
crt := conf.Get("crt").GetString()
|
||||||
|
key := conf.Get("key").GetString()
|
||||||
|
return crt, key, crt != "" && key != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTCP() (string, bool) {
|
||||||
|
tcp := conf.Get("tcp").GetString()
|
||||||
|
return tcp, tcp != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTimeout() time.Duration {
|
||||||
|
timeout := conf.Get("timeout").GetDuration()
|
||||||
return timeout
|
return timeout
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRewrites(hostMatch string) map[string]string {
|
|
||||||
v := packable.NewString()
|
|
||||||
conf.Get(nsConf, flagRewrites, v)
|
|
||||||
m := make(map[string]string)
|
|
||||||
for _, v := range strings.Split(v.String(), ",") {
|
|
||||||
vs := strings.Split(v, ":")
|
|
||||||
if len(v) < 3 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
host := vs[0]
|
|
||||||
if host != hostMatch {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
from := vs[1]
|
|
||||||
to := strings.Join(vs[2:], ":")
|
|
||||||
m[from] = to
|
|
||||||
}
|
|
||||||
return m
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,161 +1,49 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"fmt"
|
||||||
"io/ioutil"
|
"local/args"
|
||||||
"local/rproxy3/storage"
|
|
||||||
"local/rproxy3/storage/packable"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
yaml "gopkg.in/yaml.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const nsConf = "configuration"
|
var conf *args.ArgSet
|
||||||
const flagPort = "p"
|
|
||||||
const flagRoutes = "r"
|
|
||||||
const flagConf = "c"
|
|
||||||
const flagCert = "crt"
|
|
||||||
const flagTCP = "tcp"
|
|
||||||
const flagKey = "key"
|
|
||||||
const flagUser = "user"
|
|
||||||
const flagPass = "pass"
|
|
||||||
const flagRate = "rate"
|
|
||||||
const flagBurst = "burst"
|
|
||||||
const flagTimeout = "timeout"
|
|
||||||
const flagRewrites = "rw"
|
|
||||||
|
|
||||||
var conf = storage.NewMap()
|
func init() {
|
||||||
|
if err := Refresh(); err != nil {
|
||||||
type toBind struct {
|
panic(err)
|
||||||
flag string
|
}
|
||||||
value *string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type fileConf struct {
|
func Refresh() error {
|
||||||
Port string `yaml:"p"`
|
|
||||||
Routes []string `yaml:"r"`
|
|
||||||
CertPath string `yaml:"crt"`
|
|
||||||
TCPPath string `yaml:"tcp"`
|
|
||||||
KeyPath string `yaml:"key"`
|
|
||||||
Username string `yaml:"user"`
|
|
||||||
Password string `yaml:"pass"`
|
|
||||||
Rate string `yaml:"rate"`
|
|
||||||
Burst string `yaml:"burst"`
|
|
||||||
Timeout string `yaml:"timeout"`
|
|
||||||
Rewrites []string `yaml:"rw"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func Init() error {
|
|
||||||
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
|
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
|
||||||
log.SetFlags(log.Ltime | log.Lshortfile)
|
log.SetFlags(log.Ltime | log.Lshortfile)
|
||||||
if err := fromFile(); err != nil {
|
|
||||||
return err
|
as, err := parseArgs()
|
||||||
}
|
if err != nil && !strings.Contains(fmt.Sprint(os.Args), "-test") {
|
||||||
if err := fromFlags(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
conf = as
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromFile() error {
|
func parseArgs() (*args.ArgSet, error) {
|
||||||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
as := args.NewArgSet()
|
||||||
defer func() {
|
|
||||||
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
|
as.Append(args.STRING, "user", "username for basic auth", "")
|
||||||
}()
|
as.Append(args.STRING, "pass", "password for basic auth", "")
|
||||||
flag.String(flagConf, "/dev/null", "yaml config file path")
|
as.Append(args.INT, "p", "port for service", 51555)
|
||||||
flag.Parse()
|
as.Append(args.INT, "r", "rate per second for requests", 100)
|
||||||
confFlag := flag.Lookup(flagConf)
|
as.Append(args.INT, "b", "burst requests", 100)
|
||||||
if confFlag == nil || confFlag.Value.String() == "" {
|
as.Append(args.STRING, "crt", "path to crt for ssl", "")
|
||||||
return nil
|
as.Append(args.STRING, "key", "path to key for ssl", "")
|
||||||
}
|
as.Append(args.STRING, "tcp", "address for tcp only tunnel", "")
|
||||||
confBytes, err := ioutil.ReadFile(confFlag.Value.String())
|
as.Append(args.DURATION, "timeout", "timeout for tunnel", time.Minute)
|
||||||
if err != nil {
|
as.Append(args.STRING, "proxy", "double-comma separated from,scheme://to.tld:port,oauth,,", "")
|
||||||
return err
|
as.Append(args.STRING, "oauth", "url for boauthz", "")
|
||||||
}
|
|
||||||
var c fileConf
|
err := as.Parse()
|
||||||
if err := yaml.Unmarshal(confBytes, &c); err != nil {
|
return as, err
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagPort, packable.NewString(c.Port)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagRoutes, packable.NewString(strings.Join(c.Routes, ","))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagCert, packable.NewString(c.CertPath)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagTCP, packable.NewString(c.TCPPath)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagKey, packable.NewString(c.KeyPath)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagUser, packable.NewString(c.Username)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagPass, packable.NewString(c.Password)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagRate, packable.NewString(c.Rate)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagBurst, packable.NewString(c.Burst)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagTimeout, packable.NewString(c.Timeout)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, flagRewrites, packable.NewString(strings.Join(c.Rewrites, ","))); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func fromFlags() error {
|
|
||||||
binds := make([]toBind, 0)
|
|
||||||
binds = append(binds, addFlag(flagPort, "51555", "port to bind to"))
|
|
||||||
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(flagCert, "", "path to .crt"))
|
|
||||||
binds = append(binds, addFlag(flagTCP, "", "tcp addr"))
|
|
||||||
binds = append(binds, addFlag(flagKey, "", "path to .key"))
|
|
||||||
binds = append(binds, addFlag(flagUser, "", "basic auth username"))
|
|
||||||
binds = append(binds, addFlag(flagPass, "", "basic auth password"))
|
|
||||||
binds = append(binds, addFlag(flagRate, "100", "rate limit per second"))
|
|
||||||
binds = append(binds, addFlag(flagBurst, "100", "rate limit burst"))
|
|
||||||
binds = append(binds, addFlag(flagTimeout, "30", "seconds to wait for limiter"))
|
|
||||||
binds = append(binds, addFlag(flagRewrites, "", "comma-separated from:replace:replacement:oauth to rewrite in response bodies"))
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
for _, bind := range binds {
|
|
||||||
confFlag := flag.Lookup(bind.flag)
|
|
||||||
if confFlag == nil || confFlag.Value.String() == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := conf.Set(nsConf, bind.flag, packable.NewString(*bind.value)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func addFlag(key, def, help string) toBind {
|
|
||||||
def = getFlagOrDefault(key, def)
|
|
||||||
v := flag.String(key, def, help)
|
|
||||||
return toBind{
|
|
||||||
flag: key,
|
|
||||||
value: v,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getFlagOrDefault(key, def string) string {
|
|
||||||
v := packable.NewString()
|
|
||||||
if err := conf.Get(nsConf, key, v); err != nil {
|
|
||||||
return def
|
|
||||||
}
|
|
||||||
return v.String()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -6,7 +6,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := config.Init(); err != nil {
|
if err := config.Refresh(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
14
main_test.go
14
main_test.go
|
|
@ -34,8 +34,8 @@ func TestHTTPSMain(t *testing.T) {
|
||||||
"username",
|
"username",
|
||||||
"-pass",
|
"-pass",
|
||||||
"password",
|
"password",
|
||||||
"-r",
|
"-proxy",
|
||||||
"hello:" + addr,
|
"hello," + addr,
|
||||||
"-crt",
|
"-crt",
|
||||||
"./testdata/rproxy3server.crt",
|
"./testdata/rproxy3server.crt",
|
||||||
"-key",
|
"-key",
|
||||||
|
|
@ -51,7 +51,7 @@ func TestHTTPSMain(t *testing.T) {
|
||||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
r, _ := http.NewRequest("GET", "https://hello.localhost"+port, nil)
|
r, _ := http.NewRequest("GET", "https://hello.localhost:"+port, nil)
|
||||||
|
|
||||||
if resp, err := client.Do(r); err != nil {
|
if resp, err := client.Do(r); err != nil {
|
||||||
t.Fatalf("client failed: %v", err)
|
t.Fatalf("client failed: %v", err)
|
||||||
|
|
@ -89,8 +89,8 @@ func TestHTTPMain(t *testing.T) {
|
||||||
"username",
|
"username",
|
||||||
"-pass",
|
"-pass",
|
||||||
"password",
|
"password",
|
||||||
"-r",
|
"-proxy",
|
||||||
"hello:" + addr,
|
"hello," + addr,
|
||||||
}
|
}
|
||||||
main()
|
main()
|
||||||
}()
|
}()
|
||||||
|
|
@ -98,7 +98,7 @@ func TestHTTPMain(t *testing.T) {
|
||||||
time.Sleep(time.Millisecond * 100)
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
r, _ := http.NewRequest("GET", "http://hello.localhost"+port, nil)
|
r, _ := http.NewRequest("GET", "http://hello.localhost:"+port, nil)
|
||||||
|
|
||||||
if resp, err := client.Do(r); err != nil {
|
if resp, err := client.Do(r); err != nil {
|
||||||
t.Fatalf("client failed: %v", err)
|
t.Fatalf("client failed: %v", err)
|
||||||
|
|
@ -127,5 +127,5 @@ func echoServer() (string, func()) {
|
||||||
func getPort() string {
|
func getPort() string {
|
||||||
s := httptest.NewServer(nil)
|
s := httptest.NewServer(nil)
|
||||||
s.Close()
|
s.Close()
|
||||||
return s.URL[strings.LastIndex(s.URL, ":"):]
|
return s.URL[strings.LastIndex(s.URL, ":")+1:]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"io"
|
"io"
|
||||||
"local/rproxy3/config"
|
|
||||||
"local/rproxy3/storage/packable"
|
"local/rproxy3/storage/packable"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -33,10 +32,6 @@ func (s *Server) Proxy(w http.ResponseWriter, r *http.Request) {
|
||||||
targetHost: newURL.Host,
|
targetHost: newURL.Host,
|
||||||
baseTransport: http.DefaultTransport,
|
baseTransport: http.DefaultTransport,
|
||||||
}
|
}
|
||||||
transport = &rewrite{
|
|
||||||
rewrites: config.GetRewrites(mapKey(r.Host)),
|
|
||||||
baseTransport: transport,
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
log.Printf("unknown host lookup %q", r.Host)
|
log.Printf("unknown host lookup %q", r.Host)
|
||||||
|
|
@ -54,6 +49,12 @@ func (s *Server) lookup(host string) (*url.URL, error) {
|
||||||
return v.URL(), err
|
return v.URL(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) lookupBOAuthZ(host string) (bool, error) {
|
||||||
|
v := packable.NewString()
|
||||||
|
err := s.db.Get(nsBOAuthZ, host, v)
|
||||||
|
return v.String() != "", err
|
||||||
|
}
|
||||||
|
|
||||||
func mapKey(host string) string {
|
func mapKey(host string) string {
|
||||||
host = strings.Split(host, ".")[0]
|
host = strings.Split(host, ".")[0]
|
||||||
host = strings.Split(host, ":")[0]
|
host = strings.Split(host, ":")[0]
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,9 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"local/oauth2/oauth2client"
|
||||||
"local/rproxy3/config"
|
"local/rproxy3/config"
|
||||||
"local/rproxy3/storage"
|
"local/rproxy3/storage"
|
||||||
"local/rproxy3/storage/packable"
|
"local/rproxy3/storage/packable"
|
||||||
|
|
@ -20,6 +22,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const nsRouting = "routing"
|
const nsRouting = "routing"
|
||||||
|
const nsBOAuthZ = "oauth"
|
||||||
|
|
||||||
type listenerScheme int
|
type listenerScheme int
|
||||||
|
|
||||||
|
|
@ -49,12 +52,13 @@ type Server struct {
|
||||||
limiter *rate.Limiter
|
limiter *rate.Limiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Route(src, dst string) error {
|
func (s *Server) Route(src string, dst config.Proxy) error {
|
||||||
log.Printf("Adding route %q -> %q...\n", src, dst)
|
log.Printf("Adding route %q -> %v...\n", src, dst)
|
||||||
u, err := url.Parse(dst)
|
u, err := url.Parse(dst.To)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
s.db.Set(nsBOAuthZ, src, packable.NewString(fmt.Sprint(dst.BOAuthZ)))
|
||||||
return s.db.Set(nsRouting, src, packable.NewURL(u))
|
return s.db.Set(nsRouting, src, packable.NewURL(u))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,7 +107,6 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
rusr, rpwd, ok := config.GetAuth()
|
rusr, rpwd, ok := config.GetAuth()
|
||||||
if ok {
|
if ok {
|
||||||
//usr, pwd := getProxyAuth(r)
|
|
||||||
usr, pwd, ok := r.BasicAuth()
|
usr, pwd, ok := r.BasicAuth()
|
||||||
if !ok || rusr != usr || rpwd != pwd {
|
if !ok || rusr != usr || rpwd != pwd {
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
|
|
@ -111,6 +114,17 @@ func (s *Server) doAuth(foo http.HandlerFunc) http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ok, err := s.lookupBOAuthZ(mapKey(r.Host))
|
||||||
|
if err != nil {
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if boauthz, useoauth := config.GetBOAuthZ(); ok && useoauth {
|
||||||
|
err := oauth2client.Authenticate(boauthz, w, r)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
foo(w, r)
|
foo(w, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package server
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"local/rproxy3/config"
|
||||||
"local/rproxy3/storage"
|
"local/rproxy3/storage"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
@ -15,7 +16,10 @@ import (
|
||||||
func TestServerStart(t *testing.T) {
|
func TestServerStart(t *testing.T) {
|
||||||
server := mockServer()
|
server := mockServer()
|
||||||
|
|
||||||
if err := server.Route("world", "http://hello.localhost"+server.addr); err != nil {
|
p := config.Proxy{
|
||||||
|
To: "http://hello.localhost" + server.addr,
|
||||||
|
}
|
||||||
|
if err := server.Route("world", p); err != nil {
|
||||||
t.Fatalf("cannot add route: %v", err)
|
t.Fatalf("cannot add route: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,7 +52,10 @@ func mockServer() *Server {
|
||||||
|
|
||||||
func TestServerRoute(t *testing.T) {
|
func TestServerRoute(t *testing.T) {
|
||||||
server := mockServer()
|
server := mockServer()
|
||||||
if err := server.Route("world", "http://hello.localhost"+server.addr); err != nil {
|
p := config.Proxy{
|
||||||
|
To: "http://hello.localhost" + server.addr,
|
||||||
|
}
|
||||||
|
if err := server.Route("world", p); err != nil {
|
||||||
t.Fatalf("cannot add route: %v", err)
|
t.Fatalf("cannot add route: %v", err)
|
||||||
}
|
}
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue