github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/niaucchi4/obfus_test.go (about)

     1  package niaucchi4
     2  
     3  import (
     4  	"crypto/rand"
     5  	"fmt"
     6  	"log"
     7  	"net"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestObfs(t *testing.T) {
    13  	sock1, err := net.ListenPacket("udp", "127.0.0.1:10001")
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  	defer sock1.Close()
    18  	sock2, err := net.ListenPacket("udp", "127.0.0.1:10002")
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	defer sock2.Close()
    23  	cookie := make([]byte, 32)
    24  	rand.Read(cookie)
    25  	// make obfs
    26  	obfs1 := ObfsListen(cookie, sock1, false)
    27  	obfs2 := ObfsListen(cookie, sock2, false)
    28  	// test
    29  	go func() {
    30  		p := make([]byte, 1000)
    31  		for {
    32  			n, addr, err := obfs1.ReadFrom(p)
    33  			if err != nil {
    34  				panic(err)
    35  			}
    36  			log.Println("replying to ping from", addr)
    37  			obfs1.WriteTo(p[:n], addr)
    38  		}
    39  	}()
    40  	go func() {
    41  		p := make([]byte, 1000)
    42  		for {
    43  			n, addr, err := obfs2.ReadFrom(p)
    44  			if err != nil {
    45  				panic(err)
    46  			}
    47  			log.Println("RECV from", addr, "msg", string(p[:n]))
    48  		}
    49  	}()
    50  	o1addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:10001")
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	for i := 0; ; i++ {
    55  		obfs2.WriteTo([]byte(fmt.Sprintf("MSG-%v", i)), o1addr)
    56  		time.Sleep(time.Second)
    57  	}
    58  }