first version, unittest not fin
This commit is contained in:
+59
-3
@@ -1,13 +1,15 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"ssh-proxy/pkg/logger"
|
||||
"strconv"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func CheckRemoteSSH(host string, port int, timeout int) bool {
|
||||
func CheckRemoteTCP(host string, port int, timeout int) error {
|
||||
log := logger.ColorLogger{}
|
||||
connTimeout := time.Duration(timeout) * time.Second
|
||||
if connTimeout <= 0 {
|
||||
@@ -17,8 +19,62 @@ func CheckRemoteSSH(host string, port int, timeout int) bool {
|
||||
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
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkDomain returns an error if the domain name is not valid
|
||||
// See https://tools.ietf.org/html/rfc1034#section-3.5 and
|
||||
// https://tools.ietf.org/html/rfc1123#section-2.
|
||||
func CheckDomain(name string) error {
|
||||
switch {
|
||||
case len(name) == 0:
|
||||
return nil // an empty domain name will result in a cookie without a domain restriction
|
||||
case len(name) > 255:
|
||||
return fmt.Errorf("cookie domain: name length is %d, can't exceed 255", len(name))
|
||||
}
|
||||
var l int
|
||||
for i := 0; i < len(name); i++ {
|
||||
b := name[i]
|
||||
if b == '.' {
|
||||
// check domain labels validity
|
||||
switch {
|
||||
case i == l:
|
||||
return fmt.Errorf("cookie domain: invalid character '%c' at offset %d: label can't begin with a period", b, i)
|
||||
case i-l > 63:
|
||||
return fmt.Errorf("cookie domain: byte length of label '%s' is %d, can't exceed 63", name[l:i], i-l)
|
||||
case name[l] == '-':
|
||||
return fmt.Errorf("cookie domain: label '%s' at offset %d begins with a hyphen", name[l:i], l)
|
||||
case name[i-1] == '-':
|
||||
return fmt.Errorf("cookie domain: label '%s' at offset %d ends with a hyphen", name[l:i], l)
|
||||
}
|
||||
l = i + 1
|
||||
continue
|
||||
}
|
||||
// test label character validity, note: tests are ordered by decreasing validity frequency
|
||||
if !(b >= 'a' && b <= 'z' || b >= '0' && b <= '9' || b == '-' || b >= 'A' && b <= 'Z') {
|
||||
// show the printable unicode character starting at byte offset i
|
||||
c, _ := utf8.DecodeRuneInString(name[i:])
|
||||
if c == utf8.RuneError {
|
||||
return fmt.Errorf("cookie domain: invalid rune at offset %d", i)
|
||||
}
|
||||
return fmt.Errorf("cookie domain: invalid character '%c' at offset %d", c, i)
|
||||
}
|
||||
}
|
||||
// check top level domain validity
|
||||
switch {
|
||||
case l == len(name):
|
||||
return fmt.Errorf("cookie domain: missing top level domain, domain can't end with a period")
|
||||
case len(name)-l > 63:
|
||||
return fmt.Errorf("cookie domain: byte length of top level domain '%s' is %d, can't exceed 63", name[l:], len(name)-l)
|
||||
case name[l] == '-':
|
||||
return fmt.Errorf("cookie domain: top level domain '%s' at offset %d begins with a hyphen", name[l:], l)
|
||||
case name[len(name)-1] == '-':
|
||||
return fmt.Errorf("cookie domain: top level domain '%s' at offset %d ends with a hyphen", name[l:], l)
|
||||
case name[l] >= '0' && name[l] <= '9':
|
||||
return fmt.Errorf("cookie domain: top level domain '%s' at offset %d begins with a digit", name[l:], l)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ func TestCheckRemoteSSH(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "test check localhost ssh port",
|
||||
@@ -20,12 +20,12 @@ func TestCheckRemoteSSH(t *testing.T) {
|
||||
port: 22,
|
||||
timeout: 5,
|
||||
},
|
||||
want: true,
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
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 {
|
||||
if got := CheckRemoteTCP(tt.args.host, tt.args.port, tt.args.timeout); got != tt.want {
|
||||
t.Errorf("CheckRemoteSSH() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user