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

     1  // Copyright (c) 2015, 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  	"bufio"
    19  	"flag"
    20  	"log"
    21  	"net"
    22  	"os"
    23  	"os/signal"
    24  	"syscall"
    25  	"time"
    26  
    27  	"github.com/jlmucb/cloudproxy/go/apps/mixnet"
    28  )
    29  
    30  // serveClient runs the SOCKS5 proxy for clients and connects them
    31  // to the mixnet.
    32  func serveClients(routerAddrs []string, proxy *mixnet.ProxyContext) error {
    33  	for {
    34  		c, err := proxy.Accept()
    35  		if err != nil {
    36  			return err
    37  		}
    38  
    39  		go func(c net.Conn) {
    40  			defer c.Close()
    41  			err := proxy.ServeClient(c, routerAddrs, c.(*mixnet.SocksConn).DestinationAddr())
    42  			if err != nil {
    43  				log.Fatal(err)
    44  			}
    45  		}(c)
    46  	}
    47  }
    48  
    49  // Command line arguments.
    50  var (
    51  	network         = flag.String("network", "tcp", "Network protocol for the mixnet proxy and router.")
    52  	configPath      = flag.String("config", "tao.config", "Path to domain configuration file.")
    53  	timeoutDuration = flag.String("timeout", "10s", "Timeout on TCP connections, e.g. \"10s\".")
    54  	hopCount        = flag.Int("hops", mixnet.DefaultHopCount, "Number of hops in the circuit")
    55  	proxyAddr       = flag.String("addr", ":1080", "Address and port to listen to client's connections.")
    56  
    57  	directories = flag.String("dirs", "directories", "File containing addresses of directories.")
    58  
    59  	//only used for testing, where users pick the circuit
    60  	circuit = flag.String("circuit", "", "A file with pre-built circuit.")
    61  )
    62  
    63  func main() {
    64  	flag.Parse()
    65  	timeout, err := time.ParseDuration(*timeoutDuration)
    66  	if err != nil {
    67  		log.Fatalln("proxy: failed to parse timeout duration:", err)
    68  	}
    69  
    70  	f, err := os.Open(*directories)
    71  	if err != nil {
    72  		log.Fatal(err)
    73  	}
    74  	scan := bufio.NewScanner(f)
    75  	dirs := []string{}
    76  	for scan.Scan() {
    77  		dirs = append(dirs, scan.Text())
    78  	}
    79  
    80  	proxy, err := mixnet.NewProxyContext(*configPath, *network, *proxyAddr, dirs, *hopCount, timeout)
    81  	if err != nil {
    82  		log.Fatalln("failed to configure proxy:", err)
    83  	}
    84  	defer proxy.Close()
    85  
    86  	sigs := make(chan os.Signal, 1)
    87  	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
    88  	go func() {
    89  		sig := <-sigs
    90  		proxy.Close()
    91  		log.Println("proxy: closing on signal:", sig)
    92  		signo := int(sig.(syscall.Signal))
    93  		os.Exit(0x80 + signo)
    94  	}()
    95  
    96  	if *circuit == "" {
    97  		if err = serveClients(nil, proxy); err != nil {
    98  			log.Fatalln("proxy: error while serving:", err)
    99  		}
   100  	} else {
   101  		f, err := os.Open(*circuit)
   102  		if err != nil {
   103  			log.Fatal(err)
   104  		}
   105  		scan := bufio.NewScanner(f)
   106  		routers := []string{}
   107  		for scan.Scan() {
   108  			routers = append(routers, scan.Text())
   109  		}
   110  		if err = serveClients(routers, proxy); err != nil {
   111  			log.Fatalln("proxy: error while serving:", err)
   112  		}
   113  	}
   114  }