當前位置: 首頁>>代碼示例>>Golang>>正文


Golang proxy.Dialer類代碼示例

本文整理匯總了Golang中code/google/com/p/go/net/proxy.Dialer的典型用法代碼示例。如果您正苦於以下問題:Golang Dialer類的具體用法?Golang Dialer怎麽用?Golang Dialer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Dialer類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: dialServer

func (c *client) dialServer(server string, useRandomIdentity bool) (*transport.Conn, error) {
	identity := &c.identity
	identityPublic := &c.identityPublic
	if useRandomIdentity {
		var randomIdentity [32]byte
		c.randBytes(randomIdentity[:])

		var randomIdentityPublic [32]byte
		curve25519.ScalarBaseMult(&randomIdentityPublic, &randomIdentity)

		identity = &randomIdentity
		identityPublic = &randomIdentityPublic
	}

	serverIdentity, host, err := parseServer(server, c.testing)
	if err != nil {
		return nil, err
	}
	var tor proxy.Dialer
	if c.testing {
		tor = proxy.Direct
	} else {
		tor = c.torDialer()
	}
	rawConn, err := tor.Dial("tcp", host)
	if err != nil {
		return nil, err
	}
	conn := transport.NewClient(rawConn, identity, identityPublic, serverIdentity)
	if err := conn.Handshake(); err != nil {
		return nil, err
	}
	return conn, nil
}
開發者ID:jansfer,項目名稱:pond,代碼行數:34,代碼來源:network.go

示例2: dialServer

func (c *client) dialServer(server string, useRandomIdentity bool) (*transport.Conn, error) {
	identity := &c.identity
	identityPublic := &c.identityPublic
	if useRandomIdentity {
		var randomIdentity [32]byte
		c.randBytes(randomIdentity[:])

		var randomIdentityPublic [32]byte
		curve25519.ScalarBaseMult(&randomIdentityPublic, &randomIdentity)

		identity = &randomIdentity
		identityPublic = &randomIdentityPublic
	}

	serverIdentity, host, err := parseServer(server, c.dev)
	if err != nil {
		return nil, err
	}
	var tor proxy.Dialer
	if c.dev {
		tor = proxy.Direct
	} else {
		tor = c.torDialer()
	}
	rawConn, err := tor.Dial("tcp", host)
	if err != nil {
		return nil, err
	}
	// Sometimes Tor holds the connection open but we never receive
	// anything so we add a 60 second deadline.
	rawConn.SetDeadline(time.Now().Add(60 * time.Second))
	conn := transport.NewClient(rawConn, identity, identityPublic, serverIdentity)
	if err := conn.Handshake(); err != nil {
		return nil, err
	}
	return conn, nil
}
開發者ID:nico,項目名稱:pond,代碼行數:37,代碼來源:network.go

示例3: main

