Exemple #1
0
func (self *session) processResponse(res *http.Response) (r *response, err error) {
	ret := &response{
		code: res.StatusCode,
	}

	if h, ok := res.Header["Location"]; ok {
		ret.redir = h[0]
	}

	ret.body, err = ioutil.ReadAll(res.Body)
	if err != nil {
		return ret, err
	}

	ret.cookies = map[string]string{}

	for _, cookie := range res.Cookies() {
		ret.cookies[cookie.Name] = cookie.Value
		if cookie.Name == "SESSION" {
			if self.session == "" {
				self.session = cookie.Value
			}
		}
	}

	return ret, nil
}
Exemple #2
0
func setupStickySession(responseWriter http.ResponseWriter, response *http.Response,
	endpoint *route.Endpoint,
	originalEndpointId string,
	secureCookies bool,
	path string) {

	maxAge := 0

	// did the endpoint change?
	sticky := originalEndpointId != "" && originalEndpointId != endpoint.PrivateInstanceId

	for _, v := range response.Cookies() {
		if v.Name == StickyCookieKey {
			sticky = true
			if v.MaxAge < 0 {
				maxAge = v.MaxAge
			}
			break
		}
	}

	if sticky {
		cookie := &http.Cookie{
			Name:     VcapCookieId,
			Value:    endpoint.PrivateInstanceId,
			Path:     path,
			MaxAge:   maxAge,
			HttpOnly: true,
			Secure:   secureCookies,
		}

		http.SetCookie(responseWriter, cookie)
	}
}
Exemple #3
0
func ParseResponse(resp *http.Response, captureContent bool) *Response {
	if resp == nil {
		return nil
	}

	statusText := resp.Status
	if len(resp.Status) > 4 {
		statusText = resp.Status[4:]
	}
	redirectURL := resp.Header.Get("Location")
	harResponse := Response{
		Status:      resp.StatusCode,
		StatusText:  statusText,
		HttpVersion: resp.Proto,
		Cookies:     parseCookies(resp.Cookies()),
		Headers:     parseStringArrMap(resp.Header),
		RedirectUrl: redirectURL,
		BodySize:    resp.ContentLength,
		HeadersSize: calcHeaderSize(resp.Header),
	}

	if captureContent {
		parseContent(resp, &harResponse.Content)
	}

	return &harResponse
}
Exemple #4
0
func (codec *clientCodec) WriteRequest(request *rpc.Request, args interface{}) (err error) {
	httpRequest, err := NewRequest(codec.url.String(), request.ServiceMethod, args)

	if codec.cookies != nil {
		for _, cookie := range codec.cookies.Cookies(codec.url) {
			httpRequest.AddCookie(cookie)
		}
	}

	if err != nil {
		return err
	}

	var httpResponse *http.Response
	httpResponse, err = codec.httpClient.Do(httpRequest)

	if err != nil {
		return err
	}

	if codec.cookies != nil {
		codec.cookies.SetCookies(codec.url, httpResponse.Cookies())
	}

	codec.responses[request.Seq] = httpResponse
	codec.ready <- request.Seq

	return nil
}
Exemple #5
0
func setUserSession(w http.ResponseWriter, r *http.Request, username string) {
	handler.SetCookie(w, r, "_sess", username)
	resp := http.Response{Header: w.Header()}
	for _, v := range resp.Cookies() {
		r.AddCookie(v)
	}
}
Exemple #6
0
//Couchdb returns updated AuthSession tokens
func (ca *CookieAuth) updateAuth(resp *http.Response) {
	for _, cookie := range resp.Cookies() {
		if cookie.Name == "AuthSession" {
			ca.UpdatedAuthToken = cookie.Value
		}
	}
}
Exemple #7
0
func getSessionAndAppPort(url string, rPort uint16, c *C) (string, string, string) {
	var client http.Client
	var req *http.Request
	var resp *http.Response
	var err error
	var port []byte

	uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)
	req, err = http.NewRequest("GET", uri, nil)

	resp, err = client.Do(req)
	c.Assert(err, IsNil)

	port, err = ioutil.ReadAll(resp.Body)

	var session string
	var path string
	for _, cookie := range resp.Cookies() {
		if cookie.Name == "__VCAP_ID__" {
			session = cookie.Value
			path = cookie.Path
		}
	}

	return session, string(port), path
}
Exemple #8
0
func ResponseToJso(o *otto.Object, w *http.Response) {
	o.Set("status", w.Status)
	o.Set("header", w.Header)
	o.Set("cookies", w.Cookies())
	o.Set("statusCode", w.StatusCode)
	o.Set("proto", w.Proto)
	c, _ := ioutil.ReadAll(w.Body)
	o.Set("body", string(c))
	if w.TLS != nil {
		o.Set("tlsServerName", w.TLS.ServerName)
		o.Set("tlsNegotiatedProtocol", w.TLS.NegotiatedProtocol)
		names := []string{}
		ips := []string{}
		for _, k := range w.TLS.PeerCertificates {
			for _, n := range k.DNSNames {
				names = append(names, n)
			}
			for _, n := range k.IPAddresses {
				ips = append(ips, n.String())
			}
		}
		o.Set("tlsDNSNames", names)
		o.Set("tlsIPs", ips)
	}
}
func (info *Info) SetBasicInfo(url string, response *http.Response) {
	info.Url = url
	for key, values := range response.Header {
		info.RawHeaders[key] = values
	}
	cookies := response.Cookies()
	for _, cookie := range cookies {
		info.Cookies[cookie.Name] = cookie
	}
	u, err := urlLib.Parse(url)
	if err != nil {
		panic(err)
	}
	info.Host = u.Host
	ips, err := net.LookupHost(u.Host)
	if err == nil {
		info.Ip = ips
		for _, ip := range info.Ip {
			hosts, err := net.LookupAddr(ip)
			if err == nil {
				info.RealHost = hosts
			}
		}

	}
}
//下载文件,并对字符编码做相应的处理
func (this *HttpDownLoader) downloadFile(p *page.Page, req *page.Request) (*page.Page, string) {
	var err error
	var httpResp *http.Response
	var urlStr string
	var method string
	urlStr = req.GetUrl()
	if len(urlStr) == 0 {
		logs.GetFirstLogger().Error("url is empty")
		p.SetStatus(true, "url is empty")
		return p, ""
	}

	method = req.GetMethod()

	if method == "POST" {
		httpResp, err = http.Post(req.GetUrl(), "application/x-www-form-urlencoded", strings.NewReader(req.GetPostData()))
	} else {
		httpResp, err = http.Get(req.GetUrl())
	}

	if err != nil {
		logs.GetFirstLogger().Error("http visit error :" + err.Error())
		p.SetStatus(true, err.Error())
	}
	p.SetHeader(httpResp.Header)
	p.SetCookies(httpResp.Cookies())
	body, _ := ioutil.ReadAll(httpResp.Body)
	bodyStr := string(body)
	defer httpResp.Body.Close()
	return p, bodyStr
}
Exemple #11
0
func getSessionAndAppPort(url string, rPort uint16) (*http.Cookie, *http.Cookie, string) {
	var client http.Client
	var req *http.Request
	var resp *http.Response
	var err error
	var port []byte

	uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)

	req, err = http.NewRequest("GET", uri, nil)
	Expect(err).ToNot(HaveOccurred())

	resp, err = client.Do(req)
	Expect(err).ToNot(HaveOccurred())

	port, err = ioutil.ReadAll(resp.Body)
	Expect(err).ToNot(HaveOccurred())

	var sessionCookie, vcapCookie *http.Cookie
	for _, cookie := range resp.Cookies() {
		if cookie.Name == proxy.StickyCookieKey {
			sessionCookie = cookie
		} else if cookie.Name == proxy.VcapCookieId {
			vcapCookie = cookie
		}
	}

	return sessionCookie, vcapCookie, string(port)
}
Exemple #12
0
func generate(credentials string, url string, attempts int) (bool, []int) {
	transport := &http.Transport{}
	var response *http.Response

	//log.Printf("hitting %s", url)

	// first try real string without trailing bytes to test a win
	req := request(url, credentials)

	response, _ = transport.RoundTrip(req)

	if response.StatusCode == 200 {
		log.Printf("body %v", response.Body)
		//		return true, nil
	}

	cookies := response.Cookies()

	response.Body.Close()

	req = request(url, credentials+"    ")
	timing := make([]int, 0)

	for _, i := range cookies {
		req.AddCookie(i)
	}

	for true {
		before := time.Now()
		response, err := transport.RoundTrip(req)
		duration := time.Since(before)

		_ = duration

		if err != nil {
			log.Printf("Error %v", err)
			continue
		}

		if response.StatusCode == 200 {
			return true, nil
		}

		response.Body.Close()

		headertime, _ := strconv.ParseFloat(response.Header.Get("X-Runtime"), 64)
		_ = headertime

		//	log.Printf("X-Runtime = %v, %v, %v", response.Header.Get("X-Runtime"), headertime, int64(headertime*1000000))

		timing = append(timing, int(duration.Nanoseconds()))
		if len(timing) >= attempts {
			break
		}
		//sleep(200)
	}

	return false, timing
}
Exemple #13
0
// Gets a cookie with the specified name from the Response
// Returns nil on not finding a suitable cookie
func getRespCookie(resp *http.Response, name string) *http.Cookie {
	for _, c := range resp.Cookies() {
		if c.Name == name {
			return c
		}
	}
	return nil
}
Exemple #14
0
// refresh updates the cookie and expiration used to sign requests from a successful session
// creation API response.
func (s *cookieSigner) refresh(resp *http.Response) error {
	if resp.StatusCode != 204 {
		return fmt.Errorf("Authentication failed: %s", resp.Status)
	}
	s.cookies = resp.Cookies()
	s.refreshAt = time.Now().Add(time.Duration(2) * time.Hour)
	return nil
}
Exemple #15
0
func findResponseCookie(response *http.Response, name string) string {
	for _, cookie := range response.Cookies() {
		if cookie.Name == name {
			return cookie.Value
		}
	}
	return ""
}
Exemple #16
0
// SceneID takes a response of a HTTP request and returns
// a scene ID it received as cookie. It can be used to establish
// session when doing client calls.
func SceneID(resp *http.Response) (string, bool) {
	cookies := resp.Cookies()
	for _, cookie := range cookies {
		if cookie.Name == sceneID {
			return cookie.Value, true
		}
	}
	return "", false
}
Exemple #17
0
func getCookie(name string, resp *http.Response) *http.Cookie {
	for _, cookie := range resp.Cookies() {
		if cookie.Name == name {
			return cookie
		}
	}

	return nil
}
Exemple #18
0
func (c *Cache) checkFoomoSessionCookie(res *http.Response) {
	sessionCookie := getCookieByName(res.Cookies(), c.foomoSessionCookieName)
	if sessionCookie != nil {
		if c.foomoSessionCookie == nil || (c.foomoSessionCookie != nil && c.foomoSessionCookie.Value != sessionCookie.Value) {
			log.Println("images.CheckFoomoSessionCookie: we have a session cookie", sessionCookie)
			c.foomoSessionCookie = sessionCookie
		}
	}
}
Exemple #19
0
func NewEngine(apple_id, password string) (engine *ICloudEngine, e error) {
	engine = new(ICloudEngine)

	engine.ClientID = ClientID //strings.ToUpper(util.GenUuid())

	cookieJar, _ := cookiejar.New(nil)

	client := &http.Client{
		Jar: cookieJar,
	}

	var version *ICloudVersion
	if version, e = getICloudVersion(client); e != nil {
		return nil, e
	}

	engine.ReportedVersion = version
	info := map[string]string{
		"apple_id":       apple_id,
		"password":       password,
		"extended_login": "******",
	}

	data, _ := json.Marshal(info)

	var req *http.Request
	if req, e = http.NewRequest("POST", loginURL, bytes.NewReader(data)); e != nil {
		return nil, e
	}

	v := url.Values{}
	v.Add("clientBuildNumber", version.BuildNumber)
	v.Add("clientID", engine.ClientID)
	req.URL.RawQuery = v.Encode()
	req.Header.Set("Content-Type", "text/plain")
	req.Header.Add("Origin", "https://www.icloud.com")

	var resp *http.Response
	if resp, e = client.Do(req); e != nil {
		return nil, e
	}

	defer resp.Body.Close()

	var body []byte
	if body, e = ioutil.ReadAll(resp.Body); e != nil {
		return nil, e
	}

	engine.Cookiez = resp.Cookies()

	json.Unmarshal(body, engine)
	engine.Client = client
	return engine, nil
}
Exemple #20
0
func (this *Ajax) saveResponseCookie(response *http.Response) error {
	if this.ResponseCookie == nil {
		return nil
	}
	dataCookie, ok1 := this.ResponseCookie.(*[]*http.Cookie)
	if ok1 {
		*dataCookie = response.Cookies()
	} else {
		return errors.New("invalid response cookie type " + fmt.Sprintf("%v", this.ResponseCookie))
	}
	return nil
}
Exemple #21
0
func GetResponseCookies(response *http.Response) (user_id string, token_id string) {
	cookies := response.Cookies()
	for i := range cookies {
		cookie := cookies[i]
		if cookie.Name == TOKEN_COOKIE_FIELD_NAME {
			token_id = cookie.Value
		}
		if cookie.Name == USERID_COOKIE_FIELD_NAME {
			user_id = cookie.Value
		}
	}
	return
}
Exemple #22
0
// RecordResponse logs an HTTP response, associating it with the previously-logged
// HTTP request with the same ID.
func (l *Logger) RecordResponse(id string, res *http.Response) error {
	hres := &Response{
		HTTPVersion: res.Proto,
		Status:      res.StatusCode,
		StatusText:  http.StatusText(res.StatusCode),
		HeadersSize: -1,
		BodySize:    res.ContentLength,
		Headers:     headers(proxyutil.ResponseHeader(res).Map()),
		Cookies:     cookies(res.Cookies()),
	}

	if res.StatusCode >= 300 && res.StatusCode < 400 {
		hres.RedirectURL = res.Header.Get("Location")
	}

	hres.Content = &Content{
		Encoding: "base64",
		MimeType: res.Header.Get("Content-Type"),
	}

	if l.bodyLogging(res) {
		mv := messageview.New()
		if err := mv.SnapshotResponse(res); err != nil {
			return err
		}

		br, err := mv.BodyReader(messageview.Decode())
		if err != nil {
			return err
		}

		body, err := ioutil.ReadAll(br)
		if err != nil {
			return err
		}

		hres.Content.Text = body
		hres.Content.Size = int64(len(body))
	}

	l.mu.Lock()
	defer l.mu.Unlock()

	if e, ok := l.entries[id]; ok {
		e.Response = hres
		e.Time = time.Since(e.StartedDateTime).Nanoseconds() / 1000000
	}

	return nil
}
func (this *ClientWxLoginAoModel) callInterface(method string, url string, urlInfo url.Values, result interface{}) {
	//提取request的cookie
	requestCookies := this.Ctx.Request.Cookies()
	//请求url
	httpClient := &http.Client{}
	var request *http.Request
	var resp *http.Response
	var err error
	url = "http://" + officalHost + url
	if method == "get" {
		request, err = http.NewRequest("GET", url+"?"+urlInfo.Encode(), nil)
		if err != nil {
			panic(err)
		}
	} else {
		request, err = http.NewRequest("POST", url, bytes.NewReader([]byte(urlInfo.Encode())))
		request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
		if err != nil {
			panic(err)
		}
	}
	for _, value := range requestCookies {
		request.AddCookie(value)
	}
	resp, err = httpClient.Do(request)

	if err != nil {
		Throw(1, fmt.Sprintf("调用金象官方系统接口失败:[%s],url为:[%s],参数为:[%v]", err.Error(), url, urlInfo))
	}
	defer resp.Body.Close()
	respString, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		Throw(1, "获取金象官方系统接口的body失败")
	}
	if resp.StatusCode != 200 {
		Throw(1, fmt.Sprintf("调用金象官方系统接口,状态码为:[%s],url为:[%s],参数为:[%v]", resp.Status, url, urlInfo))
	}
	err = json.Unmarshal(respString, result)
	if err != nil {
		Throw(1, "获取金象官方系统接口的json解析失败:"+string(respString))
	}

	//写入cookie
	responseCookies := resp.Cookies()
	for _, value := range responseCookies {
		http.SetCookie(this.Ctx.ResponseWriter, value)
		this.Ctx.Request.AddCookie(value)
	}
}
Exemple #24
0
func setupStickySession(responseWriter http.ResponseWriter, response *http.Response, endpoint *route.Endpoint) {
	for _, v := range response.Cookies() {
		if v.Name == StickyCookieKey {
			cookie := &http.Cookie{
				Name:  VcapCookieId,
				Value: endpoint.PrivateInstanceId,
				Path:  "/",

				HttpOnly: true,
			}

			http.SetCookie(responseWriter, cookie)
			return
		}
	}
}
Exemple #25
0
func makeResponse(chain chain, response *http.Response, duration time.Duration) *Response {
	var content []byte
	var cookies []*http.Cookie
	if response != nil {
		content = getContent(&chain, response)
		cookies = response.Cookies()
	} else {
		chain.fail("expected non-nil response")
	}
	return &Response{
		chain:   chain,
		resp:    response,
		content: content,
		cookies: cookies,
		time:    duration,
	}
}
Exemple #26
0
func (s *Session) processOutputHeader(xReq *PxReq, resp *http.Response, w http.ResponseWriter) (err error) {
	defer func() {
		if e := recover(); e != nil {
			err = e.(error)
		}
	}()
	wHeader := w.Header()
	for k, array := range resp.Header {
		switch k {
		case "Set-Cookie":
			continue
		case "Location":
			targetUrl := array[0]
			nextUrl := s.processRedirect(targetUrl)
			if log.V(1) {
				log.Infof("Cook redirection %s -> %s", targetUrl, nextUrl)
			}
			wHeader.Set(k, nextUrl)
		default:
			// Alt-Svc, Alternate-Protocol
			if strings.HasPrefix(k, "Alt") {
				continue
			}
			for _, v := range array {
				wHeader.Add(k, v)
			}
		}
	}
	wHeader.Set("Server", "ezgoo")
	var alterCookiePath = xReq.nondefault == 0xf
	for _, ck := range resp.Cookies() {
		if alterCookiePath {
			if ck.Domain == NULL || strings.HasPrefix(ck.Domain, ".") {
				// prevent ck.path==/!.some-host
				ck.Path = fmt.Sprintf("/!%s%s", xReq.url.Host, ck.Path)
			} else {
				ck.Path = fmt.Sprintf("/!%s%s", ck.Domain, ck.Path)
			}
		}
		if v := cookieString(ck, &s.plainHost, true); v != NULL {
			wHeader.Add("Set-Cookie", v)
		}
	}
	return
}
// LoadPage sends HTTP requests and does some minimal parsing of the response. At this point
// this means we extract the cookies. Only handles GET and POST requests as of now.
func (bot *EvernoteAuthBot) LoadPage(uri string, method string, params url.Values) (*http.Response, error) {
	var req *http.Request
	var resp *http.Response
	var err error

	/*log.Printf("LoadPage: uri = %s, method = %s, values = %v", uri, method, params)*/

	client := &http.Client{
		CheckRedirect: redirectPolicy,
	}
	if method == "GET" {
		req, err = http.NewRequest(method, uri+"?"+params.Encode(), nil)
	} else if method == "POST" {
		req, err = http.NewRequest(method, uri, bytes.NewBufferString(params.Encode()))
		req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
		req.Header.Add("Content-Length", strconv.Itoa(len(params.Encode())))
		// TODO: might need to set the content-length here
	}

	if err != nil {
		log.Fatalf("Error trying to set up request: %v", err)
	}

	// Cookies baby, cookies
	for _, cookie := range bot.cookies {
		req.AddCookie(cookie)
	}

	resp, err = client.Do(req)
	/*log.Printf("request: %v\n", req)*/

	if err != nil {
		if e, yes := isBotError(err); yes && e.IsRedirect {
			err = nil
		}
	}

	if err == nil {
		bot.cookies = resp.Cookies()
	}

	return resp, err
}
Exemple #28
0
func setPrefDom(xReq *PxReq, w http.ResponseWriter, host string) {
	var req *http.Request
	var resp *http.Response
	var body string

	// remove client old cookies
	xReq.header.Del("Cookie")
	xReq.header.Del("Accept-Encoding")

	baseUrl := default_protocol + default_host
	ncrUrl := baseUrl + "/?gfe_rd=cr&gws_rd=cr"
	req, _ = http.NewRequest("GET", ncrUrl, nil)
	req.Header = xReq.header // use client header
	resp, body = httpCallEx(req, false)

	matches := reSig.FindStringSubmatch(body)
	if matches == nil {
		log.Infoln(body)
		panic("sig not found")
	}

	sig := matches[0]
	setprefUrl := fmt.Sprintf("%s/setprefdomain?prefdom=US&prev=%s&%s", baseUrl, url.QueryEscape(ncrUrl), sig)
	req, _ = http.NewRequest("GET", setprefUrl, nil)
	req.Header = xReq.header

	nid, nidFound := extractNID(resp.Cookies())
	if nidFound {
		req.Header.Set("Cookie", cookieString(nid, nil, false))
	} // else panic ?
	resp, _ = httpCallEx(req, true)

	nid, nidFound = extractNID(resp.Cookies())
	if !nidFound {
		dumpHeader(fmt.Sprintf("resp[%s]->%s", resp.Status, req.URL), resp.Header)
		panic("nid not found")
	} else {
		nid.HttpOnly = true
		w.Header().Set("Set-Cookie", cookieString(nid, &host, true))
	}
	w.Header().Set("Location", "/")
	w.WriteHeader(302)
}
Exemple #29
0
func (h *RequestHandler) setupStickySession(endpointResponse *http.Response, endpoint *route.Endpoint) {
	needSticky := false
	for _, v := range endpointResponse.Cookies() {
		if v.Name == StickyCookieKey {
			needSticky = true
			break
		}
	}

	if needSticky && endpoint.PrivateInstanceId != "" {
		cookie := &http.Cookie{
			Name:  VcapCookieId,
			Value: endpoint.PrivateInstanceId,
			Path:  "/",
		}

		http.SetCookie(h.response, cookie)
	}
}
Exemple #30
0
func (c *Cli) saveCookies(resp *http.Response) {
	if _, ok := resp.Header["Set-Cookie"]; !ok {
		return
	}

	cookies := resp.Cookies()
	for _, cookie := range cookies {
		if cookie.Domain == "" {
			// if it is host:port then we need to split off port
			parts := strings.Split(resp.Request.URL.Host, ":")
			host := parts[0]
			log.Debugf("Setting DOMAIN to %s for Cookie: %s", host, cookie)
			cookie.Domain = host
		}
	}

	// expiry in one week from now
	expiry := time.Now().Add(24 * 7 * time.Hour)
	for _, cookie := range cookies {
		cookie.Expires = expiry
	}

	if currentCookies := c.loadCookies(); currentCookies != nil {
		currentCookiesByName := make(map[string]*http.Cookie)
		for _, cookie := range currentCookies {
			currentCookiesByName[cookie.Name+cookie.Domain] = cookie
		}

		for _, cookie := range cookies {
			currentCookiesByName[cookie.Name+cookie.Domain] = cookie
		}

		mergedCookies := make([]*http.Cookie, 0, len(currentCookiesByName))
		for _, v := range currentCookiesByName {
			mergedCookies = append(mergedCookies, v)
		}
		jsonWrite(c.cookieFile, mergedCookies)
	} else {
		mkdir(path.Dir(c.cookieFile))
		jsonWrite(c.cookieFile, cookies)
	}
}