-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add proxy server feature. #41
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package http | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
|
||
killgrave "github.com/friendsofgo/killgrave/internal" | ||
) | ||
|
||
// Proxy represent reverse proxy server. | ||
type Proxy struct { | ||
server *httputil.ReverseProxy | ||
mode killgrave.ProxyMode | ||
} | ||
|
||
// NewProxy creates new proxy server. | ||
func NewProxy(rawurl string, mode killgrave.ProxyMode) (*Proxy, error) { | ||
u, err := url.Parse(rawurl) | ||
if err != nil { | ||
return nil, err | ||
} | ||
reverseProxy := httputil.NewSingleHostReverseProxy(u) | ||
return &Proxy{reverseProxy, mode}, nil | ||
} | ||
|
||
// Handler returns handler that sends request to another server. | ||
func (p *Proxy) Handler() http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
p.server.ServeHTTP(w, r) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you need to update the headers to allow for SSL redirection: r.URL.Host = p.url.Host
r.URL.Scheme = p.url.Scheme
r.Header.Set("X-Forwarded-Host", r.Header.Get("Host"))
r.Host = p.url.Host For that you will need to persist the result of url.Parse into the proxy struct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested with the comment that I suggested and, all working very fine :) |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package http | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
killgrave "github.com/friendsofgo/killgrave/internal" | ||
) | ||
|
||
func TestNewProxy(t *testing.T) { | ||
testCases := map[string]struct { | ||
rawUrl string | ||
mode killgrave.ProxyMode | ||
err error | ||
}{ | ||
"valid all": {"all", killgrave.ProxyAll, nil}, | ||
"valid mode none": {"none", killgrave.ProxyNone, nil}, | ||
"error rawUrl": {":http!/gogle.com", killgrave.ProxyNone, errors.New("error")}, | ||
} | ||
|
||
for name, tc := range testCases { | ||
t.Run(name, func(t *testing.T) { | ||
proxy, err := NewProxy(tc.rawUrl, tc.mode) | ||
if err != nil && tc.err == nil { | ||
t.Fatalf("not expected any erros and got %v", err) | ||
} | ||
|
||
if err == nil && tc.err != nil { | ||
t.Fatalf("expected an error and got nil") | ||
} | ||
if err != nil { | ||
return | ||
} | ||
if tc.mode != proxy.mode { | ||
t.Fatalf("expected: %v, got: %v", tc.mode, proxy.mode) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestProxyHandler(t *testing.T) { | ||
isRequestHandled := false | ||
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
isRequestHandled = true | ||
})) | ||
defer backend.Close() | ||
|
||
proxy, err := NewProxy(backend.URL, killgrave.ProxyAll) | ||
if err != nil { | ||
t.Fatal("NewProxy failed: ", err) | ||
} | ||
|
||
frontend := httptest.NewServer(proxy.Handler()) | ||
defer frontend.Close() | ||
|
||
_, err = http.Get(frontend.URL) | ||
if err != nil { | ||
t.Fatal("Frontend GET method failed: ", err) | ||
} | ||
if isRequestHandled != true { | ||
t.Fatal("Request was not proxied to backend server") | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need specifically uint8?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we prefer declare the enums with a more accurate type possible as standard, 8bits is more than enough to a enum and in most case u don't need negative numbers, so
uint8
is a elegant solution