Ejemplo n.º 1
0
// SetDownloadLimit - Sets the downloadlimit in KiloByte / Second
func (ic *Client) SetDownloadLimit(maxDownStream int) {
	if maxDownStream <= 0 {
		ic.connPool.SetBandwidth(iothrottler.Unlimited)
		log.Printf("download unlimited")
	} else {
		ic.connPool.SetBandwidth(iothrottler.Kbps * iothrottler.Bandwidth(maxDownStream*8))
		log.Printf("currentDownloadLimit: %v", iothrottler.Kbps*iothrottler.Bandwidth(maxDownStream*8))
	}
}
func main() {
	flag.Parse()

	server := &http.Server{
		Addr:         *listen,
		ReadTimeout:  1 * time.Minute,
		WriteTimeout: 1 * time.Minute,
	}

	if !*http1 {
		http2.ConfigureServer(server, &http2.Server{})
	}

	fs := http.FileServer(http.Dir("."))
	if !*disableGzip {
		oldfs := fs
		fs = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
				w.Header().Set("Content-Encoding", "gzip")
				grw := GzipResponseWriter{gzip.NewWriter(w), w}
				defer grw.WriteCloser.Close()
				w = grw
			}
			oldfs.ServeHTTP(w, r)
		})
	}

	server.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Access-Control-Allow-Origin", *cors)
		w.Header().Set("Access-Control-Allow-Methods", "GET, OPTION, HEAD, PATCH, PUT, POST, DELETE")
		log.Printf("Request for %s (Accept-Encoding: %s)", r.URL.Path, r.Header.Get("Accept-Encoding"))

		if *throttle != 0 {
			trw := NewThrottledResponseWriter(w, iothrottler.Bandwidth(*throttle)*iothrottler.Kbps)
			defer trw.Free()
			w = trw
		}

		delay := 0
		if *maxDelay > *minDelay {
			delay = mrand.Intn(*maxDelay-*minDelay) + *minDelay
		}
		if queryDelay := r.FormValue("delay"); queryDelay != "" {
			delay, _ = strconv.Atoi(queryDelay)
		}
		time.Sleep(time.Duration(delay) * time.Millisecond)

		if *spa != "" {
			path := r.URL.Path
			if _, err := os.Stat("." + path); err == nil {
				fs.ServeHTTP(w, r)
			} else {
				spaContents, err := readSPAFile(*spa)
				if err != nil {
					http.Error(w, fmt.Sprintf("Could not read SPA file: %s", err), http.StatusInternalServerError)
					return
				}
				w.Write(spaContents)
			}
		} else {
			fs.ServeHTTP(w, r)
		}

		if !*http1 && r.Header.Get("X-Is-Push") != "true" {
			pushResources(w, r)
		}
	})

	if err := configureTLS(server); err != nil {
		log.Fatalf("Error configuring TLS: %s", err)
	}

	ln, err := net.Listen("tcp", server.Addr)
	if err != nil {
		log.Fatalf("Error opening socket: %s", err)
	}

	tlsListener := tls.NewListener(ln, server.TLSConfig)
	tcl := tlsListener
	if strings.HasPrefix(*listen, ":") {
		*listen = "localhost" + *listen
	}
	log.Printf("Listening on https://%s...", *listen)
	if err := server.Serve(tcl); err != nil {
		log.Fatalf("Error starting webserver: %s", err)
	}
}