Skip to content

Commit

Permalink
Revert "feat(web): Add GT-Admin for GT server and GT client " (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
wasdxie authored Sep 22, 2023
1 parent b351016 commit b92e880
Show file tree
Hide file tree
Showing 231 changed files with 403 additions and 37,167 deletions.
32 changes: 1 addition & 31 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,5 @@

build
release
.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?
.dockerignore
53 changes: 14 additions & 39 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.Duration,
Timeout: c.Config().RemoteTimeout,
}
resp, err := client.Do(req)
if err != nil {
Expand Down Expand Up @@ -326,13 +326,8 @@ 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 {
//filter out no service error
if err != errNoService || (err == errNoService && !c.Config().EnableWebServer) {
return
}
return
}

var dialer dialer
Expand Down Expand Up @@ -361,7 +356,7 @@ func (c *Client) Start() (err error) {
break
}
c.Logger.Error().Err(err).Msg("failed to query server address")
time.Sleep(c.Config().ReconnectDelay.Duration)
time.Sleep(c.Config().ReconnectDelay)
}
}
if len(dialer.host) == 0 {
Expand Down Expand Up @@ -413,25 +408,6 @@ 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 Down Expand Up @@ -530,7 +506,7 @@ func (c *Client) connect(d dialer, connID uint) (closing bool) {
if atomic.LoadUint32(&c.closing) == 1 {
return true
}
time.Sleep(c.Config().ReconnectDelay.Duration)
time.Sleep(c.Config().ReconnectDelay)
c.idleManager.SetWait(connID)
c.idleManager.WaitIdle(connID)

Expand All @@ -542,8 +518,8 @@ func (c *Client) connect(d dialer, connID uint) (closing bool) {
if err == nil {
break
}
c.Logger.Error().Err(err).Msg("failed to query server address")
time.Sleep(c.Config().ReconnectDelay.Duration)
c.Logger.Error().Uint("connID", connID).Err(err).Msg("failed to query server address")
time.Sleep(c.Config().ReconnectDelay)
}
return
}
Expand Down Expand Up @@ -598,16 +574,14 @@ 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 不能为空除非启动了 web 服务
// services 不能为空
if len(services) == 0 {
err = errNoService
err = errors.New("no service is configured")
return
}
h := sha256.New()
Expand Down Expand Up @@ -649,7 +623,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.Duration = x.Value
configServices[i].LocalTimeout = x.Value
}
}
for _, x := range config.UseLocalAsHTTPHost {
Expand All @@ -673,11 +647,12 @@ 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.Duration == 0 {
result[i].LocalTimeout.Duration = 120 * time.Second
if result[i].LocalTimeout == 0 {
result[i].LocalTimeout = 120 * time.Second
}
if result[i].RemoteTCPRandom == nil {
result[i].RemoteTCPRandom = new(bool)
Expand Down Expand Up @@ -714,7 +689,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://", result[i].LocalURL.String())
err = fmt.Errorf("local url (-local option) '%s' must begin with http://, https:// or tcp://", config.Local[i].Value)
return
}

Expand Down Expand Up @@ -787,7 +762,7 @@ func (c *Client) ReloadServices() (err error) {
return
}
if len(services) == 0 {
err = errNoService
err = errors.New("no service is configured")
return
}
checksum := [32]byte{}
Expand Down
142 changes: 55 additions & 87 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package client

import (
"encoding/json"
"fmt"
"gopkg.in/yaml.v3"
"net/url"
"strconv"
"strings"
Expand All @@ -26,83 +24,76 @@ import (
"github.com/isrc-cas/gt/config"
"github.com/isrc-cas/gt/predef"
"github.com/rs/zerolog"
"gopkg.in/yaml.v3"
)

// Config is a client config.
type Config struct {
Version string `yaml:"-" json:"-"` // 目前未使用
Version string // 目前未使用
Services services
Options
}

// 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"`
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"`
RemoteAPI string `yaml:"remoteAPI,omitempty" json:",omitempty" usage:"The API to get remote server url"`
RemoteCert string `yaml:"remoteCert,omitempty" json:",omitempty" usage:"The path to remote cert"`
RemoteCertInsecure bool `yaml:"remoteCertInsecure,omitempty" json:",omitempty" usage:"Accept self-signed SSL certs from remote"`
RemoteConnections uint `yaml:"remoteConnections,omitempty" json:",omitempty" usage:"The max number of server connections in the pool. Valid value is 1 to 10"`
RemoteIdleConnections uint `yaml:"remoteIdleConnections,omitempty" json:",omitempty" usage:"The number of idle server connections kept in the pool"`
RemoteTimeout config.Duration `yaml:"remoteTimeout,omitempty" json:",omitempty" usage:"The timeout of remote connections. Supports values like '30s', '5m'"`

HostPrefix config.PositionSlice[string] `yaml:"-" json:"-" arg:"hostPrefix" usage:"The server will recognize this host prefix and forward data to local"`
RemoteTCPPort config.PositionSlice[uint16] `yaml:"-" json:"-" arg:"remoteTCPPort" usage:"The TCP port that the remote server will open"`
RemoteTCPRandom config.PositionSlice[bool] `yaml:"-" json:"-" arg:"remoteTCPRandom" usage:"Whether to choose a random tcp port by the remote server"`
Local config.PositionSlice[string] `yaml:"-" json:"-" arg:"local" usage:"The local service url"`
LocalTimeout config.PositionSlice[time.Duration] `yaml:"-" json:"-" arg:"localTimeout" usage:"The timeout of local connections. Supports values like '30s', '5m'"`
UseLocalAsHTTPHost config.PositionSlice[bool] `yaml:"-" json:"-" arg:"useLocalAsHTTPHost" usage:"Use the local address as host"`

SentryDSN string `yaml:"sentryDSN,omitempty" json:",omitempty" usage:"Sentry DSN to use"`
SentryLevel config.Slice[string] `yaml:"sentryLevel,omitempty" json:",omitempty" usage:"Sentry levels: trace, debug, info, warn, error, fatal, panic (default [\"error\", \"fatal\", \"panic\"])"`
SentrySampleRate float64 `yaml:"sentrySampleRate,omitempty" json:",omitempty" usage:"Sentry sample rate for event submission: [0.0 - 1.0]"`
SentryRelease string `yaml:"sentryRelease,omitempty" json:",omitempty" usage:"Sentry release to be sent with events"`
SentryEnvironment string `yaml:"sentryEnvironment,omitempty" json:",omitempty" usage:"Sentry environment to be sent with events"`
SentryServerName string `yaml:"sentryServerName,omitempty" json:",omitempty" usage:"Sentry server name to be reported"`
SentryDebug bool `yaml:"sentryDebug,omitempty" json:",omitempty" usage:"Sentry debug mode, the debug information is printed to help you understand what sentry is doing"`

WebRTCConnectionIdleTimeout config.Duration `yaml:"webrtcConnectionIdleTimeout,omitempty" usage:"The timeout of WebRTC connection. Supports values like '30s', '5m'"`
WebRTCLogLevel string `yaml:"webrtcLogLevel,omitempty" json:",omitempty" usage:"WebRTC log level: verbose, info, warning, error"`
WebRTCMinPort uint16 `yaml:"webrtcMinPort,omitempty" json:",omitempty" usage:"The min port of WebRTC peer connection"`
WebRTCMaxPort uint16 `yaml:"webrtcMaxPort,omitempty" json:",omitempty" usage:"The max port of WebRTC peer connection"`

TCPForwardAddr string `yaml:"tcpForwardAddr,omitempty" json:",omitempty" usage:"The address of TCP forward"`
TCPForwardHostPrefix string `yaml:"tcpForwardHostPrefix,omitempty" json:",omitempty" usage:"The host prefix of TCP forward"`
TCPForwardConnections uint `yaml:"tcpForwardConnections,omitempty" json:",omitempty" usage:"The max number of TCP forward peer connections in the pool. Valid value is 1 to 10"`

LogFile string `yaml:"logFile,omitempty" json:",omitempty" usage:"Path to save the log file"`
LogFileMaxSize int64 `yaml:"logFileMaxSize,omitempty" json:",omitempty" usage:"Max size of the log files"`
LogFileMaxCount uint `yaml:"logFileMaxCount,omitempty" json:",omitempty" usage:"Max count of the log files"`
LogLevel string `yaml:"logLevel,omitempty" json:",omitempty" usage:"Log level: trace, debug, info, warn, error, fatal, panic, disable"`
Version bool `arg:"version,omitempty" yaml:"-" json:"-" usage:"Show the version of this program"`

EnableWebServer bool `arg:"web" yaml:"web,omitempty" json:"-" usage:"Enable web server"`
WebAddr string `arg:"webAddr" yaml:"webAddr,omitempty" json:"-" usage:"Web server address"`
WebPort uint16 `arg:"webPort" yaml:"webPort,omitempty" json:"-" usage:"Web server port"`
EnablePprof bool `arg:"pprof" yaml:"pprof,omitempty" json:"-" usage:"Enable pprof in web server"`
SigningKey string `arg:"signingKey" yaml:"signingKey,omitempty" json:"-" usage:"JWT signing key for web server"`
Admin string `arg:"admin" yaml:"admin,omitempty" json:"-" usage:"Admin username use for login in web server"`
Password string `arg:"password" yaml:"password,omitempty" json:"-" usage:"Admin password use for login in web server"`

Signal string `arg:"s" yaml:"-" json:"-" usage:"Send signal to client processes. Supports values: reload, restart, stop, kill"`
Config string `arg:"config" yaml:"-" 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" usage:"The secret used to verify the id"`
ReconnectDelay time.Duration `yaml:"reconnectDelay" usage:"The delay before reconnect. Supports values like '30s', '5m'"`
Remote string `yaml:"remote" usage:"The remote server url. Supports tcp:// and tls://, default tcp://"`
RemoteSTUN string `yaml:"remoteSTUN" usage:"The remote STUN server address"`
RemoteAPI string `yaml:"remoteAPI" usage:"The API to get remote server url"`
RemoteCert string `yaml:"remoteCert" usage:"The path to remote cert"`
RemoteCertInsecure bool `yaml:"remoteCertInsecure" usage:"Accept self-signed SSL certs from remote"`
RemoteConnections uint `yaml:"remoteConnections" usage:"The max number of server connections in the pool. Valid value is 1 to 10"`
RemoteIdleConnections uint `yaml:"remoteIdleConnections" usage:"The number of idle server connections kept in the pool"`
RemoteTimeout time.Duration `yaml:"remoteTimeout" usage:"The timeout of remote connections. Supports values like '30s', '5m'"`

HostPrefix config.PositionSlice[string] `yaml:"-" arg:"hostPrefix" usage:"The server will recognize this host prefix and forward data to local"`
RemoteTCPPort config.PositionSlice[uint16] `yaml:"-" arg:"remoteTCPPort" usage:"The TCP port that the remote server will open"`
RemoteTCPRandom config.PositionSlice[bool] `yaml:"-" arg:"remoteTCPRandom" usage:"Whether to choose a random tcp port by the remote server"`
Local config.PositionSlice[string] `yaml:"-" arg:"local" usage:"The local service url"`
LocalTimeout config.PositionSlice[time.Duration] `yaml:"-" arg:"localTimeout" usage:"The timeout of local connections. Supports values like '30s', '5m'"`
UseLocalAsHTTPHost config.PositionSlice[bool] `yaml:"-" arg:"useLocalAsHTTPHost" usage:"Use the local address as host"`

SentryDSN string `yaml:"sentryDSN" usage:"Sentry DSN to use"`
SentryLevel config.Slice[string] `yaml:"sentryLevel" usage:"Sentry levels: trace, debug, info, warn, error, fatal, panic (default [\"error\", \"fatal\", \"panic\"])"`
SentrySampleRate float64 `yaml:"sentrySampleRate" usage:"Sentry sample rate for event submission: [0.0 - 1.0]"`
SentryRelease string `yaml:"sentryRelease" usage:"Sentry release to be sent with events"`
SentryEnvironment string `yaml:"sentryEnvironment" usage:"Sentry environment to be sent with events"`
SentryServerName string `yaml:"sentryServerName" usage:"Sentry server name to be reported"`
SentryDebug bool `yaml:"sentryDebug" usage:"Sentry debug mode, the debug information is printed to help you understand what sentry is doing"`

WebRTCConnectionIdleTimeout time.Duration `yaml:"webrtcConnectionIdleTimeout" usage:"The timeout of WebRTC connection. Supports values like '30s', '5m'"`
WebRTCLogLevel string `yaml:"webrtcLogLevel" usage:"WebRTC log level: verbose, info, warning, error"`
WebRTCMinPort uint16 `yaml:"webrtcMinPort" usage:"The min port of WebRTC peer connection"`
WebRTCMaxPort uint16 `yaml:"webrtcMaxPort" usage:"The max port of WebRTC peer connection"`

TCPForwardAddr string `yaml:"tcpForwardAddr" usage:"The address of TCP forward"`
TCPForwardHostPrefix string `yaml:"tcpForwardHostPrefix" usage:"The host prefix of TCP forward"`
TCPForwardConnections uint `yaml:"tcpForwardConnections" usage:"The max number of TCP forward peer connections in the pool. Valid value is 1 to 10"`

LogFile string `yaml:"logFile" usage:"Path to save the log file"`
LogFileMaxSize int64 `yaml:"logFileMaxSize" usage:"Max size of the log files"`
LogFileMaxCount uint `yaml:"logFileMaxCount" usage:"Max count of the log files"`
LogLevel string `yaml:"logLevel" usage:"Log level: trace, debug, info, warn, error, fatal, panic, disable"`
Version bool `arg:"version" yaml:"-" usage:"Show the version of this program"`

Signal string `arg:"s" yaml:"-" usage:"Send signal to client processes. Supports values: reload, restart, stop, kill"`
}

func defaultConfig() Config {
return Config{
Options: Options{
ReconnectDelay: config.Duration{Duration: 5 * time.Second},
RemoteTimeout: config.Duration{Duration: 45 * time.Second},
ReconnectDelay: 5 * time.Second,
RemoteTimeout: 45 * time.Second,
RemoteConnections: 3,
RemoteIdleConnections: 1,

SentrySampleRate: 1.0,
SentryRelease: predef.Version,

WebRTCConnectionIdleTimeout: config.Duration{Duration: 5 * time.Minute},
WebRTCConnectionIdleTimeout: 5 * time.Minute,
WebRTCLogLevel: "warning",

TCPForwardConnections: 3,
Expand All @@ -122,37 +113,14 @@ func (c *clientURL) UnmarshalYAML(value *yaml.Node) (err error) {
c.URL, err = url.Parse(value.Value)
return
}
func (c clientURL) MarshalYAML() (interface{}, error) {
return c.URL.String(), nil
}

func (c *clientURL) UnmarshalJSON(data []byte) error {
var urlStr string
if err := json.Unmarshal(data, &urlStr); err != nil {
return err
}
parsedURL, err := url.Parse(urlStr)
if err != nil {
return err
}
c.URL = parsedURL
return nil
}

func (c clientURL) MarshalJSON() ([]byte, error) {
if c.URL == nil {
return json.Marshal(nil)
}
return json.Marshal(c.URL.String())
}

type service struct {
HostPrefix string `yaml:"hostPrefix,omitempty" json:",omitempty"`
RemoteTCPPort uint16 `yaml:"remoteTCPPort,omitempty" json:",omitempty"`
RemoteTCPRandom *bool `yaml:"remoteTCPRandom,omitempty" json:",omitempty"`
LocalURL clientURL `yaml:"local,omitempty" json:",omitempty"`
LocalTimeout config.Duration `yaml:"localTimeout,omitempty" json:",omitempty"`
UseLocalAsHTTPHost bool `yaml:"useLocalAsHTTPHost,omitempty" json:",omitempty"`
HostPrefix string `yaml:"hostPrefix"`
RemoteTCPPort uint16 `yaml:"remoteTCPPort"`
RemoteTCPRandom *bool `yaml:"remoteTCPRandom"`
LocalURL clientURL `yaml:"local"`
LocalTimeout time.Duration `yaml:"localTimeout"`
UseLocalAsHTTPHost bool `yaml:"useLocalAsHTTPHost"`
}

func (s *service) String() string {
Expand Down
Loading

0 comments on commit b92e880

Please sign in to comment.