Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to point the specific ip and port for client? #10

Closed
toranger opened this issue Dec 14, 2018 · 4 comments
Closed

Is there any way to point the specific ip and port for client? #10

toranger opened this issue Dec 14, 2018 · 4 comments
Labels

Comments

@toranger
Copy link

No description provided.

@mozillazg
Copy link
Owner

mozillazg commented Dec 14, 2018

@toranger Sorry, but I don't understand your problem, could you explain it to me?

P.S. If you mean how to hack the COS backend address which is resolved via dns, you can hack it via setup AuthorizationTransport.Transport with your version of http.RoundTripper(e.g. a modified copy of http.DefaultTransport) .

ref: https://golang.org/pkg/net/http/#RoundTripper

@toranger
Copy link
Author

toranger commented Jan 2, 2019

@toranger Sorry, but I don't understand your problem, could you explain it to me?

P.S. If you mean how to hack the COS backend address which is resolved via dns, you can hack it via setup AuthorizationTransport.Transport with your version of http.RoundTripper(e.g. a modified copy of http.DefaultTransport) .

ref: https://golang.org/pkg/net/http/#RoundTripper

To use the port and ip instead of using the bucket host

@mozillazg
Copy link
Owner

mozillazg commented Jan 2, 2019

@toranger You can do it via setup AuthorizationTransport.Transport with your version of http.RoundTripper.

For example, one way is via change host of request url:

// demo.go
package main

import (
	"context"
	"net/http"

	"github.com/mozillazg/go-cos"
)

type DemoTransport struct {
	RemoteAddr string
	Transport  http.RoundTripper
}

func (t *DemoTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	host := req.Host
	if host == "" {
		host = req.URL.Host
	}
	req.URL.Host = t.RemoteAddr
	req.Host = host

	resp, err := t.transport().RoundTrip(req)
	return resp, err
}

func (t *DemoTransport) transport() http.RoundTripper {
	if t.Transport != nil {
		return t.Transport
	}
	return http.DefaultTransport
}

func main() {
	b, _ := cos.NewBaseURL("http://test-1253846586.cos.ap-beijing-1.myqcloud.com")
	c := cos.NewClient(b, &http.Client{
		Transport: &cos.AuthorizationTransport{
			SecretID:  "test",
			SecretKey: "test",
			Transport: &DemoTransport{
				RemoteAddr: "127.0.0.1:8080",
			},
		},
	})

	c.Bucket.Get(context.Background(), nil)
}

Start a demo server on another window:

$ echo -e 'HTTP/1.1 200 OK\r\n' | nc -l 8080

Test:

$ go run demo.go

Local demo server will receive the request:

$ echo -e 'HTTP/1.1 200 OK\r\n' | nc -l 8080
GET / HTTP/1.1
Host: test-1253846586.cos.ap-beijing-1.myqcloud.com
User-Agent: go-cos/0.11.0
Authorization: q-sign-algorithm=sha1&q-ak=test&q-sign-time=1546436220;1546439820&q-key-time=1546436220;1546439820&q-header-list=&q-url-param-list=&q-signature=749f551057b64672c6123c80be6babc699bd8030
Content-Type: application/xml
Accept-Encoding: gzip

Another way is via hack dns resolver or connection creator:

@mozillazg
Copy link
Owner

mozillazg commented Jan 3, 2019

Or just use <ip>:<port> instead of bucket host as host of bucket url ? go-cos not checking the format of bucket url in case of user may use it in private cloud.

b, _ := cos.NewBaseURL("http://127.0.0.1:8080")
c := cos.NewClient(b, &http.Client{
	Transport: &cos.AuthorizationTransport{
		SecretID:  "test",
		SecretKey: "test",
	},
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants