mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
add demo
This commit is contained in:
@@ -19,70 +19,20 @@ func main() {
|
||||
},
|
||||
}, {
|
||||
Dest: &net.IPNet{
|
||||
IP: net.ParseIP("172.16.0.0"),
|
||||
Mask: net.CIDRMask(16, 32),
|
||||
IP: net.ParseIP("192.168.0.0"),
|
||||
Mask: net.CIDRMask(24, 32),
|
||||
},
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//bytes := make([]byte, 1000)
|
||||
tunConn, err := listener.Accept()
|
||||
defer tunConn.Close()
|
||||
addr, _ := net.ResolveTCPAddr("tcp", ":1080")
|
||||
tcp, err := net.DialTCP("tcp", nil, addr)
|
||||
tcpConn, err := net.Dial("tcp", ":1080")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
go func() {
|
||||
_, err := io.Copy(tunConn, tcp)
|
||||
if err != nil {
|
||||
log.Info(err)
|
||||
}
|
||||
}()
|
||||
_, err = io.Copy(tcp, tunConn)
|
||||
if err != nil {
|
||||
log.Info(err)
|
||||
}
|
||||
//go func() {
|
||||
// res := make([]byte, 100)
|
||||
// defer tcp.Close()
|
||||
// for {
|
||||
// i, err := tcp.Read(res)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
// if _, err = tunConn.Write(res[:i]); err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// }
|
||||
//}()
|
||||
//for {
|
||||
// read, err := tunConn.Read(bytes)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// fmt.Printf("tun local: %v, tun rmeote: %v\n", tunConn.LocalAddr(), tunConn.RemoteAddr())
|
||||
// header, err := ipv4.ParseHeader(bytes[:read])
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// fmt.Printf("src: %v, dst: %v\n", header.Src, header.Dst)
|
||||
// // port-forward to 10800
|
||||
// if header.Dst.Equal(ip) {
|
||||
// _, err = tunConn.Write(bytes[:read])
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// } else {
|
||||
// fmt.Println("forward it to remote")
|
||||
// _, err = tcp.Write(bytes[:read])
|
||||
// if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
|
||||
// tcp, err = net.DialTCP("tcp", nil, addr)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
go io.Copy(tunConn, tcpConn)
|
||||
io.Copy(tcpConn, tunConn)
|
||||
}
|
||||
|
||||
30
test/pod.yaml
Normal file
30
test/pod.yaml
Normal file
@@ -0,0 +1,30 @@
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: traffic-test
|
||||
labels:
|
||||
app: traffic-test
|
||||
spec:
|
||||
terminationGracePeriodSeconds: 0
|
||||
containers:
|
||||
- name: traffic-test
|
||||
image: naison/kubevpn:v1.1.4
|
||||
imagePullPolicy: IfNotPresent
|
||||
command:
|
||||
- /bin/sh
|
||||
- -c
|
||||
args:
|
||||
- |
|
||||
sysctl net.ipv4.ip_forward=1
|
||||
iptables -F
|
||||
iptables -P INPUT ACCEPT
|
||||
iptables -P FORWARD ACCEPT
|
||||
iptables -t nat -A POSTROUTING -s 223.254.254.0/24 -o eth0 -j MASQUERADE
|
||||
iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
|
||||
tail -f /dev/null
|
||||
securityContext:
|
||||
privileged: true
|
||||
capabilities:
|
||||
add:
|
||||
- NET_ADMIN
|
||||
restartPolicy: Always
|
||||
8
test/run.sh
Normal file
8
test/run.sh
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
kubectl apply -f pod.yaml
|
||||
kubectl wait --for=condition=Ready pod/traffic-test
|
||||
cd ./server && GOARCH=amd64 GOOS=linux go build -o main
|
||||
kubectl cp main traffic-test:/app/main
|
||||
rm -fr main
|
||||
kubectl port-forward pods/traffic-test 1080
|
||||
119
test/server.go
119
test/server.go
@@ -1,119 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/tun"
|
||||
"golang.org/x/net/ipv4"
|
||||
"io"
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var connsMap = &sync.Map{}
|
||||
|
||||
func main() {
|
||||
ip := net.ParseIP("223.254.254.100")
|
||||
listener, err := tun.Listener(tun.Config{
|
||||
Addr: ip.String() + "/24",
|
||||
MTU: 1350,
|
||||
Routes: []tun.IPRoute{{
|
||||
Dest: &net.IPNet{
|
||||
IP: ip,
|
||||
Mask: net.CIDRMask(24, 32),
|
||||
},
|
||||
Gateway: nil,
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
tunConn, _ := listener.Accept()
|
||||
|
||||
localAddr, _ := net.ResolveTCPAddr("tcp", ":1080")
|
||||
tcpListener, _ := net.ListenTCP("tcp", localAddr)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
bytes := make([]byte, 1000)
|
||||
n, err := tunConn.Read(bytes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go func(data []byte) {
|
||||
header, err := ipv4.ParseHeader(data)
|
||||
if err != nil {
|
||||
log.Info(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(header.Src, header.Dst)
|
||||
load, ok := connsMap.Load(header.Dst.To16().String())
|
||||
if !ok {
|
||||
fmt.Println("can not found route ", header.Src, header.Dst)
|
||||
return
|
||||
}
|
||||
_, err = load.(net.Conn).Write(data)
|
||||
if err != nil {
|
||||
log.Info(err)
|
||||
}
|
||||
}(bytes[:n])
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
tcpConn, err := tcpListener.Accept()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go func(tcpConn net.Conn) {
|
||||
defer tcpConn.Close()
|
||||
var b = make([]byte, 1000)
|
||||
n, err := tcpConn.Read(b)
|
||||
if err != nil {
|
||||
log.Info(err)
|
||||
return
|
||||
}
|
||||
header, err := ipv4.ParseHeader(b[:n])
|
||||
if err != nil {
|
||||
log.Info(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(header.Src, header.Dst, "tcp server")
|
||||
connsMap.Store(header.Src.To16().String(), tcpConn)
|
||||
|
||||
if _, err = tunConn.Write(b[:n]); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
_, err = io.Copy(tunConn, tcpConn)
|
||||
if err != nil {
|
||||
log.Info(err)
|
||||
}
|
||||
}(tcpConn)
|
||||
//if err != nil {
|
||||
// fmt.Println(err)
|
||||
// continue
|
||||
//}
|
||||
//t = tcpConn
|
||||
//go func(tcpConn net.Conn) {
|
||||
// b := make([]byte, 1000)
|
||||
// defer tcpConn.Close()
|
||||
// for {
|
||||
// read, err := tcpConn.Read(b)
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
// header, err := ipv4.ParseHeader(b[:read])
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
// fmt.Println(header.Src, header.Dst, "tcp server")
|
||||
// if _, err = tunConn.Write(b[:read]); err != nil {
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
// }
|
||||
//}(tcpConn)
|
||||
}
|
||||
}
|
||||
37
test/server/server.go
Normal file
37
test/server/server.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/tun"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ip := net.ParseIP("223.254.254.100")
|
||||
listener, err := tun.Listener(tun.Config{
|
||||
Addr: ip.String() + "/24",
|
||||
MTU: 1350,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
tunConn, _ := listener.Accept()
|
||||
|
||||
tcpListener, err := net.Listen("tcp", ":1080")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for {
|
||||
tcpConn, err := tcpListener.Accept()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
go func(tcpConn net.Conn) {
|
||||
defer tcpConn.Close()
|
||||
go io.Copy(tunConn, tcpConn)
|
||||
io.Copy(tcpConn, tunConn)
|
||||
}(tcpConn)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user