mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
optimize code
This commit is contained in:
@@ -4,13 +4,12 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"net"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrEmptyChain is an error that implies the chain is empty.
|
||||
ErrEmptyChain = errors.New("empty chain")
|
||||
// ErrorEmptyChain is an error that implies the chain is empty.
|
||||
ErrorEmptyChain = errors.New("empty chain")
|
||||
)
|
||||
|
||||
// Chain is a proxy chain that holds a list of proxy node groups.
|
||||
@@ -58,18 +57,7 @@ func (c *Chain) dial(ctx context.Context, network, address string) (net.Conn, er
|
||||
}
|
||||
|
||||
if c.IsEmpty() {
|
||||
switch network {
|
||||
case "udp", "udp4", "udp6":
|
||||
if address == "" {
|
||||
return net.ListenUDP(network, nil)
|
||||
}
|
||||
default:
|
||||
}
|
||||
d := &net.Dialer{
|
||||
Timeout: util.DialTimeout,
|
||||
// LocalAddr: laddr, // TODO: optional local address
|
||||
}
|
||||
return d.DialContext(ctx, network, ipAddr)
|
||||
return nil, ErrorEmptyChain
|
||||
}
|
||||
|
||||
conn, err := c.getConn(ctx)
|
||||
@@ -115,7 +103,7 @@ func (c *Chain) Conn() (conn net.Conn, err error) {
|
||||
// getConn obtains a connection to the last node of the chain.
|
||||
func (c *Chain) getConn(_ context.Context) (conn net.Conn, err error) {
|
||||
if c.IsEmpty() {
|
||||
err = ErrEmptyChain
|
||||
err = ErrorEmptyChain
|
||||
return
|
||||
}
|
||||
cc, err := c.Node().Client.Dial(c.Node().Addr)
|
||||
|
||||
@@ -14,7 +14,7 @@ type Handler interface {
|
||||
// HandlerOptions describes the options for Handler.
|
||||
type HandlerOptions struct {
|
||||
Chain *Chain
|
||||
Node Node
|
||||
Node *Node
|
||||
IPRoutes []tun.IPRoute
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func ChainHandlerOption(chain *Chain) HandlerOption {
|
||||
}
|
||||
|
||||
// NodeHandlerOption set the server node for server handler.
|
||||
func NodeHandlerOption(node Node) HandlerOption {
|
||||
func NodeHandlerOption(node *Node) HandlerOption {
|
||||
return func(opts *HandlerOptions) {
|
||||
opts.Node = node
|
||||
}
|
||||
|
||||
33
core/node.go
33
core/node.go
@@ -2,15 +2,13 @@ package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidNode is an error that implies the node is invalid.
|
||||
ErrInvalidNode = errors.New("invalid node")
|
||||
ErrorInvalidNode = errors.New("invalid node")
|
||||
)
|
||||
|
||||
// Node is a proxy node, mainly used to construct a proxy chain.
|
||||
@@ -18,29 +16,27 @@ type Node struct {
|
||||
Addr string
|
||||
Protocol string
|
||||
Transport string
|
||||
Remote string // remote address, used by tcp/udp port forwarding
|
||||
url *url.URL // raw url
|
||||
Remote string // remote address, used by tcp/udp port forwarding
|
||||
Values url.Values
|
||||
Client *Client
|
||||
}
|
||||
|
||||
// ParseNode parses the node info.
|
||||
// The proxy node string pattern is [scheme://][user:pass@host]:port.
|
||||
func ParseNode(s string) (node Node, err error) {
|
||||
func ParseNode(s string) (node *Node, err error) {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return Node{}, ErrInvalidNode
|
||||
return nil, ErrorInvalidNode
|
||||
}
|
||||
u, err := url.Parse(s)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
node = Node{
|
||||
node = &Node{
|
||||
Addr: u.Host,
|
||||
Remote: strings.Trim(u.EscapedPath(), "/"),
|
||||
Values: u.Query(),
|
||||
url: u,
|
||||
}
|
||||
|
||||
u.RawQuery = ""
|
||||
@@ -54,7 +50,7 @@ func ParseNode(s string) (node Node, err error) {
|
||||
node.Protocol = "tcp"
|
||||
node.Transport = "tcp"
|
||||
default:
|
||||
return Node{}, ErrInvalidNode
|
||||
return nil, ErrorInvalidNode
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -64,25 +60,8 @@ func (node *Node) Get(key string) string {
|
||||
return node.Values.Get(key)
|
||||
}
|
||||
|
||||
// GetBool converts node parameter value to bool.
|
||||
func (node *Node) GetBool(key string) bool {
|
||||
b, _ := strconv.ParseBool(node.Values.Get(key))
|
||||
return b
|
||||
}
|
||||
|
||||
// GetInt converts node parameter value to int.
|
||||
func (node *Node) GetInt(key string) int {
|
||||
n, _ := strconv.Atoi(node.Get(key))
|
||||
return n
|
||||
}
|
||||
|
||||
func (node Node) String() string {
|
||||
var scheme string
|
||||
if node.url != nil {
|
||||
scheme = node.url.Scheme
|
||||
}
|
||||
if scheme == "" {
|
||||
scheme = fmt.Sprintf("%s+%s", node.Protocol, node.Transport)
|
||||
}
|
||||
return fmt.Sprintf("%s://%s", scheme, node.Addr)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/wencaiwulue/kubevpn/tun"
|
||||
"k8s.io/client-go/util/retry"
|
||||
"net"
|
||||
"time"
|
||||
@@ -12,7 +11,7 @@ import (
|
||||
|
||||
// Server is a proxy server.
|
||||
type Server struct {
|
||||
Listener tun.Listener
|
||||
Listener net.Listener
|
||||
Handler Handler
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package core
|
||||
import (
|
||||
"crypto/tls"
|
||||
"github.com/wencaiwulue/kubevpn/tlsconfig"
|
||||
"github.com/wencaiwulue/kubevpn/tun"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"net"
|
||||
)
|
||||
@@ -26,7 +25,7 @@ type tcpListener struct {
|
||||
}
|
||||
|
||||
// TCPListener creates a Listener for TCP proxy server.
|
||||
func TCPListener(addr string) (tun.Listener, error) {
|
||||
func TCPListener(addr string) (net.Listener, error) {
|
||||
laddr, err := net.ResolveTCPAddr("tcp", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -18,7 +18,7 @@ func SetupDNS(config *miekgdns.ClientConfig) error {
|
||||
}
|
||||
cmd := exec.Command("systemd-resolve", []string{
|
||||
"--set-dns",
|
||||
config,
|
||||
config.Servers[0],
|
||||
"--interface",
|
||||
tunName,
|
||||
"--set-domain=" + config.Search[0],
|
||||
|
||||
6
main.go
6
main.go
@@ -1,14 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/wencaiwulue/kubevpn/cmd"
|
||||
"github.com/wencaiwulue/kubevpn/util"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if !util.IsAdmin() {
|
||||
util.RunWithElevated()
|
||||
} else {
|
||||
go func() {
|
||||
log.Println(http.ListenAndServe("localhost:6060", nil))
|
||||
}()
|
||||
_ = cmd.RootCmd.Execute()
|
||||
}
|
||||
}
|
||||
|
||||
21
pkg/route.go
21
pkg/route.go
@@ -36,15 +36,13 @@ func parseChainNode(ns string) (*core.Node, error) {
|
||||
Connector: core.UDPOverTCPTunnelConnector(),
|
||||
Transporter: core.TCPTransporter(),
|
||||
}
|
||||
return &node, nil
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func (r *Route) GenRouters() ([]router, error) {
|
||||
chain, err := r.parseChain()
|
||||
if err != nil {
|
||||
if !errors.Is(err, core.ErrInvalidNode) {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil && !errors.Is(err, core.ErrorInvalidNode) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
routers := make([]router, 0, len(r.ServeNodes))
|
||||
@@ -62,23 +60,20 @@ func (r *Route) GenRouters() ([]router, error) {
|
||||
}
|
||||
}
|
||||
|
||||
var ln tun.Listener
|
||||
var ln net.Listener
|
||||
switch node.Transport {
|
||||
case "tcp":
|
||||
tcpListener, _ := core.TCPListener(node.Addr)
|
||||
ln = tls.NewListener(tcpListener, tlsconfig.TlsconfigServer)
|
||||
case "tun":
|
||||
cfg := tun.Config{
|
||||
config := tun.Config{
|
||||
Name: node.Get("name"),
|
||||
Addr: node.Get("net"),
|
||||
MTU: node.GetInt("mtu"),
|
||||
Routes: tunRoutes,
|
||||
Gateway: node.Get("gw"),
|
||||
}
|
||||
ln, err = tun.TunListener(cfg)
|
||||
//default:
|
||||
// innerLn, _ := core.TCPListener(node.Addr)
|
||||
// ln = tls.NewListener(innerLn, tlsconfig.TlsconfigServer)
|
||||
ln, err = tun.Listener(config)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -108,12 +103,12 @@ func (r *Route) GenRouters() ([]router, error) {
|
||||
}
|
||||
|
||||
type router struct {
|
||||
node core.Node
|
||||
node *core.Node
|
||||
server *core.Server
|
||||
}
|
||||
|
||||
func (r *router) Serve(ctx context.Context) error {
|
||||
log.Debugf("%s on %s", r.node.String(), r.server.Addr())
|
||||
log.Debugf("%s on %s", r.node.Protocol, r.server.Addr())
|
||||
return r.server.Serve(ctx, r.server.Handler)
|
||||
}
|
||||
|
||||
|
||||
13
tun/tun.go
13
tun/tun.go
@@ -25,15 +25,15 @@ type tunListener struct {
|
||||
config Config
|
||||
}
|
||||
|
||||
// TunListener creates a listener for tun tunnel.
|
||||
func TunListener(cfg Config) (Listener, error) {
|
||||
// Listener TunListener creates a listener for tun tunnel.
|
||||
func Listener(config Config) (net.Listener, error) {
|
||||
ln := &tunListener{
|
||||
conns: make(chan net.Conn, 1),
|
||||
closed: make(chan struct{}),
|
||||
config: cfg,
|
||||
config: config,
|
||||
}
|
||||
|
||||
conn, ifce, err := createTun(cfg)
|
||||
conn, ifce, err := createTun(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -48,11 +48,6 @@ func TunListener(cfg Config) (Listener, error) {
|
||||
return ln, nil
|
||||
}
|
||||
|
||||
// Listener is a proxy server listener, just like a net.Listener.
|
||||
type Listener interface {
|
||||
net.Listener
|
||||
}
|
||||
|
||||
func (l *tunListener) Accept() (net.Conn, error) {
|
||||
select {
|
||||
case conn := <-l.conns:
|
||||
|
||||
Reference in New Issue
Block a user