123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package dpcore
- import (
- "bytes"
- "io"
- "net"
- "net/http"
- "net/url"
- "strings"
- )
- func replaceLocationHost(urlString string, rrr *ResponseRewriteRuleSet, useTLS bool) (string, error) {
- u, err := url.Parse(urlString)
- if err != nil {
- return "", err
- }
-
-
- if useTLS {
- u.Scheme = "https"
- } else {
- u.Scheme = "http"
- }
-
-
-
-
-
-
- if rrr.ProxyDomain != u.Host && !strings.Contains(u.Host, rrr.OriginalHost+":") {
-
-
- return urlString, nil
- }
- u.Host = rrr.OriginalHost
- if strings.Contains(rrr.ProxyDomain, "/") {
-
-
-
-
-
-
- ProxyDomainURL := "http://" + rrr.ProxyDomain
- if rrr.UseTLS {
- ProxyDomainURL = "https://" + rrr.ProxyDomain
- }
- ru, err := url.Parse(ProxyDomainURL)
- if err == nil {
-
- u.Path = strings.TrimPrefix(u.Path, ru.Path)
- }
- }
- return u.String(), nil
- }
- func ReplaceLocationHost(urlString string, rrr *ResponseRewriteRuleSet, useTLS bool) (string, error) {
- return replaceLocationHost(urlString, rrr, useTLS)
- }
- func isExternalDomainName(hostname string) bool {
- host, _, err := net.SplitHostPort(hostname)
- if err != nil {
-
- ip := net.ParseIP(hostname)
- if ip != nil {
-
- return false
- }
- } else {
-
- ip := net.ParseIP(host)
- if ip != nil {
-
- return false
- }
- }
-
- internalDNSTLD := []string{".local", ".internal", ".localhost", ".home.arpa"}
- for _, tld := range internalDNSTLD {
- if strings.HasSuffix(strings.ToLower(hostname), tld) {
- return false
- }
- }
- return true
- }
- func DeepCopyRequest(req *http.Request) (*http.Request, error) {
-
- urlCopy := *req.URL
-
- headersCopy := make(http.Header, len(req.Header))
- for k, vv := range req.Header {
- vvCopy := make([]string, len(vv))
- copy(vvCopy, vv)
- headersCopy[k] = vvCopy
- }
-
- cookiesCopy := make([]*http.Cookie, len(req.Cookies()))
- for i, cookie := range req.Cookies() {
- cookieCopy := *cookie
- cookiesCopy[i] = &cookieCopy
- }
-
- var bodyCopy io.ReadCloser
- if req.Body != nil {
- var buf bytes.Buffer
- if _, err := buf.ReadFrom(req.Body); err != nil {
- return nil, err
- }
-
- if err := req.Body.Close(); err != nil {
- return nil, err
- }
- req.Body = io.NopCloser(&buf)
- bodyCopy = io.NopCloser(bytes.NewReader(buf.Bytes()))
- }
-
- reqCopy := &http.Request{
- Method: req.Method,
- URL: &urlCopy,
- Proto: req.Proto,
- ProtoMajor: req.ProtoMajor,
- ProtoMinor: req.ProtoMinor,
- Header: headersCopy,
- Body: bodyCopy,
- ContentLength: req.ContentLength,
- TransferEncoding: append([]string(nil), req.TransferEncoding...),
- Close: req.Close,
- Host: req.Host,
- Form: req.Form,
- PostForm: req.PostForm,
- MultipartForm: req.MultipartForm,
- Trailer: req.Trailer,
- RemoteAddr: req.RemoteAddr,
- TLS: req.TLS,
-
- }
- return reqCopy, nil
- }
|