github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/mixnet/mixnet_simpleclient/mixnet_simpleclient.go (about)

     1  // Copyright (c) 2016, Google Inc. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"crypto/tls"
    19  	"crypto/x509"
    20  	"errors"
    21  	"flag"
    22  	"fmt"
    23  	"log"
    24  
    25  	"golang.org/x/net/proxy"
    26  )
    27  
    28  var proxyAddr = flag.String("proxy_addr", "127.0.0.1:8000", "Address and port of the proxy.")
    29  var destination = flag.String("dest_addr", "127.0.0.1:9000", "Destination address.")
    30  var network = flag.String("network", "tcp", "Network protocol for the Tao-delegated mixnet router.")
    31  var id = flag.Int("id", 0, "ID of the client.")
    32  
    33  // Simple client that uses socks5 to write to a TLS server
    34  func main() {
    35  	flag.Parse()
    36  
    37  	dialer, err := proxy.SOCKS5(*network, *proxyAddr, nil, proxy.Direct)
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  
    42  	c, err := dialer.Dial(*network, *destination)
    43  	if err != nil {
    44  		log.Fatal(err)
    45  	}
    46  	defer c.Close()
    47  
    48  	config := &tls.Config{
    49  		RootCAs:            x509.NewCertPool(),
    50  		InsecureSkipVerify: true,
    51  	}
    52  
    53  	tlsConn := tls.Client(c, config)
    54  
    55  	msg := []byte(fmt.Sprintf("My name is %d.", *id))
    56  
    57  	if _, err = tlsConn.Write(msg); err != nil {
    58  		log.Fatal(err)
    59  	}
    60  
    61  	res := make([]byte, len(msg))
    62  	bytes, err := tlsConn.Read(res)
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  
    67  	if string(msg) != string(res[:bytes]) {
    68  		log.Fatal(errors.New("Expected:" + string(msg) + ". Got: " + string(res[:bytes]) + "."))
    69  	} else {
    70  		log.Println("Got: ", string(res[:bytes]))
    71  	}
    72  }