github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/mixnet/socks5_test.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 mixnet
    16  
    17  import (
    18  	"crypto/tls"
    19  	"crypto/x509"
    20  	"io/ioutil"
    21  	"os"
    22  	"path"
    23  	"testing"
    24  
    25  	netproxy "golang.org/x/net/proxy"
    26  )
    27  
    28  // Run proxy server.
    29  func runSocksServerOne(proxy *ProxyContext, rAddrs []string, ch chan<- testResult, exitKey *[32]byte) {
    30  	c, err := proxy.Accept()
    31  	if err != nil {
    32  		ch <- testResult{err, nil}
    33  		return
    34  	}
    35  	defer c.Close()
    36  	addr := c.(*SocksConn).dstAddr
    37  
    38  	circuit := make([]string, len(rAddrs))
    39  	copy(circuit, rAddrs)
    40  	_, id, err := proxy.CreateCircuit(circuit, addr)
    41  	if err != nil {
    42  		ch <- testResult{err, nil}
    43  		return
    44  	}
    45  
    46  	proxy.clients[id] = c
    47  
    48  	if err = proxy.HandleClient(id); err != nil {
    49  		ch <- testResult{err, nil}
    50  		return
    51  	}
    52  
    53  	if err = proxy.DestroyCircuit(id); err != nil {
    54  		ch <- testResult{err, nil}
    55  		return
    56  	}
    57  
    58  	ch <- testResult{err, []byte(addr)}
    59  }
    60  
    61  // Run a proxy server that accepts more than one message
    62  func runSocksServer(proxy *ProxyContext, rAddrs []string, ch chan<- testResult, exitKey *[32]byte) {
    63  	c, err := proxy.Accept()
    64  	if err != nil {
    65  		ch <- testResult{err, nil}
    66  		return
    67  	}
    68  	defer c.Close()
    69  	addr := c.(*SocksConn).dstAddr
    70  
    71  	circuit := make([]string, len(rAddrs))
    72  	copy(circuit, rAddrs)
    73  
    74  	ch <- testResult{proxy.ServeClient(c, circuit, addr), []byte(addr)}
    75  }
    76  
    77  // Connect to a destination through a mixnet proxy, send a message,
    78  // and wait for a response.
    79  func runSocksClient(pAddr, dAddr string, msg []byte) testResult {
    80  	dialer, err := netproxy.SOCKS5(network, pAddr, nil, netproxy.Direct)
    81  	if err != nil {
    82  		return testResult{err, nil}
    83  	}
    84  
    85  	c, err := dialer.Dial(network, dAddr)
    86  	if err != nil {
    87  		return testResult{err, nil}
    88  	}
    89  	defer c.Close()
    90  
    91  	if _, err = c.Write(msg); err != nil {
    92  		return testResult{err, nil}
    93  	}
    94  
    95  	bytes, err := c.Read(msg)
    96  	if err != nil {
    97  		return testResult{err, nil}
    98  	}
    99  
   100  	return testResult{nil, msg[:bytes]}
   101  }
   102  
   103  // Connect to a destination through a mixnet proxy, send a message,
   104  // and wait for a response.
   105  func runTLSClient(pAddr, dAddr string, msg []byte) testResult {
   106  	dialer, err := netproxy.SOCKS5(network, pAddr, nil, netproxy.Direct)
   107  	if err != nil {
   108  		return testResult{err, nil}
   109  	}
   110  
   111  	c, err := dialer.Dial(network, dAddr)
   112  	if err != nil {
   113  		return testResult{err, nil}
   114  	}
   115  	defer c.Close()
   116  
   117  	config := &tls.Config{
   118  		RootCAs:            x509.NewCertPool(),
   119  		InsecureSkipVerify: true,
   120  	}
   121  
   122  	tlsConn := tls.Client(c, config)
   123  
   124  	if _, err = tlsConn.Write(msg); err != nil {
   125  		return testResult{err, nil}
   126  	}
   127  
   128  	bytes, err := tlsConn.Read(msg)
   129  	if err != nil {
   130  		return testResult{err, nil}
   131  	}
   132  
   133  	return testResult{nil, msg[:bytes]}
   134  }
   135  
   136  // Test the SOCKS proxy server.
   137  func TestSocks(t *testing.T) {
   138  	tempDir, err := ioutil.TempDir("", "test_socks")
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	d, err := makeTrivialDomain(tempDir)
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  
   147  	proxy, err := makeProxyContext(localAddr, nil, 1, d)
   148  	if err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	defer proxy.Close()
   152  	defer os.RemoveAll(path.Base(d.ConfigPath))
   153  	proxyAddr := proxy.listener.Addr().String()
   154  
   155  	ch := make(chan testResult)
   156  	go func() {
   157  		c, err := proxy.Accept()
   158  		if err != nil {
   159  			ch <- testResult{err, nil}
   160  			return
   161  		}
   162  		defer c.Close()
   163  		ch <- testResult{nil, []byte(c.(*SocksConn).dstAddr)}
   164  	}()
   165  
   166  	dialer, err := netproxy.SOCKS5(network, proxyAddr, nil, netproxy.Direct)
   167  	if err != nil {
   168  		t.Error(err)
   169  	}
   170  
   171  	// The value of dstAddr doesn't matter here because the client never
   172  	// tries to send anything to it.
   173  	c, err := dialer.Dial(network, "127.0.0.1:1234")
   174  	if err != nil {
   175  		t.Error(err)
   176  	}
   177  	defer c.Close()
   178  
   179  	res := <-ch
   180  	if res.err != nil {
   181  		t.Error(res.err)
   182  	} else {
   183  		t.Log("server got:", string(res.msg))
   184  	}
   185  }