修复golint

This commit is contained in:
lwch
2021-10-18 10:51:43 +08:00
parent bf9503d4cc
commit 36b5c0d35e
10 changed files with 64 additions and 57 deletions

View File

@@ -35,6 +35,9 @@ jobs:
with: with:
go-version: ${{ matrix.go }} go-version: ${{ matrix.go }}
- name: Lint
run: golint
- name: Build Server - name: Build Server
run: go build -v code/server/main.go run: go build -v code/server/main.go

8
build
View File

@@ -4,10 +4,10 @@ VERSION="0.5.0"
HASH=`git log -n1 --pretty=format:%h` HASH=`git log -n1 --pretty=format:%h`
REVERSION=`git log --oneline|wc -l|tr -d ' '` REVERSION=`git log --oneline|wc -l|tr -d ' '`
BUILD_TIME=`date +'%Y-%m-%d %H:%M:%S'` BUILD_TIME=`date +'%Y-%m-%d %H:%M:%S'`
LDFLAGS="-X 'main._GIT_HASH=$HASH' LDFLAGS="-X 'main.gitHash=$HASH'
-X 'main._GIT_REVERSION=$REVERSION' -X 'main.gitReversion=$REVERSION'
-X 'main._BUILD_TIME=$BUILD_TIME' -X 'main.buildTime=$BUILD_TIME'
-X 'main._VERSION=$VERSION'" -X 'main.version=$VERSION'"
go run contrib/bindata/main.go -pkg shell -o code/client/tunnel/shell/assets.go \ go run contrib/bindata/main.go -pkg shell -o code/client/tunnel/shell/assets.go \
-prefix html/shell "$@" html/shell/... -prefix html/shell "$@" html/shell/...

View File

