mirror of
https://github.com/ICKelin/opennotr.git
synced 2025-09-26 20:01:13 +08:00
test: add tcp,udp,http echo test
This commit is contained in:
@@ -25,9 +25,9 @@
|
||||
|
||||
**状态: 开发中**
|
||||
|
||||
opennotr是一款开源的内网穿透软件,opennotr基于VPN技术构建虚拟局域网,虚拟局域网网关通过虚拟局域网IP可以访问客户端,进而实现内网穿透。
|
||||
opennotr是一款开源的内网穿透软件,基于VPN技术构建虚拟局域网,处于虚拟局域网内的机器都可以通过虚拟局域网IP可以访问客户端,进而实现内网穿透。
|
||||
|
||||
opennotr支持多种协议,包括http,https,grpc,tcp,udp,为了实现http,https,grpc协议端口复用,opennotr引入了openresty作为网关,从而多个客户端不同域名可以共享http的80,https的443端口,不需要额外的端口。
|
||||
opennotr支持多种协议,包括http,https,grpc,tcp,udp,为了实现http,https,grpc协议端口复用,opennotr引入了openresty作为网关,多个客户端不同域名可以共享http的80,https的443端口,不需要额外的端口。
|
||||
|
||||
opennotr支持定制化插件,我们内置了http, https, grpc, tcp, udp代理,可以覆盖大部分场景,同时,opennotr允许自己开发协议插件,比如说,如果您希望使用apisix来作为前置的网关,您可以开发opennotr的apisix插件,opennotr会把一些基本信息传递给插件,其余功能均由插件自己进行。
|
||||
|
||||
|
@@ -4,7 +4,7 @@ domain: "cloud.dahuizong.com"
|
||||
forwards:
|
||||
- protocol: tcp
|
||||
ports:
|
||||
2222: 22
|
||||
2222: 2222
|
||||
|
||||
- protocol: udp
|
||||
ports:
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
32
opennotrd/test/httpecho/httpclient.go
Normal file
32
opennotrd/test/httpecho/httpclient.go
Normal 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)
|
||||
}
|
||||
}
|
10
opennotrd/test/httpecho/httpserver.go
Normal file
10
opennotrd/test/httpecho/httpserver.go
Normal 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)
|
||||
}
|
30
opennotrd/test/tcpecho/tcpclient.go
Normal file
30
opennotrd/test/tcpecho/tcpclient.go
Normal 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)
|
||||
}
|
||||
}
|
38
opennotrd/test/tcpecho/tcpserver.go
Normal file
38
opennotrd/test/tcpecho/tcpserver.go
Normal 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])
|
||||
}()
|
||||
}
|
||||
}
|
33
opennotrd/test/udpecho/client.go
Normal file
33
opennotrd/test/udpecho/client.go
Normal 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)
|
||||
}
|
||||
}
|
35
opennotrd/test/udpecho/server.go
Normal file
35
opennotrd/test/udpecho/server.go
Normal 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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user