Example #1
0
func (s *STSuite) TestRequestLimitReached(c *C) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello"))
	})
	defer srv.Close()

	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	// this is our redirect to server
	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})

	// stream handler will forward requests to redirect
	st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)), MaxRequestBodyBytes(4))
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(st)
	defer proxy.Close()

	re, _, err := testutils.Get(proxy.URL, testutils.Body("this request is too long"))
	c.Assert(err, IsNil)
	c.Assert(re.StatusCode, Equals, http.StatusRequestEntityTooLarge)
}
Example #2
0
func syncApps(jsontasks *MarathonTasks, jsonapps *MarathonApps) {
	apps = Apps{Apps: make(map[string]App)}
	apps.Lock()
	defer apps.Unlock()
	for _, task := range jsontasks.Tasks {
		// Use regex to remove characters that are not allowed in hostnames
		re := regexp.MustCompile("[^0-9a-z-]")
		appid := re.ReplaceAllLiteralString(task.AppId, "")
		for _, v := range jsonapps.Apps {
			if v.Id == task.AppId {
				if s, ok := v.Labels["moxy_subdomain"]; ok {
					appid = s
				}
			}
		}
		if len(task.HealthCheckResults) == 1 {
			if task.HealthCheckResults[0].Alive == false {
				continue
			}
		}
		if s, ok := apps.Apps[appid]; ok {
			s.Lb.UpsertServer(testutils.ParseURI("http://" + task.Host + ":" + strconv.FormatInt(task.Ports[0], 10)))
			s.Tasks = append(s.Tasks, task.Host+":"+strconv.FormatInt(task.Ports[0], 10))
			apps.Apps[appid] = s
		} else {
			var s = App{}
			s.Fwd, _ = forward.New(forward.PassHostHeader(true))
			s.Lb, _ = roundrobin.New(s.Fwd)
			s.Lb.UpsertServer(testutils.ParseURI("http://" + task.Host + ":" + strconv.FormatInt(task.Ports[0], 10)))
			s.Tasks = []string{task.Host + ":" + strconv.FormatInt(task.Ports[0], 10)}
			apps.Apps[appid] = s
		}
	}
}
Example #3
0
func (s *RRSuite) TestUpsertWeight(c *C) {
	a := testutils.NewResponder("a")
	defer a.Close()

	b := testutils.NewResponder("b")
	defer b.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	c.Assert(lb.UpsertServer(testutils.ParseURI(a.URL)), IsNil)
	c.Assert(lb.UpsertServer(testutils.ParseURI(b.URL)), IsNil)

	proxy := httptest.NewServer(lb)
	defer proxy.Close()

	c.Assert(seq(c, proxy.URL, 3), DeepEquals, []string{"a", "b", "a"})

	c.Assert(lb.UpsertServer(testutils.ParseURI(b.URL), Weight(3)), IsNil)

	c.Assert(seq(c, proxy.URL, 4), DeepEquals, []string{"b", "b", "a", "b"})
}
Example #4
0
func (s *STSuite) TestNoBody(c *C) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(http.StatusOK)
	})
	defer srv.Close()

	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	// this is our redirect to server
	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})

	// stream handler will forward requests to redirect
	st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)))
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(st)
	defer proxy.Close()

	re, _, err := testutils.Get(proxy.URL)
	c.Assert(err, IsNil)
	c.Assert(re.StatusCode, Equals, http.StatusOK)
}
Example #5
0
func (s *STSuite) TestCustomErrorHandler(c *C) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello, this response is too large"))
	})
	defer srv.Close()

	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	// this is our redirect to server
	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})

	// stream handler will forward requests to redirect
	errHandler := utils.ErrorHandlerFunc(func(w http.ResponseWriter, req *http.Request, err error) {
		w.WriteHeader(http.StatusTeapot)
		w.Write([]byte(http.StatusText(http.StatusTeapot)))
	})
	st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)), MaxResponseBodyBytes(4), ErrorHandler(errHandler))
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(st)
	defer proxy.Close()

	re, _, err := testutils.Get(proxy.URL)
	c.Assert(err, IsNil)
	c.Assert(re.StatusCode, Equals, http.StatusTeapot)
}
Example #6
0
func (s *STSuite) TestChunkedEncodingLimitReached(c *C) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello"))
	})
	defer srv.Close()

	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	// this is our redirect to server
	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})

	// stream handler will forward requests to redirect
	st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)), MemRequestBodyBytes(4), MaxRequestBodyBytes(8))
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(st)
	defer proxy.Close()

	conn, err := net.Dial("tcp", testutils.ParseURI(proxy.URL).Host)
	c.Assert(err, IsNil)
	fmt.Fprintf(conn, "POST / HTTP/1.0\r\nTransfer-Encoding: chunked\r\n\r\n4\r\ntest\r\n5\r\ntest1\r\n5\r\ntest2\r\n0\r\n\r\n")
	status, err := bufio.NewReader(conn).ReadString('\n')

	c.Assert(status, Equals, "HTTP/1.0 413 Request Entity Too Large\r\n")
}
Example #7
0
// Make sure that stream handler preserves TLS settings
func (s *STSuite) TestPreservesTLS(c *C) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("ok"))
	})
	defer srv.Close()

	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	var t *tls.ConnectionState
	// this is our redirect to server
	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		t = req.TLS
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})

	// stream handler will forward requests to redirect
	st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)))
	c.Assert(err, IsNil)

	proxy := httptest.NewUnstartedServer(st)
	proxy.StartTLS()
	defer proxy.Close()

	re, _, err := testutils.Get(proxy.URL)
	c.Assert(err, IsNil)
	c.Assert(re.StatusCode, Equals, http.StatusOK)

	c.Assert(t, NotNil)
}
Example #8
0
func (s *RBSuite) TestRebalancerLive(c *C) {
	a, b := testutils.NewResponder("a"), testutils.NewResponder("b")
	defer a.Close()
	defer b.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	rb, err := NewRebalancer(lb, RebalancerBackoff(time.Millisecond), RebalancerClock(s.clock))
	c.Assert(err, IsNil)

	rb.UpsertServer(testutils.ParseURI(a.URL))
	rb.UpsertServer(testutils.ParseURI(b.URL))
	rb.UpsertServer(testutils.ParseURI("http://localhost:62345"))

	proxy := httptest.NewServer(rb)
	defer proxy.Close()

	for i := 0; i < 1000; i += 1 {
		testutils.Get(proxy.URL)
		if i%10 == 0 {
			s.clock.CurrentTime = s.clock.CurrentTime.Add(rb.backoffDuration + time.Second)
		}
	}

	// load balancer changed weights
	c.Assert(rb.servers[0].curWeight, Equals, FSMMaxWeight)
	c.Assert(rb.servers[1].curWeight, Equals, FSMMaxWeight)
	c.Assert(rb.servers[2].curWeight, Equals, 1)
}
Example #9
0
func (ff *ForwardFrontend) watchWebRequests() {
	fwd, _ := forward.New()

	proxy := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		hostname := strings.Split(req.Host, ".")[0]

		for _, event := range ff.routes {
			if event.Hostname == hostname {
				req.URL = testutils.ParseURI(fmt.Sprintf("http://%v:%v", event.Endpoint, event.Port))
				fwd.ServeHTTP(w, req)
				log.Printf("%v:%v:Serving request. Hostname: %v Target: %v Port: %v\n", ff.config.Hostname, ff.config.Port, hostname, event.Endpoint, event.Port)
			}
		}
	})

	s := &http.Server{
		Addr:    fmt.Sprintf(":%v", ff.config.Port),
		Handler: ff.basicAuth(proxy),
	}

	if ff.config.SSL {
		if ff.config.CA != "" {
			s.TLSConfig = ff.getCACert()
		}

		log.Printf("Listening on port: %v TLS: %v\n", ff.config.Port, ff.config.SSL)
		s.ListenAndServeTLS(ff.config.Cert, ff.config.Key)
	} else {
		log.Printf("Listening on port: %v TLS: %v\n", ff.config.Port, ff.config.SSL)
		s.ListenAndServe()
	}

}
Example #10
0
func (s *RBSuite) TestRebalancerRemoveServer(c *C) {
	a, b := testutils.NewResponder("a"), testutils.NewResponder("b")
	defer a.Close()
	defer b.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	rb, err := NewRebalancer(lb)
	c.Assert(err, IsNil)

	rb.UpsertServer(testutils.ParseURI(a.URL))
	rb.UpsertServer(testutils.ParseURI(b.URL))

	proxy := httptest.NewServer(rb)
	defer proxy.Close()

	c.Assert(seq(c, proxy.URL, 3), DeepEquals, []string{"a", "b", "a"})

	c.Assert(rb.RemoveServer(testutils.ParseURI(a.URL)), IsNil)
	c.Assert(seq(c, proxy.URL, 3), DeepEquals, []string{"b", "b", "b"})
}
Example #11
0
func (s *RRSuite) TestWeighted(c *C) {
	a := testutils.NewResponder("a")
	defer a.Close()

	b := testutils.NewResponder("b")
	defer b.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	lb.UpsertServer(testutils.ParseURI(a.URL), Weight(3))
	lb.UpsertServer(testutils.ParseURI(b.URL), Weight(2))

	proxy := httptest.NewServer(lb)
	defer proxy.Close()

	c.Assert(seq(c, proxy.URL, 6), DeepEquals, []string{"a", "a", "b", "a", "b", "a"})

	w, ok := lb.ServerWeight(testutils.ParseURI(a.URL))
	c.Assert(w, Equals, 3)
	c.Assert(ok, Equals, true)

	w, ok = lb.ServerWeight(testutils.ParseURI(b.URL))
	c.Assert(w, Equals, 2)
	c.Assert(ok, Equals, true)

	w, ok = lb.ServerWeight(testutils.ParseURI("http://caramba:4000"))
	c.Assert(w, Equals, -1)
	c.Assert(ok, Equals, false)
}
Example #12
0
func (s *STSuite) TestFileStreamingResponse(c *C) {
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte("hello, this response is too large to fit in memory"))
	})
	defer srv.Close()

	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	// this is our redirect to server
	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})

	// stream handler will forward requests to redirect
	st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)), MemResponseBodyBytes(4))
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(st)
	defer proxy.Close()

	re, body, err := testutils.Get(proxy.URL)
	c.Assert(err, IsNil)
	c.Assert(re.StatusCode, Equals, http.StatusOK)
	c.Assert(string(body), Equals, "hello, this response is too large to fit in memory")
}
Example #13
0
func New() (*HTTPConnectForwarder, error) {
	fwd, err := forward.New()
	if err != nil {
		return nil, err
	}
	return &HTTPConnectForwarder{
		fwd: fwd,
	}, nil
}
Example #14
0
func NewUpdater(master string) (updater *Updater) {
	fwd, err := forward.New()
	lb, err := roundrobin.New(fwd)
	masterUrl, err := url.Parse(master)
	lb.UpsertServer(masterUrl)

	if err != nil {
		panic(err)
	}

	return &Updater{lb: lb, master: master}
}
Example #15
0
func NewProxy(hosts map[string]string) (*Proxy, error) {
	clog := log.WithFields(log.Fields{})
	fwd, err := forward.New(forward.Logger(clog))
	if err != nil {
		return nil, err
	}
	return &Proxy{
		hosts: hosts,
		fwd:   fwd,
		log:   clog,
	}, nil
}
Example #16
0
// Test scenario when increaing the weight on good endpoints made it worse
func (s *RBSuite) TestRebalancerCascading(c *C) {
	a, b, d := testutils.NewResponder("a"), testutils.NewResponder("b"), testutils.NewResponder("d")
	defer a.Close()
	defer b.Close()
	defer d.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	newMeter := func() (Meter, error) {
		return &testMeter{}, nil
	}
	rb, err := NewRebalancer(lb, RebalancerMeter(newMeter), RebalancerClock(s.clock), RebalancerLogger(s.log))
	c.Assert(err, IsNil)

	rb.UpsertServer(testutils.ParseURI(a.URL))
	rb.UpsertServer(testutils.ParseURI(b.URL))
	rb.UpsertServer(testutils.ParseURI(d.URL))
	rb.servers[0].meter.(*testMeter).rating = 0.3

	proxy := httptest.NewServer(rb)
	defer proxy.Close()

	for i := 0; i < 6; i += 1 {
		testutils.Get(proxy.URL)
		testutils.Get(proxy.URL)
		s.clock.CurrentTime = s.clock.CurrentTime.Add(rb.backoffDuration + time.Second)
	}

	// We have increased the load, and the situation became worse as the other servers started failing
	c.Assert(rb.servers[0].curWeight, Equals, 1)
	c.Assert(rb.servers[1].curWeight, Equals, FSMMaxWeight)
	c.Assert(rb.servers[2].curWeight, Equals, FSMMaxWeight)

	// server a is now recovering, the weights should go back to the original state
	rb.servers[0].meter.(*testMeter).rating = 0.3
	rb.servers[1].meter.(*testMeter).rating = 0.2
	rb.servers[2].meter.(*testMeter).rating = 0.2

	for i := 0; i < 6; i += 1 {
		testutils.Get(proxy.URL)
		testutils.Get(proxy.URL)
		s.clock.CurrentTime = s.clock.CurrentTime.Add(rb.backoffDuration + time.Second)
	}

	// the algo reverted it back
	c.Assert(rb.servers[0].curWeight, Equals, 1)
	c.Assert(rb.servers[1].curWeight, Equals, 1)
	c.Assert(rb.servers[2].curWeight, Equals, 1)
}
Example #17
0
// Test scenario when one server goes down after what it recovers
func (s *RBSuite) TestRebalancerRecovery(c *C) {
	a, b := testutils.NewResponder("a"), testutils.NewResponder("b")
	defer a.Close()
	defer b.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	newMeter := func() (Meter, error) {
		return &testMeter{}, nil
	}
	rb, err := NewRebalancer(lb, RebalancerMeter(newMeter), RebalancerClock(s.clock), RebalancerLogger(s.log))
	c.Assert(err, IsNil)

	rb.UpsertServer(testutils.ParseURI(a.URL))
	rb.UpsertServer(testutils.ParseURI(b.URL))
	rb.servers[0].meter.(*testMeter).rating = 0.3

	proxy := httptest.NewServer(rb)
	defer proxy.Close()

	for i := 0; i < 6; i += 1 {
		testutils.Get(proxy.URL)
		testutils.Get(proxy.URL)
		s.clock.CurrentTime = s.clock.CurrentTime.Add(rb.backoffDuration + time.Second)
	}

	c.Assert(rb.servers[0].curWeight, Equals, 1)
	c.Assert(rb.servers[1].curWeight, Equals, FSMMaxWeight)

	c.Assert(lb.servers[0].weight, Equals, 1)
	c.Assert(lb.servers[1].weight, Equals, FSMMaxWeight)

	// server a is now recovering, the weights should go back to the original state
	rb.servers[0].meter.(*testMeter).rating = 0

	for i := 0; i < 6; i += 1 {
		testutils.Get(proxy.URL)
		testutils.Get(proxy.URL)
		s.clock.CurrentTime = s.clock.CurrentTime.Add(rb.backoffDuration + time.Second)
	}

	c.Assert(rb.servers[0].curWeight, Equals, 1)
	c.Assert(rb.servers[1].curWeight, Equals, 1)

	// Make sure we have applied the weights to the inner load balancer
	c.Assert(lb.servers[0].weight, Equals, 1)
	c.Assert(lb.servers[1].weight, Equals, 1)
}
Example #18
0
func syncApps(jsontasks *MarathonTasks, jsonapps *MarathonApps) {
	apps.Lock()
	defer apps.Unlock()
	appstmp := make(map[string]App)
	for _, task := range jsontasks.Tasks {
		// Use regex to remove characters that are not allowed in hostnames
		re := regexp.MustCompile("[^0-9a-z-]")
		appid := re.ReplaceAllLiteralString(task.AppId, "")
		apphealth := false
		for _, v := range jsonapps.Apps {
			if v.Id == task.AppId {
				if s, ok := v.Labels["moxy_subdomain"]; ok {
					appid = s
				}
				if len(v.HealthChecks) > 0 {
					apphealth = true
				}
			}
		}
		if apphealth {
			if len(task.HealthCheckResults) == 0 {
				// this means tasks is being deployed but not yet monitored as alive. Assume down.
				continue
			}
			alive := true
			for _, health := range task.HealthCheckResults {
				// check if health check is alive
				if health.Alive == false {
					alive = false
				}
			}
			if alive != true {
				// at least one health check has failed. Assume down.
				continue
			}
		}
		if s, ok := appstmp[appid]; ok {
			s.Lb.UpsertServer(testutils.ParseURI("http://" + task.Host + ":" + strconv.FormatInt(task.Ports[0], 10)))
			s.Tasks = append(s.Tasks, task.Host+":"+strconv.FormatInt(task.Ports[0], 10))
			appstmp[appid] = s
		} else {
			var s = App{}
			s.Fwd, _ = forward.New(forward.PassHostHeader(true))
			s.Lb, _ = roundrobin.New(s.Fwd)
			s.Lb.UpsertServer(testutils.ParseURI("http://" + task.Host + ":" + strconv.FormatInt(task.Ports[0], 10)))
			s.Tasks = []string{task.Host + ":" + strconv.FormatInt(task.Ports[0], 10)}
			appstmp[appid] = s
		}
	}
	apps.Apps = appstmp
}
Example #19
0
func (s *RRSuite) TestNoServers(c *C) {
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(lb)
	defer proxy.Close()

	re, _, err := testutils.Get(proxy.URL)
	c.Assert(err, IsNil)
	c.Assert(re.StatusCode, Equals, http.StatusInternalServerError)
}
Example #20
0
func new(c *C, p string) (*roundrobin.RoundRobin, *Streamer) {
	logger := utils.NewFileLogger(os.Stdout, utils.INFO)
	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New(forward.Logger(logger))
	c.Assert(err, IsNil)

	// load balancer will round robin request
	lb, err := roundrobin.New(fwd)
	c.Assert(err, IsNil)

	// stream handler will forward requests to redirect, make sure it uses files
	st, err := New(lb, Logger(logger), Retry(p), MemRequestBodyBytes(1))
	c.Assert(err, IsNil)

	return lb, st
}
Example #21
0
// Removing the server resets the state
func (s *RBSuite) TestRebalancerReset(c *C) {
	a, b, d := testutils.NewResponder("a"), testutils.NewResponder("b"), testutils.NewResponder("d")
	defer a.Close()
	defer b.Close()
	defer d.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	newMeter := func() (Meter, error) {
		return &testMeter{}, nil
	}
	rb, err := NewRebalancer(lb, RebalancerMeter(newMeter), RebalancerClock(s.clock), RebalancerLogger(s.log))
	c.Assert(err, IsNil)

	rb.UpsertServer(testutils.ParseURI(a.URL))
	rb.UpsertServer(testutils.ParseURI(b.URL))
	rb.UpsertServer(testutils.ParseURI(d.URL))
	rb.servers[0].meter.(*testMeter).rating = 0.3
	rb.servers[1].meter.(*testMeter).rating = 0
	rb.servers[2].meter.(*testMeter).rating = 0

	proxy := httptest.NewServer(rb)
	defer proxy.Close()

	for i := 0; i < 6; i += 1 {
		testutils.Get(proxy.URL)
		testutils.Get(proxy.URL)
		s.clock.CurrentTime = s.clock.CurrentTime.Add(rb.backoffDuration + time.Second)
	}

	// load balancer changed weights
	c.Assert(rb.servers[0].curWeight, Equals, 1)
	c.Assert(rb.servers[1].curWeight, Equals, FSMMaxWeight)
	c.Assert(rb.servers[2].curWeight, Equals, FSMMaxWeight)

	// Removing servers has reset the state
	rb.RemoveServer(testutils.ParseURI(d.URL))

	c.Assert(rb.servers[0].curWeight, Equals, 1)
	c.Assert(rb.servers[1].curWeight, Equals, 1)
}
Example #22
0
func main() {
	logger := utils.NewFileLogger(os.Stdout, utils.INFO)
	// Forwards incoming requests to whatever location URL points to, adds proper forwarding headers
	fwd, _ := forward.New(forward.Logger(logger))

	redirect := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		// let us forward this request to another server
		req.URL = testutils.ParseURI("http://localhost")
		fwd.ServeHTTP(w, req)
	})

	// that's it! our reverse proxy is ready!
	s := &http.Server{
		Addr:    ":8088",
		Handler: redirect,
	}
	s.ListenAndServe()
}
Example #23
0
func (s *RRSuite) TestCustomErrHandler(c *C) {
	errHandler := utils.ErrorHandlerFunc(func(w http.ResponseWriter, req *http.Request, err error) {
		w.WriteHeader(http.StatusTeapot)
		w.Write([]byte(http.StatusText(http.StatusTeapot)))
	})

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd, ErrorHandler(errHandler))
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(lb)
	defer proxy.Close()

	re, _, err := testutils.Get(proxy.URL)
	c.Assert(err, IsNil)
	c.Assert(re.StatusCode, Equals, http.StatusTeapot)
}
Example #24
0
func getBackend(r *http.Request) (Backend, bool) {
	var b = Backend{}
	host := strings.ToLower(r.Host)
	if b, ok := backends.Backends[host]; ok {
		return b, true
	}
	if bc, ok := getBackendConf(host); ok {
		b.Conf = bc
		b.Fwd, _ = forward.New(forward.PassHostHeader(true))
		b.Lb, _ = roundrobin.New(b.Fwd)
		for _, upstream := range bc.UpStreams {
			b.Lb.UpsertServer(testutils.ParseURI(upstream))
		}
		backends.Lock()
		backends.Backends[host] = b
		backends.Unlock()
		return b, true
	}
	return b, false
}
Example #25
0
func LoadConfig(configuration *Configuration, gloablConfiguration *GlobalConfiguration) (*mux.Router, error) {
	router := mux.NewRouter()
	router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
	backends := map[string]http.Handler{}
	for frontendName, frontend := range configuration.Frontends {
		log.Debug("Creating frontend %s", frontendName)
		fwd, _ := forward.New(forward.Logger(oxyLogger))
		newRoute := router.NewRoute().Name(frontendName)
		for routeName, route := range frontend.Routes {
			log.Debug("Creating route %s %s:%s", routeName, route.Rule, route.Value)
			newRouteReflect := Invoke(newRoute, route.Rule, route.Value)
			newRoute = newRouteReflect[0].Interface().(*mux.Route)
		}
		if backends[frontend.Backend] == nil {
			log.Debug("Creating backend %s", frontend.Backend)
			lb, _ := roundrobin.New(fwd)
			rb, _ := roundrobin.NewRebalancer(lb, roundrobin.RebalancerLogger(oxyLogger))
			for serverName, server := range configuration.Backends[frontend.Backend].Servers {
				if url, err := url.Parse(server.Url); err != nil {
					return nil, err
				} else {
					log.Debug("Creating server %s %s", serverName, url.String())
					rb.UpsertServer(url, roundrobin.Weight(server.Weight))
				}
			}
			backends[frontend.Backend] = rb
		} else {
			log.Debug("Reusing backend %s", frontend.Backend)
		}
		//		stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(gloablConfiguration.Replay)), stream.Logger(oxyLogger))
		var negroni = negroni.New()
		negroni.Use(middlewares.NewCircuitBreaker(backends[frontend.Backend], cbreaker.Logger(oxyLogger)))
		newRoute.Handler(negroni)
		err := newRoute.GetError()
		if err != nil {
			log.Error("Error building route ", err)
		}
	}
	return router, nil
}
Example #26
0
func (s *RBSuite) TestRebalancerNormalOperation(c *C) {
	a, b := testutils.NewResponder("a"), testutils.NewResponder("b")
	defer a.Close()
	defer b.Close()

	fwd, err := forward.New()
	c.Assert(err, IsNil)

	lb, err := New(fwd)
	c.Assert(err, IsNil)

	rb, err := NewRebalancer(lb)
	c.Assert(err, IsNil)

	rb.UpsertServer(testutils.ParseURI(a.URL))
	c.Assert(rb.Servers()[0].String(), Equals, a.URL)

	proxy := httptest.NewServer(rb)
	defer proxy.Close()

	c.Assert(seq(c, proxy.URL, 3), DeepEquals, []string{"a", "a", "a"})
}
Example #27
0
func proxyHandler() http.Handler {
	proxyHandleFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		target := backend.GetTarget(r)
		if target == "" {
			log.Println()
			http.Error(w, "Bad Gateway", 502)
			return
		}
		//WebSocket Connection
		if isWebsocket(r) {
			log.Println("Initializing WS proxy : ", target)
			ws_proxy := websocketProxy(target)
			ws_proxy.ServeHTTP(w, r)
			return
		}
		//All other HTTP requests
		httpProxy, _ := forward.New(forward.PassHostHeader(true))
		r.URL = testutils.ParseURI("http://" + target)
		httpProxy.ServeHTTP(w, r)
	})
	return proxyHandleFunc
}
Example #28
0
func (s *STSuite) TestChunkedEncodingSuccess(c *C) {
	var reqBody string
	var contentLength int64
	srv := testutils.NewHandler(func(w http.ResponseWriter, req *http.Request) {
		body, err := ioutil.ReadAll(req.Body)
		c.Assert(err, IsNil)
		reqBody = string(body)
		contentLength = req.ContentLength
		w.Write([]byte("hello"))
	})
	defer srv.Close()

	// forwarder will proxy the request to whatever destination
	fwd, err := forward.New()
	c.Assert(err, IsNil)

	// this is our redirect to server
	rdr := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.URL = testutils.ParseURI(srv.URL)
		fwd.ServeHTTP(w, req)
	})

	// stream handler will forward requests to redirect
	st, err := New(rdr, Logger(utils.NewFileLogger(os.Stdout, utils.INFO)))
	c.Assert(err, IsNil)

	proxy := httptest.NewServer(st)
	defer proxy.Close()

	conn, err := net.Dial("tcp", testutils.ParseURI(proxy.URL).Host)
	c.Assert(err, IsNil)
	fmt.Fprintf(conn, "POST / HTTP/1.0\r\nTransfer-Encoding: chunked\r\n\r\n4\r\ntest\r\n5\r\ntest1\r\n5\r\ntest2\r\n0\r\n\r\n")
	status, err := bufio.NewReader(conn).ReadString('\n')

	c.Assert(reqBody, Equals, "testtest1test2")
	c.Assert(status, Equals, "HTTP/1.0 200 OK\r\n")
	c.Assert(contentLength, Equals, int64(len(reqBody)))
}
Example #29
0
// LoadConfig returns a new gorrilla.mux Route from the specified global configuration and the dynamic
// provider configurations.
func LoadConfig(configurations configs, globalConfiguration *GlobalConfiguration) (*mux.Router, error) {
	router := mux.NewRouter()
	router.NotFoundHandler = http.HandlerFunc(notFoundHandler)
	backends := map[string]http.Handler{}
	for _, configuration := range configurations {
		for frontendName, frontend := range configuration.Frontends {
			log.Debugf("Creating frontend %s", frontendName)
			fwd, _ := forward.New(forward.Logger(oxyLogger), forward.PassHostHeader(frontend.PassHostHeader))
			newRoute := router.NewRoute().Name(frontendName)
			for routeName, route := range frontend.Routes {
				log.Debugf("Creating route %s %s:%s", routeName, route.Rule, route.Value)
				newRouteReflect, err := invoke(newRoute, route.Rule, route.Value)
				if err != nil {
					return nil, err
				}
				newRoute = newRouteReflect[0].Interface().(*mux.Route)
			}
			if backends[frontend.Backend] == nil {
				log.Debugf("Creating backend %s", frontend.Backend)
				var lb http.Handler
				rr, _ := roundrobin.New(fwd)
				if configuration.Backends[frontend.Backend] == nil {
					return nil, errors.New("Backend not found: " + frontend.Backend)
				}
				lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer)
				if err != nil {
					configuration.Backends[frontend.Backend].LoadBalancer = &types.LoadBalancer{Method: "wrr"}
				}
				switch lbMethod {
				case types.Drr:
					log.Infof("Creating load-balancer drr")
					rebalancer, _ := roundrobin.NewRebalancer(rr, roundrobin.RebalancerLogger(oxyLogger))
					lb = rebalancer
					for serverName, server := range configuration.Backends[frontend.Backend].Servers {
						url, err := url.Parse(server.URL)
						if err != nil {
							return nil, err
						}
						log.Infof("Creating server %s %s", serverName, url.String())
						rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight))
					}
				case types.Wrr:
					log.Infof("Creating load-balancer wrr")
					lb = middlewares.NewWebsocketUpgrader(rr)
					for serverName, server := range configuration.Backends[frontend.Backend].Servers {
						url, err := url.Parse(server.URL)
						if err != nil {
							return nil, err
						}
						log.Infof("Creating server %s %s", serverName, url.String())
						rr.UpsertServer(url, roundrobin.Weight(server.Weight))
					}
				}
				var negroni = negroni.New()
				if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
					log.Infof("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression)
					negroni.Use(middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger)))
				} else {
					negroni.UseHandler(lb)
				}
				backends[frontend.Backend] = negroni
			} else {
				log.Infof("Reusing backend %s", frontend.Backend)
			}
			//		stream.New(backends[frontend.Backend], stream.Retry("IsNetworkError() && Attempts() <= " + strconv.Itoa(globalConfiguration.Replay)), stream.Logger(oxyLogger))

			newRoute.Handler(backends[frontend.Backend])
			err := newRoute.GetError()
			if err != nil {
				log.Errorf("Error building route: %s", err)
			}
		}
	}
	return router, nil
}
Example #30
0
// LoadConfig returns a new gorilla.mux Route from the specified global configuration and the dynamic
// provider configurations.
func (server *Server) loadConfig(configurations configs, globalConfiguration GlobalConfiguration) (map[string]serverEntryPoint, error) {
	serverEntryPoints := server.buildEntryPoints(globalConfiguration)
	redirectHandlers := make(map[string]http.Handler)

	backends := map[string]http.Handler{}
	for _, configuration := range configurations {
		frontendNames := sortedFrontendNamesForConfig(configuration)
		for _, frontendName := range frontendNames {
			frontend := configuration.Frontends[frontendName]

			log.Debugf("Creating frontend %s", frontendName)
			fwd, _ := forward.New(forward.Logger(oxyLogger), forward.PassHostHeader(frontend.PassHostHeader))
			// default endpoints if not defined in frontends
			if len(frontend.EntryPoints) == 0 {
				frontend.EntryPoints = globalConfiguration.DefaultEntryPoints
			}
			for _, entryPointName := range frontend.EntryPoints {
				log.Debugf("Wiring frontend %s to entryPoint %s", frontendName, entryPointName)
				if _, ok := serverEntryPoints[entryPointName]; !ok {
					return nil, errors.New("Undefined entrypoint: " + entryPointName)
				}
				newRoute := serverEntryPoints[entryPointName].httpRouter.NewRoute().Name(frontendName)
				for routeName, route := range frontend.Routes {
					log.Debugf("Creating route %s %s:%s", routeName, route.Rule, route.Value)
					route, err := getRoute(newRoute, route.Rule, route.Value)
					if err != nil {
						return nil, err
					}
					newRoute = route
				}
				entryPoint := globalConfiguration.EntryPoints[entryPointName]
				if entryPoint.Redirect != nil {
					if redirectHandlers[entryPointName] != nil {
						newRoute.Handler(redirectHandlers[entryPointName])
					} else if handler, err := server.loadEntryPointConfig(entryPointName, entryPoint); err != nil {
						return nil, err
					} else {
						newRoute.Handler(handler)
						redirectHandlers[entryPointName] = handler
					}
				} else {
					if backends[frontend.Backend] == nil {
						log.Debugf("Creating backend %s", frontend.Backend)
						var lb http.Handler
						rr, _ := roundrobin.New(fwd)
						if configuration.Backends[frontend.Backend] == nil {
							return nil, errors.New("Undefined backend: " + frontend.Backend)
						}
						lbMethod, err := types.NewLoadBalancerMethod(configuration.Backends[frontend.Backend].LoadBalancer)
						if err != nil {
							configuration.Backends[frontend.Backend].LoadBalancer = &types.LoadBalancer{Method: "wrr"}
						}
						switch lbMethod {
						case types.Drr:
							log.Debugf("Creating load-balancer drr")
							rebalancer, _ := roundrobin.NewRebalancer(rr, roundrobin.RebalancerLogger(oxyLogger))
							lb = rebalancer
							for serverName, server := range configuration.Backends[frontend.Backend].Servers {
								url, err := url.Parse(server.URL)
								if err != nil {
									return nil, err
								}
								log.Debugf("Creating server %s at %s with weight %d", serverName, url.String(), server.Weight)
								rebalancer.UpsertServer(url, roundrobin.Weight(server.Weight))
							}
						case types.Wrr:
							log.Debugf("Creating load-balancer wrr")
							lb = middlewares.NewWebsocketUpgrader(rr)
							for serverName, server := range configuration.Backends[frontend.Backend].Servers {
								url, err := url.Parse(server.URL)
								if err != nil {
									return nil, err
								}
								log.Debugf("Creating server %s at %s with weight %d", serverName, url.String(), server.Weight)
								rr.UpsertServer(url, roundrobin.Weight(server.Weight))
							}
						}
						var negroni = negroni.New()
						if configuration.Backends[frontend.Backend].CircuitBreaker != nil {
							log.Debugf("Creating circuit breaker %s", configuration.Backends[frontend.Backend].CircuitBreaker.Expression)
							negroni.Use(middlewares.NewCircuitBreaker(lb, configuration.Backends[frontend.Backend].CircuitBreaker.Expression, cbreaker.Logger(oxyLogger)))
						} else {
							negroni.UseHandler(lb)
						}
						backends[frontend.Backend] = negroni
					} else {
						log.Debugf("Reusing backend %s", frontend.Backend)
					}
					server.wireFrontendBackend(frontend.Routes, newRoute, backends[frontend.Backend])
				}
				err := newRoute.GetError()
				if err != nil {
					log.Errorf("Error building route: %s", err)
				}
			}
		}
	}
	return serverEntryPoints, nil
}