proxy ver1

This commit is contained in:
Jay
2020-01-13 18:05:18 +08:00
commit c15c1c0421
7 changed files with 351 additions and 0 deletions
+24
View File
@@ -0,0 +1,24 @@
package common
import (
"net"
"ssh-proxy/pkg/logger"
"strconv"
"time"
)
func CheckRemoteSSH(host string, port int, timeout int) bool {
log := logger.ColorLogger{}
connTimeout := time.Duration(timeout) * time.Second
if connTimeout <= 0 {
connTimeout = time.Second * 5
}
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, strconv.Itoa(port)), connTimeout)
if err != nil {
log.Trace("Check Remote Host SSH Fail: Host(%s:%d) %s", host, port, err)
return false
}
defer conn.Close()
return true
}
+33
View File
@@ -0,0 +1,33 @@
package common
import "testing"
func TestCheckRemoteSSH(t *testing.T) {
type args struct {
host string
port int
timeout int
}
tests := []struct {
name string
args args
want bool
}{
{
name: "test check localhost ssh port",
args: args{
host: "localhost",
port: 22,
timeout: 5,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CheckRemoteSSH(tt.args.host, tt.args.port, tt.args.timeout); got != tt.want {
t.Errorf("CheckRemoteSSH() = %v, want %v", got, tt.want)
}
})
}
}