mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
change server port to 8422 from 8421
This commit is contained in:
2
Makefile
2
Makefile
@@ -13,7 +13,7 @@ FOLDER := github.com/wencaiwulue/kubevpn/cmd/kubevpn
|
||||
CONTROL_PLANE_FOLDER := github.com/wencaiwulue/kubevpn/pkg/controlplane/cmd/server
|
||||
|
||||
# Setup the -ldflags option for go build here, interpolate the variable values
|
||||
LDFLAGS=--ldflags "-w -s \
|
||||
LDFLAGS=--ldflags "\
|
||||
-X ${FOLDER}/cmds.Version=${VERSION} \
|
||||
-X ${FOLDER}/cmds.BuildTime=${BUILD_TIME} \
|
||||
-X ${FOLDER}/cmds.GitCommit=${GIT_COMMIT} \
|
||||
|
||||
@@ -3,11 +3,13 @@ package cmds
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
config2 "github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/driver"
|
||||
"github.com/wencaiwulue/kubevpn/pkg"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
@@ -21,7 +23,7 @@ func init() {
|
||||
connectCmd.PersistentFlags().StringArrayVar(&connect.Workloads, "workloads", []string{}, "workloads, like: pods/tomcat, deployment/nginx, replicaset/tomcat...")
|
||||
connectCmd.Flags().StringVar((*string)(&connect.Mode), "mode", string(pkg.Reverse), "default mode is reverse")
|
||||
connectCmd.Flags().StringToStringVarP(&connect.Headers, "headers", "H", map[string]string{}, "headers, format is k=v, like: k1=v1,k2=v2")
|
||||
connectCmd.Flags().BoolVar(&util.Debug, "debug", false, "true/false")
|
||||
connectCmd.Flags().BoolVar(&config2.Debug, "debug", false, "true/false")
|
||||
RootCmd.AddCommand(connectCmd)
|
||||
}
|
||||
|
||||
@@ -33,10 +35,12 @@ var connectCmd = &cobra.Command{
|
||||
if !util.IsAdmin() {
|
||||
util.RunWithElevated()
|
||||
os.Exit(0)
|
||||
} else {
|
||||
go func() { log.Info(http.ListenAndServe("localhost:6060", nil)) }()
|
||||
}
|
||||
},
|
||||
PreRun: func(*cobra.Command, []string) {
|
||||
util.InitLogger(util.Debug)
|
||||
util.InitLogger(config2.Debug)
|
||||
if util.IsWindows() {
|
||||
driver.InstallWireGuardTunDriver()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
config2 "github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/pkg"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
)
|
||||
@@ -13,7 +14,7 @@ var config pkg.Route
|
||||
func init() {
|
||||
ServerCmd.Flags().StringArrayVarP(&config.ServeNodes, "nodeCommand", "L", []string{}, "command needs to be executed")
|
||||
ServerCmd.Flags().StringVarP(&config.ChainNode, "chainCommand", "F", "", "command needs to be executed")
|
||||
ServerCmd.Flags().BoolVar(&util.Debug, "debug", false, "true/false")
|
||||
ServerCmd.Flags().BoolVar(&config2.Debug, "debug", false, "true/false")
|
||||
RootCmd.AddCommand(ServerCmd)
|
||||
}
|
||||
|
||||
@@ -22,7 +23,7 @@ var ServerCmd = &cobra.Command{
|
||||
Short: "serve",
|
||||
Long: `serve`,
|
||||
PreRun: func(*cobra.Command, []string) {
|
||||
util.InitLogger(util.Debug)
|
||||
util.InitLogger(config2.Debug)
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if err := pkg.Start(context.TODO(), config); err != nil {
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/cmd/kubevpn/cmds"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
)
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
log.Println(http.ListenAndServe("localhost:6060", nil))
|
||||
}()
|
||||
_ = cmds.RootCmd.Execute()
|
||||
}
|
||||
|
||||
@@ -1,20 +1,29 @@
|
||||
package util
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const TrafficManager = "kubevpn.traffic.manager"
|
||||
|
||||
var CIDR *net.IPNet
|
||||
var RouterIP net.IP
|
||||
|
||||
const (
|
||||
PodTrafficManager = "kubevpn.traffic.manager"
|
||||
|
||||
SidecarEnvoyProxy = "envoy-proxy"
|
||||
SidecarControlPlane = "control-plane"
|
||||
SidecarEnvoyConfig = "envoy-config"
|
||||
SidecarVPN = "vpn"
|
||||
|
||||
ImageServer = "naison/kubevpn:v2"
|
||||
ImageMesh = "naison/kubevpnmesh:v2"
|
||||
ImageControlPlane = "naison/envoy-xds-server:latest"
|
||||
|
||||
s = "223.254.254.100/24"
|
||||
)
|
||||
|
||||
var CIDR *net.IPNet
|
||||
|
||||
var RouterIP net.IP
|
||||
|
||||
func init() {
|
||||
RouterIP, CIDR, _ = net.ParseCIDR(s)
|
||||
}
|
||||
@@ -23,27 +32,9 @@ func init() {
|
||||
var Debug bool
|
||||
|
||||
var (
|
||||
smallBufferSize = 2 * 1024 // 2KB small buffer
|
||||
mediumBufferSize = 8 * 1024 // 8KB medium buffer
|
||||
largeBufferSize = 32 * 1024 // 32KB large buffer
|
||||
)
|
||||
|
||||
var (
|
||||
SPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, smallBufferSize)
|
||||
},
|
||||
}
|
||||
MPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, mediumBufferSize)
|
||||
},
|
||||
}
|
||||
LPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, largeBufferSize)
|
||||
},
|
||||
}
|
||||
SmallBufferSize = 2 * 1024 // 2KB small buffer
|
||||
MediumBufferSize = 8 * 1024 // 8KB medium buffer
|
||||
LargeBufferSize = 32 * 1024 // 32KB large buffer
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -1,4 +1,4 @@
|
||||
package tlsconfig
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
@@ -12,8 +12,8 @@ var crt embed.FS
|
||||
//go:embed server.key
|
||||
var key embed.FS
|
||||
|
||||
var TlsconfigServer *tls.Config
|
||||
var TlsconfigClient *tls.Config
|
||||
var TlsConfigServer *tls.Config
|
||||
var TlsConfigClient *tls.Config
|
||||
|
||||
func init() {
|
||||
crtBytes, _ := crt.ReadFile("server.crt")
|
||||
@@ -22,11 +22,11 @@ func init() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
TlsconfigServer = &tls.Config{
|
||||
TlsConfigServer = &tls.Config{
|
||||
Certificates: []tls.Certificate{pair},
|
||||
}
|
||||
|
||||
TlsconfigClient = &tls.Config{
|
||||
TlsConfigClient = &tls.Config{
|
||||
Certificates: []tls.Certificate{pair},
|
||||
InsecureSkipVerify: true,
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package tlsconfig
|
||||
package config
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
@@ -16,7 +16,7 @@ func init() {
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
listen, _ := net.Listen("tcp", ":9090")
|
||||
listener := tls.NewListener(listen, TlsconfigServer)
|
||||
listener := tls.NewListener(listen, TlsConfigServer)
|
||||
go func() {
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
@@ -41,7 +41,7 @@ func TestName(t *testing.T) {
|
||||
log.Errorln(err)
|
||||
}
|
||||
|
||||
client := tls.Client(dial, TlsconfigClient)
|
||||
client := tls.Client(dial, TlsConfigClient)
|
||||
client.Write([]byte("hi server"))
|
||||
all, err := io.ReadAll(client)
|
||||
if err != nil {
|
||||
24
core/pool.go
Normal file
24
core/pool.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
SPool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, config.SmallBufferSize)
|
||||
},
|
||||
}
|
||||
MPool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, config.MediumBufferSize)
|
||||
},
|
||||
}
|
||||
LPool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, config.LargeBufferSize)
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -2,8 +2,7 @@ package core
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/wencaiwulue/kubevpn/tlsconfig"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"net"
|
||||
)
|
||||
|
||||
@@ -16,8 +15,8 @@ func TCPTransporter() Transporter {
|
||||
}
|
||||
|
||||
func (tr *tcpTransporter) Dial(addr string) (net.Conn, error) {
|
||||
dialer := &net.Dialer{Timeout: util.DialTimeout}
|
||||
return tls.DialWithDialer(dialer, "tcp", addr, tlsconfig.TlsconfigClient)
|
||||
dialer := &net.Dialer{Timeout: config.DialTimeout}
|
||||
return tls.DialWithDialer(dialer, "tcp", addr, config.TlsConfigClient)
|
||||
}
|
||||
|
||||
type tcpListener struct {
|
||||
@@ -47,6 +46,6 @@ func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
|
||||
return
|
||||
}
|
||||
_ = tc.SetKeepAlive(true)
|
||||
_ = tc.SetKeepAlivePeriod(util.KeepAliveTime)
|
||||
_ = tc.SetKeepAlivePeriod(config.KeepAliveTime)
|
||||
return tc, nil
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
@@ -22,7 +22,7 @@ func (c *fakeUDPTunConnector) ConnectContext(_ context.Context, conn net.Conn, n
|
||||
case "tcp", "tcp4", "tcp6":
|
||||
return nil, fmt.Errorf("%s unsupported", network)
|
||||
}
|
||||
_ = conn.SetDeadline(time.Now().Add(util.ConnectTimeout))
|
||||
_ = conn.SetDeadline(time.Now().Add(config.ConnectTimeout))
|
||||
defer conn.SetDeadline(time.Time{})
|
||||
|
||||
targetAddr, _ := net.ResolveUDPAddr("udp", address)
|
||||
@@ -42,7 +42,7 @@ func (h *fakeUdpHandler) Init(...HandlerOption) {
|
||||
|
||||
func (h *fakeUdpHandler) Handle(ctx context.Context, conn net.Conn) {
|
||||
defer conn.Close()
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tcpserver] %s -> %s\n", conn.RemoteAddr(), conn.LocalAddr())
|
||||
}
|
||||
h.handleUDPTunnel(conn)
|
||||
@@ -59,7 +59,7 @@ func (h *fakeUdpHandler) handleUDPTunnel(conn net.Conn) {
|
||||
return
|
||||
}
|
||||
defer uc.Close()
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tcpserver] udp-tun %s <- %s\n", conn.RemoteAddr(), uc.LocalAddr())
|
||||
}
|
||||
log.Debugf("[tcpserver] udp-tun %s <-> %s", conn.RemoteAddr(), uc.LocalAddr())
|
||||
@@ -72,8 +72,8 @@ func (h *fakeUdpHandler) tunnelServerUDP(cc net.Conn, pc *net.UDPConn) (err erro
|
||||
errChan := make(chan error, 2)
|
||||
|
||||
go func() {
|
||||
b := util.MPool.Get().([]byte)
|
||||
defer util.MPool.Put(b)
|
||||
b := MPool.Get().([]byte)
|
||||
defer MPool.Put(b)
|
||||
|
||||
for {
|
||||
n, err := pc.Read(b)
|
||||
@@ -90,7 +90,7 @@ func (h *fakeUdpHandler) tunnelServerUDP(cc net.Conn, pc *net.UDPConn) (err erro
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tcpserver] udp-tun %s <<< %s length: %d", cc.RemoteAddr(), dgram.Addr(), len(dgram.Data))
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func (h *fakeUdpHandler) tunnelServerUDP(cc net.Conn, pc *net.UDPConn) (err erro
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tcpserver] udp-tun %s >>> %s length: %d", cc.RemoteAddr(), Server8422, len(dgram.Data))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package core
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -20,21 +20,17 @@ func ipToTunRouteKey(ip net.IP) string {
|
||||
}
|
||||
|
||||
type tunHandler struct {
|
||||
options *HandlerOptions
|
||||
routes sync.Map
|
||||
chExit chan struct{}
|
||||
requestChan chan []byte
|
||||
responseChan chan []byte
|
||||
options *HandlerOptions
|
||||
routes sync.Map
|
||||
chExit chan struct{}
|
||||
}
|
||||
|
||||
// TunHandler creates a handler for tun tunnel.
|
||||
func TunHandler(opts ...HandlerOption) Handler {
|
||||
h := &tunHandler{
|
||||
options: &HandlerOptions{},
|
||||
routes: sync.Map{},
|
||||
chExit: make(chan struct{}, 1),
|
||||
requestChan: make(chan []byte, 1000*1000),
|
||||
responseChan: make(chan []byte, 1000*1000),
|
||||
options: &HandlerOptions{},
|
||||
routes: sync.Map{},
|
||||
chExit: make(chan struct{}, 1),
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(h.options)
|
||||
@@ -123,13 +119,13 @@ func (h *tunHandler) findRouteFor(dst net.IP) net.Addr {
|
||||
if v, ok := h.routes.Load(ipToTunRouteKey(dst)); ok {
|
||||
return v.(net.Addr)
|
||||
}
|
||||
for _, route := range h.options.IPRoutes {
|
||||
if route.Dest.Contains(dst) && route.Gateway != nil {
|
||||
if v, ok := h.routes.Load(ipToTunRouteKey(route.Gateway)); ok {
|
||||
return v.(net.Addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
//for _, route := range h.options.IPRoutes {
|
||||
// if route.Dest.Contains(dst) && route.Gateway != nil {
|
||||
// if v, ok := h.routes.Load(ipToTunRouteKey(route.Gateway)); ok {
|
||||
// return v.(net.Addr)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -147,8 +143,8 @@ func (h *tunHandler) transportTun(ctx context.Context, tun net.Conn, conn net.Pa
|
||||
go func() {
|
||||
for ctx.Err() == nil {
|
||||
err := func() error {
|
||||
b := util.SPool.Get().([]byte)
|
||||
defer util.SPool.Put(b)
|
||||
b := SPool.Get().([]byte)
|
||||
defer SPool.Put(b)
|
||||
n, err := tun.Read(b)
|
||||
if err != nil {
|
||||
select {
|
||||
@@ -157,7 +153,6 @@ func (h *tunHandler) transportTun(ctx context.Context, tun net.Conn, conn net.Pa
|
||||
}
|
||||
return err
|
||||
}
|
||||
h.requestChan <- b[:n]
|
||||
return h.processRequest(b[:n], tun, raddr, conn)
|
||||
}()
|
||||
|
||||
@@ -171,15 +166,14 @@ func (h *tunHandler) transportTun(ctx context.Context, tun net.Conn, conn net.Pa
|
||||
go func() {
|
||||
for ctx.Err() == nil {
|
||||
err := func() error {
|
||||
b := util.SPool.Get().([]byte)
|
||||
defer util.SPool.Put(b)
|
||||
b := SPool.Get().([]byte)
|
||||
defer SPool.Put(b)
|
||||
|
||||
n, addr, err := conn.ReadFrom(b)
|
||||
if err != nil && err != shadowaead.ErrShortPacket {
|
||||
return err
|
||||
}
|
||||
h.responseChan <- b[:n]
|
||||
return h.processResponse(b, tun, raddr, addr, conn)
|
||||
return h.processResponse(b[:n], tun, raddr, addr, conn)
|
||||
}()
|
||||
|
||||
if err != nil {
|
||||
@@ -205,7 +199,7 @@ func (h *tunHandler) processResponse(b []byte, tun net.Conn, raddr net.Addr, add
|
||||
log.Debugf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||
return nil
|
||||
}
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tun] %s", header.String())
|
||||
}
|
||||
src, dst = header.Src, header.Dst
|
||||
@@ -215,7 +209,7 @@ func (h *tunHandler) processResponse(b []byte, tun net.Conn, raddr net.Addr, add
|
||||
log.Debugf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||
return nil
|
||||
}
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tun] %s", header.String())
|
||||
}
|
||||
src, dst = header.Src, header.Dst
|
||||
@@ -241,7 +235,7 @@ func (h *tunHandler) processResponse(b []byte, tun net.Conn, raddr net.Addr, add
|
||||
}
|
||||
|
||||
if addr := h.findRouteFor(dst); addr != nil {
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tun] find route: %s -> %s", dst, addr)
|
||||
}
|
||||
_, err := conn.WriteTo(b, addr)
|
||||
@@ -266,7 +260,7 @@ func (h *tunHandler) processRequest(b []byte, tun net.Conn, raddr net.Addr, conn
|
||||
log.Debugf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||
return nil
|
||||
}
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tun] %s", header.String())
|
||||
}
|
||||
src, dst = header.Src, header.Dst
|
||||
@@ -276,7 +270,7 @@ func (h *tunHandler) processRequest(b []byte, tun net.Conn, raddr net.Addr, conn
|
||||
log.Debugf("[tun] %s: %v", tun.LocalAddr(), err)
|
||||
return nil
|
||||
}
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tun] %s", header.String())
|
||||
}
|
||||
src, dst = header.Src, header.Dst
|
||||
@@ -297,7 +291,7 @@ func (h *tunHandler) processRequest(b []byte, tun net.Conn, raddr net.Addr, conn
|
||||
return nil
|
||||
}
|
||||
|
||||
if util.Debug {
|
||||
if config.Debug {
|
||||
log.Debugf("[tun] find route: %s -> %s", dst, addr)
|
||||
}
|
||||
if _, err := conn.WriteTo(b, addr); err != nil {
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
@@ -33,8 +32,8 @@ func (addr *DatagramPacket) Addr() net.Addr {
|
||||
}
|
||||
|
||||
func ReadDatagramPacket(r io.Reader) (*DatagramPacket, error) {
|
||||
b := util.LPool.Get().([]byte)
|
||||
defer util.LPool.Put(b)
|
||||
b := LPool.Get().([]byte)
|
||||
defer LPool.Put(b)
|
||||
_, err := io.ReadFull(r, b[:2])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/dns"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
@@ -73,7 +73,7 @@ func UpdateRefCount(clientset *kubernetes.Clientset, namespace, name string, inc
|
||||
},
|
||||
})
|
||||
_, err = clientset.CoreV1().Pods(namespace).
|
||||
Patch(context.TODO(), util.TrafficManager, types.JSONPatchType, patch, v1.PatchOptions{})
|
||||
Patch(context.TODO(), config.PodTrafficManager, types.JSONPatchType, patch, v1.PatchOptions{})
|
||||
return err
|
||||
}); err != nil {
|
||||
log.Errorf("update ref count error, error: %v", err)
|
||||
@@ -83,8 +83,8 @@ func UpdateRefCount(clientset *kubernetes.Clientset, namespace, name string, inc
|
||||
}
|
||||
|
||||
func cleanUpTrafficManagerIfRefCountIsZero(clientset *kubernetes.Clientset, namespace string) {
|
||||
UpdateRefCount(clientset, namespace, util.TrafficManager, -1)
|
||||
pod, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), util.TrafficManager, v1.GetOptions{})
|
||||
UpdateRefCount(clientset, namespace, config.PodTrafficManager, -1)
|
||||
pod, err := clientset.CoreV1().Pods(namespace).Get(context.TODO(), config.PodTrafficManager, v1.GetOptions{})
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
@@ -98,10 +98,10 @@ func cleanUpTrafficManagerIfRefCountIsZero(clientset *kubernetes.Clientset, name
|
||||
if refCount <= 0 {
|
||||
zero := int64(0)
|
||||
log.Info("refCount is zero, prepare to clean up resource")
|
||||
_ = clientset.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), util.TrafficManager, v1.DeleteOptions{
|
||||
_ = clientset.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), config.PodTrafficManager, v1.DeleteOptions{
|
||||
GracePeriodSeconds: &zero,
|
||||
})
|
||||
_ = clientset.CoreV1().Pods(namespace).Delete(context.TODO(), util.TrafficManager, v1.DeleteOptions{
|
||||
_ = clientset.CoreV1().Pods(namespace).Delete(context.TODO(), config.PodTrafficManager, v1.DeleteOptions{
|
||||
GracePeriodSeconds: &zero,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
errors2 "github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/dns"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -70,7 +71,7 @@ func (c *ConnectOptions) createRemoteInboundPod() (err error) {
|
||||
LocalTunIP: c.localTunIP.IP.String(),
|
||||
InboundPodTunIP: virtualShadowIp.String(),
|
||||
TrafficManagerRealIP: c.routerIP.String(),
|
||||
Route: util.CIDR.String(),
|
||||
Route: config.CIDR.String(),
|
||||
}
|
||||
// TODO OPTIMIZE CODE
|
||||
if c.Mode == Mesh {
|
||||
@@ -95,7 +96,7 @@ func (c *ConnectOptions) DoConnect() (err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
trafficMangerNet := net.IPNet{IP: util.RouterIP, Mask: util.CIDR.Mask}
|
||||
trafficMangerNet := net.IPNet{IP: config.RouterIP, Mask: config.CIDR.Mask}
|
||||
c.routerIP, err = CreateOutboundPod(c.clientset, c.Namespace, trafficMangerNet.String(), c.cidrs)
|
||||
if err != nil {
|
||||
return
|
||||
@@ -134,7 +135,7 @@ func (c *ConnectOptions) portForward(ctx context.Context) error {
|
||||
err := util.PortForwardPod(
|
||||
c.config,
|
||||
c.restclient,
|
||||
util.TrafficManager,
|
||||
config.PodTrafficManager,
|
||||
c.Namespace,
|
||||
"10800:10800",
|
||||
readyChan,
|
||||
@@ -150,7 +151,7 @@ func (c *ConnectOptions) portForward(ctx context.Context) error {
|
||||
}
|
||||
if apierrors.IsNotFound(err) {
|
||||
log.Errorln("can not found outbound pod, try to create one")
|
||||
tm := net.IPNet{IP: util.RouterIP, Mask: util.CIDR.Mask}
|
||||
tm := net.IPNet{IP: config.RouterIP, Mask: config.CIDR.Mask}
|
||||
if _, err = CreateOutboundPod(c.clientset, c.Namespace, tm.String(), c.cidrs); err != nil {
|
||||
log.Errorf("error while create traffic manager, will retry after a snap, err: %v", err)
|
||||
}
|
||||
@@ -181,7 +182,7 @@ func (c *ConnectOptions) startLocalTunServe(ctx context.Context) (err error) {
|
||||
if util.IsWindows() {
|
||||
c.localTunIP.Mask = net.CIDRMask(0, 32)
|
||||
}
|
||||
var list = []string{util.CIDR.String()}
|
||||
var list = []string{config.CIDR.String()}
|
||||
for _, ipNet := range c.cidrs {
|
||||
list = append(list, ipNet.String())
|
||||
}
|
||||
@@ -225,7 +226,7 @@ func (c *ConnectOptions) detectConflictDevice() {
|
||||
}
|
||||
|
||||
func (c *ConnectOptions) setupDNS() {
|
||||
relovConf, err := dns.GetDNSServiceIPFromPod(c.clientset, c.restclient, c.config, util.TrafficManager, c.Namespace)
|
||||
relovConf, err := dns.GetDNSServiceIPFromPod(c.clientset, c.restclient, c.config, config.PodTrafficManager, c.Namespace)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"github.com/cilium/ipam/service/allocator"
|
||||
"github.com/cilium/ipam/service/ipallocator"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
v12 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
@@ -32,13 +32,13 @@ func NewDHCPManager(client v12.ConfigMapInterface, namespace string, cidr *net.I
|
||||
|
||||
// todo optimize dhcp, using mac address, ip and deadline as unit
|
||||
func (d *DHCPManager) InitDHCP() error {
|
||||
configMap, err := d.client.Get(context.Background(), util.TrafficManager, metav1.GetOptions{})
|
||||
configMap, err := d.client.Get(context.Background(), config.PodTrafficManager, metav1.GetOptions{})
|
||||
if err == nil && configMap != nil {
|
||||
return nil
|
||||
}
|
||||
result := &v1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: util.TrafficManager,
|
||||
Name: config.PodTrafficManager,
|
||||
Namespace: d.namespace,
|
||||
Labels: map[string]string{},
|
||||
},
|
||||
@@ -90,7 +90,7 @@ func (d *DHCPManager) ReleaseIpToDHCP(ip *net.IPNet) error {
|
||||
}
|
||||
|
||||
func (d *DHCPManager) updateDHCPConfigMap(f func(*ipallocator.Range) error) error {
|
||||
cm, err := d.client.Get(context.Background(), util.TrafficManager, metav1.GetOptions{})
|
||||
cm, err := d.client.Get(context.Background(), config.PodTrafficManager, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
log.Errorf("failed to get dhcp, err: %v", err)
|
||||
return err
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
)
|
||||
|
||||
const VPN = "vpn"
|
||||
|
||||
func RemoveContainer(spec *v1.PodSpec) {
|
||||
for i := 0; i < len(spec.Containers); i++ {
|
||||
if spec.Containers[i].Name == VPN {
|
||||
if spec.Containers[i].Name == config.SidecarVPN {
|
||||
spec.Containers = append(spec.Containers[:i], spec.Containers[i+1:]...)
|
||||
}
|
||||
}
|
||||
@@ -19,15 +18,15 @@ func RemoveContainer(spec *v1.PodSpec) {
|
||||
func AddContainer(spec *v1.PodSpec, c util.PodRouteConfig) {
|
||||
// remove vpn container if already exist
|
||||
for i := 0; i < len(spec.Containers); i++ {
|
||||
if spec.Containers[i].Name == VPN {
|
||||
if spec.Containers[i].Name == config.SidecarVPN {
|
||||
spec.Containers = append(spec.Containers[:i], spec.Containers[i+1:]...)
|
||||
}
|
||||
}
|
||||
t := true
|
||||
zero := int64(0)
|
||||
spec.Containers = append(spec.Containers, v1.Container{
|
||||
Name: VPN,
|
||||
Image: "naison/kubevpn:v2",
|
||||
Name: config.SidecarVPN,
|
||||
Image: config.ImageServer,
|
||||
Command: []string{"/bin/sh", "-c"},
|
||||
Args: []string{
|
||||
"sysctl net.ipv4.ip_forward=1;" +
|
||||
@@ -38,7 +37,7 @@ func AddContainer(spec *v1.PodSpec, c util.PodRouteConfig) {
|
||||
"iptables -t nat -A POSTROUTING ! -p icmp -j MASQUERADE;" +
|
||||
"sysctl -w net.ipv4.conf.all.route_localnet=1;" +
|
||||
"iptables -t nat -A OUTPUT -o lo ! -p icmp -j DNAT --to-destination " + c.LocalTunIP + ";" +
|
||||
"kubevpn serve -L 'tun://0.0.0.0:8421/" + c.TrafficManagerRealIP + ":8421?net=" + c.InboundPodTunIP + "&route=" + c.Route + "' --debug=true",
|
||||
"kubevpn serve -L 'tun://0.0.0.0:8421/" + c.TrafficManagerRealIP + ":8422?net=" + c.InboundPodTunIP + "&route=" + c.Route + "' --debug=true",
|
||||
},
|
||||
SecurityContext: &v1.SecurityContext{
|
||||
Capabilities: &v1.Capabilities{
|
||||
|
||||
@@ -1,33 +1,27 @@
|
||||
package mesh
|
||||
|
||||
import (
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
)
|
||||
|
||||
const (
|
||||
VPN = "vpn"
|
||||
EnvoyProxy = "envoy-proxy"
|
||||
ControlPlane = "control-plane"
|
||||
EnvoyConfig = "envoy-config"
|
||||
)
|
||||
|
||||
func RemoveContainers(spec *v1.PodTemplateSpec) {
|
||||
for i := 0; i < len(spec.Spec.Volumes); i++ {
|
||||
if spec.Spec.Volumes[i].Name == EnvoyConfig {
|
||||
if spec.Spec.Volumes[i].Name == config.SidecarEnvoyConfig {
|
||||
spec.Spec.Volumes = append(spec.Spec.Volumes[:i], spec.Spec.Volumes[i+1:]...)
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(spec.Spec.Containers); i++ {
|
||||
for j := 0; j < len(spec.Spec.Containers[i].VolumeMounts); j++ {
|
||||
if spec.Spec.Containers[i].VolumeMounts[j].Name == EnvoyConfig {
|
||||
if spec.Spec.Containers[i].VolumeMounts[j].Name == config.SidecarEnvoyConfig {
|
||||
spec.Spec.Containers[i].VolumeMounts = append(spec.Spec.Containers[i].VolumeMounts[:j],
|
||||
spec.Spec.Containers[i].VolumeMounts[j+1:]...)
|
||||
}
|
||||
}
|
||||
if sets.NewString(EnvoyProxy, ControlPlane, VPN).Has(spec.Spec.Containers[i].Name) {
|
||||
if sets.NewString(config.SidecarEnvoyProxy, config.SidecarControlPlane, config.SidecarVPN).Has(spec.Spec.Containers[i].Name) {
|
||||
spec.Spec.Containers = append(spec.Spec.Containers[:i], spec.Spec.Containers[i+1:]...)
|
||||
i--
|
||||
}
|
||||
@@ -37,13 +31,13 @@ func RemoveContainers(spec *v1.PodTemplateSpec) {
|
||||
func AddMeshContainer(spec *v1.PodTemplateSpec, configMapName string, c util.PodRouteConfig) {
|
||||
// remove volume envoyConfig if already exist
|
||||
for i := 0; i < len(spec.Spec.Volumes); i++ {
|
||||
if spec.Spec.Volumes[i].Name == EnvoyConfig {
|
||||
if spec.Spec.Volumes[i].Name == config.SidecarEnvoyConfig {
|
||||
spec.Spec.Volumes = append(spec.Spec.Volumes[:i], spec.Spec.Volumes[i+1:]...)
|
||||
}
|
||||
}
|
||||
// remove envoy proxy containers if already exist
|
||||
for i := 0; i < len(spec.Spec.Containers); i++ {
|
||||
if sets.NewString(EnvoyProxy, ControlPlane, VPN).Has(spec.Spec.Containers[i].Name) {
|
||||
if sets.NewString(config.SidecarEnvoyProxy, config.SidecarControlPlane, config.SidecarVPN).Has(spec.Spec.Containers[i].Name) {
|
||||
spec.Spec.Containers = append(spec.Spec.Containers[:i], spec.Spec.Containers[i+1:]...)
|
||||
i--
|
||||
}
|
||||
@@ -51,7 +45,7 @@ func AddMeshContainer(spec *v1.PodTemplateSpec, configMapName string, c util.Pod
|
||||
zero := int64(0)
|
||||
t := true
|
||||
spec.Spec.Volumes = append(spec.Spec.Volumes, v1.Volume{
|
||||
Name: EnvoyConfig,
|
||||
Name: config.SidecarEnvoyConfig,
|
||||
VolumeSource: v1.VolumeSource{
|
||||
ConfigMap: &v1.ConfigMapVolumeSource{
|
||||
LocalObjectReference: v1.LocalObjectReference{
|
||||
@@ -71,11 +65,11 @@ func AddMeshContainer(spec *v1.PodTemplateSpec, configMapName string, c util.Pod
|
||||
},
|
||||
})
|
||||
spec.Spec.Containers = append(spec.Spec.Containers, v1.Container{
|
||||
Name: VPN,
|
||||
Image: "naison/kubevpn:v2",
|
||||
Name: config.SidecarVPN,
|
||||
Image: config.ImageServer,
|
||||
Command: []string{"/bin/sh", "-c"},
|
||||
Args: []string{
|
||||
"kubevpn serve -L 'tun://0.0.0.0:8421/" + c.TrafficManagerRealIP + ":8421?net=" + c.InboundPodTunIP + "&route=" + c.Route + "' --debug=true",
|
||||
"kubevpn serve -L 'tun://0.0.0.0:8421/" + c.TrafficManagerRealIP + ":8422?net=" + c.InboundPodTunIP + "&route=" + c.Route + "' --debug=true",
|
||||
},
|
||||
SecurityContext: &v1.SecurityContext{
|
||||
Capabilities: &v1.Capabilities{
|
||||
@@ -100,18 +94,18 @@ func AddMeshContainer(spec *v1.PodTemplateSpec, configMapName string, c util.Pod
|
||||
ImagePullPolicy: v1.PullAlways,
|
||||
})
|
||||
spec.Spec.Containers = append(spec.Spec.Containers, v1.Container{
|
||||
Name: EnvoyProxy,
|
||||
Image: "naison/kubevpnmesh:v2",
|
||||
Name: config.SidecarEnvoyProxy,
|
||||
Image: config.ImageMesh,
|
||||
Command: []string{"/bin/sh", "-c"},
|
||||
Args: []string{
|
||||
"sysctl net.ipv4.ip_forward=1;" +
|
||||
"iptables -F;" +
|
||||
"iptables -P INPUT ACCEPT;" +
|
||||
"iptables -P FORWARD ACCEPT;" +
|
||||
"iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80:60000 ! -s 127.0.0.1 ! -d " + util.CIDR.String() + " -j DNAT --to 127.0.0.1:15006;" +
|
||||
"iptables -t nat -A POSTROUTING -p tcp -m tcp --dport 80:60000 ! -s 127.0.0.1 ! -d " + util.CIDR.String() + " -j MASQUERADE;" +
|
||||
"iptables -t nat -A PREROUTING -i eth0 -p udp --dport 80:60000 ! -s 127.0.0.1 ! -d " + util.CIDR.String() + " -j DNAT --to 127.0.0.1:15006;" +
|
||||
"iptables -t nat -A POSTROUTING -p udp -m udp --dport 80:60000 ! -s 127.0.0.1 ! -d " + util.CIDR.String() + " -j MASQUERADE;" +
|
||||
"iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80:60000 ! -s 127.0.0.1 ! -d " + config.CIDR.String() + " -j DNAT --to 127.0.0.1:15006;" +
|
||||
"iptables -t nat -A POSTROUTING -p tcp -m tcp --dport 80:60000 ! -s 127.0.0.1 ! -d " + config.CIDR.String() + " -j MASQUERADE;" +
|
||||
"iptables -t nat -A PREROUTING -i eth0 -p udp --dport 80:60000 ! -s 127.0.0.1 ! -d " + config.CIDR.String() + " -j DNAT --to 127.0.0.1:15006;" +
|
||||
"iptables -t nat -A POSTROUTING -p udp -m udp --dport 80:60000 ! -s 127.0.0.1 ! -d " + config.CIDR.String() + " -j MASQUERADE;" +
|
||||
"envoy -l debug -c /etc/envoy/base-envoy.yaml",
|
||||
},
|
||||
SecurityContext: &v1.SecurityContext{
|
||||
@@ -131,7 +125,7 @@ func AddMeshContainer(spec *v1.PodTemplateSpec, configMapName string, c util.Pod
|
||||
ImagePullPolicy: v1.PullAlways,
|
||||
VolumeMounts: []v1.VolumeMount{
|
||||
{
|
||||
Name: EnvoyConfig,
|
||||
Name: config.SidecarEnvoyConfig,
|
||||
ReadOnly: false,
|
||||
MountPath: "/etc/envoy/",
|
||||
//SubPath: "envoy.yaml",
|
||||
@@ -139,8 +133,8 @@ func AddMeshContainer(spec *v1.PodTemplateSpec, configMapName string, c util.Pod
|
||||
},
|
||||
})
|
||||
spec.Spec.Containers = append(spec.Spec.Containers, v1.Container{
|
||||
Name: ControlPlane,
|
||||
Image: "naison/envoy-xds-server:latest",
|
||||
Name: config.SidecarControlPlane,
|
||||
Image: config.ImageControlPlane,
|
||||
Command: []string{"envoy-xds-server"},
|
||||
Args: []string{"--watchDirectoryFileName", "/etc/envoy-config/envoy-config.yaml"},
|
||||
VolumeMounts: []v1.VolumeMount{
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/pkg/exchange"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
@@ -32,7 +33,7 @@ import (
|
||||
func CreateOutboundPod(clientset *kubernetes.Clientset, namespace string, trafficManagerIP string, nodeCIDR []*net.IPNet) (net.IP, error) {
|
||||
manager, _, err := polymorphichelpers.GetFirstPod(clientset.CoreV1(),
|
||||
namespace,
|
||||
fields.OneTermEqualSelector("app", util.TrafficManager).String(),
|
||||
fields.OneTermEqualSelector("app", config.PodTrafficManager).String(),
|
||||
time.Second*5,
|
||||
func(pods []*v1.Pod) sort.Interface {
|
||||
return sort.Reverse(podutils.ActivePods(pods))
|
||||
@@ -50,7 +51,7 @@ func CreateOutboundPod(clientset *kubernetes.Clientset, namespace string, traffi
|
||||
"iptables -F",
|
||||
"iptables -P INPUT ACCEPT",
|
||||
"iptables -P FORWARD ACCEPT",
|
||||
"iptables -t nat -A POSTROUTING -s " + util.CIDR.String() + " -o eth0 -j MASQUERADE",
|
||||
"iptables -t nat -A POSTROUTING -s " + config.CIDR.String() + " -o eth0 -j MASQUERADE",
|
||||
}
|
||||
for _, ipNet := range nodeCIDR {
|
||||
args = append(args, "iptables -t nat -A POSTROUTING -s "+ipNet.String()+" -o eth0 -j MASQUERADE")
|
||||
@@ -61,17 +62,17 @@ func CreateOutboundPod(clientset *kubernetes.Clientset, namespace string, traffi
|
||||
zero := int64(0)
|
||||
manager = &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: util.TrafficManager,
|
||||
Name: config.PodTrafficManager,
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{"app": util.TrafficManager},
|
||||
Labels: map[string]string{"app": config.PodTrafficManager},
|
||||
Annotations: map[string]string{"ref-count": "1"},
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
RestartPolicy: v1.RestartPolicyAlways,
|
||||
Containers: []v1.Container{
|
||||
{
|
||||
Name: "vpn",
|
||||
Image: "naison/kubevpn:v2",
|
||||
Name: config.SidecarVPN,
|
||||
Image: config.ImageServer,
|
||||
Command: []string{"/bin/sh", "-c"},
|
||||
Args: []string{strings.Join(args, ";")},
|
||||
SecurityContext: &v1.SecurityContext{
|
||||
@@ -118,7 +119,7 @@ func CreateOutboundPod(clientset *kubernetes.Clientset, namespace string, traffi
|
||||
case e := <-watchStream.ResultChan():
|
||||
if podT, ok := e.Object.(*v1.Pod); ok {
|
||||
if phase != podT.Status.Phase {
|
||||
log.Infof("pod %s status is %s", util.TrafficManager, podT.Status.Phase)
|
||||
log.Infof("pod %s status is %s", config.PodTrafficManager, podT.Status.Phase)
|
||||
}
|
||||
if podT.Status.Phase == v1.PodRunning {
|
||||
return net.ParseIP(podT.Status.PodIP), nil
|
||||
@@ -126,7 +127,7 @@ func CreateOutboundPod(clientset *kubernetes.Clientset, namespace string, traffi
|
||||
phase = podT.Status.Phase
|
||||
}
|
||||
case <-time.Tick(time.Minute * 10):
|
||||
return nil, errors.New(fmt.Sprintf("wait pod %s to be ready timeout", util.TrafficManager))
|
||||
return nil, errors.New(fmt.Sprintf("wait pod %s to be ready timeout", config.PodTrafficManager))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -131,7 +132,7 @@ func TestPreCheck(t *testing.T) {
|
||||
}
|
||||
|
||||
func init() {
|
||||
util.InitLogger(util.Debug)
|
||||
util.InitLogger(config.Debug)
|
||||
}
|
||||
|
||||
func TestBackoff(t *testing.T) {
|
||||
|
||||
@@ -5,8 +5,8 @@ import (
|
||||
"crypto/tls"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"github.com/wencaiwulue/kubevpn/core"
|
||||
"github.com/wencaiwulue/kubevpn/tlsconfig"
|
||||
"github.com/wencaiwulue/kubevpn/tun"
|
||||
"net"
|
||||
"strings"
|
||||
@@ -64,7 +64,7 @@ func (r *Route) GenRouters() ([]router, error) {
|
||||
switch node.Transport {
|
||||
case "tcp":
|
||||
tcpListener, _ := core.TCPListener(node.Addr)
|
||||
ln = tls.NewListener(tcpListener, tlsconfig.TlsconfigServer)
|
||||
ln = tls.NewListener(tcpListener, config.TlsConfigServer)
|
||||
case "tun":
|
||||
config := tun.Config{
|
||||
Name: node.Get("name"),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package test
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
88
test/local.go
Normal file
88
test/local.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/tun"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ip := net.ParseIP("223.254.254.102")
|
||||
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),
|
||||
},
|
||||
}, {
|
||||
Dest: &net.IPNet{
|
||||
IP: net.ParseIP("172.16.0.0"),
|
||||
Mask: net.CIDRMask(16, 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)
|
||||
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)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
}
|
||||
119
test/server.go
Normal file
119
test/server.go
Normal file
@@ -0,0 +1,119 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package tun
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -26,7 +26,7 @@ func createTun(cfg Config) (conn net.Conn, itf *net.Interface, err error) {
|
||||
|
||||
mtu := cfg.MTU
|
||||
if mtu <= 0 {
|
||||
mtu = util.DefaultMTU
|
||||
mtu = config.DefaultMTU
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("ifconfig %s inet %s %s mtu %d up", ifce.Name(), cfg.Addr, ip.String(), mtu)
|
||||
|
||||
@@ -3,7 +3,7 @@ package tun
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
@@ -36,7 +36,7 @@ func createTun(cfg Config) (conn net.Conn, itf *net.Interface, err error) {
|
||||
|
||||
mtu := cfg.MTU
|
||||
if mtu <= 0 {
|
||||
mtu = util.DefaultMTU
|
||||
mtu = config.DefaultMTU
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("ip link set dev %s mtu %d", ifce.Name(), mtu)
|
||||
|
||||
@@ -5,7 +5,7 @@ package tun
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -29,7 +29,7 @@ func createTun(cfg Config) (conn net.Conn, itf *net.Interface, err error) {
|
||||
|
||||
mtu := cfg.MTU
|
||||
if mtu <= 0 {
|
||||
mtu = util.DefaultMTU
|
||||
mtu = config.DefaultMTU
|
||||
}
|
||||
|
||||
cmd := fmt.Sprintf("ifconfig %s inet %s mtu %d up", ifce.Name(), cfg.Addr, mtu)
|
||||
|
||||
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
@@ -29,11 +30,11 @@ func AddFirewallRule() {
|
||||
"firewall",
|
||||
"add",
|
||||
"rule",
|
||||
"name=" + TrafficManager,
|
||||
"name=" + config.PodTrafficManager,
|
||||
"dir=in",
|
||||
"action=allow",
|
||||
"enable=yes",
|
||||
"remoteip=" + CIDR.String() + ",LocalSubnet",
|
||||
"remoteip=" + config.CIDR.String() + ",LocalSubnet",
|
||||
}...)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
log.Infof("error while exec command: %s, out: %s, err: %v", cmd.Args, string(out), err)
|
||||
@@ -46,7 +47,7 @@ func FindRule() bool {
|
||||
"firewall",
|
||||
"show",
|
||||
"rule",
|
||||
"name=" + TrafficManager,
|
||||
"name=" + config.PodTrafficManager,
|
||||
}...)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
log.Infof("find route out: %s error: %v", string(out), err)
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"github.com/wencaiwulue/kubevpn/config"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/kubectl/pkg/polymorphichelpers"
|
||||
@@ -479,7 +480,7 @@ func Heartbeats(ctx context.Context) {
|
||||
c2 <- struct{}{}
|
||||
case <-c2:
|
||||
for i := 0; i < 4; i++ {
|
||||
_, _ = Ping(RouterIP.String())
|
||||
_, _ = Ping(config.RouterIP.String())
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
log "github.com/sirupsen/logrus"
|
||||
config2 "github.com/wencaiwulue/kubevpn/config"
|
||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/cli-runtime/pkg/genericclioptions"
|
||||
@@ -42,7 +43,7 @@ func TestShell(t *testing.T) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
out, err := Shell(clientset, restclient, config, TrafficManager, namespace, "cat /etc/resolv.conf | grep nameserver | awk '{print$2}'")
|
||||
out, err := Shell(clientset, restclient, config, config2.PodTrafficManager, namespace, "cat /etc/resolv.conf | grep nameserver | awk '{print$2}'")
|
||||
serviceList, err := clientset.CoreV1().Services(v1.NamespaceSystem).List(context.Background(), v1.ListOptions{
|
||||
FieldSelector: fields.OneTermEqualSelector("metadata.name", "kube-dns").String(),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user