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 14, 2023
1 parent 5131c6f commit 5780811
Show file tree
Hide file tree
Showing 235 changed files with 38,904 additions and 420 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
56 changes: 49 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ endif
ifdef RACE_CHECK
export GO_RACE=-race
endif
UPDATE_SUBMODULE_COMMAND=git submodule update --init --recursive
UPDATE_SUBMODULE_COMMAND=git submodule update --init --recursive --progress -v
ifdef WITH_OFFICIAL_WEBRTC
UPDATE_SUBMODULE_COMMAND=echo 'skiped update_submodule'
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,27 +127,63 @@ 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:
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 build/* release/*
rm -r 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"
sh -c "command -v ninja"
Expand Down
61 changes: 47 additions & 14 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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 @@ -326,11 +326,19 @@ func (c *Client) Start() (err error) {
}

err = c.parseServices()
//make sure even if there is no service,
//web server still can start for config
if err != nil {
return
//filter out no service error
if err != errNoService || (err == errNoService && !c.Config().EnableWebServer) {
return
}
}

var dialer dialer
if len(c.Config().RemoteAPI) == 0 && len(c.Config().Remote) == 0 {
c.Config().Remote = "tcp://127.0.0.1:80"
}
if len(c.Config().Remote) > 0 {
if !strings.Contains(c.Config().Remote, "://") {
c.Config().Remote = "tcp://" + c.Config().Remote
Expand All @@ -356,7 +364,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 Down Expand Up @@ -408,6 +416,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 @@ -434,10 +461,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 Down Expand Up @@ -506,7 +538,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 +551,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 +606,16 @@ 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 不能为空
// services 不能为空除非启动了 web 服务
if len(services) == 0 {
err = errors.New("no service is configured")
err = errNoService
return
}
h := sha256.New()
Expand Down Expand Up @@ -623,7 +657,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 +681,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 +722,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 @@ -762,7 +795,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 5780811

Please sign in to comment.