Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/huwf5/gt into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdxie committed Oct 7, 2023
2 parents d455f6e + bbed36a commit 9427fec
Show file tree
Hide file tree
Showing 57 changed files with 24,793 additions and 284 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ release
web/front/logs
web/front/*.log
web/front/npm-debug.log*
web/front/package-lock.json
web/front/yarn-debug.log*
web/front/yarn-error.log*
web/front/pnpm-debug.log*
Expand Down
27 changes: 11 additions & 16 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,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 build_web_server build_web_client check_npm front_build duplicate_dist_server clean_duplication_client clean_web clean_dist clean_duplication clean_duplication_server clean_duplication_client
.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 Down Expand Up @@ -125,42 +125,37 @@ 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 build_web_client
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
make check_and_create_file FILENAME=release/client.yaml
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 build_web_server
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
make check_and_create_file FILENAME=release/server.yaml

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

check_and_create_file:
@if [ ! -f "$(FILENAME)" ]; then \
touch "$(FILENAME)"; \
echo "options:" >> "$(FILENAME)";\
echo " web: true" >> "$(FILENAME)";\
echo "Created $(FILENAME)"; \
fi

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:pro)
(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
Expand Down
7 changes: 5 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ func (c *Client) Start() (err error) {
}

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 Down Expand Up @@ -459,10 +462,10 @@ func (c *Client) Close() {
// Shutdown stops the client gracefully.
func (c *Client) Shutdown() {
defer c.Logger.Close()
c.ShutdownWithClosingLogger()
c.ShutdownWithoutClosingLogger()
}

func (c *Client) ShutdownWithClosingLogger() {
func (c *Client) ShutdownWithoutClosingLogger() {
if !atomic.CompareAndSwapUint32(&c.closing, 0, 1) {
return
}
Expand Down
12 changes: 8 additions & 4 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type Config struct {
// Options is the config options for a client.
type Options struct {
Config string `arg:"config" yaml:"-" json:"-" usage:"The config file path to load"`
ID string `yaml:"id" usage:"The unique id used to connect to server. Now it's the prefix of the domain."`
Secret string `yaml:"secret" json:",omitempty" usage:"The secret used to verify the id"`
ID string `yaml:"id,omitempty" json:",omitempty" usage:"The unique id used to connect to server. Now it's the prefix of the domain."`
Secret string `yaml:"secret,omitempty" json:",omitempty" usage:"The secret used to verify the id"`
ReconnectDelay config.Duration `yaml:"reconnectDelay,omitempty" json:",omitempty" usage:"The delay before reconnect. Supports values like '30s', '5m'"`
Remote string `yaml:"remote,omitempty" json:",omitempty" usage:"The remote server url. Supports tcp:// and tls://, default tcp://"`
RemoteSTUN string `yaml:"remoteSTUN,omitempty" json:",omitempty" usage:"The remote STUN server address"`
Expand Down Expand Up @@ -94,8 +94,11 @@ type Options struct {
func defaultConfig() Config {
return Config{
Options: Options{
Config: predef.GetDefaultClientConfigPath(),
ID: util.RandomString(predef.DefaultIDSize),
Remote: "tcp://127.0.0.1:80",

// In the `start` function of `client.go`, if neither 'Remote' nor 'RemoteAPI' is specified,
// the default value for 'Remote' will be set to "tcp://127.0.0.1:80".

ReconnectDelay: config.Duration{Duration: 5 * time.Second},
RemoteTimeout: config.Duration{Duration: 45 * time.Second},
Expand All @@ -114,7 +117,8 @@ func defaultConfig() Config {
LogFileMaxSize: 512 * 1024 * 1024,
LogLevel: zerolog.InfoLevel.String(),

WebAddr: "127.0.0.1:7000",
EnableWebServer: true,
WebAddr: "127.0.0.1:7000",
},
}
}
Expand Down
20 changes: 13 additions & 7 deletions client/web/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/isrc-cas/gt/client"
"github.com/isrc-cas/gt/config"
util2 "github.com/isrc-cas/gt/util"
"github.com/isrc-cas/gt/web/server/model/request"
"github.com/isrc-cas/gt/web/server/util"
"github.com/jinzhu/copier"
Expand All @@ -24,11 +25,16 @@ func VerifyUser(user request.User, c *client.Client) (err error) {
}
}

func ChangeUserInfo(user request.UserInfo, c *client.Client) (err error) {
var cfg client.Config
err = copier.Copy(&cfg, c.Config()) // SigningKey is also copied
// ChangeUserInfo Match the user information in the configuration file
func ChangeUserInfo(user request.UserInfo, c *client.Client) error {
// Get From File
cfg, err := GetConfigFromFile(c)
if err != nil {
return err
// Get From Running
err = copier.Copy(&cfg, c.Config()) // SigningKey is also copied
if err != nil {
return err
}
}
cfg.Admin = user.Username
cfg.Password = user.Password
Expand Down Expand Up @@ -136,7 +142,7 @@ func GetConnectionInfo(c *client.Client) (info []request.SimplifiedConnection, e
// GetConfigFromFile need to set configPath before
func GetConfigFromFile(c *client.Client) (cfg client.Config, err error) {
fullPath := c.Config().Options.Config
if fullPath == "" {
if len(fullPath) == 0 {
err = errors.New("config path is empty")
return
}
Expand All @@ -159,10 +165,10 @@ func SaveConfigToFile(cfg *client.Config) (fullPath string, err error) {
if cfg.Config != "" {
fullPath = cfg.Options.Config
} else {
fullPath = filepath.Join(util.GetAppDir(), "client.yaml")
fullPath = filepath.Join(util2.GetAppDir(), "client.yaml")
cfg.Options.Config = fullPath
}
err = util.WriteYamlToFile(fullPath, yamlData)
err = util2.WriteYamlToFile(fullPath, yamlData)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion client/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewWebServer(c *client.Client) (*Server, error) {
addr := c.Config().WebAddr
c.Logger.Info().Msg("start web server on " + addr)

fullPath := filepath.Join(webUtil.GetAppDir(), "web_client.log")
fullPath := filepath.Join(util.GetAppDir(), "web_client.log")
f, _ := os.Create(fullPath)
gin.DefaultWriter = io.MultiWriter(f)

Expand Down
4 changes: 2 additions & 2 deletions cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ func main() {
continue
}
// avoid port conflict
c.ShutdownWithClosingLogger()
c.ShutdownWithoutClosingLogger()

err = runCmd(os.Args)
if err != nil {
c.Logger.Error().Err(err).Msg("failed to stop web server")
c.Logger.Error().Err(err).Msg("failed to start new process")
continue
}
// yield control to the new process
Expand Down
31 changes: 23 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
package config

import (
"errors"
"flag"
"fmt"
"github.com/isrc-cas/gt/predef"
"os"
"reflect"
"strings"
Expand All @@ -32,24 +34,29 @@ var (
positionMtx sync.Mutex
)

var errNoDefaultFile = errors.New("default config file is not exist")

// ParseFlags parses args and sets the result to config and options.
func ParseFlags(args []string, config, options interface{}) error {
if len(args) < 2 {
return nil
}
positionMtx.Lock()
defer positionMtx.Unlock()
position.Store(0)

if len(args) == 0 {
return nil
}
flagSet, n2fi, configPath := registerFlags(args[0], options)
err := flagSet.Parse(args[1:])
if err != nil {
return err
if len(args) > 1 {
// 有参数时,解析参数
err := flagSet.Parse(args[1:])
if err != nil {
return err
}
}

if configPath != nil {
err = Yaml2Interface(*configPath, config)
if err != nil {
err := Yaml2Interface(*configPath, config)
if err != nil && !errors.Is(err, errNoDefaultFile) {
return err
}
}
Expand All @@ -66,6 +73,10 @@ func Yaml2Interface(path string, dstInterface interface{}) (err error) {

file, err := os.Open(path)
if err != nil {
// 如果文件不存在,且是默认文件,则返回 errNoDefaultFile
if os.IsNotExist(err) && isDefaultFile(path) {
return errNoDefaultFile
}
err = fmt.Errorf("open yaml file %q failed: %v", path, err)
return
}
Expand Down Expand Up @@ -197,3 +208,7 @@ func ShowUsage(options interface{}) {
flagSet, _, _ := registerFlags(os.Args[0], options)
flagSet.Usage()
}

func isDefaultFile(path string) bool {
return path == predef.GetDefaultClientConfigPath() || path == predef.GetDefaultServerConfigPath()
}
22 changes: 22 additions & 0 deletions predef/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@

package predef

import (
"github.com/isrc-cas/gt/util"
"path/filepath"
)

const (
// MinIDSize is the minimum size of ID
MinIDSize = 1
Expand Down Expand Up @@ -69,3 +74,20 @@ var (

// MagicNumber 常量数字,见 https://en.wikipedia.org/wiki/Magic_number_(programming)
const MagicNumber byte = 0xF0

var (
DefaultClientConfigPath string
DefaultServerConfigPath string
)

func init() {
DefaultClientConfigPath = filepath.Join(util.GetAppDir(), "client.yaml")
DefaultServerConfigPath = filepath.Join(util.GetAppDir(), "server.yaml")
}

func GetDefaultClientConfigPath() string {
return DefaultClientConfigPath
}
func GetDefaultServerConfigPath() string {
return DefaultServerConfigPath
}
4 changes: 3 additions & 1 deletion server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Options struct {
func defaultConfig() Config {
return Config{
Options: Options{
Config: predef.GetDefaultServerConfigPath(),
Addr: "80",
Timeout: config.Duration{Duration: 90 * time.Second},
TLSMinVersion: "tls1.2",
Expand All @@ -125,7 +126,8 @@ func defaultConfig() Config {

MaxHandShakeOptions: 30,

WebAddr: "127.0.0.1:8000",
EnableWebServer: true,
WebAddr: "127.0.0.1:8000",
},
}
}
Expand Down
19 changes: 12 additions & 7 deletions server/web/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/isrc-cas/gt/config"
"github.com/isrc-cas/gt/server"
util2 "github.com/isrc-cas/gt/util"
"github.com/isrc-cas/gt/web/server/model/request"
"github.com/isrc-cas/gt/web/server/util"
"github.com/jinzhu/copier"
Expand All @@ -23,11 +24,15 @@ func VerifyUser(user request.User, s *server.Server) (err error) {
}
}

func ChangeUserInfo(user request.UserInfo, s *server.Server) (err error) {
var cfg server.Config
err = copier.Copy(&cfg, s.Config()) // SigningKey is also copied
func ChangeUserInfo(user request.UserInfo, s *server.Server) error {
// Get From File
cfg, err := GetConfigFromFile(s)
if err != nil {
return err
// Get From Running
err = copier.Copy(&cfg, s.Config()) // SigningKey is also copied
if err != nil {
return err
}
}
cfg.Admin = user.Username
cfg.Password = user.Password
Expand Down Expand Up @@ -130,7 +135,7 @@ func GetConnectionInfo(s *server.Server) (serverPool []request.SimplifiedConnect

func GetConfigFromFile(s *server.Server) (cfg server.Config, err error) {
fullPath := s.Config().Options.Config
if fullPath == "" {
if len(fullPath) == 0 {
err = errors.New("config path is empty")
return
}
Expand All @@ -149,10 +154,10 @@ func SaveConfigToFile(cfg *server.Config) (fullPath string, err error) {
if cfg.Options.Config != "" {
fullPath = cfg.Options.Config
} else {
fullPath = filepath.Join(util.GetAppDir(), "server.yaml")
fullPath = filepath.Join(util2.GetAppDir(), "server.yaml")
cfg.Options.Config = fullPath
}
err = util.WriteYamlToFile(fullPath, yamlData)
err = util2.WriteYamlToFile(fullPath, yamlData)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion server/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewWebServer(s *server.Server) (*Server, error) {
s.Logger.Info().Msg("start web server on " + addr)

//set the log file
fullPath := filepath.Join(webUtil.GetAppDir(), "web_server.log")
fullPath := filepath.Join(util.GetAppDir(), "web_server.log")
f, _ := os.Create(fullPath)
gin.DefaultWriter = io.MultiWriter(f)

Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 9427fec

Please sign in to comment.