func main() {
	flag.Parse()

	oldState, err := terminal.MakeRaw(0)
	if err != nil {
		panic(err.Error())
	}
	defer terminal.Restore(0, oldState)
	term := terminal.NewTerminal(os.Stdin, "> ")
	updateTerminalSize(term)

	resizeChan := make(chan os.Signal)
	go func() {
		for _ = range resizeChan {
			updateTerminalSize(term)
		}
	}()
	signal.Notify(resizeChan, syscall.SIGWINCH)

	if len(*configFile) == 0 {
		homeDir := os.Getenv("HOME")
		if len(homeDir) == 0 {
			alert(term, "$HOME not set. Please either export $HOME or use the -config-file option.\n")
			return
		}
		persistentDir := filepath.Join(homeDir, "Persistent")
		if stat, err := os.Lstat(persistentDir); err == nil && stat.IsDir() {
			// Looks like Tails.
			homeDir = persistentDir
		}
		*configFile = filepath.Join(homeDir, ".xmpp-client")
	}

	config, err := ParseConfig(*configFile)
	if err != nil {
		alert(term, "Failed to parse config file: "+err.Error())
		config = new(Config)
		if !enroll(config, term) {
			return
		}
		config.filename = *configFile
		config.Save()
	}

	password := config.Password
	if len(password) == 0 {
		if password, err = term.ReadPassword(fmt.Sprintf("Password for %s (will not be saved to disk): ", config.Account)); err != nil {
			alert(term, "Failed to read password: "+err.Error())
			return
		}
	}

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

	var addr string
	addrTrusted := false

	if len(config.Server) > 0 && config.Port > 0 {
		addr = fmt.Sprintf("%s:%d", config.Server, config.Port)
		addrTrusted = true
	} else {
		if len(config.Proxies) > 0 {
			alert(term, "Cannot connect via a proxy without Server and Port being set in the config file as an SRV lookup would leak information.")
			return
		}
		host, port, err := xmpp.Resolve(domain)
		if err != nil {
			alert(term, "Failed to resolve XMPP server: "+err.Error())
			return
		}
		addr = fmt.Sprintf("%s:%d", host, port)
	}

	var dialer proxy.Dialer
	for i := len(config.Proxies) - 1; i >= 0; i-- {
		u, err := url.Parse(config.Proxies[i])
		if err != nil {
			alert(term, "Failed to parse "+config.Proxies[i]+" as a URL: "+err.Error())
			return
		}
		if dialer == nil {
			dialer = proxy.Direct
		}
		if dialer, err = proxy.FromURL(u, dialer); err != nil {
			alert(term, "Failed to parse "+config.Proxies[i]+" as a proxy: "+err.Error())
			return
		}
	}

	var certSHA256 []byte
	if len(config.ServerCertificateSHA256) > 0 {
		certSHA256, err = hex.DecodeString(config.ServerCertificateSHA256)
		if err != nil {
			alert(term, "Failed to parse ServerCertificateSHA256 (should be hex string): "+err.Error())
//.........這裏部分代碼省略.........
開發者ID:copiesofcopies,項目名稱:xmpp-client,代碼行數:101,代碼來源:ui.go

示例4: main

func main() {
	runtime.GOMAXPROCS(2)
	flag.Parse()

	if len(*secret) == 0 {
		fmt.Fprintf(os.Stderr, "The shared secret must be given as --secret\n")
		os.Exit(2)
	}
	if len(*keyFile) == 0 {
		fmt.Fprintf(os.Stderr, "The path to a find containing the public key material to exchange must be given as --key-file\n")
		os.Exit(2)
	}

	pubKeyBytes, err := ioutil.ReadFile(*keyFile)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to read key file: %s\n", err)
		os.Exit(2)
	}

	const maxBody = 4096
	if limit := maxBody - 24 - secretbox.Overhead; len(pubKeyBytes) > limit {
		fmt.Fprintf(os.Stderr, "--key-file is too large (%d bytes of a maximum of %d)\n", len(pubKeyBytes), limit)
		os.Exit(2)
	}

	// Run scrypt on a goroutine so that we can overlap it with the network
	// delay.
	keyChan := make(chan []byte)
	go deriveKey(keyChan, *secret)

	var dialer proxy.Dialer
	dialer = proxy.Direct
	if len(*torProxy) > 0 {
		fmt.Fprintf(os.Stderr, "Using SOCKS5 proxy at %s\n", *torProxy)
		dialer, err = proxy.SOCKS5("tcp", *torProxy, nil, dialer)
		if err != nil {
			panic(err)
		}
	}

	fmt.Fprintf(os.Stderr, "Starting TCP connection to %s\n", *server)
	rawConn, err := dialer.Dial("tcp", *server)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to connect to server: %s\n", err)
		os.Exit(2)
	}

	var conn net.Conn
	if *useTLS {
		hostname, _, err := net.SplitHostPort(*server)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to split %s into host and post: %s\n", *server, err)
			os.Exit(2)
		}
		tlsConn := tls.Client(rawConn, &tls.Config{
			ServerName: hostname,
		})
		fmt.Fprintf(os.Stderr, "Starting TLS handshake\n")
		if err := tlsConn.Handshake(); err != nil {
			rawConn.Close()
			fmt.Fprintf(os.Stderr, "TLS handshake failed: %s\n", err)
			os.Exit(2)
		}
		conn = tlsConn
	} else {
		conn = rawConn
	}
	defer conn.Close()

	var keySlice []byte
	select {
	case keySlice = <-keyChan:
	default:
		fmt.Fprintf(os.Stderr, "Waiting for key derivation to complete. This may take some time.\n")
		keySlice = <-keyChan
	}
	var key [32]byte
	copy(key[:], keySlice)

	mac := hmac.New(sha256.New, key[:])
	mac.Write(pubKeyBytes)
	nonceSlice := mac.Sum(nil)
	var nonce [24]byte
	copy(nonce[:], nonceSlice)

	box := make([]byte, len(nonce)+secretbox.Overhead+len(pubKeyBytes))
	copy(box, nonce[:])
	secretbox.Seal(box[len(nonce):len(nonce)], pubKeyBytes, &nonce, &key)

	h := sha256.New()
	h.Write(key[:])
	tag := h.Sum(nil)

	body := bytes.NewReader(box)
	request, err := http.NewRequest("POST", "http://"+*server+"/exchange/"+hex.EncodeToString(tag), body)
	if err != nil {
		panic(err)
	}
	request.ContentLength = int64(len(box))
	request.Header.Add("Content-Type", "application/octet-stream")
//.........這裏部分代碼省略.........
開發者ID:ioerror,項目名稱:panda,代碼行數:101,代碼來源:client.go

示例5: main

func main() {
	log.SetFlags(0)
	flag.Usage = usage
	flag.Parse()

	if flag.NArg() != 0 {
		usage()
	}

	// Setup the TLS client configuration.
	config := &tls.Config{ServerName: *host}
	if *skipVerify {
		if *verbose {
			log.Print("WARNING: Skipping certificate verification")
		}
		config.InsecureSkipVerify = true
	}
	if *certFile != "" {
		data, err := ioutil.ReadFile(*certFile)
		if err != nil {
			log.Fatalf("could not read certificate file: %s", err)
		}
		p := x509.NewCertPool()
		if ok := p.AppendCertsFromPEM(data); !ok {
			log.Fatalf("could not parse certificates in %q", *certFile)
		}
		config.RootCAs = p
	}

	// Determine how we should converse with the remote server.
	fn, ok := tlsMethods[*method]
	if !ok {
		log.Fatalf("unknown connection method %q", method)
	}

	// Determine the proxy dialer method.
	var dialer proxy.Dialer = proxy.Direct
	if *proxyURL != "" {
		u, err := url.Parse(*proxyURL)
		if err != nil {
			log.Fatalf("failed to parse proxy url: %s", err)
		}
		d, err := proxy.FromURL(u, dialer)
		if err != nil {
			log.Fatal(err)
		}
		dialer = d
		usualHandshakeRTT = proxyHandshakeRTT
	}

	// Connect to the remote server.
	c, err := dialer.Dial("tcp", net.JoinHostPort(*host, *port))
	if err != nil {
		log.Fatalf("could not dial remote host: %s", err)
	}

	// Wrap the connection in a shim so we can sniff the TLS traffic.
	s := &shim{Conn: c}

	// Run our predetermined TLS conversation.
	// If successful, the connection will be automatically closed.
	if err := fn(s, config); err != nil {
		s.Close()
		log.Fatalf("error in TLS conversation: %s", err)
	}

	// Warn if the TLS handshake took too long.
	rtt := s.HandshakeRTT()
	if rtt > usualHandshakeRTT {
		log.Printf("WARNING: the TLS handshake took too long (%s)\n", rtt)
	}

	// Show the server's time in Unix format.
	if *showTime {
		fmt.Println(s.ServerTime().Format(time.UnixDate))
	}

	// Go no further if we don't have to set the clock.
	if *dontSetClock {
		return
	}

	// Check for unreasonable time values.
	t := s.ServerTime().Add(rtt)
	if t.After(maxReasonableTime) {
		log.Fatal("remote time is too far in the future")
	}
	if t.Before(recentCompileTime) {
		log.Fatal("remote time is too far in the past")
	}

	// Set the system clock.
	if err := setClock(t); err != nil {
		log.Fatal(err)
	}
}
開發者ID:ality,項目名稱:go-tlsdate,代碼行數:96,代碼來源:main.go


注:本文中的code/google/com/p/go/net/proxy.Dialer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。