示例#1
0
文件: proxy.go 项目: BillSun/asuran
func (p *Proxy) rewriteUrl(target string, w http.ResponseWriter, r *http.Request, rangeInfo string, prof *profile.Profile, f *life.Life, act profile.UrlProxyAction) bool {
	var content []byte = nil
	contentSource := ""
	switch act.Act {
	case profile.UrlActRewritten:
		u, err := url.QueryUnescape(act.ContentValue)
		if err != nil {
			return false
		}

		content = []byte(u)
		contentSource = "rewrite"
	case profile.UrlActRestore:
		content = prof.Restore(act.ContentValue)
		if content == nil {
			return false
		}
	default:
		return false
	}

	start := time.Now()
	w.Write(content)
	c := cache.NewUrlCache(target, r, nil, nil, contentSource, content, rangeInfo, start, time.Now(), nil)
	c.ResponseCode = 200
	if f != nil {
		p.saveContentToCache(target, f, c, false)
	}

	return true
}
示例#2
0
文件: html.go 项目: clarelin/asuran
func formatEditStoreData(profileIP string, prof *profile.Profile, id string) editStoreData {
	encodedContent := ""
	if len(id) > 0 {
		c := prof.Restore(id)
		if len(c) > 0 {
			encodedContent = strings.Replace(url.QueryEscape(string(c)), "+", "%20", -1)
		}
	}

	return editStoreData{profileIP, id, encodedContent}
}
示例#3
0
文件: proxy.go 项目: clarelin/asuran
func (p *Proxy) rewriteUrl(target string, w http.ResponseWriter, r *http.Request, rangeInfo string, prof *profile.Profile, f *life.Life, act profile.UrlProxyAction, speed profile.SpeedAction, bodyDelay profile.DelayAction) bool {
	var content []byte = nil
	contentSource := ""
	switch act.Act {
	case profile.UrlActRewritten:
		fallthrough
	case profile.UrlActTcpWritten:
		u, err := url.QueryUnescape(act.ContentValue)
		if err != nil {
			return false
		}

		content = []byte(u)
		if act.Act == profile.UrlActRewritten {
			contentSource = "rewrite"
		} else if act.Act == profile.UrlActTcpWritten {
			contentSource = "tcpwrite"
		}
	case profile.UrlActRestore:
		content = prof.Restore(act.ContentValue)
		if content == nil {
			return false
		}
	default:
		return false
	}

	if act.Act != profile.UrlActTcpWritten {
		p.procHeader(w.Header(), prof.SettingStringDef(target, "content-type", "default"))
	}

	var writeWrapper func(w io.Writer) io.Writer = nil
	switch bodyDelay.Act {
	case profile.DelayActDelayEach:
		fallthrough
	case profile.DelayActTimeout:
		writeWrapper = func(w io.Writer) io.Writer {
			return newDelayWriter(bodyDelay, w, p.r)
		}
	}

	switch speed.Act {
	case profile.SpeedActConstant:
		if writeWrapper != nil {
			wrap := writeWrapper
			writeWrapper = func(w io.Writer) io.Writer {
				return newSpeedWriter(speed, wrap(w))
			}
		} else {
			writeWrapper = func(w io.Writer) io.Writer {
				return newSpeedWriter(speed, w)
			}
		}
	}

	start := time.Now()
	if act.Act == profile.UrlActTcpWritten {
		net.TcpWriteHttp(w, writeWrapper, content)
	} else {
		if writeWrapper != nil {
			writeWrapper(w).Write(content)
		} else {
			w.Write(content)
		}
	}

	c := cache.NewUrlCache(target, r, nil, nil, contentSource, content, rangeInfo, start, time.Now(), nil)
	if act.Act == profile.UrlActTcpWritten {
		c.ResponseCode = 599
	} else {
		c.ResponseCode = 200
	}
	if f != nil {
		p.saveContentToCache(target, f, c, false)
	}

	return true
}