github.com/coreos/goproxy@v0.0.0-20190513173959-f8dc2d7ba04e/examples/goproxy-no-reddit-at-worktime/noreddit.go (about)

     1  package main
     2  
     3  import (
     4  	"github.com/elazarl/goproxy"
     5  	"log"
     6  	"net/http"
     7  	"time"
     8  )
     9  
    10  func main() {
    11  	proxy := goproxy.NewProxyHttpServer()
    12  	proxy.OnRequest(goproxy.DstHostIs("www.reddit.com")).DoFunc(
    13  		func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
    14  			h, _, _ := time.Now().Clock()
    15  			if h >= 8 && h <= 17 {
    16  				return r, goproxy.NewResponse(r,
    17  					goproxy.ContentTypeText, http.StatusForbidden,
    18  					"Don't waste your time!")
    19  			} else {
    20  				ctx.Warnf("clock: %d, you can waste your time...", h)
    21  			}
    22  			return r, nil
    23  		})
    24  	log.Fatalln(http.ListenAndServe(":8080", proxy))
    25  }