Skip to content

Commit

Permalink
v1.1.0 Feature/optimize (#18)
Browse files Browse the repository at this point in the history
* 优化窗口大小监听逻辑,降低CPU使用
cp命令显示功能优化

* cp 命令允许remote->remote

* 优化代码
增加socks5代理

* bug fix
update failed #15
main包没有了 #17
install,无法使用,看脚本里没有autossh可执行文件 #16

* 优化安装脚本

* Update README.md

* add license info
  • Loading branch information
islenbo authored Aug 21, 2019
1 parent 2ca609f commit 0171f03
Show file tree
Hide file tree
Showing 22 changed files with 580 additions and 297 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ servers.json
config.json
releases/
.idea
main
test.go
app.log
vendor
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 lenbo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
66 changes: 57 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# autossh

一个ssh远程客户端,可一键登录远程服务器,主要用来弥补Mac/Linux Terminal ssh无法保存密码的不足
一个SSH远程客户端,可一键登录远程服务器,主要用来弥补Mac/Linux Terminal SSH无法保存密码的不足

![演示](https://raw.githubusercontent.com/islenbo/autossh/b3e18c35ebced882ace59be7843d9a58d1ac74d7/doc/images/ezgif-1-a4ddae192f.gif)
![演示](https://raw.githubusercontent.com/islenbo/autossh/c9b52688dabbba8ef6403c2f83f8d758ae0e4dfe/doc/images/ezgif-5-42b5117192fc.gif)

## 功能说明
- 核心代码重构,使用go.mod管理依赖
Expand All @@ -13,6 +13,7 @@
- 删除功能支持ctrl+d退出
- 优化帮助菜单显示
- 修复若干Bug
- 支持SOCKS5代理

## 下载
[https://github.com/islenbo/autossh/releases](https://github.com/islenbo/autossh/releases)
Expand All @@ -22,19 +23,66 @@
- Windows用户可手动编译,参考编译章节。

## config.json字段说明
- TODO 字段说明
```yaml
show_detail: bool <是否显示服务器详情(用户、IP)>
options:
ServerAliveInterval: int <是否定时发送心跳包,与SSH ServerAliveInterval属性用法相同>
servers:
- name: string <显示名称>
ip: string <服务器IP>
port: int <端口>
user: string <用户名>
password: string <密码>
method: string <鉴权方式,password-密码 key-密钥>
key: string <私钥路径>
options:
ServerAliveInterval: int <与根节点ServerAliveInterval用法相同,该值会覆盖根节点的值>
alias: string <别名>
log:
enable: bool <是否启用日志>
filename: string <日志路径, 如 /tmp/%n-%u-%dt.log 支持变量请参考下方《日志变量》章节>
mode: string <遇到同名日志的处理模式,cover-覆盖 append-追加,默认为cover>
groups:
- group_name: string <组名>
prefix: string <组前缀>
servers: array <服务器列表,配置与servers相同,配置说明略>
collapse: bool <是否折叠>
proxy:
type: string <代理方式,目前仅支持SOCKS5>
server: string <代理服务器地址>
port: int <端口号>
user: string <用户,若无留空>
password: string <密码,若无留空>
```
## 日志变量
变量 | 说明 | 示例
--- | --- | ---
%g | 组名(group_name) | MyGroup1
%n | 显示名称(name) | vagrant
%u | 用户名(user) | root
%a | 别名(alias) | vagrant
%dt | 日期时间 | 20190821223010
%d | 日期 | 20190821
## Q&amp;A
- Q: Downloads中为什么没有Windows的包?
- A: Windows下有很多ssh工具,autossh主要是面向Mac/Linux群体。
### Q: 为什么没有Windows的包?
A: Windows下有很多优秀的SSH工具,autossh主要是面向Mac/Linux群体,Windows用户可自行编译。
### Q: cp 命令出现报错: ssh: subsystem request failed
A: 修改服务器 /etc/ssh/sshd_config 将 `Subsystem sftp /usr/libexec/openssh/sftp-server` 的注释打开,重启 sshd 服务。

## 编译
export GO111MODULE="on"
export GOFLAGS=" -mod=vendor"
go build main.go
```bash
sh build.sh
```

## 依赖
- 查阅 go.mod

## 注意
v0.X版本配置文件无法与v1.X版本兼容,请勿使用!
v0.X版本配置文件无法与v1.X版本兼容,请勿使用!

## License
MIT
3 changes: 3 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash

export GO111MODULE="on"
go mod tidy

PROJECT="autossh"
VERSION="v1.1.0"
BUILD=`date +%FT%T%z`
Expand Down
Binary file added doc/images/ezgif-5-42b5117192fc.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ go 1.12

require (
github.com/kr/fs v0.1.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/pkg/errors v0.8.1
github.com/pkg/sftp v1.10.0
golang.org/x/crypto v0.0.0-20181024132630-e84da0312774c21d64ee2317962ef669b27ffb41
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862 // indirect
)

replace (
golang.org/x/crypto => github.com/golang/crypto v0.0.0-20190701094942-4def268fd1a4
golang.org/x/net => github.com/golang/net v0.0.0-20190813141303-74dc4d7220e7
golang.org/x/sys => github.com/golang/sys v0.0.0-20190813064441-fde4db37ae7a
)
24 changes: 14 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
github.com/islenbo/scp v0.0.0-20170824162307-f7b48647feef3e30991f2ecc16b2b9a977d9a7c3 h1:GZQQKQUN0Bp02sJNGN6/FoqXeKRVqftdsv6WFhwhcR4=
github.com/islenbo/scp v0.0.0-20170824162307-f7b48647feef3e30991f2ecc16b2b9a977d9a7c3/go.mod h1:h2o9ndCCKJY6lGA8rN7Da+3RVv2h5d36KKTyLGuS/T8=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/crypto v0.0.0-20190701094942-4def268fd1a4 h1:SqpWDZAu6UkmbvUTCtyNpBZLY8110TJ7bgxIki3pZw0=
github.com/golang/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
github.com/golang/net v0.0.0-20190813141303-74dc4d7220e7 h1:0oy3VQWim3zJeCPQgw9ka5X1odfKEgRUxblrM6z/rCY=
github.com/golang/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
github.com/golang/sys v0.0.0-20190813064441-fde4db37ae7a h1:hVLU4+cxX4r89gounKarktyMqZ2cx/5Y2jeGLtWqzUE=
github.com/golang/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.0 h1:DGA1KlA9esU6WcicH+P8PxFZOl15O6GYtab1cIJdOlE=
github.com/pkg/sftp v1.10.0/go.mod h1:NxmoDg/QLVWluQDUYG7XBZTLUpKeFa8e3aMf1BfjyHk=
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef h1:7D6Nm4D6f0ci9yttWaKjM1TMAXrH5Su72dojqYGntFY=
github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef/go.mod h1:WLFStEdnJXpjK8kd4qKLwQKX/1vrDzp5BcDyiZJBHJM=
golang.org/x/crypto v0.0.0-20181024132630-e84da0312774c21d64ee2317962ef669b27ffb41 h1:IlSGaL0SqFiWKa+4DitAJUo/USS7vA2+5VhRfkkF048=
golang.org/x/crypto v0.0.0-20181024132630-e84da0312774c21d64ee2317962ef669b27ffb41/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862 h1:rM0ROo5vb9AdYJi1110yjWGMej9ITfKddS89P3Fkhug=
golang.org/x/sys v0.0.0-20190509141414-a5b02f93d862/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5 changes: 2 additions & 3 deletions install
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
cd $(dirname $0)

mkdir -p ~/autossh/
cp ./autossh ~/autossh/
cp -f ./autossh ~/autossh/

CONFIG_FILE=`cd ~/autossh/ && pwd`/config.json
if [[ ! -f ${CONFIG_FILE} ]]; then
echo "not exists"
cp ./config.json ~/autossh/
fi

HAS_ALIAS=`cat ~/.bash_profile | grep autossh | wc -l`
if [[ ${HAS_ALIAS} -eq 0 ]]; then
echo "alias autossh='~/autossh/autossh'" >> ~/.bash_profile
echo "export PATH=$PATH:~/autossh/" >> ~/.bash_profile
fi

source ~/.bash_profile
Expand Down
53 changes: 28 additions & 25 deletions src/app/app.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,62 @@
package app

import (
"autossh/src/utils"
"flag"
"os"
"path/filepath"
)

var (
Version string
Build string

varVersion bool
varHelp bool
varUpgrade bool
varCp bool
varConfig = "./config.json"
c string
v bool
h bool
upgrade bool
cp bool
)

func init() {
flag.BoolVar(&varVersion, "v", varVersion, "版本信息")
flag.BoolVar(&varVersion, "version", varVersion, "版本信息")
flag.BoolVar(&varHelp, "h", varHelp, "帮助信息")
flag.BoolVar(&varHelp, "help", varHelp, "帮助信息")
flag.StringVar(&varConfig, "c", varConfig, "指定配置文件路径")
flag.StringVar(&varConfig, "config", varConfig, "指定配置文件路径")
// 取执行文件所在目录下的config.json
dir, _ := os.Executable()
c = filepath.Dir(dir) + "/config.json"

flag.StringVar(&c, "c", c, "指定配置文件路径")
flag.StringVar(&c, "config", c, "指定配置文件路径")

flag.BoolVar(&v, "v", v, "版本信息")
flag.BoolVar(&v, "version", v, "版本信息")

flag.BoolVar(&h, "h", h, "帮助信息")
flag.BoolVar(&h, "help", h, "帮助信息")

flag.Usage = usage
flag.Parse()

if len(flag.Args()) > 0 {
arg := flag.Arg(0)
switch arg {
case "upgrade":
varUpgrade = true
upgrade = true
case "cp":
varCp = true
cp = true
default:
defaultServer = arg
}
}
}

func Run() {
if exists, _ := utils.FileIsExists(varConfig); !exists {
utils.Errorln("Can't read config file", varConfig)
return
}

if varVersion {
if v {
showVersion()
} else if varHelp {
} else if h {
showHelp()
} else if varUpgrade {
} else if upgrade {
showUpgrade()
} else if varCp {
showCp(varConfig)
} else if cp {
showCp(c)
} else {
showServers(varConfig)
showServers(c)
}
}
32 changes: 26 additions & 6 deletions src/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

type Config struct {
ShowDetail bool `json:"show_detail"`
Servers []Server `json:"servers"`
Groups []Group `json:"groups"`
Servers []*Server `json:"servers"`
Groups []*Group `json:"groups"`
Options map[string]interface{} `json:"options"`

// 服务器map索引,可通过编号、别名快速定位到某一个服务器
Expand All @@ -28,6 +28,21 @@ type Group struct {
Prefix string `json:"prefix"`
Servers []Server `json:"servers"`
Collapse bool `json:"collapse"`
Proxy *Proxy `json:"proxy"`
}

type ProxyType string

const (
ProxyTypeSocks5 ProxyType = "SOCKS5"
)

type Proxy struct {
Type ProxyType `json:"type"`
Server string `json:"server"`
Port int `json:"port"`
User string `json:"user"`
Password string `json:"password"`
}

type LogMode string
Expand Down Expand Up @@ -60,7 +75,7 @@ type ServerIndex struct {
func (cfg *Config) createServerIndex() {
cfg.serverIndex = make(map[string]ServerIndex)
for i := range cfg.Servers {
server := &cfg.Servers[i]
server := cfg.Servers[i]
server.Format()
index := strconv.Itoa(i + 1)

Expand All @@ -81,11 +96,12 @@ func (cfg *Config) createServerIndex() {
}

for i := range cfg.Groups {
group := &cfg.Groups[i]
group := cfg.Groups[i]
for j := range group.Servers {
server := &group.Servers[j]
server.Format()
server.groupName = group.GroupName
server.group = group
index := group.Prefix + strconv.Itoa(j+1)

if _, ok := cfg.serverIndex[index]; ok {
Expand Down Expand Up @@ -136,15 +152,19 @@ func (cfg *Config) backup() error {
return err
}

defer srcFile.Close()
defer func() {
_ = srcFile.Close()
}()

path, _ := filepath.Abs(filepath.Dir(cfg.file))
backupFile := path + "/config-" + time.Now().Format("20060102150405") + ".json"
desFile, err := os.Create(backupFile)
if err != nil {
return err
}
defer desFile.Close()
defer func() {
_ = desFile.Close()
}()

_, err = io.Copy(desFile, srcFile)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/app/handle_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func handleAdd(cfg *Config, _ []string) error {
groups := make(map[string]*Group)
for i := range cfg.Groups {
group := &cfg.Groups[i]
group := cfg.Groups[i]
groups[group.Prefix] = group
utils.Info("["+group.Prefix+"]"+group.GroupName, "\t")
}
Expand All @@ -34,7 +34,7 @@ func handleAdd(cfg *Config, _ []string) error {
group.Servers = append(group.Servers, server)
server.groupName = group.GroupName
} else {
cfg.Servers = append(cfg.Servers, server)
cfg.Servers = append(cfg.Servers, &server)
}

return cfg.saveConfig(true)
Expand Down
Loading

0 comments on commit 0171f03

Please sign in to comment.