diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0c555a1..efe18c8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -35,6 +35,9 @@ jobs: with: go-version: ${{ matrix.go }} + - name: Lint + run: golint + - name: Build Server run: go build -v code/server/main.go diff --git a/build b/build index 73a5e65..9563275 100755 --- a/build +++ b/build @@ -4,10 +4,10 @@ VERSION="0.5.0" HASH=`git log -n1 --pretty=format:%h` REVERSION=`git log --oneline|wc -l|tr -d ' '` BUILD_TIME=`date +'%Y-%m-%d %H:%M:%S'` -LDFLAGS="-X 'main._GIT_HASH=$HASH' --X 'main._GIT_REVERSION=$REVERSION' --X 'main._BUILD_TIME=$BUILD_TIME' --X 'main._VERSION=$VERSION'" +LDFLAGS="-X 'main.gitHash=$HASH' +-X 'main.gitReversion=$REVERSION' +-X 'main.buildTime=$BUILD_TIME' +-X 'main.version=$VERSION'" go run contrib/bindata/main.go -pkg shell -o code/client/tunnel/shell/assets.go \ -prefix html/shell "$@" html/shell/... diff --git a/build_all.go b/build_all.go index 04c05a8..b880d61 100644 --- a/build_all.go +++ b/build_all.go @@ -95,10 +95,10 @@ func build(t target) { err = copyFile("CHANGELOG.md", path.Join(buildDir, "CHANGELOG.md")) runtime.Assert(err) - ldflags := "-X 'main._GIT_HASH=" + gitHash() + "' " + - "-X 'main._GIT_REVERSION=" + gitReversion() + "' " + - "-X 'main._BUILD_TIME=" + buildTime() + "' " + - "-X 'main._VERSION=" + version + "'" + ldflags := "-X 'main.gitHash=" + gitHash() + "' " + + "-X 'main.gitReversion=" + gitReversion() + "' " + + "-X 'main.buildTime=" + buildTime() + "' " + + "-X 'main.version=" + version + "'" logging.Info("build server...") cmd := exec.Command("go", "build", "-o", path.Join(buildDir, "np-svr"+t.ext), diff --git a/code/client/dashboard/dashboard.go b/code/client/dashboard/dashboard.go index f7e83e6..508c540 100644 --- a/code/client/dashboard/dashboard.go +++ b/code/client/dashboard/dashboard.go @@ -16,6 +16,7 @@ type Dashboard struct { Version string } +// New create dashboard object func New(cfg *global.Configure, pl *pool.Pool, mgr *tunnel.Mgr, version string) *Dashboard { return &Dashboard{ cfg: cfg, @@ -25,6 +26,7 @@ func New(cfg *global.Configure, pl *pool.Pool, mgr *tunnel.Mgr, version string) } } +// ListenAndServe listen and serve http handler func (db *Dashboard) ListenAndServe(addr string, port uint16) error { mux := http.NewServeMux() mux.HandleFunc("/api/info", db.Info) diff --git a/code/client/dashboard/h_render.go b/code/client/dashboard/h_render.go index ff83228..b2458ab 100644 --- a/code/client/dashboard/h_render.go +++ b/code/client/dashboard/h_render.go @@ -15,7 +15,7 @@ import ( func (db *Dashboard) Render(w http.ResponseWriter, r *http.Request) { dir := strings.TrimPrefix(r.URL.Path, "/") if filepath.Ext(dir) == ".html" { - db.renderHtml(w, r, dir) + db.renderHTML(w, r, dir) return } data, err := Asset(dir) @@ -28,10 +28,10 @@ func (db *Dashboard) Render(w http.ResponseWriter, r *http.Request) { io.Copy(w, bytes.NewReader(data)) return } - db.renderHtml(w, r, "index.html") + db.renderHTML(w, r, "index.html") } -func (db *Dashboard) renderHtml(w http.ResponseWriter, r *http.Request, name string) { +func (db *Dashboard) renderHTML(w http.ResponseWriter, r *http.Request, name string) { data, err := Asset(name) if err != nil { if os.IsNotExist(err) { diff --git a/code/client/dashboard/h_tunnels.go b/code/client/dashboard/h_tunnels.go index 1a70482..09dbb9e 100644 --- a/code/client/dashboard/h_tunnels.go +++ b/code/client/dashboard/h_tunnels.go @@ -6,6 +6,7 @@ import ( "net/http" ) +// Tunnels get tunnels list func (db *Dashboard) Tunnels(w http.ResponseWriter, r *http.Request) { type link struct { ID string `json:"id"` diff --git a/code/client/main.go b/code/client/main.go index d80e310..cfd478c 100644 --- a/code/client/main.go +++ b/code/client/main.go @@ -23,17 +23,17 @@ import ( ) var ( - _VERSION string = "0.0.0" - _GIT_HASH string - _GIT_REVERSION string - _BUILD_TIME string + version string = "0.0.0" + gitHash string + gitReversion string + buildTime string ) func showVersion() { fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n", - _VERSION, - _BUILD_TIME, - _GIT_HASH, _GIT_REVERSION) + version, + buildTime, + gitHash, gitReversion) os.Exit(0) } @@ -108,7 +108,7 @@ func (a *app) run() { } if a.cfg.DashboardEnabled { - db := dashboard.New(a.cfg, pl, mgr, _VERSION) + db := dashboard.New(a.cfg, pl, mgr, version) runtime.Assert(db.ListenAndServe(a.cfg.DashboardListen, a.cfg.DashboardPort)) } else { select {} diff --git a/code/client/tunnel/mgr.go b/code/client/tunnel/mgr.go index c460058..301b99a 100644 --- a/code/client/tunnel/mgr.go +++ b/code/client/tunnel/mgr.go @@ -2,6 +2,7 @@ package tunnel import "sync" +// Link link interface type Link interface { GetID() string // GetBytes rx, tx diff --git a/code/client/tunnel/reverse/tunnel.go b/code/client/tunnel/reverse/tunnel.go index 9409526..944781b 100644 --- a/code/client/tunnel/reverse/tunnel.go +++ b/code/client/tunnel/reverse/tunnel.go @@ -29,67 +29,67 @@ func New(cfg global.Tunnel) *Tunnel { } // GetName get reverse tunnel name -func (tunnel *Tunnel) GetName() string { - return tunnel.Name +func (tn *Tunnel) GetName() string { + return tn.Name } // GetTypeName get reverse tunnel type name -func (tunnel *Tunnel) GetTypeName() string { +func (tn *Tunnel) GetTypeName() string { return "reverse" } // GetTarget get target of this tunnel -func (tunnel *Tunnel) GetTarget() string { - return tunnel.cfg.Target +func (tn *Tunnel) GetTarget() string { + return tn.cfg.Target } // GetLinks get tunnel links -func (t *Tunnel) GetLinks() []tunnel.Link { - ret := make([]tunnel.Link, 0, len(t.links)) - t.RLock() - for _, link := range t.links { +func (tn *Tunnel) GetLinks() []tunnel.Link { + ret := make([]tunnel.Link, 0, len(tn.links)) + tn.RLock() + for _, link := range tn.links { ret = append(ret, link) } - t.RUnlock() + tn.RUnlock() return ret } // GetRemote get remote target name -func (t *Tunnel) GetRemote() string { - return t.cfg.Target +func (tn *Tunnel) GetRemote() string { + return tn.cfg.Target } // GetPort get listen port -func (t *Tunnel) GetPort() uint16 { - return t.cfg.LocalPort +func (tn *Tunnel) GetPort() uint16 { + return tn.cfg.LocalPort } // Handle handle tunnel -func (tunnel *Tunnel) Handle(pool *pool.Pool) { - if tunnel.cfg.Type == "tcp" { - tunnel.handleTcp(pool) +func (tn *Tunnel) Handle(pool *pool.Pool) { + if tn.cfg.Type == "tcp" { + tn.handleTCP(pool) } else { // TODO func() {}() } } -func (tunnel *Tunnel) handleTcp(pool *pool.Pool) { +func (tn *Tunnel) handleTCP(pool *pool.Pool) { defer func() { if err := recover(); err != nil { - logging.Error("close tcp tunnel: %s, err=%v", tunnel.cfg.Name, err) + logging.Error("close tcp tunnel: %s, err=%v", tn.cfg.Name, err) } }() l, err := net.ListenTCP("tcp", &net.TCPAddr{ - IP: net.ParseIP(tunnel.cfg.LocalAddr), - Port: int(tunnel.cfg.LocalPort), + IP: net.ParseIP(tn.cfg.LocalAddr), + Port: int(tn.cfg.LocalPort), }) runtime.Assert(err) defer l.Close() for { conn, err := l.Accept() if err != nil { - logging.Error("accept from %s tunnel, err=%v", tunnel.cfg.Name, err) + logging.Error("accept from %s tunnel, err=%v", tn.cfg.Name, err) continue } @@ -106,17 +106,17 @@ func (tunnel *Tunnel) handleTcp(pool *pool.Pool) { logging.Error("no connection available") continue } - link := NewLink(tunnel, id, tunnel.cfg.Target, conn, remote) - tunnel.Lock() - tunnel.links[id] = link - tunnel.Unlock() - remote.SendConnectReq(id, tunnel.cfg) + link := NewLink(tn, id, tn.cfg.Target, conn, remote) + tn.Lock() + tn.links[id] = link + tn.Unlock() + remote.SendConnectReq(id, tn.cfg) link.Forward() } } -func (tunnel *Tunnel) remove(id string) { - tunnel.Lock() - delete(tunnel.links, id) - tunnel.Unlock() +func (tn *Tunnel) remove(id string) { + tn.Lock() + delete(tn.links, id) + tn.Unlock() } diff --git a/code/server/main.go b/code/server/main.go index 6dc4e41..edc5ffe 100644 --- a/code/server/main.go +++ b/code/server/main.go @@ -17,17 +17,17 @@ import ( ) var ( - _VERSION string = "0.0.0" - _GIT_HASH string - _GIT_REVERSION string - _BUILD_TIME string + version string = "0.0.0" + gitHash string + gitReversion string + buildTime string ) func showVersion() { fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n", - _VERSION, - _BUILD_TIME, - _GIT_HASH, _GIT_REVERSION) + version, + buildTime, + gitHash, gitReversion) os.Exit(0) }