github.com/codingeasygo/util@v0.0.0-20231206062002-1ce2f004b7d9/proxy/socks/socks_test.go (about)

     1  package socks
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/binary"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"net"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/codingeasygo/util/xio"
    16  )
    17  
    18  func TestProxy(t *testing.T) {
    19  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    20  		fmt.Fprintf(w, "abc")
    21  	}))
    22  	server := NewServer()
    23  	_, err := server.Start("tcp", ":8011")
    24  	if err != nil {
    25  		t.Error(err)
    26  		return
    27  	}
    28  	{ //CONNECT
    29  		client := http.Client{
    30  			Transport: &http.Transport{
    31  				Dial: func(network, address string) (conn net.Conn, err error) {
    32  					conn, err = Dial(":8011", address)
    33  					return
    34  				},
    35  			},
    36  		}
    37  		{ //ok
    38  			resp, err := client.Get(ts.URL)
    39  			if err != nil {
    40  				t.Error(err)
    41  				return
    42  			}
    43  			data, _ := ioutil.ReadAll(resp.Body)
    44  			resp.Body.Close()
    45  			if string(data) != "abc" {
    46  				t.Error("error")
    47  				return
    48  			}
    49  		}
    50  		{ //error
    51  			_, err := client.Get("http://127.0.0.1:233")
    52  			if err == nil {
    53  				t.Error(err)
    54  				return
    55  			}
    56  		}
    57  	}
    58  	{ //ERROR
    59  		conn, _ := net.Dial("tcp", ":8011")
    60  		time.Sleep(10 * time.Millisecond)
    61  		conn.Close()
    62  		Dial("127.0.0.1:233", "")
    63  	}
    64  	server.Stop()
    65  	go func() {
    66  		server.Run(":8011")
    67  	}()
    68  	time.Sleep(10 * time.Millisecond)
    69  	server.Stop()
    70  }
    71  
    72  func proxyDial(t *testing.T, remote string, port uint16) {
    73  	conn, err := net.Dial("tcp", "localhost:2081")
    74  	if err != nil {
    75  		t.Error(err)
    76  		return
    77  	}
    78  	defer conn.Close()
    79  	buf := make([]byte, 1024*64)
    80  	proxyReader := bufio.NewReader(conn)
    81  	_, err = conn.Write([]byte{0x05, 0x01, 0x00})
    82  	if err != nil {
    83  		return
    84  	}
    85  	err = xio.FullBuffer(proxyReader, buf, 2, nil)
    86  	if err != nil {
    87  		return
    88  	}
    89  	if buf[0] != 0x05 || buf[1] != 0x00 {
    90  		return
    91  	}
    92  	buf[0], buf[1], buf[2], buf[3] = 0x05, 0x01, 0x00, 0x03
    93  	buf[4] = byte(len(remote))
    94  	copy(buf[5:], []byte(remote))
    95  	binary.BigEndian.PutUint16(buf[5+len(remote):], port)
    96  	_, err = conn.Write(buf[:buf[4]+7])
    97  	if err != nil {
    98  		return
    99  	}
   100  	readed, err := proxyReader.Read(buf)
   101  	if err != nil {
   102  		return
   103  	}
   104  	fmt.Printf("->%v\n", buf[0:readed])
   105  	fmt.Fprintf(conn, "abc")
   106  }
   107  
   108  func proxyDial2(t *testing.T, remote string, port uint16) {
   109  	conn, err := net.Dial("tcp", "localhost:2081")
   110  	if err != nil {
   111  		t.Error(err)
   112  		return
   113  	}
   114  	defer conn.Close()
   115  	buf := make([]byte, 1024*64)
   116  	proxyReader := bufio.NewReader(conn)
   117  	_, err = conn.Write([]byte{0x05, 0x01, 0x00})
   118  	if err != nil {
   119  		return
   120  	}
   121  	err = xio.FullBuffer(proxyReader, buf, 2, nil)
   122  	if err != nil {
   123  		return
   124  	}
   125  	if buf[0] != 0x05 || buf[1] != 0x00 {
   126  		return
   127  	}
   128  	buf[0], buf[1], buf[2], buf[3] = 0x05, 0x01, 0x00, 0x13
   129  	buf[4] = byte(len(remote))
   130  	copy(buf[5:], []byte(remote))
   131  	binary.BigEndian.PutUint16(buf[5+len(remote):], port)
   132  	_, err = conn.Write(buf[:buf[4]+7])
   133  	if err != nil {
   134  		return
   135  	}
   136  	readed, err := proxyReader.Read(buf)
   137  	if err != nil {
   138  		return
   139  	}
   140  	fmt.Printf("->%v\n", buf[0:readed])
   141  	fmt.Fprintf(conn, "abc")
   142  }
   143  
   144  func proxyDialIP(t *testing.T, bys []byte, port uint16) {
   145  	conn, err := net.Dial("tcp", "localhost:2081")
   146  	if err != nil {
   147  		t.Error(err)
   148  		return
   149  	}
   150  	defer conn.Close()
   151  	buf := make([]byte, 1024*64)
   152  	proxyReader := bufio.NewReader(conn)
   153  	_, err = conn.Write([]byte{0x05, 0x01, 0x00})
   154  	if err != nil {
   155  		return
   156  	}
   157  	err = xio.FullBuffer(proxyReader, buf, 2, nil)
   158  	if err != nil {
   159  		return
   160  	}
   161  	if buf[0] != 0x05 || buf[1] != 0x00 {
   162  		return
   163  	}
   164  	buf[0], buf[1], buf[2], buf[3] = 0x05, 0x01, 0x00, 0x01
   165  	copy(buf[4:], bys)
   166  	binary.BigEndian.PutUint16(buf[8:], port)
   167  	_, err = conn.Write(buf[:10])
   168  	if err != nil {
   169  		return
   170  	}
   171  	readed, err := proxyReader.Read(buf)
   172  	if err != nil {
   173  		return
   174  	}
   175  	fmt.Printf("->%v\n", buf[0:readed])
   176  }
   177  
   178  type CodableErr struct {
   179  	Err error
   180  }
   181  
   182  func (c *CodableErr) Error() string {
   183  	return c.Err.Error()
   184  }
   185  
   186  func (c *CodableErr) Code() byte {
   187  	return 0x01
   188  }
   189  
   190  func TestSocksProxy(t *testing.T) {
   191  	proxy := NewServer()
   192  	proxy.Dialer = xio.PiperDialerF(func(uri string, bufferSize int) (raw xio.Piper, err error) {
   193  		raw = xio.NewEchoPiper(bufferSize)
   194  		return
   195  	})
   196  	go func() {
   197  		proxy.Run(":2081")
   198  	}()
   199  	proxyDial(t, "localhost", 80)
   200  	proxyDial2(t, "localhost:80", 0)
   201  	proxyDial(t, "localhost", 81)
   202  	proxyDialIP(t, make([]byte, 4), 80)
   203  	// proxyDialIPv6(t, make([]byte, 16), 80)
   204  	{ //test error
   205  		//
   206  		conn, conb, _ := xio.CreatePipedConn()
   207  		go proxy.ProcConn(conb)
   208  		conn.Close()
   209  		//
   210  		conn, conb, _ = xio.CreatePipedConn()
   211  		go proxy.ProcConn(conb)
   212  		conn.Write([]byte{0x00, 0x00})
   213  		conn.Close()
   214  		//
   215  		conn, conb, _ = xio.CreatePipedConn()
   216  		go proxy.ProcConn(conb)
   217  		conn.Write([]byte{0x05, 0x01})
   218  		conn.Close()
   219  		//
   220  		conn, conb, _ = xio.CreatePipedConn()
   221  		go proxy.ProcConn(conb)
   222  		conn.Write([]byte{0x05, 0x01, 0x00})
   223  		conn.Close()
   224  		//
   225  		conn, conb, _ = xio.CreatePipedConn()
   226  		go proxy.ProcConn(conb)
   227  		conn.Write([]byte{0x05, 0x01, 0x00})
   228  		conn.Read(make([]byte, 1024))
   229  		conn.Close()
   230  		//
   231  		conn, conb, _ = xio.CreatePipedConn()
   232  		go proxy.ProcConn(conb)
   233  		conn.Write([]byte{0x05, 0x01, 0x00})
   234  		conn.Read(make([]byte, 1024))
   235  		conn.Write([]byte{0x00, 0x01, 0x00, 0x00, 0x00})
   236  		conn.Close()
   237  		//
   238  		conn, conb, _ = xio.CreatePipedConn()
   239  		go proxy.ProcConn(conb)
   240  		conn.Write([]byte{0x05, 0x01, 0x00})
   241  		conn.Read(make([]byte, 1024))
   242  		buf := []byte{0x05, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x010}
   243  		binary.BigEndian.PutUint16(buf[8:], 80)
   244  		conn.Write(buf)
   245  		conn.Close()
   246  		time.Sleep(time.Second)
   247  	}
   248  	proxy.Stop()
   249  }
   250  
   251  func TestDialType(t *testing.T) {
   252  	proxy := NewServer()
   253  	proxy.Dialer = xio.PiperDialerF(func(uri string, bufferSize int) (raw xio.Piper, err error) {
   254  		fmt.Printf("--->%v\n", uri)
   255  		raw = xio.NewEchoPiper(bufferSize)
   256  		return
   257  	})
   258  	go func() {
   259  		proxy.Run(":2081")
   260  	}()
   261  	_, err := DialType("127.0.0.1:2081", 0x05, "xx://xxx")
   262  	if err != nil {
   263  		t.Error(err)
   264  		return
   265  	}
   266  	proxy.Stop()
   267  }
   268  
   269  func TestDialError(t *testing.T) {
   270  	proxy := NewServer()
   271  	proxy.Start("tcp", ":2081")
   272  
   273  	///
   274  	proxy.Dialer = xio.PiperDialerF(func(uri string, bufferSize int) (raw xio.Piper, err error) {
   275  		err = fmt.Errorf("test error")
   276  		return
   277  	})
   278  	_, err := DialType("127.0.0.1:2081", 0x05, "xx://xxx")
   279  	if err == nil || err.Error() != "test error" {
   280  		t.Error(err)
   281  		return
   282  	}
   283  
   284  	//
   285  	proxy.Dialer = xio.PiperDialerF(func(uri string, bufferSize int) (raw xio.Piper, err error) {
   286  		err = fmt.Errorf("%v", hex.EncodeToString(make([]byte, 4096)))
   287  		return
   288  	})
   289  	_, err = DialType("127.0.0.1:2081", 0x05, "xx://xxx")
   290  	if err == nil {
   291  		t.Error(err)
   292  		return
   293  	}
   294  
   295  	//
   296  	proxy.Stop()
   297  }