Skip to content

Commit

Permalink
feat: Add GT-Admin for GT-Server and GT-Client
Browse files Browse the repository at this point in the history
  • Loading branch information
huwf5 committed Oct 23, 2023
1 parent 5131c6f commit 9676ece
Show file tree
Hide file tree
Showing 212 changed files with 39,620 additions and 401 deletions.
36 changes: 35 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,39 @@

build
release
.vscode
.dockerignore

# IDE files
.vscode
.idea

# Frontend specific ignores
web/front/logs
web/front/*.log
web/front/npm-debug.log*
web/front/yarn-debug.log*
web/front/yarn-error.log*
web/front/pnpm-debug.log*
web/front/lerna-debug.log*

web/front/node_modules
web/front/dist
web/front/dist-ssr
web/front/stats.html
web/front/*.local

# Editor directories and files
web/front/.vscode/*
web/front/!.vscode/extensions.json
web/front/!.vscode/settings.json
web/front/.idea
web/front/.DS_Store
web/front/*.suo
web/front/*.ntvs*
web/front/*.njsproj
web/front/*.sln
web/front/*.sw?

# dist
server/web/dist
client/web/dist
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ RUN sed -i 's|deb.debian.org|mirrors.163.com|g' /etc/apt/sources.list && \
apt update && \
apt install xz-utils bzip2 sudo lsb-release ninja-build generate-ninja file patch -y

# 安装nodejs 20
RUN apt-get install -y ca-certificates curl gnupg && \
mkdir -p /etc/apt/keyrings && \
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg && \
NODE_MAJOR=20 && \
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list && \
apt-get update && \
apt-get install nodejs -y

# golang 切换国内源并且提前安装好依赖
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn
Expand Down
58 changes: 50 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ endif
RELEASE_OPTIONS=$(GO_RACE) -tags release -trimpath -ldflags "$(GO_STATIC_LINK_FLAG) -s -w -X 'github.com/isrc-cas/gt/predef.Version=$(VERSION)'"
DEBUG_OPTIONS=$(GO_RACE) -trimpath -ldflags "$(GO_STATIC_LINK_FLAG) -X 'github.com/isrc-cas/gt/predef.Version=$(VERSION)'"
SOURCES=$(shell ls -1 **/*.go)
FRONTEND_DIR=web/front
SOURCES_FRONT = $(shell find $(FRONTEND_DIR) -type d \( -name 'node_modules' -o -name 'dist' \) -prune -o -type f \( -name '*.ts' -o -name '*.vue' -o -name '*.scss' -o -name '*.json' -o -name '*.cjs' -o -name '*.config.ts' -o -name '*.html' \) -print)
SERVER_FRONT_DIR=server/web
CLIENT_FRONT_DIR=client/web
TARGET?=$(shell gcc -dumpmachine)
TARGET_OS=$(shell echo $(TARGET) | awk -F '-' '{print $$2}')
ifeq ($(TARGET_OS), native)
Expand All @@ -57,7 +61,7 @@ export CGO_CXXFLAGS=-I$(shell pwd)/dep/_google-webrtc/src -I$(shell pwd)/dep/_go
export CGO_LDFLAGS=$(shell pwd)/dep/_google-webrtc/src/out/release-$(TARGET)/obj/libwebrtc.a -ldl -pthread
export CGO_ENABLED=1

.PHONY: all build docker_build_linux_arm64 fmt build_client docker_build_linux_arm64_client gofumpt build_server docker_build_linux_arm64_server golangci-lint check_webrtc_dependencies docker_release_linux_amd64 release clean docker_release_linux_amd64_client release_client compile_webrtc docker_release_linux_amd64_server release_server docker_create_image docker_build_linux_amd64 docker_release_linux_arm64 revive docker_build_linux_amd64_client docker_release_linux_arm64_client test docker_build_linux_amd64_server docker_release_linux_arm64_server update_submodule
.PHONY: all build docker_build_linux_arm64 fmt build_client docker_build_linux_arm64_client gofumpt build_server docker_build_linux_arm64_server golangci-lint check_webrtc_dependencies docker_release_linux_amd64 release clean docker_release_linux_amd64_client release_client compile_webrtc docker_release_linux_amd64_server release_server docker_create_image docker_build_linux_amd64 docker_release_linux_arm64 revive docker_build_linux_amd64_client docker_release_linux_arm64_client test docker_build_linux_amd64_server docker_release_linux_arm64_server update_submodule build_web_server build_web_client release_web_server release_web_client check_npm front_release duplicate_dist_server clean_duplication_client clean_web clean_dist clean_duplication clean_duplication_server clean_duplication_client

