github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/mixnet/mixnet_simpleserver/mixnet_simpleserver.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  	"flag"
    21  	"io"
    22  	"log"
    23  	"net"
    24  
    25  	"github.com/jlmucb/cloudproxy/go/apps/mixnet"
    26  )
    27  
    28  var addr = flag.String("addr", ":8123", "Port to listen to.")
    29  var network = flag.String("network", "tcp", "Network protocol for the Tao-delegated mixnet router.")
    30  var cert_file = flag.String("cert", "cert.pem", "Name of the certificate file")
    31  var key_file = flag.String("key", "key.pem", "Name of the key file")
    32  
    33  // A simple TLS server echoes back client's message.
    34  func main() {
    35  	flag.Parse()
    36  
    37  	cert, err := tls.LoadX509KeyPair(*cert_file, *key_file)
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  	config := &tls.Config{
    42  		RootCAs:            x509.NewCertPool(),
    43  		Certificates:       []tls.Certificate{cert},
    44  		InsecureSkipVerify: true,
    45  		ClientAuth:         tls.RequestClientCert,
    46  	}
    47  	l, err := tls.Listen(*network, *addr, config)
    48  	if err != nil {
    49  		log.Fatal(err)
    50  	}
    51  	defer l.Close()
    52  
    53  	for {
    54  		c, err := l.Accept()
    55  		if err != nil {
    56  			log.Fatal(err)
    57  		}
    58  
    59  		go func(c net.Conn) {
    60  			defer c.Close()
    61  			buf := make([]byte, mixnet.MaxMsgBytes+1)
    62  			for {
    63  				bytes, err := c.Read(buf)
    64  				if err != nil {
    65  					if err == io.EOF {
    66  						return
    67  					}
    68  					log.Fatal(err)
    69  				} else {
    70  					_, err := c.Write(buf[:bytes])
    71  					if err != nil {
    72  						log.Fatal(err)
    73  					}
    74  				}
    75  			}
    76  		}(c)
    77  	}
    78  }