Exemplo n.º 1
0
// TODO(tiborvass): remove authConfig param once registry client v2 is vendored
func NewSession(client *http.Client, authConfig *cliconfig.AuthConfig, endpoint *Endpoint) (r *Session, err error) {
	r = &Session{
		authConfig:    authConfig,
		client:        client,
		indexEndpoint: endpoint,
	}

	var alwaysSetBasicAuth bool

	// If we're working with a standalone private registry over HTTPS, send Basic Auth headers
	// alongside all our requests.
	if endpoint.VersionString(1) != IndexServerAddress() && endpoint.URL.Scheme == "https" {
		info, err := endpoint.Ping()
		if err != nil {
			return nil, err
		}

		if info.Standalone && authConfig != nil {
			logrus.Debugf("Endpoint %s is eligible for private registry. Enabling decorator.", endpoint.String())
			alwaysSetBasicAuth = true
		}
	}

	// Annotate the transport unconditionally so that v2 can
	// properly fallback on v1 when an image is not found.
	client.Transport = AuthTransport(client.Transport, authConfig, alwaysSetBasicAuth)

	jar, err := cookiejar.New(nil)
	if err != nil {
		return nil, errors.New("cookiejar.New is not supposed to return an error")
	}
	client.Jar = jar

	return r, nil
}
Exemplo n.º 2
0
func NewArgs(c *http.Client) *Args {
	if c.Jar == nil {
		options := cookiejar.Options{
			PublicSuffixList: publicsuffix.List,
		}
		jar, _ := cookiejar.New(&options)
		c.Jar = jar
	}
	headers := map[string]string{}
	for k, v := range DefaultHeaders {
		headers[k] = v
	}

	return &Args{
		Client:    c,
		Headers:   headers,
		Cookies:   nil,
		Data:      nil,
		Params:    nil,
		Files:     nil,
		Json:      nil,
		Proxy:     "",
		BasicAuth: BasicAuth{},
	}
}
Exemplo n.º 3
0
func hakai(c http.Client, config *Config, offset map[string]int) {
	u, err := url.Parse(config.Domain)
	if err != nil {
		log.Fatal(err)
	}

	queryParams := map[string]string{}
	for k, v := range config.QueryParams {
		vv := ReplaceNames(v, offset)
		queryParams[k] = vv
	}

	cookieJar, _ := cookiejar.New(nil)
	c.Jar = cookieJar
	attacker := Attacker{
		Client:      &c,
		Url:         u,
		Gzip:        config.Gzip,
		UserAgent:   config.UserAgent,
		QueryParams: &queryParams,
		ExVarOffset: offset,
	}
	for offset, action := range config.Actions {
		attacker.Action = action
		attacker.Attack(offset)
	}
}
Exemplo n.º 4
0
func NewEngine(client *http.Client, apple_id, password string) (engine *ICloudEngine, e error) {

	cookieJar, err := cookiejar.New(nil)
	if err != nil {
		log.Fatal(err)
	}
	client.Jar = cookieJar

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

	engine = new(ICloudEngine)
	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", ClientID)
	req.URL.RawQuery = v.Encode()
	req.Header.Set("Content-Type", "text/plain")
	req.Header.Add("Origin", "https://www.icloud.com")
	req.Header.Add("Referer", "https://www.icloud.com/")
	req.Header.Set("User-Agent", "Opera/9.52 (X11; Linux i686; U; en)")

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

	defer resp.Body.Close()
	if resp.StatusCode == 421 {
		return nil, ErrFailedLogin
	}

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

	json.Unmarshal(body, engine)
	engine.Client = client
	engine.ClientID = ClientID
	return engine, nil
}
Exemplo n.º 5
0
func getCsrfToken(client *http.Client, url string) string {
	// CSRF support
	jar := &cookieJar{}
	jar.jar = make(map[string][]*http.Cookie)
	client.Jar = jar
	headResponse, err := client.Head(url)
	checkError(err)
	token := headResponse.Header.Get(csrfTokenHeader)
	return token
}
Exemplo n.º 6
0
func (self *Client) do(req *http.Request) (*http.Response, error) {
	var client http.Client
	if self.TlsTransport != nil {
		client = http.Client{
			Transport: self.TlsTransport,
		}
	} else {
		client = http.Client{}
	}

	// Adding cookie jar
	if self.CookieJar != nil {
		client.Jar = self.CookieJar
	}

	// Copying headers
	for k := range self.Header {
		req.Header.Set(k, self.Header.Get(k))
	}

	if req.Body == nil {
		req.Header.Del("Content-Type")
		req.Header.Del("Content-Length")
	}

	res, err := client.Do(req)

	if debugLevelEnabled(debugLevelVerbose) {

		log.Printf("Fetching %v\n", req.URL.String())

		log.Printf("> %s %s", req.Method, req.Proto)
		for k := range req.Header {
			for kk := range req.Header[k] {
				log.Printf("> %s: %s", k, req.Header[k][kk])
			}
		}

		log.Printf("< %s %s", res.Proto, res.Status)
		for k := range res.Header {
			for kk := range res.Header[k] {
				log.Printf("< %s: %s", k, res.Header[k][kk])
			}
		}

		log.Printf("\n")
	}

	return res, err
}
Exemplo n.º 7
0
func newHttpTransporter() (trans *httpTransporter) {
	tr := new(http.Transport)
	tr.DisableCompression = true
	tr.DisableKeepAlives = false
	tr.MaxIdleConnsPerHost = 4
	jar := cookieJar
	if DisableGlobalCookie {
		jar, _ = cookiejar.New(nil)
	}
	client := new(http.Client)
	client.Jar = jar
	client.Transport = tr
	trans = new(httpTransporter)
	trans.Client = client
	trans.Header = new(http.Header)
	return
}
Exemplo n.º 8
0
Arquivo: rest.go Projeto: hiphoox/goog
func (self *Client) do(req *http.Request) (*http.Response, error) {
	client := new(http.Client)

	// Adding cookie jar
	if self.CookieJar != nil {
		client.Jar = self.CookieJar
	}

	// Copying headers
	for k := range self.Header {
		req.Header.Set(k, self.Header.Get(k))
	}

	if req.Body == nil {
		req.Header.Del("Content-Type")
		req.Header.Del("Content-Length")
	}

	res, err := client.Do(req)

	if enableDebug == true {

		log.Printf("Fetching %v\n", req.URL.String())

		log.Printf("> %s %s", req.Method, req.Proto)
		for k := range req.Header {
			for kk := range req.Header[k] {
				log.Printf("> %s: %s", k, req.Header[k][kk])
			}
		}

		log.Printf("< %s %s", res.Proto, res.Status)
		for k := range res.Header {
			for kk := range res.Header[k] {
				log.Printf("< %s: %s", k, res.Header[k][kk])
			}
		}

		log.Printf("\n")
	}

	return res, err
}
Exemplo n.º 9
0
// DefaultSession configures the default rets session
func DefaultSession(user, pwd, userAgent, userAgentPw, retsVersion string, transport http.RoundTripper) (Requester, error) {
	if transport == nil {
		transport = wirelog.NewHTTPTransport()
	}

	client := http.Client{
		Transport: transport,
	}

	jar, err := cookiejar.New(nil)
	if err != nil {
		return nil, err
	}
	client.Jar = jar
	// 4) send the request
	request := func(ctx context.Context, req *http.Request) (*http.Response, error) {
		return ctxhttp.Do(ctx, &client, req)
	}
	// 3) www auth
	wwwAuth := (&WWWAuthTransport{
		Requester: request,
		Username:  user,
		Password:  pwd,
	}).Request
	// 2) apply ua auth headers per request, if there is a pwd
	uaAuth := (&UserAgentAuthentication{
		Requester:         wwwAuth,
		UserAgent:         userAgent,
		UserAgentPassword: userAgentPw,
		GetRETSVersion:    CreateRETSVersioner(retsVersion),
		GetSessionID:      CreateSessionIDer(client.Jar),
	}).Request
	// 1) apply default headers first (outermost wrapping)
	headers := func(ctx context.Context, req *http.Request) (*http.Response, error) {
		req.Header.Set(UserAgent, userAgent)
		req.Header.Set(RETSVersion, retsVersion)
		req.Header.Set(Accept, "*/*")
		return uaAuth(ctx, req)
	}
	return headers, nil
}
Exemplo n.º 10
0
func NewArgs(c *http.Client) *Args {
	if c.Jar == nil {
		options := cookiejar.Options{
			PublicSuffixList: publicsuffix.List,
		}
		jar, _ := cookiejar.New(&options)
		c.Jar = jar
	}

	return &Args{
		Client:    c,
		Headers:   defaultHeaders,
		Cookies:   nil,
		Data:      nil,
		Params:    nil,
		Files:     nil,
		Json:      nil,
		Proxy:     "",
		BasicAuth: BasicAuth{},
	}
}
Exemplo n.º 11
0
func doLoginRequest(client *http.Client, host, token string) (string, error) {
	endpoint := host + "/-/loginwithtoken?token=" + token

	jar, err := cookiejar.New(nil)
	if err != nil {
		// cookiejar does not return any error, here for future compatibility
		panic(err)
	}
	client.Jar = jar

	req, err := http.NewRequest("GET", endpoint, nil)
	if err != nil {
		return "", fmt.Errorf("Error sending request. err: %s", err)
	}

	// store the cookie values upon redirects
	resp, err := client.Do(req)
	if err != nil {
		return "", fmt.Errorf("Error sending request. err: %s", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode > 299 {
		return "", fmt.Errorf("Invalid response code from server. code: %d", resp.StatusCode)
	}

	urll, err := url.Parse(endpoint)
	if err != nil {
		// this should not happen since we already sent a request to it previously
		panic(err)
	}

	clientID := getClientID(jar.Cookies(urll))

	return clientID, nil
}
Exemplo n.º 12
0
func (chatter *Chatter) _Init() error {
	transport := &http.Transport{
		MaxIdleConnsPerHost: 30,
		Proxy:               http.ProxyFromEnvironment,
		Dial: (&net.Dialer{
			Timeout:   30 * time.Second,
			KeepAlive: time.Minute,
		}).Dial,
		TLSHandshakeTimeout: 10 * time.Second,
	}
	httpClient := new(http.Client)
	chatter.httpClient = httpClient
	// NOTE: only livehouse.in using http currently, dont care so far
	options := cookiejar.Options{PublicSuffixList: publicsuffix.List}
	jar, err := cookiejar.New(&options)
	if err != nil {
		log.Fatal(err)
		return errors.New("Cant create new cookieJar")
	}
	httpClient.Transport = transport
	httpClient.Jar = jar
	chatter.LiveUrl = fmt.Sprintf("https://livehouse.in/channel/%s", chatter.ChannelId)
	return nil
}
Exemplo n.º 13
0
func WebRequest(queryUrl, method string, params *url.Values, cookies *cookiejar.Jar, headers *map[string]string, referer *string) ([]byte, error) {
	if referer == nil {
		aux := UrlCommunityBase
		referer = &aux
	}

	client := new(http.Client)

	// Create request
	var req *http.Request
	var err error
	switch method {
	case "GET":
		if params != nil {
			if strings.Contains(queryUrl, "?") {
				queryUrl = queryUrl + "&"
			} else {
				queryUrl = queryUrl + "?"
			}
			queryUrl = queryUrl + params.Encode()
		}
		req, err = http.NewRequest(method, queryUrl, nil)
		if err != nil {
			panic("failed to create http request")
		}
	case "POST":
		if params == nil {
			params = &url.Values{}
		}
		req, err = http.NewRequest(method, queryUrl, bytes.NewBufferString(params.Encode()))
		if err != nil {
			panic("failed to create http request")
		}
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
	default:
		panic("Only POST and GET requests supported")
	}

	// Set request header params
	req.Header.Set("Accept", "text/javascript, text/html, application/xml, text/xml, */*")
	req.Header.Set("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Google Nexus 4 - 4.1.1 - API 16 - 768x1280 Build/JRO03S) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30")
	req.Header.Set("Referer", *referer)

	if headers != nil {
		for key, val := range *headers {
			req.Header.Add(key, val)
		}
	}

	// Set cookies
	if cookies != nil {
		client.Jar = cookies
	}

	// Make request
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("request failed with status code: %v", resp.StatusCode)
	}
	defer resp.Body.Close()

	return ioutil.ReadAll(resp.Body)
}
Exemplo n.º 14
0
func setHTTPClient(c *http.Client) {
	c.Jar = mustInitCookieJar()
	c.CheckRedirect = checkRedirect
	client = c
}