修复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:
go-version: ${{ matrix.go }}
- name: Lint
run: golint
- name: Build Server
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`
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/...

View File

@@ -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),

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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"`

View File

@@ -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 {}

View File

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

View File

@@ -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()
}

View File

@@ -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)
}