-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
faf7c5e
commit b2458c2
Showing
33 changed files
with
3,047 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package cmds | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
cmdutil "k8s.io/kubectl/pkg/cmd/util" | ||
"k8s.io/kubectl/pkg/util/i18n" | ||
"k8s.io/kubectl/pkg/util/templates" | ||
|
||
"github.com/wencaiwulue/kubevpn/pkg/daemon" | ||
"github.com/wencaiwulue/kubevpn/pkg/daemon/rpc" | ||
) | ||
|
||
// CmdSSHDaemon | ||
// 设置本地的IP是223.254.0.1/32 ,记得一定是掩码 32位, | ||
// 这样别的路由不会走到这里来 | ||
func CmdSSHDaemon(_ cmdutil.Factory) *cobra.Command { | ||
var clientIP string | ||
cmd := &cobra.Command{ | ||
Use: "ssh-daemon", | ||
Hidden: true, | ||
Short: "Ssh daemon server", | ||
Long: `Ssh daemon server`, | ||
Example: templates.Examples(i18n.T(` | ||
# SSH daemon server | ||
kubevpn ssh-daemon --client-ip 223.254.0.123/32 | ||
`)), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
err := daemon.StartupDaemon(cmd.Context()) | ||
return err | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := daemon.GetClient(true).SshStart( | ||
cmd.Context(), | ||
&rpc.SshStartRequest{ | ||
ClientIP: clientIP, | ||
}, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprint(os.Stdout, client.ServerIP) | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringVar(&clientIP, "client-ip", "", "Client cidr") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package action | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"sync" | ||
"time" | ||
|
||
"github.com/containernetworking/cni/pkg/types" | ||
log "github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
|
||
"github.com/wencaiwulue/kubevpn/pkg/config" | ||
"github.com/wencaiwulue/kubevpn/pkg/core" | ||
"github.com/wencaiwulue/kubevpn/pkg/daemon/rpc" | ||
"github.com/wencaiwulue/kubevpn/pkg/handler" | ||
"github.com/wencaiwulue/kubevpn/pkg/tun" | ||
"github.com/wencaiwulue/kubevpn/pkg/util" | ||
) | ||
|
||
var _, bits = config.DockerCIDR.Mask.Size() | ||
var DefaultServerIP = (&net.IPNet{IP: config.DockerRouterIP, Mask: net.CIDRMask(bits, bits)}).String() | ||
|
||
var serverIP string | ||
var mux sync.Mutex | ||
var sshCancelFunc context.CancelFunc | ||
|
||
func (svr *Server) SshStart(ctx context.Context, req *rpc.SshStartRequest) (*rpc.SshStartResponse, error) { | ||
mux.Lock() | ||
defer mux.Unlock() | ||
|
||
clientIP, clientCIDR, err := net.ParseCIDR(req.ClientIP) | ||
if err != nil { | ||
log.Errorf("parse cidr error: %v", err) | ||
return nil, err | ||
} | ||
if serverIP == "" { | ||
r := core.Route{ | ||
ServeNodes: []string{ | ||
"tun://127.0.0.1:8422?net=" + DefaultServerIP, | ||
"tcp://:10800", | ||
}, | ||
Retries: 5, | ||
} | ||
servers, err := handler.Parse(r) | ||
if err != nil { | ||
log.Errorf("parse route error: %v", err) | ||
return nil, err | ||
} | ||
ctx, sshCancelFunc = context.WithCancel(context.Background()) | ||
go func() { | ||
err := handler.Run(ctx, servers) | ||
if err != nil { | ||
log.Errorf("run route error: %v", err) | ||
} | ||
}() | ||
|
||
ctx2, cancelF := context.WithCancel(ctx) | ||
wait.UntilWithContext(ctx2, func(ctx context.Context) { | ||
ip, _, _ := net.ParseCIDR(DefaultServerIP) | ||
ok, err := util.Ping(ip.String()) | ||
if err != nil { | ||
} else if ok { | ||
cancelF() | ||
} else { | ||
// todo | ||
cancelF() | ||
} | ||
}, time.Millisecond*20) | ||
if err != nil { | ||
return nil, err | ||
} | ||
serverIP = DefaultServerIP | ||
} | ||
|
||
serverip, _, err := net.ParseCIDR(serverIP) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tunDevice, err := util.GetTunDevice(serverip) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = tun.AddRoutes(tunDevice.Name, types.Route{ | ||
Dst: net.IPNet{ | ||
IP: clientIP, | ||
Mask: clientCIDR.Mask, | ||
}, | ||
GW: nil, | ||
}) | ||
if err != nil { | ||
log.Errorf("add route error: %v", err) | ||
return nil, err | ||
} | ||
|
||
return &rpc.SshStartResponse{ServerIP: serverIP}, nil | ||
} | ||
|
||
func (svr *Server) SshStop(ctx context.Context, req *rpc.SshStopRequest) (*rpc.SshStopResponse, error) { | ||
if sshCancelFunc != nil { | ||
sshCancelFunc() | ||
} | ||
return &rpc.SshStopResponse{}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.