diff options
Diffstat (limited to 'roseh.moe.go')
| -rw-r--r-- | roseh.moe.go | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/roseh.moe.go b/roseh.moe.go index 29451df..7fd4ebd 100644 --- a/roseh.moe.go +++ b/roseh.moe.go @@ -44,9 +44,7 @@ import ( ) var ( - port = flag.Int("port", 42069, "port to listen on") - selfURL = flag.String("self-url", "http://localhost:42069", "base URL of the server") - domain = flag.String("domain", "localhost", "domain for auth cookie") + selfURL = urlFlag("url", "http://localhost:42069", "base URL of the server") secretsFile = flag.String("secrets", "secrets.xml", "secrets file") configFile = flag.String("config", "config.xml", "configuration file (XML, baby!!)") https = flag.String("https", "", "directory containing cert.pem and key.pem, or empty string to use unencrypted http") @@ -56,6 +54,35 @@ var ( cgitConfig = flag.String("cgit-config", "", "cgit config file") ) +type urlValue struct { + URL *url.URL +} + +func (v urlValue) String() string { + if v.URL != nil { + return v.URL.String() + } + return "" +} + +func (v urlValue) Set(s string) error { + u, err := url.Parse(s) + if err != nil { + return err + } + *v.URL = *u + return nil +} + +func urlFlag(name, def, doc string) *url.URL { + u, err := url.Parse(def) + if err != nil { + panic(err) + } + flag.Var(urlValue{u}, name, doc) + return u +} + var notepadPassword, secretKey, authenticatorKey []byte func loadSecrets() error { @@ -540,7 +567,7 @@ func wormholeUpload(w http.ResponseWriter, r *http.Request) { http.Error(w, "Internal error", http.StatusInternalServerError) return } - downloadURL := *selfURL + "/wormhole/" + hole + downloadURL := selfURL.String() + "/wormhole/" + hole if err := wormholeSuccessTemplate.Execute(w, wormholeSuccessTemplateArgs{ URL: downloadURL, QR: "/qr.png?t=" + url.QueryEscape(downloadURL) + "&s=" + base64.RawURLEncoding.EncodeToString(mac([]byte(downloadURL), "qr")), @@ -571,7 +598,7 @@ func wormholeAPIUpload(w http.ResponseWriter, r *http.Request) { http.Error(w, "Failed to write hole file", http.StatusInternalServerError) return } - resultURL := fmt.Sprintf("%s/wormhole/%s", *selfURL, hole) + resultURL := fmt.Sprintf("%s/wormhole/%s", selfURL, hole) w.Header().Set("Content-Type", "text/plain") w.Header().Set("Location", resultURL) w.WriteHeader(http.StatusCreated) @@ -682,7 +709,7 @@ func attachCookie(w http.ResponseWriter) { Name: authCookieName, Value: makeToken(), Path: "/", - Domain: *domain, + Domain: selfURL.Hostname(), Expires: time.Now().Add(cookieExpiration), Secure: true, HttpOnly: true, @@ -761,7 +788,7 @@ type loginTemplateArgs struct { func redirectLogin(w http.ResponseWriter, r *http.Request, redirect string) { redirect += base64.RawURLEncoding.EncodeToString(mac([]byte(redirect), "redirect")) - http.Redirect(w, r, *selfURL+"/login?redirect="+url.QueryEscape(redirect), http.StatusSeeOther) + http.Redirect(w, r, "//"+selfURL.Host+"/login?redirect="+url.QueryEscape(redirect), http.StatusSeeOther) } func serveLogin(w http.ResponseWriter, r *http.Request) { @@ -1085,22 +1112,22 @@ func main() { apiMux.HandleFunc("PUT /api/wormhole/{name}", wormholeAPIUpload) mux.Handle("/api/", apiMux) mux.HandleFunc("/", notFound) - http.Handle(*domain+"/", http.NewCrossOriginProtection().Handler(&gzipHandler{mux})) + http.Handle(selfURL.Hostname()+"/", http.NewCrossOriginProtection().Handler(&gzipHandler{mux})) jellyfinURL, err := url.Parse("http://127.0.0.1:8096") if err != nil { log.Fatal(err) } - http.Handle("cinema."+*domain+"/", &authHandler{h: &httputil.ReverseProxy{ + http.Handle("cinema."+selfURL.Hostname()+"/", &authHandler{h: &httputil.ReverseProxy{ Rewrite: func(r *httputil.ProxyRequest) { r.SetURL(jellyfinURL) r.SetXForwarded() }, }}) cgitStaticFiles := http.FileServer(http.Dir(*cgit)) - http.Handle("code."+*domain+"/cgit.css", cgitStaticFiles) - http.Handle("code."+*domain+"/cgit.js", cgitStaticFiles) - http.Handle("code."+*domain+"/robots.txt", cgitStaticFiles) - http.HandleFunc("code."+*domain+"/favicon.ico", favicon) + http.Handle("code."+selfURL.Hostname()+"/cgit.css", cgitStaticFiles) + http.Handle("code."+selfURL.Hostname()+"/cgit.js", cgitStaticFiles) + http.Handle("code."+selfURL.Hostname()+"/robots.txt", cgitStaticFiles) + http.HandleFunc("code."+selfURL.Hostname()+"/favicon.ico", favicon) cgitConfigAbsPath, err := filepath.Abs(*cgitConfig) if err != nil { log.Fatal(err) @@ -1110,9 +1137,9 @@ func main() { Env: []string{"CGIT_CONFIG=" + cgitConfigAbsPath}, } cgitCGI.PathLocationHandler = cgitCGI - http.Handle("code."+*domain+"/", cgitCGI) + http.Handle("code."+selfURL.Hostname()+"/", cgitCGI) - addr := fmt.Sprintf(":%d", *port) + addr := ":" + selfURL.Port() log.Printf("Listening on %q", addr) if *https != "" { log.Fatal(http.ListenAndServeTLS(addr, *https+"/cert.pem", *https+"/key.pem", nil)) |
