2017-03-10 00:52:32 +08:00
|
|
|
package tests
|
2016-05-17 19:13:37 +08:00
|
|
|
|
|
|
|
import (
|
2017-03-09 02:03:47 +08:00
|
|
|
"bufio"
|
2016-05-17 19:13:37 +08:00
|
|
|
"fmt"
|
2016-06-06 11:43:41 +08:00
|
|
|
"io"
|
2017-05-21 22:42:42 +08:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2016-05-17 19:13:37 +08:00
|
|
|
|
2017-05-21 22:42:42 +08:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
2016-05-17 19:13:37 +08:00
|
|
|
)
|
|
|
|
|
2017-03-10 00:52:32 +08:00
|
|
|
func StartEchoServer() {
|
2017-05-21 22:42:42 +08:00
|
|
|
l, err := frpNet.ListenTcp("127.0.0.1", 10701)
|
2016-05-17 19:13:37 +08: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-28 00:27:30 +08:00
|
|
|
func StartUdpEchoServer() {
|
2017-05-21 22:42:42 +08:00
|
|
|
l, err := frpNet.ListenUDP("127.0.0.1", 10703)
|
2017-03-28 00:27:30 +08: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 22:42:42 +08:00
|
|
|
func StartUnixDomainServer() {
|
|
|
|
unixPath := "/tmp/frp_echo_server.sock"
|
|
|
|
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-09 02:03:47 +08:00
|
|
|
func echoWorker(c net.Conn) {
|
|
|
|
br := bufio.NewReader(c)
|
2016-05-17 19:13:37 +08:00
|
|
|
for {
|
2017-03-09 02:03:47 +08:00
|
|
|
buf, err := br.ReadString('\n')
|
2016-06-06 11:43:41 +08:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2016-05-17 19:13:37 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("echo server read error: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-09 02:03:47 +08:00
|
|
|
c.Write([]byte(buf + "\n"))
|
2016-05-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
}
|