diff --git a/build-all.sh b/build-all.sh index e323188..35abca3 100755 --- a/build-all.sh +++ b/build-all.sh @@ -31,11 +31,19 @@ wget https://github.com/v2ray/geoip/raw/release/geoip.dat -O geoip.dat SCRIPT_NAME=`basename "$0"` FAILURES="" +PACKAGE_NAME="github.com/p4gefau1t/trojan-go" +VERSION=`git describe` +COMMIT=`git rev-parse HEAD` + +VAR_SETTING="" +VAR_SETTING="$VAR_SETTING -X $PACKAGE_NAME/constant.Version=$VERSION" +VAR_SETTING="$VAR_SETTING -X $PACKAGE_NAME/constant.Commit=$COMMIT" + for PLATFORM in $PLATFORMS; do GOOS=${PLATFORM%/*} GOARCH=${PLATFORM#*/} ZIP_FILENAME="trojan-go-${GOOS}-${GOARCH}.zip" - CMD="CGO_ENABLE=0 GOOS=${GOOS} GOARCH=${GOARCH} go build -tags \"full\" -o temp -ldflags=\"-s -w\"" + CMD="CGO_ENABLE=0 GOOS=${GOOS} GOARCH=${GOARCH} go build -tags \"full\" -o temp -ldflags=\"-s -w ${VAR_SETTING}\"" echo "${CMD}" eval $CMD || FAILURES="${FAILURES} ${PLATFORM}" zip -j release/$ZIP_FILENAME temp/* ./*.dat @@ -51,7 +59,7 @@ for GOOS in $PLATFORMS_ARM; do GOARCH="arm" for GOARM in 7 6 5; do ZIP_FILENAME="trojan-go-${GOOS}-${GOARCH}v${GOARM}.zip" - CMD="CGO_ENABLE=0 GOARM=${GOARM} GOOS=${GOOS} GOARCH=${GOARCH} go build -tags \"full\" -o temp -ldflags \"-s -w\"" + CMD="CGO_ENABLE=0 GOARM=${GOARM} GOOS=${GOOS} GOARCH=${GOARCH} go build -tags \"full\" -o temp -ldflags \"-s -w ${VAR_SETTING}\"" echo "${CMD}" eval "${CMD}" || FAILURES="${FAILURES} ${GOOS}/${GOARCH}v${GOARM}" zip -j release/$ZIP_FILENAME temp/* ./*.dat diff --git a/build.sh b/build.sh index c60a4b3..cd089f6 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,11 @@ #!/bin/bash -CGO_ENABLE=0 go build -tags "full" -ldflags="-s -w" +PACKAGE_NAME="github.com/p4gefau1t/trojan-go" +VERSION=`git describe` +COMMIT=`git rev-parse HEAD` + +VAR_SETTING="" +VAR_SETTING="$VAR_SETTING -X $PACKAGE_NAME/constant.Version=$VERSION" +VAR_SETTING="$VAR_SETTING -X $PACKAGE_NAME/constant.Commit=$COMMIT" + +CGO_ENABLE=0 go build -tags "full" -ldflags="-s -w $VAR_SETTING" diff --git a/common/common.go b/common/common.go index 69e23a5..eaabb54 100644 --- a/common/common.go +++ b/common/common.go @@ -1,31 +1,18 @@ package common import ( - "bufio" "crypto/sha256" "fmt" - "io" "log" "os" "path/filepath" ) -const ( - Version = "v0.7.2" -) - type Runnable interface { Run() error Close() error } -func NewBufioReadWriter(rw io.ReadWriter) *bufio.ReadWriter { - if bufrw, ok := rw.(*bufio.ReadWriter); ok { - return bufrw - } - return bufio.NewReadWriter(bufio.NewReader(rw), bufio.NewWriter(rw)) -} - func SHA224String(password string) string { hash := sha256.New224() hash.Write([]byte(password)) diff --git a/constant/constant.go b/constant/constant.go new file mode 100644 index 0000000..98759ed --- /dev/null +++ b/constant/constant.go @@ -0,0 +1,6 @@ +package constant + +var ( + Version = "VERSION" + Commit = "COMMIT" +) diff --git a/docs/config.toml b/docs/config.toml index feb345d..40e9744 100755 --- a/docs/config.toml +++ b/docs/config.toml @@ -14,9 +14,8 @@ enableMissingTranslationPlaceholders = false [params] # Source Code repository section - description = "An unidentifiable mechanism that helps you bypass GFW. " + description = "An unidentifiable mechanism that helps you bypass GFW." github_repository = "https://github.com/p4gefau1t/trojan-go" - version = "0.7.2" # Documentation repository section # documentation repository (set edit link to documentation repository) diff --git a/proxy/option.go b/proxy/option.go index 632f08c..a8c6522 100644 --- a/proxy/option.go +++ b/proxy/option.go @@ -2,6 +2,7 @@ package proxy import ( "flag" + "github.com/p4gefau1t/trojan-go/constant" "github.com/p4gefau1t/trojan-go/log" "github.com/p4gefau1t/trojan-go/option" "io/ioutil" @@ -24,11 +25,12 @@ func (o *Option) Handle() error { isJSON := false if strings.HasSuffix(*o.path, ".json") { isJSON = true - } else if strings.HasSuffix(*o.path, ".yaml") { + } else if strings.HasSuffix(*o.path, ".yaml") || strings.HasSuffix(*o.path, ".yml") { isJSON = false } else { log.Fatal("unsupported filename suffix", *o.path, ". use .yaml or .json instead.") } + log.Info("trojan-go", constant.Version, "initializing") proxy, err := NewProxyFromConfigData(data, isJSON) if err != nil { log.Fatal(err) diff --git a/statistic/memory/memory.go b/statistic/memory/memory.go index 7d24ca5..362c35f 100644 --- a/statistic/memory/memory.go +++ b/statistic/memory/memory.go @@ -2,6 +2,7 @@ package memory import ( "context" + "github.com/p4gefau1t/trojan-go/log" "sync" "sync/atomic" "time" @@ -235,6 +236,7 @@ func NewAuthenticator(ctx context.Context) (statistic.Authenticator, error) { hash := common.SHA224String(password) u.AddUser(hash) } + log.Debug("memory authenticator created") return u, nil } diff --git a/statistic/mysql/mysql.go b/statistic/mysql/mysql.go index 10c1fcd..3712239 100644 --- a/statistic/mysql/mysql.go +++ b/statistic/mysql/mysql.go @@ -36,7 +36,7 @@ func (a *Authenticator) updater() { s, err := a.db.Exec("UPDATE `users` SET `upload`=`upload`+?, `download`=`download`+? WHERE `password`=?;", recv, sent, hash) if err != nil { - log.Error(common.NewError("Failed to update data to user").Base(err)) + log.Error(common.NewError("failed to update data to user table").Base(err)) continue } if r, err := s.RowsAffected(); err != nil { @@ -45,12 +45,12 @@ func (a *Authenticator) updater() { } } } - log.Info("Buffered data has been written into the database") + log.Info("buffered data has been written into the database") //update memory rows, err := a.db.Query("SELECT password,quota,download,upload FROM users") if err != nil { - log.Error(common.NewError("Failed to pull data from the database").Base(err)) + log.Error(common.NewError("failed to pull data from the database").Base(err)) time.Sleep(a.updateDuration) continue } @@ -59,7 +59,7 @@ func (a *Authenticator) updater() { var quota, download, upload int64 err := rows.Scan(&hash, "a, &download, &upload) if err != nil { - log.Error(common.NewError("Failed to obtain data from the query result").Base(err)) + log.Error(common.NewError("failed to obtain data from the query result").Base(err)) break } if download+upload < quota || quota < 0 { @@ -107,6 +107,7 @@ func NewAuthenticator(ctx context.Context) (statistic.Authenticator, error) { Authenticator: memoryAuth.(*memory.Authenticator), } go a.updater() + log.Debug("mysql authenticator created") return a, nil } diff --git a/statistic/statistics.go b/statistic/statistics.go index 1d3e65a..97326b8 100644 --- a/statistic/statistics.go +++ b/statistic/statistics.go @@ -2,6 +2,7 @@ package statistic import ( "context" + "github.com/p4gefau1t/trojan-go/log" "io" "strings" @@ -53,6 +54,7 @@ func RegisterAuthenticatorCreator(name string, creator Creator) { func NewAuthenticator(ctx context.Context, name string) (Authenticator, error) { // one authenticator for each context if auth, found := createdAuth[ctx]; found { + log.Debug("authenticator has been created:", name) return auth, nil } creator, found := authCreators[strings.ToUpper(name)] diff --git a/tunnel/trojan/client.go b/tunnel/trojan/client.go index 75d04a4..e56507c 100644 --- a/tunnel/trojan/client.go +++ b/tunnel/trojan/client.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "github.com/p4gefau1t/trojan-go/api" + "github.com/p4gefau1t/trojan-go/config" "github.com/p4gefau1t/trojan-go/statistic/memory" "github.com/p4gefau1t/trojan-go/tunnel/mux" "net" @@ -146,7 +147,10 @@ func NewClient(ctx context.Context, client tunnel.Client) (*Client, error) { return nil, err } - go api.RunService(ctx, Name+"_CLIENT", auth) + cfg := config.FromContext(ctx, Name).(*Config) + if cfg.API.Enabled { + go api.RunService(ctx, Name+"_CLIENT", auth) + } var user statistic.User for _, u := range auth.ListUsers() { diff --git a/tunnel/trojan/config.go b/tunnel/trojan/config.go index b9f806c..b9df543 100644 --- a/tunnel/trojan/config.go +++ b/tunnel/trojan/config.go @@ -8,11 +8,16 @@ type Config struct { RemoteHost string `json:"remote_addr" yaml:"remote-addr"` RemotePort int `json:"remote_port" yaml:"remote-port"` DisableHTTPCheck bool `json:"disable_http_check" yaml:"disable-http-check"` - MySQL MySQLConfig `json:"mysql" yaml:"mysql"` + MySQL MySQLConfig `json,yaml:"mysql"` + API APIConfig `json,yaml:"api"` } type MySQLConfig struct { - Enabled bool `json:"enabled" yaml:"enabled"` + Enabled bool `json,yaml:"enabled"` +} + +type APIConfig struct { + Enabled bool `json,yaml:"enabled"` } func init() { diff --git a/tunnel/trojan/server.go b/tunnel/trojan/server.go index 4f95ff3..bdd49a4 100644 --- a/tunnel/trojan/server.go +++ b/tunnel/trojan/server.go @@ -190,19 +190,27 @@ func (s *Server) AcceptPacket(tunnel.Tunnel) (tunnel.PacketConn, error) { } } -func NewServer(ctx context.Context, underlay tunnel.Server) (tunnel.Server, error) { +func NewServer(ctx context.Context, underlay tunnel.Server) (*Server, error) { cfg := config.FromContext(ctx, Name).(*Config) // TODO replace this dirty code - auth, err := statistic.NewAuthenticator(ctx, memory.Name) + var auth statistic.Authenticator + var err error if cfg.MySQL.Enabled { + log.Debug("mysql enabled") auth, err = statistic.NewAuthenticator(ctx, mysql.Name) + } else { + log.Debug("auth by config file") + auth, err = statistic.NewAuthenticator(ctx, memory.Name) } - go api.RunService(ctx, Name+"_SERVER", auth) - if err != nil { - return nil, common.NewError("failed to create authenticator").Base(err) + return nil, common.NewError("trojan failed to create authenticator") } + + if cfg.API.Enabled { + go api.RunService(ctx, Name+"_SERVER", auth) + } + redirAddr := tunnel.NewAddressFromHostPort("tcp", cfg.RemoteHost, cfg.RemotePort) ctx, cancel := context.WithCancel(ctx) s := &Server{ diff --git a/version/version.go b/version/version.go index 3c5b232..9232fb9 100644 --- a/version/version.go +++ b/version/version.go @@ -3,6 +3,7 @@ package version import ( "flag" "fmt" + "github.com/p4gefau1t/trojan-go/constant" "github.com/p4gefau1t/trojan-go/option" "runtime" @@ -23,7 +24,11 @@ func (*versionOption) Priority() int { func (c *versionOption) Handle() error { if *c.flag { - fmt.Println("Trojan-Go", common.Version, fmt.Sprintf("(%s %s/%s)", runtime.Version(), runtime.GOOS, runtime.GOARCH)) + fmt.Println("Trojan-Go", constant.Version) + fmt.Println("Go Version:", runtime.Version()) + fmt.Println("OS/Arch:", runtime.GOOS+"/"+runtime.GOARCH) + fmt.Println("Git Commit:", constant.Commit) + fmt.Println("") fmt.Println("Developed by PageFault (p4gefau1t)") fmt.Println("Licensed under GNU General Public License version 3") fmt.Println("GitHub Repository:\thttps://github.com/p4gefau1t/trojan-go")