@@ -95,10 +95,10 @@ func build(t target) {
err = copyFile("CHANGELOG.md", path.Join(buildDir, "CHANGELOG.md")) err = copyFile("CHANGELOG.md", path.Join(buildDir, "CHANGELOG.md"))
runtime.Assert(err) runtime.Assert(err)
ldflags := "-X 'main._GIT_HASH=" + gitHash() + "' " + ldflags := "-X 'main.gitHash=" + gitHash() + "' " +
"-X 'main._GIT_REVERSION=" + gitReversion() + "' " + "-X 'main.gitReversion=" + gitReversion() + "' " +
"-X 'main._BUILD_TIME=" + buildTime() + "' " + "-X 'main.buildTime=" + buildTime() + "' " +
"-X 'main._VERSION=" + version + "'" "-X 'main.version=" + version + "'"
logging.Info("build server...") logging.Info("build server...")
cmd := exec.Command("go", "build", "-o", path.Join(buildDir, "np-svr"+t.ext), cmd := exec.Command("go", "build", "-o", path.Join(buildDir, "np-svr"+t.ext),

View File

@@ -16,6 +16,7 @@ type Dashboard struct {
Version string Version string
} }
// New create dashboard object
func New(cfg *global.Configure, pl *pool.Pool, mgr *tunnel.Mgr, version string) *Dashboard { func New(cfg *global.Configure, pl *pool.Pool, mgr *tunnel.Mgr, version string) *Dashboard {
return &Dashboard{ return &Dashboard{
cfg: cfg, 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 { func (db *Dashboard) ListenAndServe(addr string, port uint16) error {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/api/info", db.Info) mux.HandleFunc("/api/info", db.Info)

View File

@@ -15,7 +15,7 @@ import (
func (db *Dashboard) Render(w http.ResponseWriter, r *http.Request) { func (db *Dashboard) Render(w http.ResponseWriter, r *http.Request) {
dir := strings.TrimPrefix(r.URL.Path, "/") dir := strings.TrimPrefix(r.URL.Path, "/")
if filepath.Ext(dir) == ".html" { if filepath.Ext(dir) == ".html" {
db.renderHtml(w, r, dir) db.renderHTML(w, r, dir)
return return
} }
data, err := Asset(dir) data, err := Asset(dir)
@@ -28,10 +28,10 @@ func (db *Dashboard) Render(w http.ResponseWriter, r *http.Request) {
io.Copy(w, bytes.NewReader(data)) io.Copy(w, bytes.NewReader(data))
return 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) data, err := Asset(name)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {

View File

@@ -6,6 +6,7 @@ import (
"net/http" "net/http"
) )
// Tunnels get tunnels list
func (db *Dashboard) Tunnels(w http.ResponseWriter, r *http.Request) { func (db *Dashboard) Tunnels(w http.ResponseWriter, r *http.Request) {
type link struct { type link struct {
ID string `json:"id"` ID string `json:"id"`

View File

@@ -23,17 +23,17 @@ import (
) )
var ( var (
_VERSION string = "0.0.0" version string = "0.0.0"
_GIT_HASH string gitHash string
_GIT_REVERSION string gitReversion string
_BUILD_TIME string buildTime string
) )
func showVersion() { func showVersion() {
fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n", fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n",
_VERSION, version,
_BUILD_TIME, buildTime,
_GIT_HASH, _GIT_REVERSION) gitHash, gitReversion)
os.Exit(0) os.Exit(0)
} }
@@ -108,7 +108,7 @@ func (a *app) run() {
} }
if a.cfg.DashboardEnabled { 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)) runtime.Assert(db.ListenAndServe(a.cfg.DashboardListen, a.cfg.DashboardPort))
} else { } else {
select {} select {}

View File

@@ -2,6 +2,7 @@ package tunnel
import "sync" import "sync"
// Link link interface
type Link interface { type Link interface {
GetID() string GetID() string
// GetBytes rx, tx // GetBytes rx, tx

View File

@@ -29,67 +29,67 @@ func New(cfg global.Tunnel) *Tunnel {
} }
// GetName get reverse tunnel name // GetName get reverse tunnel name
func (tunnel *Tunnel) GetName() string { func (tn *Tunnel) GetName() string {
return tunnel.Name return tn.Name
} }
// GetTypeName get reverse tunnel type name // GetTypeName get reverse tunnel type name
func (tunnel *Tunnel) GetTypeName() string { func (tn *Tunnel) GetTypeName() string {
return "reverse" return "reverse"
} }
// GetTarget get target of this tunnel // GetTarget get target of this tunnel
func (tunnel *Tunnel) GetTarget() string { func (tn *Tunnel) GetTarget() string {
return tunnel.cfg.Target return tn.cfg.Target
} }
// GetLinks get tunnel links // GetLinks get tunnel links
func (t *Tunnel) GetLinks() []tunnel.Link { func (tn *Tunnel) GetLinks() []tunnel.Link {
ret := make([]tunnel.Link, 0, len(t.links)) ret := make([]tunnel.Link, 0, len(tn.links))
t.RLock() tn.RLock()
for _, link := range t.links { for _, link := range tn.links {
ret = append(ret, link) ret = append(ret, link)
} }
t.RUnlock() tn.RUnlock()
return ret return ret
} }
// GetRemote get remote target name // GetRemote get remote target name
func (t *Tunnel) GetRemote() string { func (tn *Tunnel) GetRemote() string {
return t.cfg.Target return tn.cfg.Target
} }
// GetPort get listen port // GetPort get listen port
func (t *Tunnel) GetPort() uint16 { func (tn *Tunnel) GetPort() uint16 {
return t.cfg.LocalPort return tn.cfg.LocalPort
} }
// Handle handle tunnel // Handle handle tunnel
func (tunnel *Tunnel) Handle(pool *pool.Pool) { func (tn *Tunnel) Handle(pool *pool.Pool) {
if tunnel.cfg.Type == "tcp" { if tn.cfg.Type == "tcp" {
tunnel.handleTcp(pool) tn.handleTCP(pool)
} else { } else {
// TODO // TODO
func() {}() func() {}()
} }
} }
func (tunnel *Tunnel) handleTcp(pool *pool.Pool) { func (tn *Tunnel) handleTCP(pool *pool.Pool) {
defer func() { defer func() {
if err := recover(); err != nil { 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{ l, err := net.ListenTCP("tcp", &net.TCPAddr{
IP: net.ParseIP(tunnel.cfg.LocalAddr), IP: net.ParseIP(tn.cfg.LocalAddr),
Port: int(tunnel.cfg.LocalPort), Port: int(tn.cfg.LocalPort),
}) })
runtime.Assert(err) runtime.Assert(err)
defer l.Close() defer l.Close()
for { for {
conn, err := l.Accept() conn, err := l.Accept()
if err != nil { 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 continue
} }
@@ -106,17 +106,17 @@ func (tunnel *Tunnel) handleTcp(pool *pool.Pool) {
logging.Error("no connection available") logging.Error("no connection available")
continue continue
} }
link := NewLink(tunnel, id, tunnel.cfg.Target, conn, remote) link := NewLink(tn, id, tn.cfg.Target, conn, remote)
tunnel.Lock() tn.Lock()
tunnel.links[id] = link tn.links[id] = link
tunnel.Unlock() tn.Unlock()
remote.SendConnectReq(id, tunnel.cfg) remote.SendConnectReq(id, tn.cfg)
link.Forward() link.Forward()
} }
} }
func (tunnel *Tunnel) remove(id string) { func (tn *Tunnel) remove(id string) {
tunnel.Lock() tn.Lock()
delete(tunnel.links, id) delete(tn.links, id)
tunnel.Unlock() tn.Unlock()
} }

View File

@@ -17,17 +17,17 @@ import (
) )
var ( var (
_VERSION string = "0.0.0" version string = "0.0.0"
_GIT_HASH string gitHash string
_GIT_REVERSION string gitReversion string
_BUILD_TIME string buildTime string
) )
func showVersion() { func showVersion() {
fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n", fmt.Printf("version: v%s\ntime: %s\ncommit: %s.%s\n",
_VERSION, version,
_BUILD_TIME, buildTime,
_GIT_HASH, _GIT_REVERSION) gitHash, gitReversion)
os.Exit(0) os.Exit(0)
} }