all: gofumpt golangci-lint test release

Expand All @@ -84,6 +88,8 @@ golangci-lint:
--exclude 'S1000: should use a simple channel send/receive instead of `select` with a single case'

update_submodule:
git config --global --add safe.directory /go/src/github.com/isrc-cas/gt
git config --global --add safe.directory /go/src/github.com/isrc-cas/gt/dep/_google-webrtc
$(UPDATE_SUBMODULE_COMMAND)

docker_create_image: update_submodule
Expand Down Expand Up @@ -121,26 +127,62 @@ docker_release_linux_arm64_server: docker_create_image

build: build_server build_client
release: release_server release_client
build_client: $(SOURCES) Makefile compile_webrtc
build_client: $(SOURCES) Makefile compile_webrtc build_web_client
$(eval CGO_CXXFLAGS+=-O0 -g -ggdb)
$(eval NAME=$(GOOS)-$(GOARCH)-client)
go build $(DEBUG_OPTIONS) -o build/$(NAME)$(EXE) ./cmd/client
release_client: $(SOURCES) Makefile compile_webrtc
release_client: $(SOURCES) Makefile compile_webrtc release_web_client
$(eval CGO_CXXFLAGS+=-O3)
$(eval NAME=$(GOOS)-$(GOARCH)-client)
go build $(RELEASE_OPTIONS) -o release/$(NAME)$(EXE) ./cmd/client
build_server: $(SOURCES) Makefile compile_webrtc
build_server: $(SOURCES) Makefile compile_webrtc build_web_server
$(eval CGO_CXXFLAGS+=-O0 -g -ggdb)
$(eval NAME=$(GOOS)-$(GOARCH)-server)
go build $(DEBUG_OPTIONS) -o build/$(NAME)$(EXE) ./cmd/server
release_server: $(SOURCES) Makefile compile_webrtc
release_server: $(SOURCES) Makefile compile_webrtc release_web_server
$(eval CGO_CXXFLAGS+=-O3)
$(eval NAME=$(GOOS)-$(GOARCH)-server)
go build $(RELEASE_OPTIONS) -o release/$(NAME)$(EXE) ./cmd/server

