Exemplo n.º 1
1
// 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
}
Exemplo n.º 2
0
// 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
}
Exemplo n.º 3
0
// 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
}
Exemplo n.º 4
0
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
}
Exemplo n.º 5
0
func PhoneProxy(p, u, uid, key string) (err error) {
	dialer, err := proxy.FromURL(p, proxy.Dialer)
	if err != nil {
		return
	}
	return
}
Exemplo n.º 6
0
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)
	}
}
Exemplo n.º 7
0
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
}
Exemplo n.º 8
0
// 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
}
Exemplo n.º 9
0
Arquivo: tor.go Projeto: 0x27/coyim
// 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)
}
Exemplo n.º 10
0
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,
		},
	}
}
Exemplo n.º 11
0
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
}
Exemplo n.º 12
0
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
}
Exemplo n.º 13
0
Arquivo: run.go Projeto: Yawning/boom
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,
		}
	}
}
Exemplo n.º 14
0
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
}
Exemplo n.º 15
0
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)

}
Exemplo n.º 16
0
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
}
Exemplo n.º 17
0
// 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
}
Exemplo n.º 18
0
// 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
}
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
}
Exemplo n.º 20
0
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
}
Exemplo n.º 21
0
// NewServer creates Server.
// If c is not valid, this returns non-nil error.
func NewServer(c *Config) (*Server, error) {
	if err := c.validate(); err != nil {
		return nil, err
	}

	dialer := defaultDialer
	if c.Dialer != nil {
		dialer = c.Dialer
	}
	proxy_dialer, err := proxy.FromURL(c.ProxyURL, dialer)
	if err != nil {
		return nil, err
	}

	l, err := net.Listen("tcp", c.Listen)
	if err != nil {
		return nil, err
	}

	return &Server{c, proxy_dialer, l}, nil
}
Exemplo n.º 22
0
func dialConn(tracker *ConnTracker, addr string, target string, f base.ClientFactory, proxyURI *url.URL) {
	// 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) - failed to obtain proxy dialer: %s", target, log.ElideError(err))
			return
		}
		dialFn = dialer.Dial
	}

	fmt.Println("Dialing....")

	// Deal with arguments.
	args, err := f.ParseArgs(&pt.Args{})
	if err != nil {
		fmt.Println("Invalid arguments")
		log.Errorf("(%s) - invalid arguments: %s", target, err)
		delete(*tracker, addr)
		return
	}

	fmt.Println("Dialing ", target)
	remote, err := f.Dial("tcp", target, dialFn, args)
	if err != nil {
		fmt.Println("outgoing connection failed", err)
		log.Errorf("(%s) - outgoing connection failed: %s", target, log.ElideError(err))
		fmt.Println("Failed")
		delete(*tracker, addr)
		return
	}

	fmt.Println("Success")

	(*tracker)[addr] = ConnState{&remote, false}
}
Exemplo n.º 23
0
// 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
		}
	}
	// ToDo: Replace proxy.Direct with
	uplinkDialer := &net.Dialer{
		Timeout: time.Second * time.Duration(Timeout),
		// Deadline:  time.Second * time.Duration(Timeout),
		KeepAlive: 0,
	}
	// dialer, err := proxy.FromURL(socksURL, proxy.Direct)
	dialer, err := proxy.FromURL(socksURL, uplinkDialer)
	if err != nil {
		return nil, err
	}
	tr := &http.Transport{
		//Proxy: http.ProxyFromEnvironment,
		Dial:                  dialer.Dial,
		DisableKeepAlives:     true,
		MaxIdleConnsPerHost:   1,
		ResponseHeaderTimeout: time.Second * time.Duration(Timeout),
	}
	client := &http.Client{
		Transport: tr,
		Timeout:   time.Second * time.Duration(Timeout), // 45 second timeout is pretty nice
	}
	return client, nil
}
Exemplo n.º 24
0
func buildProxyChain(proxies []string) (dialer proxy.Dialer, err error) {
	for i := len(proxies) - 1; i >= 0; i-- {
		u, e := url.Parse(proxies[i])
		if e != nil {
			err = errors.New("Failed to parse " + proxies[i] + " as a URL: " + e.Error())
			return
		}

		if dialer == nil {
			dialer = &net.Dialer{
				Timeout: 60 * time.Second,
			}
		}

		if dialer, err = proxy.FromURL(u, dialer); err != nil {
			err = errors.New("Failed to parse " + proxies[i] + " as a proxy: " + err.Error())
			return
		}
	}

	return
}
Exemplo n.º 25
0
func NewDocument(http_url string) (*Document, error) {

	var res *http.Response
	var e error
	if Http_protocol == "http" {
		res, e = http.Get(http_url)
		if e != nil {
			return nil, e
		}
		//return NewDocumentFromResponse(res)
	}
	// Load the URL
	if Http_protocol == "https" {
		tbProxyURL, err := url.Parse("socks5://127.0.0.1:9150")
		if err != nil {
			//fatalf("Failed to parse proxy URL: %v\n", err)
			return nil, err
		}
		tbDialer, err := proxy.FromURL(tbProxyURL, proxy.Direct)
		if err != nil {
			//fatalf("Failed to obtain proxy dialer: %v\n", err)
			return nil, err
		}
		tbTransport := &http.Transport{Dial: tbDialer.Dial}
		client := &http.Client{Transport: tbTransport}

		res, e = client.Get(http_url)
		//res, e := http.Get(url)
		if e != nil {
			return nil, e
		}
		//return NewDocumentFromResponse(res)
	}

	return NewDocumentFromResponse(res)

}
Exemplo n.º 26
0
// This is a rip off of proxy.FromEnvironment with a custom forward dialer
func getDialer(forward proxy.Dialer) proxy.Dialer {
	allProxy := os.Getenv("all_proxy")
	if len(allProxy) == 0 {
		return forward
	}

	proxyURL, err := url.Parse(allProxy)
	if err != nil {
		return forward
	}
	prxy, err := proxy.FromURL(proxyURL, forward)
	if err != nil {
		return forward
	}

	noProxy := os.Getenv("no_proxy")
	if len(noProxy) == 0 {
		return prxy
	}

	perHost := proxy.NewPerHost(prxy, forward)
	perHost.AddFromString(noProxy)
	return perHost
}
Exemplo n.º 27
0
func getURL(site string) *http.Response {

	tbProxyURL, err := url.Parse("socks5://127.0.0.1:9150")
	if err != nil {
		fatalf("Failed to parse proxy URL: %v\n", err)
	}

	tbDialer, err := proxy.FromURL(tbProxyURL, proxy.Direct)
	if err != nil {
		fatalf("Failed to obtain proxy dialer: %v\n", err)
	}

	tbTransport := &http.Transport{Dial: tbDialer.Dial}
	client := &http.Client{Transport: tbTransport}

	resp, err := client.Get(site)
	if err != nil {
		fatalf("Failed to issue GET request: %v\n", err)
	}

	fmt.Printf("GET returned: %v\n", resp.Status)
	return resp

}
Exemplo n.º 28
0
Arquivo: ui.go Projeto: VKCheung/coyim
func (c *cliUI) enroll(conf *config.ApplicationConfig, currentConf *config.Account) bool {
	var err error
	c.warn("Enrolling new config file")

	var domain string
	for {
		c.term.SetPrompt("Account (i.e. [email protected], enter to quit): ")
		if currentConf.Account, err = c.term.ReadLine(); err != nil || len(currentConf.Account) == 0 {
			return false
		}

		parts := strings.SplitN(currentConf.Account, "@", 2)
		if len(parts) != 2 {
			c.alert("invalid username (want [email protected]): " + currentConf.Account)
			continue
		}
		domain = parts[1]
		break
	}

	c.term.SetPrompt("Enable debug logging to /tmp/xmpp-client-debug.log? ")
	if debugLog, err := c.term.ReadLine(); err != nil || !config.ParseYes(debugLog) {
		c.info("Not enabling debug logging...")
	} else {
		c.info("Debug logging enabled...")
		conf.RawLogFile = "/tmp/xmpp-client-debug.log"
	}

	c.term.SetPrompt("Use Tor?: ")
	if useTorQuery, err := c.term.ReadLine(); err != nil || len(useTorQuery) == 0 || !config.ParseYes(useTorQuery) {
		c.info("Not using Tor...")
		currentConf.Proxies = []string{}
	} else {
		c.info("Using Tor...")
	}

	c.term.SetPrompt("File to import libotr private key from (enter to generate): ")

	var pkeys []otr3.PrivateKey
	for {
		importFile, err := c.term.ReadLine()
		if err != nil {
			return false
		}
		if len(importFile) > 0 {
			privKeyBytes, err := ioutil.ReadFile(importFile)
			if err != nil {
				c.alert("Failed to open private key file: " + err.Error())
				continue
			}
			var priv otr3.DSAPrivateKey
			if !priv.Import(privKeyBytes) {
				c.alert("Failed to parse libotr private key file (the parser is pretty simple I'm afraid)")
				continue
			}
			pkeys = append(pkeys, &priv)
			break
		} else {
			c.info("Generating private key...")
			pkeys, err = otr3.GenerateMissingKeys([][]byte{})
			if err != nil {
				c.alert("Failed to generate private key - this implies something is really bad with your system, so we bail out now")
				return false
			}
			break
		}
	}

	currentConf.PrivateKeys = config.SerializedKeys(pkeys)
	currentConf.OTRAutoAppendTag = true
	currentConf.OTRAutoStartSession = true
	currentConf.OTRAutoTearDown = false

	// Force Tor for servers with well known Tor hidden services.
	if _, ok := servers.Get(domain); ok && currentConf.HasTorAuto() {
		const torProxyURL = "socks5://127.0.0.1:9050"
		c.info("It appears that you are using a well known server and we will use its Tor hidden service to connect.")
		currentConf.Proxies = []string{torProxyURL}
		c.term.SetPrompt("> ")
		return true
	}

	var proxyStr string
	proxyDefaultPrompt := ", enter for none"
	if currentConf.HasTorAuto() {
		proxyDefaultPrompt = ", which is the default"
	}
	c.term.SetPrompt("Proxy (i.e socks5://127.0.0.1:9050" + proxyDefaultPrompt + "): ")

	for {
		if proxyStr, err = c.term.ReadLine(); err != nil {
			return false
		}
		if len(proxyStr) == 0 {
			if !currentConf.HasTorAuto() {
				break
			} else {
				proxyStr = "socks5://127.0.0.1:9050"
			}
		}
		u, err := url.Parse(proxyStr)
		if err != nil {
			c.alert("Failed to parse " + proxyStr + " as a URL: " + err.Error())
			continue
		}
		if _, err = proxy.FromURL(u, proxy.Direct); err != nil {
			c.alert("Failed to parse " + proxyStr + " as a proxy: " + err.Error())
			continue
		}
		break
	}

	c.term.SetPrompt("> ")

	return true
}
Exemplo n.º 29
0
func enroll(config *Config, term *terminal.Terminal) bool {
	var err error
	warn(term, "Enrolling new config file")

	var domain string
	for {
		term.SetPrompt("Account (i.e. [email protected], enter to quit): ")
		if config.Account, err = term.ReadLine(); err != nil || len(config.Account) == 0 {
			return false
		}

		parts := strings.SplitN(config.Account, "@", 2)
		if len(parts) != 2 {
			alert(term, "invalid username (want [email protected]): "+config.Account)
			continue
		}
		domain = parts[1]
		break
	}

	term.SetPrompt("Enable debug logging to /tmp/xmpp-client-debug.log? ")
	if debugLog, err := term.ReadLine(); err != nil || debugLog != "yes" {
		info(term, "Not enabling debug logging...")
	} else {
		info(term, "Debug logging enabled...")
		config.RawLogFile = "/tmp/xmpp-client-debug.log"
	}

	term.SetPrompt("Use Tor?: ")
	if useTorQuery, err := term.ReadLine(); err != nil || len(useTorQuery) == 0 || useTorQuery[0] != 'y' && useTorQuery[0] != 'Y' {
		info(term, "Not using Tor...")
		config.UseTor = false
	} else {
		info(term, "Using Tor...")
		config.UseTor = true
	}

	term.SetPrompt("File to import libotr private key from (enter to generate): ")

	var priv otr.PrivateKey
	for {
		importFile, err := term.ReadLine()
		if err != nil {
			return false
		}
		if len(importFile) > 0 {
			privKeyBytes, err := ioutil.ReadFile(importFile)
			if err != nil {
				alert(term, "Failed to open private key file: "+err.Error())
				continue
			}

			if !priv.Import(privKeyBytes) {
				alert(term, "Failed to parse libotr private key file (the parser is pretty simple I'm afraid)")
				continue
			}
			break
		} else {
			info(term, "Generating private key...")
			priv.Generate(rand.Reader)
			break
		}
	}
	config.PrivateKey = priv.Serialize(nil)

	config.OTRAutoAppendTag = true
	config.OTRAutoStartSession = true
	config.OTRAutoTearDown = false

	// List well known Tor hidden services.
	knownTorDomain := map[string]string{
		"jabber.ccc.de":             "okj7xc6j2szr2y75.onion",
		"riseup.net":                "4cjw6cwpeaeppfqz.onion",
		"jabber.calyxinstitute.org": "ijeeynrc6x2uy5ob.onion",
		"jabber.otr.im":             "5rgdtlawqkcplz75.onion",
		"wtfismyip.com":             "ofkztxcohimx34la.onion",
	}

	// Autoconfigure well known Tor hidden services.
	if hiddenService, ok := knownTorDomain[domain]; ok && config.UseTor {
		const torProxyURL = "socks5://127.0.0.1:9050"
		info(term, "It appears that you are using a well known server and we will use its Tor hidden service to connect.")
		config.Server = hiddenService
		config.Port = 5222
		config.Proxies = []string{torProxyURL}
		term.SetPrompt("> ")
		return true
	}

	var proxyStr string
	proxyDefaultPrompt := ", enter for none"
	if config.UseTor {
		proxyDefaultPrompt = ", which is the default"
	}
	term.SetPrompt("Proxy (i.e socks5://127.0.0.1:9050" + proxyDefaultPrompt + "): ")

	for {
		if proxyStr, err = term.ReadLine(); err != nil {
			return false
		}
		if len(proxyStr) == 0 {
			if !config.UseTor {
				break
			} else {
				proxyStr = "socks5://127.0.0.1:9050"
			}
		}
		u, err := url.Parse(proxyStr)
		if err != nil {
			alert(term, "Failed to parse "+proxyStr+" as a URL: "+err.Error())
			continue
		}
		if _, err = proxy.FromURL(u, proxy.Direct); err != nil {
			alert(term, "Failed to parse "+proxyStr+" as a proxy: "+err.Error())
			continue
		}
		break
	}

	if len(proxyStr) > 0 {
		config.Proxies = []string{proxyStr}

		info(term, "Since you selected a proxy, we need to know the server and port to connect to as a SRV lookup would leak information every time.")
		term.SetPrompt("Server (i.e. xmpp.example.com, enter to lookup using unproxied DNS): ")
		if config.Server, err = term.ReadLine(); err != nil {
			return false
		}
		if len(config.Server) == 0 {
			var port uint16
			info(term, "Performing SRV lookup")
			if config.Server, port, err = xmpp.Resolve(domain); err != nil {
				alert(term, "SRV lookup failed: "+err.Error())
				return false
			}
			config.Port = int(port)
			info(term, "Resolved "+config.Server+":"+strconv.Itoa(config.Port))
		} else {
			for {
				term.SetPrompt("Port (enter for 5222): ")
				portStr, err := term.ReadLine()
				if err != nil {
					return false
				}
				if len(portStr) == 0 {
					portStr = "5222"
				}
				if config.Port, err = strconv.Atoi(portStr); err != nil || config.Port <= 0 || config.Port > 65535 {
					info(term, "Port numbers must be 0 < port <= 65535")
					continue
				}
				break
			}
		}
	}

	term.SetPrompt("> ")

	return true
}
Exemplo n.º 30
0
func main() {
	proxyPtr := flag.String("proxy", "", "socks proxy")
	debugPtr = flag.Bool("debug", false, "enable request/response dump")
	flag.Parse()
	log.SetOutput(os.Stderr)

	if *proxyPtr != "" {
		proxyURL, err := url.Parse("socks5://" + *proxyPtr)
		if err != nil {
			log.Fatalf("Failed to parse proxy URL: %v", err)
		}
		dialer, err := proxy.FromURL(proxyURL, proxy.Direct)
		if err != nil {
			log.Fatalf("Failed to obtain proxy dialer: %v", err)
		}
		http.DefaultTransport = &http.Transport{
			Dial: dialer.Dial,
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: true,
			},
		}
		log.Printf("Set proxy to %s", proxyURL)
	}

	switch flag.Arg(0) {
	case "discovery":
		switch flag.NArg() {
		case 2:
			if v, err := DiscoverPools(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v.Json())
			}
		default:
			log.Fatalf("Usage: %s discovery PATH", os.Args[0])
		}
	case "pool_hashrate":
		switch flag.NArg() {
		case 3:
			if v, err := PoolHashrate(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s pool_hashrate URL APIKEY", os.Args[0])
		}
	case "pool_workers":
		switch flag.NArg() {
		case 3:
			if v, err := PoolWorkers(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s pool_workers URL APIKEY", os.Args[0])
		}
	case "pool_efficiency":
		switch flag.NArg() {
		case 3:
			if v, err := PoolEfficiency(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s pool_efficiency URL APIKEY", os.Args[0])
		}
	case "pool_lastblock":
		switch flag.NArg() {
		case 3:
			if v, err := PoolLastBlock(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s pool_lastblock URL APIKEY", os.Args[0])
		}
	case "pool_nextblock":
		switch flag.NArg() {
		case 3:
			if v, err := PoolNextBlock(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s pool_nextblock URL APIKEY", os.Args[0])
		}
	case "user_hashrate":
		switch flag.NArg() {
		case 3:
			if v, err := UserHashrate(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s user_hashrate URL APIKEY", os.Args[0])
		}
	case "user_sharerate":
		switch flag.NArg() {
		case 3:
			if v, err := UserSharerate(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s user_sharerate URL APIKEY", os.Args[0])
		}
	case "user_shares_valid":
		switch flag.NArg() {
		case 3:
			if v, err := UserSharesValid(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s user_shares_valid URL APIKEY", os.Args[0])
		}
	case "user_shares_invalid":
		switch flag.NArg() {
		case 3:
			if v, err := UserSharesInvalid(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s user_shares_invalid URL APIKEY", os.Args[0])
		}
	case "user_balance_confirmed":
		switch flag.NArg() {
		case 3:
			if v, err := UserBalanceConfirmed(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s user_balance_confirmed URL APIKEY", os.Args[0])
		}
	case "user_balance_unconfirmed":
		switch flag.NArg() {
		case 3:
			if v, err := UserBalanceUnconfirmed(flag.Args()[1:]); err != nil {
				log.Fatalf("Error: %s", err.Error())
			} else {
				fmt.Print(v)
			}
		default:
			log.Fatalf("Usage: %s user_balance_unconfirmed URL APIKEY", os.Args[0])
		}
	default:
		log.Fatal("You must specify one of the following action: " +
			"'discovery', " +
			"'pool_hashrate', 'pool_workers', 'pool_efficiency', 'pool_lastblock', 'pool_nextblock', " +
			"'user_hashrate', 'user_sharerate', 'user_shares_valid', 'user_shares_invalid', " +
			"'user_balance_confirmed' or 'user_balance_unconfirmed'.")

	}
}