예제 #1
0
파일: transport.go 프로젝트: trobert3/winrm
func (conf *SoapRequest) HttpCertAuth(data []byte) (*http.Response, error) {
	protocol := strings.Split(conf.Endpoint, ":")
	if protocol[0] != "http" && protocol[0] != "https" {
		return nil, errors.New("Invalid protocol. Expected http or https")
	}
	header := conf.GetHttpHeader()
	header["Authorization"] = "http://schemas.dmtf.org/wbem/wsman/1/wsman/secprofile/https/mutual"

	if conf.HttpClient == nil {
		conf.HttpClient = &http.Client{}
	}

	if protocol[0] != "https" {
		return nil, errors.New("Ivalid protocol for this transport type")
	}

	cert, err := tls.LoadX509KeyPair(conf.CertAuth.Cert, conf.CertAuth.Key)
	if err != nil {
		return nil, err
	}

	tlsConfig := &tls.Config{
		InsecureSkipVerify: true,
		Certificates: []tls.Certificate{
			cert,
		},
	}

	tr := &http.Transport{
		TLSClientConfig: tlsConfig,
	}
	conf.HttpClient.Transport = tr
	body := bytes.NewBuffer(data)
	req, err := http.NewRequest("POST", conf.Endpoint, body)
	req.ContentLength = int64(len(data))
	for k, v := range header {
		req.Header.Add(k, v)
	}
	resp, err := conf.HttpClient.Do(req)
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != 200 {
		return nil, errors.New(fmt.Sprintf("Remote host returned error status code: %d", resp.StatusCode))
	}
	//fmt.Printf("%v\n%v\n", resp, err)
	return resp, err
}
예제 #2
0
파일: transport.go 프로젝트: trobert3/winrm
func (conf *SoapRequest) HttpBasicAuth(data []byte) (*http.Response, error) {
	protocol := strings.Split(conf.Endpoint, ":")
	if protocol[0] != "http" && protocol[0] != "https" {
		return nil, errors.New("Invalid protocol. Expected http or https")
	}

	header := conf.GetHttpHeader()

	if conf.HttpClient == nil {
		conf.HttpClient = &http.Client{}
	}
	// Ignore SSL certificate errors
	if protocol[0] == "https" {
		tr := &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: conf.HttpInsecure},
		}
		conf.HttpClient.Transport = tr
	}
	body := bytes.NewBuffer(data)
	req, err := http.NewRequest("POST", conf.Endpoint, body)
	req.ContentLength = int64(len(data))
	req.SetBasicAuth(conf.Username, conf.Passwd)

	for k, v := range header {
		req.Header.Add(k, v)
	}

	resp, err := conf.HttpClient.Do(req)
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != 200 {
		return nil, errors.New(fmt.Sprintf("Remote host returned error status code: %d", resp.StatusCode))
	}
	return resp, err
}