本文整理汇总了Golang中golang.org/x/net/proxy.FromURL函数的典型用法代码示例。如果您正苦于以下问题:Golang FromURL函数的具体用法?Golang FromURL怎么用?Golang FromURL使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FromURL函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Create
// Create a new socks proxy
func (sprox Proxy) Create() (*http.Client, error) {
if AcceptNoSocks && string(sprox) == "" {
return &http.Client{}, nil
} else if string(sprox) == "" {
return nil, ErrNoProxy
}
socksURL, err := urlPackage.Parse(string(sprox))
if err != nil {
return nil, err
}
if socksURL.Scheme == "" {
socksURL, err = urlPackage.Parse("socks5://" + string(sprox))
if err != nil {
return nil, err
}
}
dialer, err := proxy.FromURL(socksURL, proxy.Direct)
if err != nil {
return nil, err
}
tr := &http.Transport{
//Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
}
client := &http.Client{
Transport: tr,
Timeout: time.Second * time.Duration(Timeout), // 45 second timeout is pretty nice
}
return client, nil
}
示例2: DialerFromEnvironment
// DialerFromEnvironment takes in a "direct" *net.Dialer and returns a
// proxy.Dialer which will route the connections through the proxy using the
// given dialer.
func DialerFromEnvironment(direct *net.Dialer) (proxy.Dialer, error) {
allProxy := GetProxyEnv("all_proxy")
if len(allProxy) == 0 {
return direct, nil
}
proxyURL, err := url.Parse(allProxy)
if err != nil {
return direct, err
}
proxyFromURL, err := proxy.FromURL(proxyURL, direct)
if err != nil {
return direct, err
}
noProxy := GetProxyEnv("no_proxy")
if len(noProxy) == 0 {
return proxyFromURL, nil
}
perHost := proxy.NewPerHost(proxyFromURL, direct)
perHost.AddFromString(noProxy)
return perHost, nil
}
示例3: Proxy
// Proxy function accepts a proxy url string to setup proxy url for any request.
// It provides a convenience way to setup proxy which have advantages over usual old ways.
// One example is you might try to set `http_proxy` environment. This means you are setting proxy up for all the requests.
// You will not be able to send different request with different proxy unless you change your `http_proxy` environment again.
// Another example is using Golang proxy setting. This is normal prefer way to do but too verbase compared to GoRequest's Proxy:
//
// gorequest.New().Proxy("http://myproxy:9999").
// Post("http://www.google.com").
// End()
//
// To set no_proxy, just put empty string to Proxy func:
//
// gorequest.New().Proxy("").
// Post("http://www.google.com").
// End()
//
func (s *SuperAgent) Proxy(proxyUrl string) *SuperAgent {
parsedProxyUrl, err := url.Parse(proxyUrl)
if err != nil {
s.Errors = append(s.Errors, err)
} else if proxyUrl == "" {
s.Transport.Proxy = nil
} else if parsedProxyUrl.Scheme == "http" || parsedProxyUrl.Scheme == "https" {
s.Transport.Proxy = http.ProxyURL(parsedProxyUrl)
if parsedProxyUrl.User != nil {
user := parsedProxyUrl.User.Username()
pwd, _ := parsedProxyUrl.User.Password()
s = s.Set("Proxy-Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(user+":"+pwd)))
}
} else if parsedProxyUrl.Scheme == "socks5" {
dialer, err := proxy.FromURL(parsedProxyUrl, s)
if err != nil {
s.Errors = append(s.Errors, err)
} else {
s.Transport.Dial = dialer.Dial
}
} else {
s.Errors = append(s.Errors, errors.New("proxy: unknown scheme: "+parsedProxyUrl.Scheme))
}
return s
}
示例4: clientHandler
func clientHandler(termMon *termmon.TermMonitor, f base.ClientFactory, conn net.Conn, proxyURI *url.URL) {
defer conn.Close()
termMon.OnHandlerStart()
defer termMon.OnHandlerFinish()
name := f.Transport().Name()
// Read the client's SOCKS handshake.
socksReq, err := socks5.Handshake(conn)
if err != nil {
log.Errorf("%s - client failed socks handshake: %s", name, err)
return
}
addrStr := log.ElideAddr(socksReq.Target)
// Deal with arguments.
args, err := f.ParseArgs(&socksReq.Args)
if err != nil {
log.Errorf("%s(%s) - invalid arguments: %s", name, addrStr, err)
socksReq.Reply(socks5.ReplyGeneralFailure)
return
}
// Obtain the proxy dialer if any, and create the outgoing TCP connection.
dialFn := proxy.Direct.Dial
if proxyURI != nil {
dialer, err := proxy.FromURL(proxyURI, proxy.Direct)
if err != nil {
// This should basically never happen, since config protocol
// verifies this.
log.Errorf("%s(%s) - failed to obtain proxy dialer: %s", name, addrStr, log.ElideError(err))
socksReq.Reply(socks5.ReplyGeneralFailure)
return
}
dialFn = dialer.Dial
}
fmt.Println("Got dialer", dialFn, proxyURI, proxy.Direct)
remote, err := f.Dial("tcp", socksReq.Target, dialFn, args)
if err != nil {
log.Errorf("%s(%s) - outgoing connection failed: %s", name, addrStr, log.ElideError(err))
socksReq.Reply(socks5.ErrorToReplyCode(err))
return
}
defer remote.Close()
err = socksReq.Reply(socks5.ReplySucceeded)
if err != nil {
log.Errorf("%s(%s) - SOCKS reply failed: %s", name, addrStr, log.ElideError(err))
return
}
if err = copyLoop(conn, remote); err != nil {
log.Warnf("%s(%s) - closed connection: %s", name, addrStr, log.ElideError(err))
} else {
log.Infof("%s(%s) - closed connection", name, addrStr)
}
return
}
示例5: PhoneProxy
func PhoneProxy(p, u, uid, key string) (err error) {
dialer, err := proxy.FromURL(p, proxy.Dialer)
if err != nil {
return
}
return
}
示例6: main
func main() {
torProx, err := url.Parse("socks5://127.0.0.1:9050")
if err != nil {
panic(err)
}
torDial, err := proxy.FromURL(torProx, proxy.Direct)
if err != nil {
panic(err)
}
transport := &http.Transport{Dial: torDial.Dial}
client := &http.Client{Transport: transport}
req, err := http.NewRequest("POST", "http://ewanvalentine.io", nil)
if err != nil {
panic(err)
}
req.Header.Add("Content-Length", "10000")
req.Header.Add("Keep-Alive", "900")
const N = 100
for {
go SendRequest(client, req)
}
}
示例7: applyProxy
func applyProxy(a *Args) (err error) {
if a.Proxy == "" {
return nil
}
u, err := url.Parse(a.Proxy)
if err != nil {
return err
}
switch u.Scheme {
case "http", "https":
a.Client.Transport = &http.Transport{
Proxy: http.ProxyURL(u),
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
// KeepAlive: 30 * time.Second,
}).Dial,
// TLSHandshakeTimeout: 10 * time.Second,
}
case "socks5":
dialer, err := proxy.FromURL(u, proxy.Direct)
if err != nil {
return err
}
a.Client.Transport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
// TLSHandshakeTimeout: 10 * time.Second,
}
}
return
}
示例8: Connect
// Connect connects the IRC client to the server configured in Config.Server.
// To enable explicit SSL on the connection to the IRC server, set Config.SSL
// to true before calling Connect(). The port will default to 6697 if SSL is
// enabled, and 6667 otherwise.
// To enable connecting via a proxy server, set Config.Proxy to the proxy URL
// (example socks5://localhost:9000) before calling Connect().
//
// Upon successful connection, Connected will return true and a REGISTER event
// will be fired. This is mostly for internal use; it is suggested that a
// handler for the CONNECTED event is used to perform any initial client work
// like joining channels and sending messages.
func (conn *Conn) Connect() error {
conn.mu.Lock()
defer conn.mu.Unlock()
conn.initialise()
if conn.cfg.Server == "" {
return fmt.Errorf("irc.Connect(): cfg.Server must be non-empty")
}
if conn.connected {
return fmt.Errorf("irc.Connect(): Cannot connect to %s, already connected.", conn.cfg.Server)
}
if !hasPort(conn.cfg.Server) {
if conn.cfg.SSL {
conn.cfg.Server = net.JoinHostPort(conn.cfg.Server, "6697")
} else {
conn.cfg.Server = net.JoinHostPort(conn.cfg.Server, "6667")
}
}
if conn.cfg.Proxy != "" {
proxyURL, err := url.Parse(conn.cfg.Proxy)
if err != nil {
return err
}
conn.proxyDialer, err = proxy.FromURL(proxyURL, conn.dialer)
if err != nil {
return err
}
logging.Info("irc.Connect(): Connecting to %s.", conn.cfg.Server)
if s, err := conn.proxyDialer.Dial("tcp", conn.cfg.Server); err == nil {
conn.sock = s
} else {
return err
}
} else {
logging.Info("irc.Connect(): Connecting to %s.", conn.cfg.Server)
if s, err := conn.dialer.Dial("tcp", conn.cfg.Server); err == nil {
conn.sock = s
} else {
return err
}
}
if conn.cfg.SSL {
logging.Info("irc.Connect(): Performing SSL handshake.")
s := tls.Client(conn.sock, conn.cfg.SSLConfig)
if err := s.Handshake(); err != nil {
return err
}
conn.sock = s
}
conn.postConnect(true)
conn.connected = true
conn.dispatch(&Line{Cmd: REGISTER, Time: time.Now()})
return nil
}
示例9: NewTorProxy
// NewTorProxy creates a new proxy using the Tor service detected at the machine.
func NewTorProxy() (proxy.Dialer, error) {
u, err := url.Parse(newTorProxy(ournet.Tor.Address()))
if err != nil {
return nil, err
}
return proxy.FromURL(u, proxy.Direct)
}
示例10: torHTTPClient
func torHTTPClient() *http.Client {
u, _ := url.Parse("socks5://127.0.0.1:9150/")
d, _ := proxy.FromURL(u, proxy.Direct)
return &http.Client{
Transport: &http.Transport{
Dial: d.Dial,
},
}
}
示例11: clientHandler
func clientHandler(target string, termMon *termmon.TermMonitor, f base.ClientFactory, conn net.Conn, proxyURI *url.URL) {
defer conn.Close()
termMon.OnHandlerStart()
defer termMon.OnHandlerFinish()
fmt.Println("handling...")
name := f.Transport().Name()
fmt.Println("Transport is", name)
// Deal with arguments.
args, err := f.ParseArgs(&pt.Args{})
if err != nil {
fmt.Println("Invalid arguments")
log.Errorf("%s(%s) - invalid arguments: %s", name, target, err)
return
}
fmt.Println("Making dialer...")
// Obtain the proxy dialer if any, and create the outgoing TCP connection.
dialFn := proxy.Direct.Dial
if proxyURI != nil {
dialer, err := proxy.FromURL(proxyURI, proxy.Direct)
if err != nil {
// This should basically never happen, since config protocol
// verifies this.
fmt.Println("failed to obtain dialer", proxyURI, proxy.Direct)
log.Errorf("%s(%s) - failed to obtain proxy dialer: %s", name, target, log.ElideError(err))
return
}
dialFn = dialer.Dial
}
fmt.Println("Dialing...")
remote, err := f.Dial("tcp", target, dialFn, args)
if err != nil {
fmt.Println("outgoing connection failed")
log.Errorf("%s(%s) - outgoing connection failed: %s", name, target, log.ElideError(err))
return
}
defer remote.Close()
fmt.Println("copying...")
if err = copyLoop(conn, remote); err != nil {
log.Warnf("%s(%s) - closed connection: %s", name, target, log.ElideError(err))
} else {
log.Infof("%s(%s) - closed connection", name, target)
}
fmt.Println("done")
return
}
示例12: ProxyDialer
func ProxyDialer(config *ProxyConfig, forward Dialer) (Dialer, error) {
if config == nil || config.URL == "" {
return forward, nil
}
url, err := url.Parse(config.URL)
if err != nil {
return nil, err
}
if _, err := proxy.FromURL(url, nil); err != nil {
return nil, err
}
logp.Info("proxy host: '%s'", url.Host)
return DialerFunc(func(network, address string) (net.Conn, error) {
var err error
var addresses []string
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
if config.LocalResolve {
addresses, err = net.LookupHost(host)
if err != nil {
logp.Warn(`DNS lookup failure "%s": %v`, host, err)
return nil, err
}
} else {
// Do not resolve the address locally. It will be resolved on the
// SOCKS server. The beat will have no control over the randomization
// of the IP used when multiple IPs are returned by DNS.
addresses = []string{host}
}
dialer, err := proxy.FromURL(url, forward)
if err != nil {
return nil, err
}
return dialWith(dialer, network, host, addresses, port)
}), nil
}
示例13: worker
func (b *Boomer) worker(wg *sync.WaitGroup, ch chan *http.Request) {
host, _, _ := net.SplitHostPort(b.Req.OriginalHost)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: b.AllowInsecure,
ServerName: host,
},
DisableCompression: b.DisableCompression,
DisableKeepAlives: b.DisableKeepAlives,
// TODO(jbd): Add dial timeout.
TLSHandshakeTimeout: time.Duration(b.Timeout) * time.Millisecond,
Proxy: http.ProxyURL(b.ProxyAddr),
}
//client := &http.Client{Transport: tr}
//_ = client
for req := range ch {
if b.SocksAddr != nil {
torSocksAddr := *b.SocksAddr
if b.TorAutoIsolate {
torSocksAddr.User = url.UserPassword(b.isolatedSocksAuth())
}
proxyDialer, err := proxy.FromURL(&torSocksAddr, proxy.Direct)
if err != nil {
panic(err)
}
tr.Dial = proxyDialer.Dial
}
client := &http.Client{Transport: tr}
s := time.Now()
code := 0
size := int64(0)
resp, err := client.Do(req)
if err == nil {
size = resp.ContentLength
code = resp.StatusCode
resp.Body.Close()
}
if b.bar != nil {
b.bar.Increment()
}
wg.Done()
b.results <- &result{
statusCode: code,
duration: time.Now().Sub(s),
err: err,
contentLength: size,
}
}
}
示例14: clientHandler
func clientHandler(f base.ClientFactory, conn net.Conn, proxyURI *url.URL, linkInfo LinkInfo) {
defer conn.Close()
termMon.onHandlerStart()
defer termMon.onHandlerFinish()
name := f.Transport().Name()
fakeReq, err := parseClientParameters(strings.Replace(linkInfo.PtArgs, ",", ";", -1))
if err != nil {
log.Errorf("%s - client failed socks handshake: %s", name, err)
return
}
addrStr := log.ElideAddr(linkInfo.ServerAddr)
// Deal with arguments.
args, err := f.ParseArgs(&fakeReq)
if err != nil {
log.Errorf("%s(%s) - invalid arguments: %s", name, addrStr, err)
return
}
// Obtain the proxy dialer if any, and create the outgoing TCP connection.
dialFn := proxy.Direct.Dial
if proxyURI != nil {
dialer, err := proxy.FromURL(proxyURI, proxy.Direct)
if err != nil {
// This should basically never happen, since config protocol
// verifies this.
log.Errorf("%s(%s) - failed to obtain proxy dialer: %s", name, addrStr, log.ElideError(err))
return
}
dialFn = dialer.Dial
}
remote, er := f.Dial("tcp", linkInfo.ServerAddr, dialFn, args)
if er != nil {
log.Errorf("%s(%s) - outgoing connection failed: %s", name, addrStr, log.ElideError(er))
return
}
defer remote.Close()
if err = copyLoop(conn, remote); err != nil {
log.Warnf("%s(%s) - closed connection: %s", name, addrStr, log.ElideError(err))
} else {
log.Infof("%s(%s) - closed connection", name, addrStr)
}
return
}
示例15: Test_buildProxyChain_Returns
func (s *ConnectionPolicySuite) Test_buildProxyChain_Returns(c *C) {
proxies := []string{
"socks5://proxy.local",
"socks5://proxy.remote",
}
direct := &net.Dialer{Timeout: 60 * time.Second}
p1, _ := proxy.FromURL(
&url.URL{
Scheme: "socks5",
Host: "proxy.remote",
}, direct)
expectedProxy, _ := proxy.FromURL(&url.URL{
Scheme: "socks5",
Host: "proxy.local",
}, p1)
chain, err := buildProxyChain(proxies)
c.Check(err, IsNil)
c.Check(chain, DeepEquals, expectedProxy)
}