test: add tcp,udp,http echo test

This commit is contained in:
ICKelin
2021-05-01 15:06:23 +08:00
parent 1cf8e1b627
commit 29616d57a6
9 changed files with 215 additions and 9 deletions

View File

@@ -25,9 +25,9 @@
**状态: 开发中**
opennotr是一款开源的内网穿透软件opennotr基于VPN技术构建虚拟局域网虚拟局域网网关通过虚拟局域网IP可以访问客户端进而实现内网穿透。
opennotr是一款开源的内网穿透软件基于VPN技术构建虚拟局域网处于虚拟局域网内的机器都可以通过虚拟局域网IP可以访问客户端进而实现内网穿透。
opennotr支持多种协议包括httphttpsgrpctcpudp为了实现httphttpsgrpc协议端口复用opennotr引入了openresty作为网关从而多个客户端不同域名可以共享http的80https的443端口不需要额外的端口。
opennotr支持多种协议包括httphttpsgrpctcpudp为了实现httphttpsgrpc协议端口复用opennotr引入了openresty作为网关多个客户端不同域名可以共享http的80https的443端口不需要额外的端口。
opennotr支持定制化插件我们内置了http, https, grpc, tcp, udp代理可以覆盖大部分场景同时opennotr允许自己开发协议插件比如说如果您希望使用apisix来作为前置的网关您可以开发opennotr的apisix插件opennotr会把一些基本信息传递给插件其余功能均由插件自己进行。

View File

@@ -4,7 +4,7 @@ domain: "cloud.dahuizong.com"
forwards:
- protocol: tcp
ports:
2222: 22
2222: 2222
- protocol: udp
ports:

View File

@@ -13,12 +13,33 @@ var pluginMgr = &PluginManager{
plugins: make(map[string]IPlugin),
}
// PluginMeta defineds data that the plugins needs
// these members are filled by server.go
type PluginMeta struct {
Protocol string
From string
To string
Domain string
Ctx interface{} // data pass to plugin
// plugin register protocol
// eg: tcp, udp, http, http2, h2c
Protocol string
// From specific local listener address of plugin
// browser or other clients will connect to this address
// it's no use for restyproxy plugin.
From string
// To specific VIP:port of our VPN peer node.
// For example:
// our VPN virtual lan cidr is 100.64.100.1/24
// the connected VPN client's VPN lan ip is 100.64.100.10/24
// and it wants to export 8080 as http port, so the $To is
// 100.64.100.10:8080
To string
// Domain specific the domain of our VPN peer node.
// It could be empty
Domain string
// Data you want to passto plugin
// Reserve
Ctx interface{}
RecycleSignal chan struct{}
}
@@ -26,10 +47,17 @@ func (item *PluginMeta) identify() string {
return fmt.Sprintf("%s:%s:%s", item.Protocol, item.From, item.Domain)
}
// IPlugin defines proxy plugin API
// IPlugin defines plugin interface
// Plugin should implements the IPlugin
type IPlugin interface {
// Setup calls at the begin of plugin system initialize
// plugin system will pass the raw message to plugin's Setup function
Setup(json.RawMessage) error
// Close a proxy, it may be called by client's connection close
StopProxy(item *PluginMeta)
// Run a proxy, it may be called by client's connection established
RunProxy(item *PluginMeta) error
}

View File

@@ -0,0 +1,32 @@
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func main() {
remoteAddr := flag.String("r", "", "remote address")
flag.Parse()
url := fmt.Sprintf("http://%s/echo", *remoteAddr)
for i := 0; i < 100; i++ {
beg := time.Now()
rsp, err := http.Get(url)
if err != nil {
break
}
cnt, err := ioutil.ReadAll(rsp.Body)
if err != nil {
fmt.Println(err)
break
}
rsp.Body.Close()
fmt.Printf("echo http packet %d %s rtt %dms\n", i+1, string(cnt), time.Now().Sub(beg).Milliseconds())
time.Sleep(time.Second * 1)
}
}

View File

@@ -0,0 +1,10 @@
package main
import "net/http"
func main() {
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.RemoteAddr))
})
http.ListenAndServe(":8080", nil)
}

View File

@@ -0,0 +1,30 @@
package main
import (
"flag"
"fmt"
"net"
"time"
)
func main() {
remoteAddr := flag.String("r", "", "remote address")
flag.Parse()
buf := make([]byte, 1024)
for i := 0; i < 100; i++ {
beg := time.Now()
conn, err := net.Dial("tcp", *remoteAddr)
if err != nil {
fmt.Println(err)
break
}
conn.Write(buf)
conn.Read(buf)
conn.Close()
fmt.Printf("echo tcp packet %d rtt %dms\n", i+1, time.Now().Sub(beg).Milliseconds())
time.Sleep(time.Second * 1)
}
}

View File

@@ -0,0 +1,38 @@
package main
import (
"flag"
"fmt"
"net"
)
func main() {
localAddress := flag.String("l", "", "local address")
flag.Parse()
listener, err := net.Listen("tcp", *localAddress)
if err != nil {
fmt.Println(err)
return
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
break
}
go func() {
defer conn.Close()
buf := make([]byte, 1024)
nr, err := conn.Read(buf)
if err != nil {
fmt.Println(err)
return
}
conn.Write(buf[:nr])
}()
}
}

View File

@@ -0,0 +1,33 @@
package main
import (
"flag"
"fmt"
"net"
"time"
)
func main() {
remoteAddr := flag.String("r", "", "remote address")
flag.Parse()
rconn, err := net.Dial("udp", *remoteAddr)
if err != nil {
fmt.Println(err)
return
}
buf := make([]byte, 1024)
for i := 0; i < 100; i++ {
beg := time.Now()
_, err := rconn.Write(buf)
if err != nil {
fmt.Println(err)
break
}
rconn.Read(buf)
fmt.Printf("echo udp packet %d rtt %dms\n", i+1, time.Now().Sub(beg).Milliseconds())
time.Sleep(time.Second * 1)
}
}

View File

@@ -0,0 +1,35 @@
package main
import (
"flag"
"fmt"
"net"
)
func main() {
localAddress := flag.String("l", "", "local address")
flag.Parse()
laddr, err := net.ResolveUDPAddr("udp", *localAddress)
if err != nil {
fmt.Println(err)
return
}
lconn, err := net.ListenUDP("udp", laddr)
if err != nil {
fmt.Println(err)
return
}
defer lconn.Close()
buf := make([]byte, 64*1024)
for {
nr, raddr, err := lconn.ReadFromUDP(buf)
if err != nil {
fmt.Println(err)
break
}
lconn.WriteToUDP(buf[:nr], raddr)
}
}