-
Notifications
You must be signed in to change notification settings - Fork 31
NOISSUE - Add WebSocket support to HTTP Proxy #87
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
Merged
+670
−284
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
f7222b3
remove print line
arvindh123 055c834
add websocket to http proxy
arvindh123 a4b314e
add websocket to http proxy
arvindh123 54a860c
initalized NewOriginChecker
arvindh123 daca640
add toolchain
arvindh123 08a9742
remove toolchain
arvindh123 68e1828
update the workflow
arvindh123 2702b97
update the workflow
arvindh123 23c27d4
go mod tidy
arvindh123 594a37f
convert to interface
arvindh123 4925184
convert to interface
arvindh123 77cf55e
convert to interface
arvindh123 24dabae
format files
arvindh123 33a6f53
fix url path
arvindh123 33b3378
fix targetUrl
arvindh123 68f6904
fix mqtt ws
arvindh123 c9edf69
add env for listen and target portocol,host,port,path
arvindh123 0949901
upgrade liniter
arvindh123 cbf2969
fix linter errors
arvindh123 b2c948b
upgrade liniter
arvindh123 01cb32c
fix websocket proxy
arvindh123 1972a33
update getPass func
arvindh123 c0b8fce
websocket process
arvindh123 c5324d4
fix linter errors
arvindh123 fe28d8d
move checkers to single interface
arvindh123 5b1db62
update prefix
arvindh123 e77af86
modified checker
arvindh123 e40a4bc
consolidate and format function names
arvindh123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) Abstract Machines | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package http | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "regexp" | ||
| ) | ||
|
|
||
| const errNotByPassed = "route - %s is not in bypass list" | ||
|
||
|
|
||
| var errByPassDisabled = errors.New("bypass disabled") | ||
|
|
||
| type byPassChecker struct { | ||
| enabled bool | ||
| byPassPatterns []*regexp.Regexp | ||
| } | ||
|
|
||
| var _ Checker = (*byPassChecker)(nil) | ||
|
|
||
| func NewByPassChecker(byPassPatterns []string) (Checker, error) { | ||
| enabled := len(byPassPatterns) != 0 | ||
| var byp []*regexp.Regexp | ||
| for _, expr := range byPassPatterns { | ||
| re, err := regexp.Compile(expr) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| byp = append(byp, re) | ||
| } | ||
|
|
||
| return &byPassChecker{ | ||
| enabled: enabled, | ||
| byPassPatterns: byp, | ||
| }, nil | ||
| } | ||
|
|
||
| func (bpc *byPassChecker) Check(r *http.Request) error { | ||
| if !bpc.enabled { | ||
| return errByPassDisabled | ||
| } | ||
| for _, pattern := range bpc.byPassPatterns { | ||
| if pattern.MatchString(r.URL.Path) { | ||
| return nil | ||
| } | ||
| } | ||
| return fmt.Errorf(errNotByPassed, r.URL.Path) | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,10 @@ const ( | |
| upgradeHeaderVal = "websocket" | ||
| ) | ||
|
|
||
| type Checker interface { | ||
| Check(r *http.Request) error | ||
| } | ||
|
|
||
| func isWebSocketRequest(r *http.Request) bool { | ||
| return strings.EqualFold(r.Header.Get(connHeaderKey), connHeaderVal) && | ||
| strings.EqualFold(r.Header.Get(upgradeHeaderKey), upgradeHeaderVal) | ||
|
|
@@ -45,8 +49,8 @@ func (p Proxy) getUserPass(r *http.Request) (string, string) { | |
| switch { | ||
| case ok: | ||
| return username, password | ||
| case len(r.URL.Query()[authzQueryKey]) != 0: | ||
| password = r.URL.Query()[authzQueryKey][0] | ||
| case len(r.URL.Query().Get(authzQueryKey)) != 0: | ||
|
||
| password = r.URL.Query().Get(authzQueryKey) | ||
| return username, password | ||
dborovcanin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case r.Header.Get(authzHeaderKey) != "": | ||
| password = r.Header.Get(authzHeaderKey) | ||
|
|
@@ -63,7 +67,7 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
|
|
||
| r.URL.Path = strings.TrimPrefix(r.URL.Path, p.config.PathPrefix) | ||
|
|
||
| if err := p.checkers.ShouldBypass(r); err == nil { | ||
| if err := p.bypass.Check(r); err == nil { | ||
| p.target.ServeHTTP(w, r) | ||
| return | ||
| } | ||
|
|
@@ -109,9 +113,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
| p.target.ServeHTTP(w, r) | ||
| } | ||
|
|
||
| func checkOrigin(oc Checkers) func(r *http.Request) bool { | ||
| func checkOrigin(allowedOrigins []string) func(r *http.Request) bool { | ||
| oc := NewOriginChecker(allowedOrigins) | ||
| return func(r *http.Request) bool { | ||
| return oc.CheckOrigin(r) == nil | ||
| return oc.Check(r) == nil | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -134,7 +139,7 @@ type Proxy struct { | |
| session session.Handler | ||
| logger *slog.Logger | ||
| wsUpgrader websocket.Upgrader | ||
| checkers Checkers | ||
| bypass Checker | ||
| } | ||
|
|
||
| func NewProxy(config mgate.Config, handler session.Handler, logger *slog.Logger, allowedOrigins []string, bypassPaths []string) (Proxy, error) { | ||
|
|
@@ -143,19 +148,20 @@ func NewProxy(config mgate.Config, handler session.Handler, logger *slog.Logger, | |
| Host: net.JoinHostPort(config.TargetHost, config.TargetPort), | ||
| } | ||
|
|
||
| c, err := NewCheckers(allowedOrigins, bypassPaths) | ||
| bpc, err := NewByPassChecker(bypassPaths) | ||
| if err != nil { | ||
| return Proxy{}, err | ||
| } | ||
| wsUpgrader := websocket.Upgrader{CheckOrigin: checkOrigin(c)} | ||
|
|
||
| wsUpgrader := websocket.Upgrader{CheckOrigin: checkOrigin(allowedOrigins)} | ||
|
|
||
| return Proxy{ | ||
| config: config, | ||
| target: httputil.NewSingleHostReverseProxy(targetUrl), | ||
| session: handler, | ||
| logger: logger, | ||
| wsUpgrader: wsUpgrader, | ||
| checkers: c, | ||
| bypass: bpc, | ||
| }, nil | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Copyright (c) Abstract Machines | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package http | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| ) | ||
|
|
||
| var errNotAllowed = "origin - %s is not allowed" | ||
|
|
||
| type originChecker struct { | ||
| enabled bool | ||
| allowedOrigins map[string]struct{} | ||
| } | ||
|
|
||
| var _ Checker = (*originChecker)(nil) | ||
|
|
||
| func NewOriginChecker(allowedOrigins []string) Checker { | ||
| enabled := len(allowedOrigins) != 0 | ||
| ao := make(map[string]struct{}) | ||
| for _, allowedOrigin := range allowedOrigins { | ||
| ao[allowedOrigin] = struct{}{} | ||
| } | ||
|
|
||
| return &originChecker{ | ||
| enabled: enabled, | ||
| allowedOrigins: ao, | ||
| } | ||
| } | ||
|
|
||
| func (oc *originChecker) Check(r *http.Request) error { | ||
| if !oc.enabled { | ||
| return nil | ||
| } | ||
| origin := r.Header.Get("Origin") | ||
| _, allowed := oc.allowedOrigins[origin] | ||
| if allowed { | ||
| return nil | ||
| } | ||
| return fmt.Errorf(errNotAllowed, origin) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Use
bypassinstead ofbyPasssince it's cleaner to usebypassthanby pass.