2017-03-09 17:52:32 +01:00
|
|
|
package tests
|
2016-05-17 13:13:37 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-06-06 05:43:41 +02:00
|
|
|
"io"
|
2017-05-21 16:42:42 +02:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2016-05-17 13:13:37 +02:00
|
|
|
|
2017-05-21 16:42:42 +02:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
2016-05-17 13:13:37 +02:00
|
|
|
)
|
|
|
|
|
2017-12-16 16:59:46 +01:00
|
|
|
func StartTcpEchoServer() {
|
2017-12-18 12:35:09 +01:00
|
|
|
l, err := frpNet.ListenTcp("127.0.0.1", TEST_TCP_PORT)
|
2016-05-17 13:13:37 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("echo server listen error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("echo server accept error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go echoWorker(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:27:30 +02:00
|
|
|
func StartUdpEchoServer() {
|
2017-12-18 12:35:09 +01:00
|
|
|
l, err := frpNet.ListenUDP("127.0.0.1", TEST_UDP_PORT)
|
2017-03-27 18:27:30 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("udp echo server listen error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("udp echo server accept error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go echoWorker(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 16:42:42 +02:00
|
|
|
func StartUnixDomainServer() {
|
2017-12-16 16:59:46 +01:00
|
|
|
unixPath := TEST_UNIX_DOMAIN_ADDR
|
2017-05-21 16:42:42 +02:00
|
|
|
os.Remove(unixPath)
|
|
|
|
syscall.Umask(0)
|
|
|
|
l, err := net.Listen("unix", unixPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unix domain server listen error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unix domain server accept error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go echoWorker(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:03:47 +01:00
|
|
|
func echoWorker(c net.Conn) {
|
2017-12-16 16:59:46 +01:00
|
|
|
buf := make([]byte, 2048)
|
|
|
|
|
2016-05-17 13:13:37 +02:00
|
|
|
for {
|
2017-12-16 16:59:46 +01:00
|
|
|
n, err := c.Read(buf)
|
2016-05-17 13:13:37 +02:00
|
|
|
if err != nil {
|
2017-12-16 16:59:46 +01:00
|
|
|
if err == io.EOF {
|
|
|
|
c.Close()
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
fmt.Printf("echo server read error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2016-05-17 13:13:37 +02:00
|
|
|
}
|
|
|
|
|
2017-12-16 16:59:46 +01:00
|
|
|
c.Write(buf[:n])
|
2016-05-17 13:13:37 +02:00
|
|
|
}
|
|
|
|
}
|