diff --git a/parser.go b/parser.go index 68dfc32..9f34bd0 100644 --- a/parser.go +++ b/parser.go @@ -41,6 +41,17 @@ type corruptedT struct { size int64 } +type flagsT struct { + version bool + enableDebug bool + enableErrorHeaders bool + ignoreMissingHeaders bool + ignoreSSLErrors bool + portInt uint + errorVideoPath string + customdl string +} + type extractorF func(string, string, string, debugF) (string, int64, error) type sendErrorVideoF func(http.ResponseWriter, error) diff --git a/streamer.go b/streamer.go index 81751db..bd262a6 100644 --- a/streamer.go +++ b/streamer.go @@ -21,30 +21,14 @@ const defaultExpireTime = 3 * 60 * 60 const defaultErrorHeader = "Error-Header-" func main() { - var version bool - var enableDebug bool - var enableErrorHeaders bool - var ignoreMissingHeaders bool - var ignoreSSLErrors bool - var portInt uint - var errorVideoPath string - var customdl string - flag.BoolVar(&version, "version", false, "prints current yt-proxy version") - flag.BoolVar(&enableDebug, "debug", false, "turn on debug") - flag.BoolVar(&enableErrorHeaders, "error-headers", false, "show errors in headers (insecure)") - flag.BoolVar(&ignoreMissingHeaders, "ignore-missing-headers", false, "do not strictly check video headers") - flag.BoolVar(&ignoreSSLErrors, "ignore-ssl-errors", false, "ignore video server SSL errors (insecure)") - flag.UintVar(&portInt, "port", 8080, "listen port") - flag.StringVar(&errorVideoPath, "error-video", "corrupted.mp4", "file that will be shown on errors") - flag.StringVar(&customdl, "custom-extractor", "", "use custom url extractor, will be called like this: program_name url video_height video_format") - flag.Parse() - if version { + flags := parseCLIFlags() + if flags.version { fmt.Println(appVersion) os.Exit(0) } var extractor extractorF - if len(customdl) > 0 { - extractor = getCustomDL(customdl) + if len(flags.customdl) > 0 { + extractor = getCustomDL(flags.customdl) } else { extractor = getYTDL() } @@ -52,19 +36,19 @@ func main() { requests = make(chan requestChan) var links linksCache links.cache = make(map[string]lnkT) - debug := getDebugFunc(enableDebug) - errorVideo := readErrorVideo(errorVideoPath) - sendErrorVideo := getSendErrorVideoFunc(enableErrorHeaders, errorVideo) - httpRequest := getDoRequestFunc(ignoreSSLErrors) + debug := getDebugFunc(flags.enableDebug) + errorVideo := readErrorVideo(flags.errorVideoPath) + sendErrorVideo := getSendErrorVideoFunc(flags.enableErrorHeaders, errorVideo) + httpRequest := getDoRequestFunc(flags.ignoreSSLErrors) go parseLinks(requests, debug, &links, extractor) - port := fmt.Sprintf("%d", portInt) + port := fmt.Sprintf("%d", flags.portInt) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { log.Println(r.RemoteAddr, r.RequestURI) http.NotFound(w, r) }) http.HandleFunc("/play/", func(w http.ResponseWriter, r *http.Request) { log.Println(r.RemoteAddr, r.RequestURI) - playVideo(w, r, requests, debug, sendErrorVideo, !ignoreMissingHeaders, httpRequest) + playVideo(w, r, requests, debug, sendErrorVideo, !flags.ignoreMissingHeaders, httpRequest) }) s := &http.Server{ Addr: ":" + port, @@ -230,3 +214,17 @@ func errorToHeaders(e error) ([]string, []string) { } return headers, filtered } + +func parseCLIFlags() flagsT { + var f flagsT + flag.BoolVar(&f.version, "version", false, "prints current yt-proxy version") + flag.BoolVar(&f.enableDebug, "debug", false, "turn on debug") + flag.BoolVar(&f.enableErrorHeaders, "error-headers", false, "show errors in headers (insecure)") + flag.BoolVar(&f.ignoreMissingHeaders, "ignore-missing-headers", false, "do not strictly check video headers") + flag.BoolVar(&f.ignoreSSLErrors, "ignore-ssl-errors", false, "do not check video server certificate (insecure)") + flag.UintVar(&f.portInt, "port", 8080, "listen port") + flag.StringVar(&f.errorVideoPath, "error-video", "corrupted.mp4", "file that will be shown on errors") + flag.StringVar(&f.customdl, "custom-extractor", "", "use custom url extractor, will be called like this: program_name url video_height video_format") + flag.Parse() + return f +}