-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from tobychui/v3.0.6
V3.0.6 Update - Added fastly_client_ip to X-Real-IP auto rewrite - Added atomic accumulator to TCP proxy - Added white logo for future dark theme - Added multi selection for white / blacklist #176 - Moved custom header rewrite to dpcore - Restructure dpcore header rewrite sequence - Added advance custom header settings (zoraxy to upstream and zoraxy to downstream mode) - Added header remove feature - Removed password requirement for SMTP #162 #80 - Restructured TCP proxy into Stream Proxy (Support both TCP and UDP) #147 - Added stream proxy auto start #169 - Optimized UX for reminding user to click Apply after port change - Added version number to footer #160
- Loading branch information
Showing
39 changed files
with
14,892 additions
and
15,892 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,46 @@ | ||
package dynamicproxy | ||
|
||
/* | ||
CustomHeader.go | ||
This script handle parsing and injecting custom headers | ||
into the dpcore routing logic | ||
*/ | ||
|
||
//SplitInboundOutboundHeaders split user defined headers into upstream and downstream headers | ||
//return upstream header and downstream header key-value pairs | ||
//if the header is expected to be deleted, the value will be set to empty string | ||
func (ept *ProxyEndpoint) SplitInboundOutboundHeaders() ([][]string, [][]string) { | ||
if len(ept.UserDefinedHeaders) == 0 { | ||
//Early return if there are no defined headers | ||
return [][]string{}, [][]string{} | ||
} | ||
|
||
//Use pre-allocation for faster performance | ||
upstreamHeaders := make([][]string, len(ept.UserDefinedHeaders)) | ||
downstreamHeaders := make([][]string, len(ept.UserDefinedHeaders)) | ||
upstreamHeaderCounter := 0 | ||
downstreamHeaderCounter := 0 | ||
|
||
//Sort the headers into upstream or downstream | ||
for _, customHeader := range ept.UserDefinedHeaders { | ||
thisHeaderSet := make([]string, 2) | ||
thisHeaderSet[0] = customHeader.Key | ||
thisHeaderSet[1] = customHeader.Value | ||
if customHeader.IsRemove { | ||
//Prevent invalid config | ||
thisHeaderSet[1] = "" | ||
} | ||
|
||
//Assign to slice | ||
if customHeader.Direction == HeaderDirection_ZoraxyToUpstream { | ||
upstreamHeaders[upstreamHeaderCounter] = thisHeaderSet | ||
upstreamHeaderCounter++ | ||
} else if customHeader.Direction == HeaderDirection_ZoraxyToDownstream { | ||
downstreamHeaders[downstreamHeaderCounter] = thisHeaderSet | ||
downstreamHeaderCounter++ | ||
} | ||
} | ||
|
||
return upstreamHeaders, downstreamHeaders | ||
} |
This file contains 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
This file contains 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,121 @@ | ||
package dpcore | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
/* | ||
Header.go | ||
This script handles headers rewrite and remove | ||
in dpcore. | ||
Added in Zoraxy v3.0.6 by tobychui | ||
*/ | ||
|
||
// removeHeaders Remove hop-by-hop headers listed in the "Connection" header, Remove hop-by-hop headers. | ||
func removeHeaders(header http.Header, noCache bool) { | ||
// Remove hop-by-hop headers listed in the "Connection" header. | ||
if c := header.Get("Connection"); c != "" { | ||
for _, f := range strings.Split(c, ",") { | ||
if f = strings.TrimSpace(f); f != "" { | ||
header.Del(f) | ||
} | ||
} | ||
} | ||
|
||
// Remove hop-by-hop headers | ||
for _, h := range hopHeaders { | ||
if header.Get(h) != "" { | ||
header.Del(h) | ||
} | ||
} | ||
|
||
//Restore the Upgrade header if any | ||
if header.Get("Zr-Origin-Upgrade") != "" { | ||
header.Set("Upgrade", header.Get("Zr-Origin-Upgrade")) | ||
header.Del("Zr-Origin-Upgrade") | ||
} | ||
|
||
//Disable cache if nocache is set | ||
if noCache { | ||
header.Del("Cache-Control") | ||
header.Set("Cache-Control", "no-store") | ||
} | ||
|
||
} | ||
|
||
// rewriteUserAgent rewrite the user agent based on incoming request | ||
func rewriteUserAgent(header http.Header, UA string) { | ||
//Hide Go-HTTP-Client UA if the client didnt sent us one | ||
if header.Get("User-Agent") == "" { | ||
// If the outbound request doesn't have a User-Agent header set, | ||
// don't send the default Go HTTP client User-Agent | ||
header.Del("User-Agent") | ||
header.Set("User-Agent", UA) | ||
} | ||
} | ||
|
||
// Add X-Forwarded-For Header and rewrite X-Real-Ip according to sniffing logics | ||
func addXForwardedForHeader(req *http.Request) { | ||
if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil { | ||
// If we aren't the first proxy retain prior | ||
// X-Forwarded-For information as a comma+space | ||
// separated list and fold multiple headers into one. | ||
if prior, ok := req.Header["X-Forwarded-For"]; ok { | ||
clientIP = strings.Join(prior, ", ") + ", " + clientIP | ||
} | ||
req.Header.Set("X-Forwarded-For", clientIP) | ||
if req.TLS != nil { | ||
req.Header.Set("X-Forwarded-Proto", "https") | ||
} else { | ||
req.Header.Set("X-Forwarded-Proto", "http") | ||
} | ||
|
||
if req.Header.Get("X-Real-Ip") == "" { | ||
//Check if CF-Connecting-IP header exists | ||
CF_Connecting_IP := req.Header.Get("CF-Connecting-IP") | ||
Fastly_Client_IP := req.Header.Get("Fastly-Client-IP") | ||
if CF_Connecting_IP != "" { | ||
//Use CF Connecting IP | ||
req.Header.Set("X-Real-Ip", CF_Connecting_IP) | ||
} else if Fastly_Client_IP != "" { | ||
//Use Fastly Client IP | ||
req.Header.Set("X-Real-Ip", Fastly_Client_IP) | ||
} else { | ||
// Not exists. Fill it in with first entry in X-Forwarded-For | ||
ips := strings.Split(clientIP, ",") | ||
if len(ips) > 0 { | ||
req.Header.Set("X-Real-Ip", strings.TrimSpace(ips[0])) | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
|
||
// injectUserDefinedHeaders inject the user headers from slice | ||
// if a value is empty string, the key will be removed from header. | ||
// if a key is empty string, the function will return immediately | ||
func injectUserDefinedHeaders(header http.Header, userHeaders [][]string) { | ||
for _, userHeader := range userHeaders { | ||
if len(userHeader) == 0 { | ||
//End of header slice | ||
return | ||
} | ||
headerKey := userHeader[0] | ||
headerValue := userHeader[1] | ||
if headerValue == "" { | ||
//Remove header from head | ||
header.Del(headerKey) | ||
continue | ||
} | ||
|
||
//Default: Set header value | ||
header.Del(headerKey) //Remove header if it already exists | ||
header.Set(headerKey, headerValue) | ||
} | ||
} |
Oops, something went wrong.