本文整理汇总了Golang中golang.org/x/net/proxy.FromURL函数的典型用法代码示例。如果您正苦于以下问题:Golang FromURL函数的具体用法?Golang FromURL怎么用?Golang FromURL使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FromURL函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
}
开发者ID:mewbak,项目名称:repbin,代码行数:31,代码来源:socks.go
示例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
}
开发者ID:40a,项目名称:bootkube,代码行数:29,代码来源:proxy.go
示例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
}
开发者ID:AquaKlaster,项目名称:gorequest,代码行数:41,代码来源:main.go
示例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
}
开发者ID:OperatorFoundation,项目名称:obfs4,代码行数:60,代码来源:pt_socks5.go
示例5: PhoneProxy
func PhoneProxy(p, u, uid, key string) (err error) {
dialer, err := proxy.FromURL(p, proxy.Dialer)
if err != nil {
return
}
return
}
开发者ID:TheCreeper,项目名称:UselessLocker,代码行数:7,代码来源:home.go
示例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)
}
}
开发者ID:EwanValentine,项目名称:goloris,代码行数:30,代码来源:main.go
示例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
}
开发者ID:jmptrader,项目名称:request,代码行数:32,代码来源:proxy.go
示例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
}
开发者ID:mephux,项目名称:komanda-cli,代码行数:70,代码来源:connection.go
示例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)
}
开发者ID:0x27,项目名称:coyim,代码行数:9,代码来源:tor.go
示例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,
},
}
}
开发者ID:ality,项目名称:go-cmds,代码行数:9,代码来源:archmirrors.go
示例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
}
开发者ID:OperatorFoundation,项目名称:obfs4,代码行数:57,代码来源:transparent_tcp.go
示例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
}
开发者ID:ChongFeng,项目名称:beats,代码行数:44,代码来源:proxy.go
示例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,
}
}
}
开发者ID:Yawning,项目名称:boom,代码行数:53,代码来源:run.go
示例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
}
开发者ID:pminmax945,项目名称:obfs4,代码行数:51,代码来源:obfs4proxy.go
示例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)
}
开发者ID:0x27,项目名称:coyim,代码行数:23,代码来源:connection_policy_test.go
示例16: Validate
func (c *ProxyConfig) Validate() error {
if c.URL == "" {
return nil
}
url, err := url.Parse(c.URL)
if err != nil {
return err
}
if _, err := proxy.FromURL(url, nil); err != nil {
return err
}
return nil
}
开发者ID:ChongFeng,项目名称:beats,代码行数:15,代码来源:proxy.go
示例17: Connect
// Connect connects to the host by using the specified port or the default one if the value is <=0.
func (ftp *FTP) Connect(host string, port int, socks5ProxyUrl string) (resp *Response, err error) {
if len(host) == 0 {
return nil, errors.New("The host must be specified")
}
ftp.Host = host
if port <= 0 {
port = DefaultFtpPort
}
addr := fmt.Sprintf("%s:%d", ftp.Host, ftp.Port)
// use the system proxy if emtpy
if socks5ProxyUrl == "" {
ftp.writeInfo("using environment proxy, url: ", os.Getenv("all_proxy"))
ftp.dialer = proxy.FromEnvironment()
} else {
ftp.dialer = proxy.Direct
if u, err1 := url.Parse(socks5ProxyUrl); err1 == nil {
p, err2 := proxy.FromURL(u, proxy.Direct)
if err2 == nil {
ftp.dialer = p
}
}
}
err = ftp.NewConn(addr)
if err != nil {
return
}
ftp.writeInfo("host:", ftp.Host, " port:", strconv.Itoa(ftp.Port), " proxy enabled:", ftp.dialer != proxy.Direct)
// NOTE: this is an absolute time that needs refreshing after each READ/WRITE net operation
//ftp.conn.conn.SetDeadline(getTimeoutInMsec(ftp.timeoutInMsec))
if resp, err = ftp.Read(NONE_FTP_CMD); err != nil {
return
}
ftp.welcome = resp.Message
ftp.writeInfo("Successfully connected on local address:", ftp.conn.LocalAddr())
return
}
开发者ID:stinkyfingers,项目名称:ftp4go,代码行数:47,代码来源:client.go
示例18: DialWithTimeout
// Dial establishes a connection to a Riemann server at addr, on the network
// netwrk, with a timeout of timeout
//
// Known networks are "tcp", "tcp4", "tcp6", "udp", "udp4", and "udp6".
func DialWithTimeout(netwrk, addr string, timeout time.Duration) (c *Client, err error) {
c = new(Client)
var cnet network
switch netwrk {
case "tcp", "tcp4", "tcp6":
cnet = new(tcp)
case "udp", "udp4", "udp6":
cnet = new(udp)
default:
return nil, fmt.Errorf("dial %q: unsupported network %q", netwrk, netwrk)
}
var proxyUrl = os.Getenv("RIEMANN_PROXY")
// Get a proxy Dialer that will create the connection on our
// behalf via the SOCKS5 proxy. Specify the authentication
// and re-create the dialer/transport/client if tor's
// IsolateSOCKSAuth is needed.
var dialer proxy.Dialer
if dialer == nil {
dialer = proxy.Direct
}
if proxyUrl != "" {
u, err := url.Parse(proxyUrl)
if err != nil {
fmt.Errorf("Failed to obtain proxy dialer: %v\n", err)
}
if dialer, err = proxy.FromURL(u, dialer); err != nil {
fmt.Errorf("Failed to parse " + proxyUrl + " as a proxy: " + err.Error())
return nil, err
}
}
c.net = cnet
c.timeout = timeout
c.connection, err = dialer.Dial(netwrk, addr)
if err != nil {
return nil, err
}
return c, nil
}
开发者ID:ktf,项目名称:raidman,代码行数:48,代码来源:raidman.go
示例19: NewProxyDialFunc
func NewProxyDialFunc(config *UpstreamProxyConfig) DialFunc {
if config.ProxyURIString == "" {
return config.ForwardDialFunc
}
proxyURI, err := url.Parse(config.ProxyURIString)
if err != nil {
return func(network, addr string) (net.Conn, error) {
return nil, proxyError(fmt.Errorf("proxyURI url.Parse: %v", err))
}
}
dialer, err := proxy.FromURL(proxyURI, config)
if err != nil {
return func(network, addr string) (net.Conn, error) {
return nil, proxyError(fmt.Errorf("proxy.FromURL: %v", err))
}
}
return dialer.Dial
}
开发者ID:Psiphon-Labs,项目名称:psiphon-tunnel-core,代码行数:19,代码来源:upstreamproxy.go
示例20: newDialer
func newDialer() (proxy.Dialer, error) {
var proxyUrl = os.Getenv("RIEMANN_PROXY")
var dialer proxy.Dialer = proxy.Direct
// Get a proxy Dialer that will create the connection on our
// behalf via the SOCKS5 proxy. Specify the authentication
// and re-create the dialer/transport/client if tor's
// IsolateSOCKSAuth is needed.
if len(proxyUrl) > 0 {
u, err := url.Parse(proxyUrl)
if err != nil {
return nil, fmt.Errorf("failed to obtain proxy dialer: %v\n", err)
}
if dialer, err = proxy.FromURL(u, dialer); err != nil {
return nil, fmt.Errorf("failed to parse " + proxyUrl + " as a proxy: " + err.Error())
}
}
return dialer, nil
}
开发者ID:amir,项目名称:raidman,代码行数:20,代码来源:raidman.go
注:本文中的golang.org/x/net/proxy.FromURL函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论