Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
yzxiu committed Mar 23, 2023
0 parents commit dcf9fe7
Show file tree
Hide file tree
Showing 45 changed files with 4,346 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Binaries for programs and plugins
#*.exe
*.exe~
*.dll
*.so
*.dylib
bin
testbin/*

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*

# editor and IDE paraphernalia
.idea
.vscode
*.swp
*.swo
*~

record.md

client-linux-amd64
client-linux-arm64
client-darwin-amd64
client-darwin-arm64
client-windows-amd64.exe

.run
/bin
deploy-template.yaml
/scripts
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM alpine:3.15.4
RUN apk update && apk add iptables tcpdump
RUN echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
WORKDIR /
COPY bin/vtun-linux-amd64 .
USER 65532:65532
ENTRYPOINT ["/vtun-linux-amd64"]


# iptables -t nat -A POSTROUTING -s 10.99.99.0/24 -j MASQUERADE
# docker exec -t vtun-server iptables -t nat -A POSTROUTING -s 10.99.99.0/24 -j MASQUERADE

# windows route
# route add 10.233.0.0 MASK 255.255.0.0 10.99.99.1

# macos route
# route -n add -net 10.233.0.0 -netmask 255.255.0.0 10.99.99.1

# linux route
# ip route add 10.233.0.0/16 via 10.99.99.1

# client
# -l=:3000 -s=192.168.242.77:3001 -c=10.99.99.11/24 -k=123456 -p tcp
10 changes: 10 additions & 0 deletions Dockerfile.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM alpine:3.15.4
RUN apk --update add tzdata && \
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
apk del tzdata && \
rm -rf /var/cache/apk/*
WORKDIR /
COPY bin/info-server-linux-amd64 .
USER 65532:65532
ENTRYPOINT ["/info-server-linux-amd64"]
Empty file added Makefile
Empty file.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 访问k8s pod ip,service ip

## 服务端

```bash
k apply -f https://raw.githubusercontent.com/yzxiu/k8s-tun/master/deploy.yaml
```

## 客户端
#### Linux & Mac

1. 设置权限

```shell
chmod +x client-linux-amd64
```

2. 运行

```shell
sudo ./client-linux-amd64 -s <node ip>:<node port>
```

3. 退出

ctrl + c

#### Windows

1. 安装驱动

安装附带的tap-windows-9.24.2-I601-Win10驱动

2. 右键,以管理员身份运行

3. 退出

关闭窗口 或者 ctrl + c
6 changes: 6 additions & 0 deletions client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
envs:
- name: local
infoUrl: "127.0.0.1:30011"
- name: local-31
infoUrl: "192.168.4.31:30011"

9 changes: 9 additions & 0 deletions cmd/client/envs/envs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package envs

var InitEnvs = `
envs:
- name: local
infoUrl: "127.0.0.1:30011"
- name: local-31
infoUrl: "192.168.4.31:30011"
`
44 changes: 44 additions & 0 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"flag"

log "github.com/sirupsen/logrus"
"github.com/yzxiu/k8s-tun/cmd/vtun/app"
"github.com/yzxiu/k8s-tun/pkg/client"
)

var (
infoUrl string
selectedEnv string
uuidFile bool
logLevel int
heartbeatPeriod int
heartbeatRetryTimes int
)

func main() {
flag.IntVar(&logLevel, "v", 4, "log level, 4 is info, 5 is debug")
flag.StringVar(&selectedEnv, "e", "", "env to client")
flag.StringVar(&infoUrl, "s", "", "info server url")
flag.BoolVar(&uuidFile, "u", false, "use uuid file")
flag.IntVar(&heartbeatPeriod, "h", 60, "heartbeat period")
flag.IntVar(&heartbeatRetryTimes, "t", 10, "heartbeat retry times")

flag.Parse()
log.SetLevel(log.Level(logLevel))
// client
c := client.GetClient(infoUrl)
// uuid
u := client.GetUUID(uuidFile)
// select config
env := client.SelectConfig(selectedEnv, c.Envs)
// get server info
sInfo := client.GetServerInfo(env, u)
// get tun config
tunConfig := client.GetTunConfig(sInfo)
// heartbeat
stopCh := client.HeartBeat(env, sInfo, u, heartbeatPeriod, heartbeatRetryTimes)
// start tun
app.StartTun(tunConfig, stopCh)
}
63 changes: 63 additions & 0 deletions cmd/info-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"

log "github.com/sirupsen/logrus"
"github.com/yzxiu/k8s-tun/pkg/info"
)

func main() {
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
log.SetFormatter(&MyFormatter{})
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
server := info.Server()
go func() {
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.WithError(err).Errorf("server start error")
}
}()
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.WithError(err).Errorf("server shutdown error")
}
}

type MyFormatter struct{}

func (m *MyFormatter) Format(entry *log.Entry) ([]byte, error) {
var b *bytes.Buffer
if entry.Buffer != nil {
b = entry.Buffer
} else {
b = &bytes.Buffer{}
}

timestamp := entry.Time.Format("2006-01-02 15:04:05")
var newLog string

//HasCaller()为true才会有调用信息
if entry.HasCaller() {
fName := filepath.Base(entry.Caller.File)
newLog = fmt.Sprintf("[%s] [%s] [%s:%d %s] %s\n",
timestamp, entry.Level, fName, entry.Caller.Line, entry.Caller.Function, entry.Message)
} else {
newLog = fmt.Sprintf("[%s] [%s] %s\n", timestamp, entry.Level, entry.Message)
}

b.WriteString(newLog)
return b.Bytes(), nil
}
72 changes: 72 additions & 0 deletions cmd/vtun/app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package app

import (
"encoding/json"
"runtime"

log "github.com/sirupsen/logrus"
"github.com/yzxiu/k8s-tun/pkg/common/cipher"
"github.com/yzxiu/k8s-tun/pkg/common/config"
"github.com/yzxiu/k8s-tun/pkg/common/netutil"
"github.com/yzxiu/k8s-tun/pkg/tcp"
"github.com/yzxiu/k8s-tun/pkg/tun"
"github.com/yzxiu/k8s-tun/pkg/udp"
"github.com/yzxiu/k8s-tun/pkg/ws"
)

func StartTun(tunConfig *config.TunConfig, quit <-chan struct{}) {
initConfig(tunConfig)
go startApp(tunConfig)
<-quit
stopApp(tunConfig)
}

func initConfig(config *config.TunConfig) {
if !config.ServerMode {
}
if !config.ServerMode && config.GlobalMode {
switch runtime.GOOS {
case "linux":
config.LocalGateway = netutil.GetLinuxLocalGateway()
case "darwin":
config.LocalGateway = netutil.GetMacLocalGateway()
}
}
cipher.GenerateKey(config.Key)
configJson, _ := json.Marshal(config)
log.Debugf("init config:%s", string(configJson))
}

func startApp(config *config.TunConfig) {
switch config.Protocol {
case "udp":
if config.ServerMode {
udp.StartServer(config)
} else {
udp.StartClient(config)
}
case "tcp":
if config.ServerMode {
tcp.StartServer(config)
} else {
tcp.StartClient(config)
}
case "ws":
if config.ServerMode {
ws.StartServer(config)
} else {
ws.StartClient(config)
}
default:
if config.ServerMode {
ws.StartServer(config)
} else {
ws.StartClient(config)
}
}
}

func stopApp(config *config.TunConfig) {
tun.Reset(config)
log.Debugf("stopped!!!")
}
28 changes: 28 additions & 0 deletions cmd/vtun/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"flag"

"github.com/yzxiu/k8s-tun/cmd/vtun/app"
"github.com/yzxiu/k8s-tun/pkg/common/config"
"github.com/yzxiu/k8s-tun/pkg/common/signal"
)

func main() {
tunConfig := config.TunConfig{}
flag.StringVar(&tunConfig.CIDR, "c", "172.16.0.10/24", "tun interface cidr")
flag.StringVar(&tunConfig.DstCIDR, "dst", "10.233.64.0/18,10.233.0.0/18", "vpn cidr")
flag.IntVar(&tunConfig.MTU, "mtu", 1500, "tun mtu")
flag.StringVar(&tunConfig.LocalAddr, "l", ":3000", "local address")
flag.StringVar(&tunConfig.ServerAddr, "s", ":3001", "server address")
flag.StringVar(&tunConfig.Key, "k", "freedom@2022", "key")
flag.StringVar(&tunConfig.Protocol, "p", "wss", "protocol tcp/udp/ws/wss")
flag.StringVar(&tunConfig.WebSocketPath, "path", "/freedom", "websocket path")
flag.BoolVar(&tunConfig.ServerMode, "S", false, "server mode")
flag.BoolVar(&tunConfig.GlobalMode, "g", false, "client global mode")
flag.BoolVar(&tunConfig.Obfs, "obfs", false, "enable data obfuscation")
flag.IntVar(&tunConfig.Timeout, "t", 30, "dial timeout in seconds")
flag.Parse()
tunConfig.UpdateFromEnv()
app.StartTun(&tunConfig, signal.SetupSignalHandler())
}
Loading

0 comments on commit dcf9fe7

Please sign in to comment.