clean:
rm build/* release/*
rm -r dep/_google-webrtc/src/out/*
build_web_server: $(SOURCES_FRONT) Makefile check_npm front_build duplicate_dist_server
build_web_client: $(SOURCES_FRONT) Makefile check_npm front_build duplicate_dist_client

release_web_server: $(SOURCES_FRONT) Makefile check_npm front_release duplicate_dist_server
release_web_client: $(SOURCES_FRONT) Makefile check_npm front_release duplicate_dist_client

check_npm:
npm --version || curl -qL https://www.npmjs.com/install.sh | sh

front_build: $(SOURCES_FRONT)
(cd $(FRONTEND_DIR) && npm install && npm run "build:test")

front_release: $(SOURCES_FRONT)
(cd $(FRONTEND_DIR) && npm install && npm run "build:pro")

duplicate_dist_server:
cp -r $(FRONTEND_DIR)/dist $(SERVER_FRONT_DIR)/dist

duplicate_dist_client:
cp -r $(FRONTEND_DIR)/dist $(CLIENT_FRONT_DIR)/dist

clean: clean_web
rm -rf build/* release/*
rm -rf dep/_google-webrtc/src/out/*

clean_web: clean_dist
rm -rf $(FRONTEND_DIR)/node_modules
rm -f $(FRONTEND_DIR)/package-lock.json

clean_dist: clean_duplication
rm -rf $(FRONTEND_DIR)/dist

clean_duplication: clean_duplication_server clean_duplication_client

clean_duplication_server:
rm -rf $(SERVER_FRONT_DIR)/dist
clean_duplication_client:
rm -rf $(CLIENT_FRONT_DIR)/dist


check_webrtc_dependencies:
sh -c "command -v gn"
Expand Down
84 changes: 67 additions & 17 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import (

// New parses the command line args and creates a Client. out 用于测试
func New(args []string, out io.Writer) (c *Client, err error) {
conf := defaultConfig()
conf := getDefaultConfig(args)
err = config.ParseFlags(args, &conf, &conf.Options)
if err != nil {
return
Expand Down Expand Up @@ -96,6 +96,17 @@ func New(args []string, out io.Writer) (c *Client, err error) {
c.apiServer.ReadTimeout = 30 * time.Second
return
}
func getDefaultConfig(args []string) (conf Config) {
if predef.IsNoArgs() {
conf = defaultConfigWithNoArgs()
} else {
conf = defaultConfig()
if util.Contains(args, "-webAddr") {
conf.Config = predef.GetDefaultClientConfigPath()
}
}
return
}

func processSignal(signal string) (err error) {
switch signal {
Expand Down Expand Up @@ -247,7 +258,7 @@ func (d *dialer) initWithRemoteAPI(c *Client) (err error) {
req.URL.RawQuery = query.Encode()
req.Header.Set("Request-Id", strconv.FormatInt(time.Now().Unix(), 10))
client := http.Client{
Timeout: c.Config().RemoteTimeout,
Timeout: c.Config().RemoteTimeout.Duration,
}
resp, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -356,7 +367,7 @@ func (c *Client) Start() (err error) {
break
}
c.Logger.Error().Err(err).Msg("failed to query server address")
time.Sleep(c.Config().ReconnectDelay)
time.Sleep(c.Config().ReconnectDelay.Duration)
}
}
if len(dialer.host) == 0 {
Expand All @@ -378,6 +389,8 @@ func (c *Client) Start() (err error) {

conf4Log := *c.Config()
conf4Log.Secret = "******"
conf4Log.Password = "******"
conf4Log.SigningKey = "******"
c.Logger.Info().Msg(spew.Sdump(conf4Log))
for i := uint(1); i <= c.Config().RemoteConnections; i++ {
go c.connectLoop(dialer, i)
Expand Down Expand Up @@ -408,6 +421,25 @@ func (c *Client) Config() *Config {
return c.config.Load()
}

func (c *Client) GetConnectionPoolStatus() (status map[uint]Status) {
if c.idleManager == nil {
return
}
return c.idleManager.GetConnectionStatus()
}

func (c *Client) GetConnectionPoolNetInfo() (pools []PoolInfo) {
c.tunnelsRWMtx.RLock()
defer c.tunnelsRWMtx.RUnlock()
for conn := range c.tunnels {
pools = append(pools, PoolInfo{
LocalAddr: conn.LocalAddr(),
RemoteAddr: conn.RemoteAddr(),
})
}
return
}

// Close stops the client agent.
func (c *Client) Close() {
if !atomic.CompareAndSwapUint32(&c.closing, 0, 1) {
Expand All @@ -425,7 +457,9 @@ func (c *Client) Close() {
p.Close()
}
c.peersRWMtx.Unlock()
c.idleManager.Close()
if c.idleManager != nil {
c.idleManager.Close()
}
c.Logger.Info().Err(c.apiServer.Close()).Msg("api server close")
if c.tcpForwardListener != nil {
_ = c.tcpForwardListener.Close()
Expand All @@ -434,10 +468,15 @@ func (c *Client) Close() {

// Shutdown stops the client gracefully.
func (c *Client) Shutdown() {
defer c.Logger.Close()
c.ShutdownWithoutClosingLogger()
}

func (c *Client) ShutdownWithoutClosingLogger() {
if !atomic.CompareAndSwapUint32(&c.closing, 0, 1) {
return
}
defer c.Logger.Close()

c.tunnelsRWMtx.Lock()
for t := range c.tunnels {
t.SendCloseSignal()
Expand All @@ -449,7 +488,9 @@ func (c *Client) Shutdown() {
}
c.peersRWMtx.Unlock()

c.idleManager.Close()
if c.idleManager != nil {
c.idleManager.Close()
}
c.waitTunnelsShutdown.Wait()

c.Logger.Info().Err(c.apiServer.Close()).Msg("api server close")
Expand Down Expand Up @@ -506,7 +547,7 @@ func (c *Client) connect(d dialer, connID uint) (closing bool) {
if atomic.LoadUint32(&c.closing) == 1 {
return true
}
time.Sleep(c.Config().ReconnectDelay)
time.Sleep(c.Config().ReconnectDelay.Duration)
c.idleManager.SetWait(connID)
c.idleManager.WaitIdle(connID)

Expand All @@ -519,7 +560,7 @@ func (c *Client) connect(d dialer, connID uint) (closing bool) {
break
}
c.Logger.Error().Uint("connID", connID).Err(err).Msg("failed to query server address")
time.Sleep(c.Config().ReconnectDelay)
time.Sleep(c.Config().ReconnectDelay.Duration)
}
return
}
Expand Down Expand Up @@ -574,14 +615,15 @@ func (c *Client) WaitUntilReady(timeout time.Duration) (err error) {
return
}

var errNoService = errors.New("no service is configured")

func (c *Client) parseServices() (err error) {
services, err := parseServices(c.Config())
if err != nil {
return
}
// services 不能为空
if len(services) == 0 {
err = errors.New("no service is configured")
err = errNoService
return
}
h := sha256.New()
Expand Down Expand Up @@ -623,7 +665,7 @@ func parseServices(config *Config) (result services, err error) {
if configServicesLen == 1 ||
(x.Position > config.Local[i].Position &&
(i == configServicesLen-1 || x.Position < config.Local[i+1].Position)) {
configServices[i].LocalTimeout = x.Value
configServices[i].LocalTimeout.Duration = x.Value
}
}
for _, x := range config.UseLocalAsHTTPHost {
Expand All @@ -647,12 +689,11 @@ func parseServices(config *Config) (result services, err error) {
for i := 0; i < len(result); i++ {
if result[i].LocalURL.URL == nil {
err = errors.New("local url (-local option) cannot be empty")
return
}

// 设置默认值
if result[i].LocalTimeout == 0 {
result[i].LocalTimeout = 120 * time.Second
if result[i].LocalTimeout.Duration == 0 {
result[i].LocalTimeout.Duration = 120 * time.Second
}
if result[i].RemoteTCPRandom == nil {
result[i].RemoteTCPRandom = new(bool)
Expand Down Expand Up @@ -689,7 +730,7 @@ func parseServices(config *Config) (result services, err error) {
return
}
default:
err = fmt.Errorf("local url (-local option) '%s' must begin with http://, https:// or tcp://", config.Local[i].Value)
err = fmt.Errorf("local url (-local option) '%s' must begin with http://, https:// or tcp://", result[i].LocalURL.String())
return
}

Expand Down Expand Up @@ -733,7 +774,16 @@ func (c *Client) ReloadServices() (err error) {
c.reloading.Store(false)
}()

conf := defaultConfig()
conf := getDefaultConfig(os.Args)
// ignore the webSetting
conf.WebAddr = c.Config().WebAddr
conf.WebKeyFile = c.Config().WebKeyFile
conf.WebCertFile = c.Config().WebCertFile
conf.EnablePprof = c.Config().EnablePprof
conf.SigningKey = c.Config().SigningKey
conf.Admin = c.Config().Admin
conf.Password = c.Config().Password

err = config.ParseFlags(os.Args, &conf, &conf.Options)
if err != nil {
return
Expand Down Expand Up @@ -762,7 +812,7 @@ func (c *Client) ReloadServices() (err error) {
return
}
if len(services) == 0 {
err = errors.New("no service is configured")
err = errNoService
return
}
checksum := [32]byte{}
Expand Down
Loading

0 comments on commit 9676ece

Please sign in to comment.