From c3639c4631b56bb800cf5c1a924cf7b5d64b873f Mon Sep 17 00:00:00 2001 From: Tom Andrade Date: Mon, 18 Mar 2019 22:40:08 +0100 Subject: [PATCH] Add flags to switch proto and path --- main.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index eba81d6..3547095 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,11 @@ import ( "fmt" "log" "math/rand" + "net" "net/url" "os" "regexp" + "strings" "time" "github.com/gomodule/redigo/redis" @@ -27,6 +29,8 @@ const ( var domain string var redisServer string var listenAddr string +var proto string +var path string var src = rand.NewSource(time.Now().UnixNano()) var pool = newPool() @@ -84,6 +88,9 @@ func set(c redis.Conn, url, suffix string) error { } func redirect(ctx *web.Context, val string) { + if path != "" { + val = strings.Replace(val, path, "", 1) + } r, _ := regexp.Compile("[a-zA-Z0-9]+") key := r.FindString(val) conn := pool.Get() @@ -97,13 +104,12 @@ func redirect(ctx *web.Context, val string) { } func shortner(ctx *web.Context) { - // return fmt.Sprintf("%v\n", ctx.Params) + _, port, _ := net.SplitHostPort(listenAddr) u, err := url.ParseRequestURI(ctx.Params["url"]) if err != nil { ctx.Abort(400, "Bad URL") } else { suffix := RandStringBytesMaskImprSrc(10) - // nURL := "https://" + domain + "/" + suffix conn := pool.Get() defer conn.Close() for { @@ -114,11 +120,16 @@ func shortner(ctx *web.Context) { break } } + if port != "80" && proto == "http" { + port = ":" + port + "/" + } else if port != "443" && proto == "https" { + port = ":" + port + "/" + } err := set(conn, u.String(), suffix) if err != nil { ctx.Abort(500, "Internal Error") } else { - ctx.WriteString("URL shortened at: https://" + domain + "/" + suffix) + ctx.WriteString("URL shortened at: " + proto + "://" + domain + port + path + suffix + "\n") } } } @@ -145,13 +156,19 @@ func main() { flag.StringVar(&domain, "domain", "localhost", "Domain to write to the URLs") flag.StringVar(&redisServer, "redis", "localhost:6379", "ip/hostname of the redis server to connect") flag.StringVar(&listenAddr, "addr", "localhost:8080", "Address to listen for connections") - version := flag.Bool("v", false, "prints current roxy version") + flag.StringVar(&path, "path", "", "Path to the base URL (https://localhost/PATH/... remember to append a / at the end") + flag.StringVar(&proto, "proto", "https", "proto to the base URL (HTTPS://localhost/path/... no real https here just to set the url (for like a proxy offloading https") + version := flag.Bool("v", false, "prints current version") flag.Parse() if *version { fmt.Printf("%s", appVersion) os.Exit(0) } + if path != "" && !strings.HasSuffix(path, "/") { + path = path + "/" + } + web.Post("/", shortner) web.Get("/(.*)", redirect) log.Printf("Domain: %s, Redis: %s\n", domain, redisServer)