1
0

utils.go 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package sshprox
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. )
  7. //Check if a given domain and port is a valid ssh server
  8. func IsSSHConnectable(ipOrDomain string, port int) bool {
  9. timeout := time.Second * 3
  10. conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ipOrDomain, port), timeout)
  11. if err != nil {
  12. return false
  13. }
  14. defer conn.Close()
  15. // Send an SSH version identification string to the server to check if it's SSH
  16. _, err = conn.Write([]byte("SSH-2.0-Go\r\n"))
  17. if err != nil {
  18. return false
  19. }
  20. // Wait for a response from the server
  21. buf := make([]byte, 1024)
  22. _, err = conn.Read(buf)
  23. if err != nil {
  24. return false
  25. }
  26. // Check if the response starts with "SSH-2.0"
  27. return string(buf[:7]) == "SSH-2.0"
  28. }
  29. //Check if the port is used by other process or application
  30. func isPortInUse(port int) bool {
  31. address := fmt.Sprintf(":%d", port)
  32. listener, err := net.Listen("tcp", address)
  33. if err != nil {
  34. return true
  35. }
  36. listener.Close()
  37. return false
  38. }