From 7e05271a79f304b0704e6fd4231d5728d8079d01 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Wed, 21 Jun 2023 11:20:20 +0100 Subject: [PATCH] Read start up config from a config file --- .gitignore | 1 - certs/certs.go | 9 +- cmd/violet/conf.go | 19 ++++ cmd/violet/main.go | 137 ++--------------------- cmd/violet/serve.go | 179 ++++++++++++++++++++++++++++++ cmd/violet/setup.go | 221 +++++++++++++++++++++++++++++++++++++ domains/domains.go | 3 - error-pages/error-pages.go | 2 +- go.mod | 10 +- go.sum | 54 ++++++++- router/manager.go | 13 ++- servers/api.go | 5 + 12 files changed, 511 insertions(+), 142 deletions(-) create mode 100644 cmd/violet/conf.go create mode 100644 cmd/violet/serve.go create mode 100644 cmd/violet/setup.go diff --git a/.gitignore b/.gitignore index 857e074..ebf6758 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ *.sqlite *.local -violet diff --git a/certs/certs.go b/certs/certs.go index f9abc83..78d5be6 100644 --- a/certs/certs.go +++ b/certs/certs.go @@ -37,7 +37,11 @@ func New(certDir fs.FS, keyDir fs.FS, selfCert bool) *Certs { s: &sync.RWMutex{}, m: make(map[string]*tls.Certificate), } - c.r = rescheduler.NewRescheduler(c.threadCompile) + + // the rescheduler isn't even used in self cert mode so why initialise it + if !selfCert { + c.r = rescheduler.NewRescheduler(c.threadCompile) + } // in self-signed mode generate a CA certificate to sign other certificates if c.ss { @@ -55,9 +59,6 @@ func New(certDir fs.FS, keyDir fs.FS, selfCert bool) *Certs { } c.ca = ca } - - // run compile to get the initial data - c.Compile() return c } diff --git a/cmd/violet/conf.go b/cmd/violet/conf.go new file mode 100644 index 0000000..f6a327c --- /dev/null +++ b/cmd/violet/conf.go @@ -0,0 +1,19 @@ +package main + +type startUpConfig struct { + Database string `json:"db"` + MjwtPubKey string `json:"mjwt_pub_key"` + CertPath string `json:"cert_path"` + KeyPath string `json:"key_path"` + SelfSigned bool `json:"self_signed"` + ErrorPagePath string `json:"error_page_path"` + Listen listenConfig `json:"listen"` + InkscapeCmd string `json:"inkscape"` + RateLimit uint64 `json:"rate_limit"` +} + +type listenConfig struct { + Api string `json:"api"` + Http string `json:"http"` + Https string `json:"https"` +} diff --git a/cmd/violet/main.go b/cmd/violet/main.go index 3e29d92..6281b5f 100644 --- a/cmd/violet/main.go +++ b/cmd/violet/main.go @@ -1,139 +1,22 @@ package main import ( - "database/sql" + "context" _ "embed" "flag" - "fmt" - "github.com/MrMelon54/mjwt" - "github.com/MrMelon54/violet/certs" - "github.com/MrMelon54/violet/domains" - errorPages "github.com/MrMelon54/violet/error-pages" - "github.com/MrMelon54/violet/favicons" - "github.com/MrMelon54/violet/proxy" - "github.com/MrMelon54/violet/router" - "github.com/MrMelon54/violet/servers" - "github.com/MrMelon54/violet/utils" + "github.com/google/subcommands" _ "github.com/mattn/go-sqlite3" - "log" - "net/http" "os" - "os/signal" - "syscall" - "time" -) - -// flags - each one has a usage field lol -var ( - databasePath = flag.String("db", "", "/path/to/database.sqlite : path to the database file") - mjwtPubKey = flag.String("mjwt", "", "/path/to/mjwt-public-key.pem : path to the pem encoded rsa public key file") - keyPath = flag.String("keys", "", "/path/to/keys : path contains the keys with names matching the certificates and '.key' extensions") - certPath = flag.String("certs", "", "/path/to/certificates : path contains the certificates to load in armoured PEM encoding") - selfSigned = flag.Bool("ss", false, "enable self-signed certificate mode") - errorPagePath = flag.String("errors", "", "/path/to/error-pages : path contains the custom error pages") - apiListen = flag.String("api", "127.0.0.1:8080", "address for api listening") - httpListen = flag.String("http", "0.0.0.0:80", "address for http listening") - httpsListen = flag.String("https", "0.0.0.0:443", "address for https listening") - inkscapeCmd = flag.String("inkscape", "inkscape", "Path to inkscape binary") - rateLimit = flag.Uint64("rate-limit", 300, "Rate limit (max requests per minute)") ) func main() { - log.Println("[Violet] Starting...") + subcommands.Register(subcommands.HelpCommand(), "") + subcommands.Register(subcommands.FlagsCommand(), "") + subcommands.Register(subcommands.CommandsCommand(), "") + subcommands.Register(&serveCmd{}, "") + subcommands.Register(&setupCmd{}, "") + flag.Parse() - - // the cert and key paths are useless in self-signed mode - if !*selfSigned { - if *certPath != "" { - // create path to cert dir - err := os.MkdirAll(*certPath, os.ModePerm) - if err != nil { - log.Fatalf("[Violet] Failed to create certificate path '%s' does not exist", *certPath) - } - } - if *keyPath != "" { - // create path to key dir - err := os.MkdirAll(*keyPath, os.ModePerm) - if err != nil { - log.Fatalf("[Violet] Failed to create certificate key path '%s' does not exist", *keyPath) - } - } - } - - // load the MJWT RSA public key from a pem encoded file - mjwtVerify, err := mjwt.NewMJwtVerifierFromFile(*mjwtPubKey) - if err != nil { - log.Fatalf("[Violet] Failed to load MJWT verifier public key from file: '%s'", *mjwtPubKey) - } - - // open sqlite database - db, err := sql.Open("sqlite3", *databasePath) - if err != nil { - log.Fatalf("[Violet] Failed to open database '%s'...", *databasePath) - } - - allowedDomains := domains.New(db) // load allowed domains - acmeChallenges := utils.NewAcmeChallenge() // load acme challenge store - allowedCerts := certs.New(os.DirFS(*certPath), os.DirFS(*keyPath), *selfSigned) // load certificate manager - reverseProxy := proxy.NewHybridTransport() // load reverse proxy - dynamicFavicons := favicons.New(db, *inkscapeCmd) // load dynamic favicon provider - dynamicErrorPages := errorPages.New(os.DirFS(*errorPagePath)) // load dynamic error page provider - dynamicRouter := router.NewManager(db, reverseProxy) // load dynamic router manager - - // struct containing config for the http servers - srvConf := &servers.Conf{ - ApiListen: *apiListen, - HttpListen: *httpListen, - HttpsListen: *httpsListen, - RateLimit: *rateLimit, - DB: db, - Domains: allowedDomains, - Acme: acmeChallenges, - Certs: allowedCerts, - Favicons: dynamicFavicons, - Verify: mjwtVerify, - ErrorPages: dynamicErrorPages, - Router: dynamicRouter, - } - - var srvApi, srvHttp, srvHttps *http.Server - if *apiListen != "" { - srvApi = servers.NewApiServer(srvConf, utils.MultiCompilable{allowedDomains, allowedCerts, dynamicFavicons, dynamicErrorPages, dynamicRouter}) - log.Printf("[API] Starting API server on: '%s'\n", srvApi.Addr) - go utils.RunBackgroundHttp("API", srvApi) - } - if *httpListen != "" { - srvHttp = servers.NewHttpServer(srvConf) - log.Printf("[HTTP] Starting HTTP server on: '%s'\n", srvHttp.Addr) - go utils.RunBackgroundHttp("HTTP", srvHttp) - } - if *httpsListen != "" { - srvHttps = servers.NewHttpsServer(srvConf) - log.Printf("[HTTPS] Starting HTTPS server on: '%s'\n", srvHttps.Addr) - go utils.RunBackgroundHttps("HTTPS", srvHttps) - } - - // Wait for exit signal - sc := make(chan os.Signal, 1) - signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) - <-sc - fmt.Println() - - // Stop servers - log.Printf("[Violet] Stopping...") - n := time.Now() - - // close http servers - if srvApi != nil { - srvApi.Close() - } - if srvHttp != nil { - srvHttp.Close() - } - if srvHttps != nil { - srvHttps.Close() - } - - log.Printf("[Violet] Took '%s' to shutdown\n", time.Now().Sub(n)) - log.Println("[Violet] Goodbye") + ctx := context.Background() + os.Exit(int(subcommands.Execute(ctx))) } diff --git a/cmd/violet/serve.go b/cmd/violet/serve.go new file mode 100644 index 0000000..2164f98 --- /dev/null +++ b/cmd/violet/serve.go @@ -0,0 +1,179 @@ +package main + +import ( + "context" + "database/sql" + "encoding/json" + "flag" + "fmt" + "github.com/MrMelon54/mjwt" + "github.com/MrMelon54/violet/certs" + "github.com/MrMelon54/violet/domains" + errorPages "github.com/MrMelon54/violet/error-pages" + "github.com/MrMelon54/violet/favicons" + "github.com/MrMelon54/violet/proxy" + "github.com/MrMelon54/violet/router" + "github.com/MrMelon54/violet/servers" + "github.com/MrMelon54/violet/utils" + "github.com/google/subcommands" + "io/fs" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" +) + +type serveCmd struct{ configPath string } + +func (s *serveCmd) Name() string { return "serve" } +func (s *serveCmd) Synopsis() string { return "Serve reverse proxy server" } +func (s *serveCmd) SetFlags(f *flag.FlagSet) { + f.StringVar(&s.configPath, "conf", "", "/path/to/config.json : path to the config file") +} +func (s *serveCmd) Usage() string { + return `serve [-conf ] + Serve reverse proxy server using information from config file +` +} + +func (s *serveCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + log.Println("[Violet] Starting...") + + if s.configPath == "" { + log.Println("[Violet] Error: config flag is missing") + return subcommands.ExitUsageError + } + + openConf, err := os.Open(s.configPath) + if err != nil { + if os.IsNotExist(err) { + log.Println("[Violet] Error: missing config file") + } else { + log.Println("[Violet] Error: open config file: ", err) + } + return subcommands.ExitFailure + } + + var conf startUpConfig + err = json.NewDecoder(openConf).Decode(&conf) + if err != nil { + log.Println("[Violet] Error: invalid config file: ", err) + return subcommands.ExitFailure + } + + normalLoad(conf) + return subcommands.ExitSuccess +} + +func normalLoad(conf startUpConfig) { + // the cert and key paths are useless in self-signed mode + if !conf.SelfSigned { + if conf.CertPath != "" { + // create path to cert dir + err := os.MkdirAll(conf.CertPath, os.ModePerm) + if err != nil { + log.Fatalf("[Violet] Failed to create certificate path '%s'", conf.CertPath) + } + } + if conf.KeyPath != "" { + // create path to key dir + err := os.MkdirAll(conf.KeyPath, os.ModePerm) + if err != nil { + log.Fatalf("[Violet] Failed to create certificate key path '%s'", conf.KeyPath) + } + } + } + + // errorPageDir stores an FS interface for accessing the error page directory + var errorPageDir fs.FS + if conf.ErrorPagePath != "" { + errorPageDir = os.DirFS(conf.ErrorPagePath) + err := os.MkdirAll(conf.ErrorPagePath, os.ModePerm) + if err != nil { + log.Fatalf("[Violet] Failed to create error page path '%s'", conf.ErrorPagePath) + } + } + + // load the MJWT RSA public key from a pem encoded file + mjwtVerify, err := mjwt.NewMJwtVerifierFromFile(conf.MjwtPubKey) + if err != nil { + log.Fatalf("[Violet] Failed to load MJWT verifier public key from file: '%s'", conf.MjwtPubKey) + } + + // open sqlite database + db, err := sql.Open("sqlite3", conf.Database) + if err != nil { + log.Fatalf("[Violet] Failed to open database '%s'...", conf.Database) + } + + allowedDomains := domains.New(db) // load allowed domains + acmeChallenges := utils.NewAcmeChallenge() // load acme challenge store + allowedCerts := certs.New(os.DirFS(conf.CertPath), os.DirFS(conf.KeyPath), conf.SelfSigned) // load certificate manager + hybridTransport := proxy.NewHybridTransport() // load reverse proxy + dynamicFavicons := favicons.New(db, conf.InkscapeCmd) // load dynamic favicon provider + dynamicErrorPages := errorPages.New(errorPageDir) // load dynamic error page provider + dynamicRouter := router.NewManager(db, hybridTransport) // load dynamic router manager + + // struct containing config for the http servers + srvConf := &servers.Conf{ + ApiListen: conf.Listen.Api, + HttpListen: conf.Listen.Http, + HttpsListen: conf.Listen.Https, + RateLimit: conf.RateLimit, + DB: db, + Domains: allowedDomains, + Acme: acmeChallenges, + Certs: allowedCerts, + Favicons: dynamicFavicons, + Verify: mjwtVerify, + ErrorPages: dynamicErrorPages, + Router: dynamicRouter, + } + + // create the compilable list and run a first time compile + allCompilables := utils.MultiCompilable{allowedDomains, allowedCerts, dynamicFavicons, dynamicErrorPages, dynamicRouter} + allCompilables.Compile() + + var srvApi, srvHttp, srvHttps *http.Server + if srvConf.ApiListen != "" { + srvApi = servers.NewApiServer(srvConf, allCompilables) + log.Printf("[API] Starting API server on: '%s'\n", srvApi.Addr) + go utils.RunBackgroundHttp("API", srvApi) + } + if srvConf.HttpListen != "" { + srvHttp = servers.NewHttpServer(srvConf) + log.Printf("[HTTP] Starting HTTP server on: '%s'\n", srvHttp.Addr) + go utils.RunBackgroundHttp("HTTP", srvHttp) + } + if srvConf.HttpsListen != "" { + srvHttps = servers.NewHttpsServer(srvConf) + log.Printf("[HTTPS] Starting HTTPS server on: '%s'\n", srvHttps.Addr) + go utils.RunBackgroundHttps("HTTPS", srvHttps) + } + + // Wait for exit signal + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) + <-sc + fmt.Println() + + // Stop servers + log.Printf("[Violet] Stopping...") + n := time.Now() + + // close http servers + if srvApi != nil { + srvApi.Close() + } + if srvHttp != nil { + srvHttp.Close() + } + if srvHttps != nil { + srvHttps.Close() + } + + log.Printf("[Violet] Took '%s' to shutdown\n", time.Now().Sub(n)) + log.Println("[Violet] Goodbye") +} diff --git a/cmd/violet/setup.go b/cmd/violet/setup.go new file mode 100644 index 0000000..13a59af --- /dev/null +++ b/cmd/violet/setup.go @@ -0,0 +1,221 @@ +package main + +import ( + "context" + "database/sql" + "encoding/json" + "flag" + "fmt" + "github.com/AlecAivazis/survey/v2" + "github.com/MrMelon54/violet/domains" + "github.com/MrMelon54/violet/proxy" + "github.com/MrMelon54/violet/router" + "github.com/MrMelon54/violet/target" + "github.com/google/subcommands" + "log" + "net" + "net/http" + "net/url" + "os" + "path" + "path/filepath" + "strconv" +) + +type setupCmd struct { + wdPath string +} + +func (s *setupCmd) Name() string { return "setup" } +func (s *setupCmd) Synopsis() string { return "Walkthrough creating a config file" } +func (s *setupCmd) SetFlags(f *flag.FlagSet) { + f.StringVar(&s.wdPath, "wd", ".", "Path to the directory to create config files in (defaults to the working directory)") +} +func (s *setupCmd) Usage() string { + return `setup + Setup Violet automatically by answering questions. +` +} + +func (s *setupCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + // get absolute path to specify files + wdAbs, err := filepath.Abs(s.wdPath) + if err != nil { + fmt.Println("[Violet] Failed to get full directory path: ", err) + return subcommands.ExitFailure + } + + // ask about running the setup steps + createFile := false + err = survey.AskOne(&survey.Confirm{Message: fmt.Sprintf("Create Violet config files in this directory: '%s'?", wdAbs)}, &createFile) + if err != nil { + fmt.Println("[Violet] Error: ", err) + return subcommands.ExitFailure + } + if !createFile { + fmt.Println("[Violet] Goodbye") + return subcommands.ExitSuccess + } + + // store answers from questions + var answers struct { + SelfSigned bool + ErrorPages bool + ApiListen string + HttpListen string + HttpsListen string + RateLimit uint64 + FirstDomain string + ApiUrl string + } + + // ask main questions + err = survey.Ask([]*survey.Question{ + { + Name: "SelfSigned", + Prompt: &survey.Confirm{Message: "Enable self-signed certificate?"}, + }, + { + Name: "ErrorPages", + Prompt: &survey.Confirm{Message: "Enable custom error pages?"}, + }, + { + Name: "ApiListen", + Prompt: &survey.Input{Message: "API listen address", Default: "127.0.0.1:8080"}, + Validate: listenAddressValidator, + }, + { + Name: "HttpListen", + Prompt: &survey.Input{Message: "HTTP listen address", Default: ":80"}, + }, + { + Name: "HttpsListen", + Prompt: &survey.Input{Message: "HTTPS listen address", Default: ":443"}, + }, + { + Name: "RateLimit", + Prompt: &survey.Input{Message: "Rate limit", Default: "300", Help: "Number of allowed requests per minute per IP"}, + Validate: func(ans interface{}) error { + if ansStr, ok := ans.(string); ok { + _, err := strconv.ParseUint(ansStr, 10, 64) + return err + } + return nil + }, + }, + { + Name: "FirstDomain", + Prompt: &survey.Input{Message: "First domain", Default: "example.com", Help: "Setup the first domain or it will be more difficult to setup later"}, + Validate: survey.Required, + }, + }, &answers) + if err != nil { + fmt.Println("[Violet] Error: ", err) + return subcommands.ExitFailure + } + + // generate database path + databaseFile := filepath.Join(wdAbs, "violet.sqlite") + errorPagePath := "" + if answers.ErrorPages { + errorPagePath = filepath.Join(wdAbs, "error-pages") + } + + // write config file + confFile := filepath.Join(wdAbs, "config.json") + createConf, err := os.Create(confFile) + if err != nil { + return 0 + } + confEncode := json.NewEncoder(createConf) + confEncode.SetIndent("", " ") + err = confEncode.Encode(startUpConfig{ + Database: filepath.Join(wdAbs, "violet.sqlite"), + MjwtPubKey: filepath.Join(wdAbs, "mjwt.public.key"), + CertPath: filepath.Join(wdAbs, "certs"), + KeyPath: filepath.Join(wdAbs, "keys"), + SelfSigned: answers.SelfSigned, + ErrorPagePath: errorPagePath, + Listen: listenConfig{ + Api: answers.ApiListen, + Http: answers.HttpListen, + Https: answers.HttpsListen, + }, + InkscapeCmd: "inkscape", + RateLimit: answers.RateLimit, + }) + if err != nil { + fmt.Println("[Violet] Failed to write config file: ", err) + return subcommands.ExitFailure + } + + // open sqlite database + db, err := sql.Open("sqlite3", databaseFile) + if err != nil { + log.Fatalf("[Violet] Failed to open database '%s'...", databaseFile) + } + + // domain manager to add a domain, no need to compile here as the program needs + // to be run again with the serve subcommand + allowedDomains := domains.New(db) + allowedDomains.Put(answers.FirstDomain, true) + + // don't bother with this part is the api won't be listening + if answers.ApiListen != "" { + // ask for url + err = survey.AskOne(&survey.Input{Message: "API URL", Default: "api.example.com/violet", Help: "Enter the URL which should point to the internal Violet API"}, &answers.ApiUrl, survey.WithValidator(func(ans interface{}) error { + if ansStr, ok := ans.(string); ok { + _, err := url.Parse(ansStr) + return err + } + return nil + })) + if err != nil { + fmt.Println("[Violet] Error: ", err) + return subcommands.ExitFailure + } + + // parse the api url + apiUrl, err := url.Parse(answers.ApiUrl) + if err != nil { + fmt.Println("[Violet] Failed to parse API URL: ", err) + return subcommands.ExitFailure + } + + // add with the route manager, no need to compile as this will run when opened + // with the serve subcommand + routeManager := router.NewManager(db, proxy.NewHybridTransportWithCalls(&nilTransport{}, &nilTransport{})) + routeManager.Add(path.Join(apiUrl.Host, apiUrl.Path), target.Route{ + Pre: true, + Host: answers.ApiListen, + Cors: true, + ForwardHost: true, + ForwardAddr: true, + }, true) + } + + fmt.Println("[Violet] Setup complete") + fmt.Printf("[Violet] Run the reverse proxy with `violet serve -conf %s`\n", confFile) + + return subcommands.ExitSuccess +} + +func listenAddressValidator(ans interface{}) error { + if ansStr, ok := ans.(string); ok { + // empty string means disable + if ansStr == "" { + return nil + } + + // use ResolveTCPAddr to validate the input + _, err := net.ResolveTCPAddr("tcp", ansStr) + return err + } + return nil +} + +type nilTransport struct{} + +func (n *nilTransport) RoundTrip(_ *http.Request) (*http.Response, error) { + return nil, fmt.Errorf("not sure how you are sending a request") +} diff --git a/domains/domains.go b/domains/domains.go index 9380e6c..212ba94 100644 --- a/domains/domains.go +++ b/domains/domains.go @@ -36,9 +36,6 @@ func New(db *sql.DB) *Domains { log.Printf("[WARN] Failed to generate 'domains' table\n") return nil } - - // run compile to get the initial data - a.Compile() return a } diff --git a/error-pages/error-pages.go b/error-pages/error-pages.go index 86819fb..ef2d1ec 100644 --- a/error-pages/error-pages.go +++ b/error-pages/error-pages.go @@ -78,7 +78,7 @@ func (e *ErrorPages) threadCompile() { if e.dir != nil { err := e.internalCompile(errorPageMap) if err != nil { - log.Printf("[Certs] Compile failed: %s\n", err) + log.Printf("[ErrorPages] Compile failed: %s\n", err) return } } diff --git a/go.mod b/go.mod index 7947ba5..2016034 100644 --- a/go.mod +++ b/go.mod @@ -3,26 +3,34 @@ module github.com/MrMelon54/violet go 1.20 require ( + github.com/AlecAivazis/survey/v2 v2.3.7 github.com/MrMelon54/certgen v0.0.1 github.com/MrMelon54/mjwt v0.0.2 github.com/MrMelon54/png2ico v1.0.1 github.com/MrMelon54/rescheduler v0.0.1 github.com/MrMelon54/trie v0.0.2 + github.com/google/subcommands v1.2.0 github.com/julienschmidt/httprouter v1.3.0 github.com/mattn/go-sqlite3 v1.14.16 github.com/rs/cors v1.9.0 github.com/sethvargo/go-limiter v0.7.2 github.com/stretchr/testify v1.8.4 golang.org/x/net v0.9.0 - golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 + golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang-jwt/jwt/v4 v4.5.0 // indirect + github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect github.com/kr/pretty v0.1.0 // indirect + github.com/mattn/go-colorable v0.1.2 // indirect + github.com/mattn/go-isatty v0.0.8 // indirect + github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/sys v0.7.0 // indirect + golang.org/x/term v0.7.0 // indirect golang.org/x/text v0.9.0 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index be21a2d..a80e40d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ= +github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo= github.com/MrMelon54/certgen v0.0.1 h1:ycWdZ2RlxQ5qSuejeBVv4aXjGo5hdqqL4j4EjrXnFMk= github.com/MrMelon54/certgen v0.0.1/go.mod h1:GHflVlSbtFLJZLpN1oWyUvDBRrR8qCWiwZLXCCnS2Gc= github.com/MrMelon54/mjwt v0.0.2 h1:jDqyPnFloh80XdSmZ6jt9qhUj/ULcoQ4QSHXPdkAIE4= @@ -8,19 +10,36 @@ github.com/MrMelon54/rescheduler v0.0.1 h1:gzNvL8X81M00uYN0i9clFVrXCkG1UuLNYxDcv github.com/MrMelon54/rescheduler v0.0.1/go.mod h1:OQDFtZHdS4/qA/r7rtJUQA22/hbpnZ9MGQCXOPjhC6w= github.com/MrMelon54/trie v0.0.2 h1:ZXWcX5ij62O9K4I/anuHmVg8L3tF0UGdlPceAASwKEY= github.com/MrMelon54/trie v0.0.2/go.mod h1:sGCGOcqb+DxSxvHgSOpbpkmA7mFZR47YDExy9OCbVZI= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s= +github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w= +github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI= +github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog= +github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU= +github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= +github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE= +github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4= +github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -29,16 +48,47 @@ github.com/rs/cors v1.9.0 h1:l9HGsTsHJcvW14Nk7J9KFz8bzeAWXn3CG6bgt7LsrAE= github.com/rs/cors v1.9.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/sethvargo/go-limiter v0.7.2 h1:FgC4N7RMpV5gMrUdda15FaFTkQ/L4fEqM7seXMs4oO8= github.com/sethvargo/go-limiter v0.7.2/go.mod h1:C0kbSFbiriE5k2FFOe18M1YZbAR2Fiwf72uGu0CXCcU= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ= +golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/router/manager.go b/router/manager.go index 447621e..994d1c4 100644 --- a/router/manager.go +++ b/router/manager.go @@ -10,6 +10,7 @@ import ( "github.com/MrMelon54/violet/utils" "log" "net/http" + "path" "strings" "sync" ) @@ -59,9 +60,6 @@ func NewManager(db *sql.DB, proxy *proxy.HybridTransport) *Manager { log.Printf("[WARN] Failed to generate 'redirects' table\n") return nil } - - // run compile to get the initial router - m.Compile() return m } @@ -167,6 +165,15 @@ func (m *Manager) internalCompile(router *Router) error { return rows.Err() } +func (m *Manager) Add(source string, route target.Route, active bool) { + m.s.Lock() + defer m.s.Unlock() + _, err := m.db.Exec(`INSERT INTO routes (source, pre, destination, abs, cors, secure_mode, forward_host, forward_addr, ignore_cert, active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, source, route.Pre, path.Join(route.Host, route.Path), route.Abs, route.Cors, route.SecureMode, route.ForwardHost, route.ForwardAddr, route.IgnoreCert, active) + if err != nil { + log.Printf("[Violet] Database error: %s\n", err) + } +} + // addRoute is an alias to parse the src and dst then add the route func addRoute(router *Router, src string, dst string, t target.Route) error { srcHost, srcPath, dstHost, dstPort, dstPath, err := parseSrcDstHost(src, dst) diff --git a/servers/api.go b/servers/api.go index 51f4150..135982b 100644 --- a/servers/api.go +++ b/servers/api.go @@ -52,6 +52,11 @@ func NewApiServer(conf *Conf, compileTarget utils.MultiCompilable) *http.Server conf.Domains.Compile() }) + // Endpoint for routes + r.POST("/route", func(rw http.ResponseWriter, req *http.Request, _ httprouter.Params) { + + }) + // Endpoint for acme-challenge r.PUT("/acme-challenge/:domain/:key/:value", func(rw http.ResponseWriter, req *http.Request, params httprouter.Params) { if !hasPerms(conf.Verify, req, "violet:acme-challenge") {