コード例 #1
0
ファイル: session.go プロジェクト: AdamOssenford/docker-pi
// 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
}
コード例 #2
0
ファイル: request.go プロジェクト: disunbow/request
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{},
	}
}
コード例 #3
0
ファイル: main.go プロジェクト: kumatch/gohakai
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)
	}
}
コード例 #4
0
ファイル: engine.go プロジェクト: tgreiser/icloud
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
}
コード例 #5
0
ファイル: restUtil.go プロジェクト: dorsha/go2oo
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
}
コード例 #6
0
ファイル: main.go プロジェクト: briantoth/rest
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
}
コード例 #7
0
ファイル: http_client.go プロジェクト: henrypfhu/hprose-go
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
}
コード例 #8
0
ファイル: rest.go プロジェクト: 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
}
コード例 #9
0
ファイル: client.go プロジェクト: jpfielding/gorets
// 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
}
コード例 #10
0
ファイル: request.go プロジェクト: jmptrader/request
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{},
	}
}
コード例 #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
}
コード例 #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
}
コード例 #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)
}
コード例 #14
0
func setHTTPClient(c *http.Client) {
	c.Jar = mustInitCookieJar()
	c.CheckRedirect = checkRedirect
	client = c
}